There are times you want to have some entry checks that rarely happen in a same candle. For example, let’s say I have these checks
- rsi have crossed above 30 in the past 10 candles maximum
- close price below EMA 40
Is it possible for both logic to happen in the same candle? Yes, it is possible, but the likelihood is low. Giving the first check a 10-candle lookback period increases the likelihood that both logics will be met. First, I’ll share the code, then I’ll explain it.
def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: dataframe.loc[ ( (qtpylib.crossed_above(dataframe['rsi'], 30).rolling(10).max() > 0) & (dataframe['close'] < dataframe['ema_40']) ), ['enter_long', 'enter_tag']] = (1, 'delayed_entry') return dataframe
I believe the
(dataframe['close'] < dataframe['ema_40']) logic don’t need any explanation, so I’ll just explain the first logic. First
qtpylib.crossed_above(dataframe['rsi'], 30) check whether rsi has crossed above 30. After that, since you want to give it maximum lookback period of 10 candles, you add
.rolling(10) to it. Next, to know whether the first logic ever triggered in the last 10 candles, you need to understand first that True and False are also treated as 1 and 0. So if the logic never triggered in the past 10 candles, then
.rolling(10).max() will give you 0. But if it ever triggered even just once, the max value will be 1.
That’s how you do a delayed entry trigger. The issue with this method is that you have to define the maximum number of candles for the lookback. You have to do your own research to get the optimal lookback number. But what if you don’t want to define the lookback limit? I’ll explain it later in part 2