Comparing current data with another at some point in time?
-
I have the below code and have added a custom pandas dataframe with a column called Peaks.
What I want is to be able to compare the current close of a candle to a previous high. The previous high is set to 0 and is updated with every next and wherever Peaks is 1, previous high is updated to that candles high.
Since next is called at least once on every candle, my way of comparing is incorrect and what I really want is to be able to compare current candles close to whatever the high of the candle was when Peaks == 1. I am not sure of how to do this, any help will be great.
def next(self): prev_high = 0 if self.data.Peaks[0] == 1.0: prev_high = self.high[0] if self.close[0] > prev_high: print(f'Date: {self.datas[0].datetime.date()}, Close: {self.close[0]}, Prev High: {prev_high}')
-
@vypy1
I think you need to setprev_high
as a parameter of the strategy. This will allow it to carry forward to the nextnext
. (that sounds odd)def __init__(self): self.prev_high = 0
Then:
def next(self): if self.data.Peaks[0] == 1.0: self.prev_high = self.high[0] if self.close[0] > self.prev_high: print(f'Date: {self.datas[0].datetime.date()}, Close: {self.close[0]}, Prev High: {self.prev_high}')
-
@run-out That did not sound odd at all :P
Thanks this works, this is what I was expecting it to do.