Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    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

    Indicators/Strategies/Analyzers
    2
    6
    86
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • V
      vaclavku last edited by

      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

      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        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.

        1 Reply Last reply Reply Quote 1
        • V
          vaclavku last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • V
            vaclavku last edited by

            @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...

            1 Reply Last reply Reply Quote 0
            • A
              ab_trader last edited by

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

              1 Reply Last reply Reply Quote 3
              • V
                vaclavku last edited by

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

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                $(document).ready(function () { app.coldLoad(); }); }