For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Position size changes without buy or sell order



  • Hi All,

    I have run backtest for different time frames and noticed that sometimes some of the stocks get 'virtual' positions without having bought them before.
    This is what I have in next() for buying:

        def next(self):
            portvalue = cerebro.broker.getvalue()
            availablecash=cerebro.broker.getcash()
            dt=self.datetime.date()
            
            for i, d in enumerate(self.datas):
                dn =d._name
                pos = self.getposition(d).size
                print(dt,dn,"Size:",pos)
                if pos==0:
                    if d.volume[0]==-1:
                        if booleanvalue==True:
                            dec_,int_=math.modf(allocation*portvalue/d.close[0])
                            print(dn,'at $',round(d.close[0],2),'BUY CREATE, Size:',int_, 'Cash Spent to Buy:',round(d.close[0]*int_,2))
                            self.buy(data=d, size=int_)
                            self.order = self.buy()
    

    (Please note that I add to cerebro a data set, in which buy and sell signals are indicated by the modified volume values, that's why I use the condition d.volume[0]==-1.)
    If I run the backtest for the period 31 Jul 2018 - 20 Aug 2018, 2 stocks are purchased during the first 4 days:

    2018-07-31 NM.V Size: 0
    2018-07-31 PAT.V Size: 0
    
    2018-08-01 NM.V Size: 0
    2018-08-01 PAT.V Size: 0
               PAT.V at $ 1.71 BUY CREATE, Size: 14619.0 Cash Spent to Buy: 24998.49
    
    2018-08-02 NM.V Size: 0
               NM.V at $ 1.54 BUY CREATE, Size: 16067.0 Cash Spent to Buy: 24743.18
    2018-08-02 PAT.V Size: 14619.0
    
    2018-08-03 NM.V Size: 16067.0
    2018-08-03 PAT.V Size: 14619.0
    

    But if I run the backtest for only 31 Jul 2018 - 17 Aug 2018, only one stock is bought but the position size of an other is also changing (and that's why is not purchased, since the condition pos==0 is false):

    2018-07-31 NM.V Size: 0
    2018-07-31 PAT.V Size: 0
    
    2018-08-01 NM.V Size: 0
    2018-08-01 PAT.V Size: 0
               PAT.V at $ 1.71 BUY CREATE, Size: 14619.0 Cash Spent to Buy: 24998.49
    
    2018-08-02 NM.V Size: 1
    2018-08-02 PAT.V Size: 14619.0
    
    2018-08-03 NM.V Size: 1
    2018-08-03 PAT.V Size: 14619.0
    

    Please help me how it can happen.


  • administrators

    @tizenhet said in Position size changes without buy or sell order:

                            self.buy(data=d, size=int_)
                            self.order = self.buy()
    

Log in to reply
 

});