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/

    Adding a value to a line

    General Code/Help
    2
    4
    40
    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.
    • Olly Fallon
      Olly Fallon last edited by

      Hi

      I am a very novice code and have tried to find solutions online before posting.

      in def next(self) I am checking the color of the candle. e.g

       if self.data.open[0] < self.data.close[0]:
                  self.candle_color = 'green'
      

      What I cannot work out is how to store this value for accessing in the same way we access other data. e.g

      self.candle_color[-1], self.candle_color[-2]
      

      etc

      I have tried adding

      lines = ('candle_color',)
      

      To the start of the strategy class but this does not appear to make any difference.

      If I try and access self.candle_color[-1] it will simply return the "n" of the word "green"

      Can anyone point me in the right direction to be able to store this value in the way I need?

      Many thanks

      E 1 Reply Last reply Reply Quote 0
      • E
        EMR @Olly Fallon last edited by

        @olly-fallon Not sure if you can set a string as value of a line. Maybe someone who know BT more than me will answer about this.
        Why not using an int, or a bool (which basically is a 0/1 value) ?
        for example, you can set in the _init of the strategy :

            def __init__(self):
                self.candle_is_green = bt.If(self.data.close > self.data.open, True, False)
        

        and check the result in the next :

            def next(self):
                print(f"self.candle_is_green[0] : {self.candle_is_green[0]}")
                print(f"self.candle_is_green[-1] : {self.candle_is_green[-1]}")
                print(f"self.candle_is_green[-2] : {self.candle_is_green[-2]}")
        

        Note that with this code, if open = close the candle_is_green would be False

        Olly Fallon 1 Reply Last reply Reply Quote 0
        • Olly Fallon
          Olly Fallon @EMR last edited by

          @emr That's great - I think I can work with it the way you have specified.

          Many thanks for taking the time to help out!

          E 1 Reply Last reply Reply Quote 0
          • E
            EMR @Olly Fallon last edited by

            @olly-fallon You are welcome, glad it helped !

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors