Adding a value to a line
-
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
-
@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
-
@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!
-
@olly-fallon You are welcome, glad it helped !