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

How to write size into self.mystats.write



  • Hello guys,
    anyone can help me? I try to write the size of trade while next().
    My understanding is that first define self.tradesize in notify_trade:

    def notify_trade(self, trade):
            if trade.justopened:
                self.tradesize = trade.size 
    

    Then, I try to calculate the trade in next():

            if self.tradesize:
                self.tsize = self.tradesize[0]
            else:
                self.tsize = 0
    

    I get the error: "AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'tradesize'"

    What do I do wrong?

    Many thanks,
    Vaclav



  • self.tradesize as it is calculated in the notify_trade() is a number. So I don't think that self.tradesize[0] is a valid call. Otherwise I don't see any other issues with the piece of code above. Also it is better to provide full error message since python shows lines of code which causes the error.



  • Hello, thanks for support. To be honest, I don't want leave the topic without a clue not meet the issue in future.
    When I log the self.tradesize in notify_trade, it works well.
    However, I understand the issue in the way, the instance of class does not know the (self.)tradesize.

      File "C:\UsersXXX\___master\master.py", line 137, in runstrat
        cebrun = cerebro.run()
      File "C:\UsersXXX\lib\site-packages\backtrader\cerebro.py", line 1127, in run
        runstrat = self.runstrategies(iterstrat)
      File "C:\UsersXXX\lib\site-packages\backtrader\cerebro.py", line 1293, in runstrategies
        self._runonce(runstrats)
      File "C:\UsersXXX\lib\site-packages\backtrader\cerebro.py", line 1695, in _runonce
        strat._oncepost(dt0)
      File "C:\UsersXXX\lib\site-packages\backtrader\strategy.py", line 311, in _oncepost
        self.nextstart()  # only called for the 1st value
      File "C:\UsersXXX\lib\site-packages\backtrader\lineiterator.py", line 347, in nextstart
        self.next()
      File "C:\UsersXXX\___master\modules\strategy.py", line 149, in next
        if self.tradesize:
      File "C:\UsersXXX\lib\site-packages\backtrader\lineseries.py", line 461, in __getattr__
        return getattr(self.lines, name)
    AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'tradesize'
    

    Please, note: I try it without the index as well.

            if self.tradesize:
                self.tsize = self.tradesize
            else:
                self.tsize = 0
    

    Line 149 refers to the condition...
    Thank you for your time.



  • @ab_trader : I've also tried make it in init

    self.tsize = bt.If(self.tradesize, self.tradesize, 0)
    

    The issue is almost the same.

    This however works well:

        def notify_trade(self, trade):
            if trade.justopened:
                print('----TRADE OPENED----')
                self.tradesize = trade.size
                print(self.tradesize)
    

    And prints the size...



  • Put self.tradesize = None or self.tradesize = 0 in the __init()__. It is undefined for first next() calls before you get your trade open.



  • @ab_trader : Many thanks. A new added value for me. Very appreciated.


Log in to reply
 

});