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

Creating only buy orders but still portfolio value seeing a net increase, why // how is this happening?



  • This is the code I'm using. Nothing fancy, just followed the tutorial ---

    import os.path
    import sys
    
    import backtrader as bt
    import backtrader.feeds as btfeeds
    
    
    class GoogleDriveData(btfeeds.GenericCSVData):
        params = (
            ('dtformat', ('%Y%m%d')),
            ('tmformat', ('%H:%M')),
            ('datetime', 1),
            ('time', 2),
            ('high', 3),
            ('low', 4),
            ('open', 5),
            ('close', 6),
            ('volume', 7),
            ('openinterest', -1),
            ('timeframe', bt.TimeFrame.Minutes)
        )
    
    
    class TestStrategy(bt.Strategy):
        def log(self, txt, dt=None):
            dt = dt or self.datas[0].datetime.datetime(0)
            print(f'{dt}, {txt}')
    
        def __init__(self):
            self.dataclose = self.datas[0].close
    
        def next(self):
            self.log(f'Close, {self.dataclose[0]}')
    
            if self.dataclose[0] < self.dataclose[-1] < self.dataclose[-2]:
                self.log(f'BUY CREATE, {self.dataclose[0]}')
                self.buy()
    
    
    if __name__ == '__main__':
        cerebro = bt.Cerebro()
        cerebro.addstrategy(TestStrategy)
    
        cerebro.broker.setcash(1000000.0)
    
        modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
        datapath = os.path.join(modpath, 'TATAMOTORS.txt')
        data = GoogleDriveData(dataname=datapath)
        cerebro.adddata(data)
    
        print(f'Starting Portfolio Value: {cerebro.broker.getvalue()}')
        cerebro.run()
        print(f'Final Portfolio Value: {cerebro.broker.getvalue()}')
    

    And the final portfolio value is ---

    Final Portfolio Value: 1004834.0999999973
    

  • administrators

    Because what you have bought has more value?

    There is not much information in your post ... but it would seem obvious ...



  • @backtrader I'm only buying stocks, not selling anything. As I'm only buying, it would seem obvious that my portfolio balance should go down, not up. Am I missing something here?


  • administrators

    @skulltech said in Creating only buy orders but still portfolio value seeing a net increase, why // how is this happening?:

    Am I missing something here?

    It seems you miss that whatever you buy, does still have a value. Ok, you can ask the bank to send you the stocks in paper format and you can burn them. You will then obviously decrease the value of the assets you bought down to zero.

    In any other case, the actual value will be determined by the last price you have to do the calculations.

    Your information about at which price you buy the assets and what the last price is: None. But it seems obvious for anyone that the final price is higher.


Log in to reply
 

});