How to get the previous candle's value(close or ema or RSI))

I’ve been trying to tweak one of the strategies from algobulls strategy pool. I’m not able to get the previous candle’s close or any other indicator value. The point is to enter trade on the next candle when the current candle completely closes satisfying my set criteria. For example, If I want to buy when RSI of the previous close has crossed 30 in the upward direction and additionally the previous candle must satisfy a few other conditions as well. I’ve been trying the below method following another python library but it doesn’t seem to work. Any suggestions would be helpful.

rsi_value = talib.RSI(hist_data['close'], timeperiod=self.time_period)
        overbought_list = [self.overbought_value] * rsi_value.size
        oversold_list = [self.oversold_value] * rsi_value.size
        ema_one = talib.EMA(hist_data['close'], timeperiod=20)
        ema_two = talib.EMA(hist_data['close'], timeperiod=50)
        ema_three = talib.EMA(hist_data['close'], timeperiod=200)


        oversold_crossover_value = self.utils.crossover(rsi_value, oversold_list)
        overbought_crossover_value = self.utils.crossover(rsi_value, overbought_list)

        if oversold_crossover_value[-1] == -1 and hist_data[-1] > ema_one[-1] and ema_one[-1] > ema_two[-1] and ema_two[-1] > ema_three[-1]:
            action = self.ActionConstants.ENTRY_BUY_OR_EXIT_SELL
        elif overbought_crossover_value[-1] == 1 and hist_data[-1] < ema_one[-1] and ema_one[-1] < ema_two[-1] and ema_two[-1] < ema_three[-1]:
            action = self.ActionConstants.ENTRY_SELL_OR_EXIT_BUY
        else:
            action = self.ActionConstants.NO_ACTION

Hi @shakthi_jagdish,

The OHLC values of the latest candle can be accessed as follows:

  • hist_data['open'].iloc[-1]
  • hist_data['high'].iloc[-1]
  • hist_data['low'].iloc[-1]
  • hist_data['close'].iloc[-1]

You can also print the latest candle to see all the columns available including the OHLC columns as follows:

  • hist_data.iloc[-1]

The code snippet can be modified as follows to get the latest values:

Let me know if this works.
Also, do reach out again in case of any further issues.

Cheers!
` aki

1 Like

@aki_brainzz, the following code seems to work. I wasn’t able to attach iloc to oversold_crossover_value and overbought_crossover_value because it’s throwing an error. Apparently, those two are of type ‘int’. The print statement isn’t printing in the logs. I’m not sure how I can print the runtime variables. Please let me know how I can get the previous position’s oversold_crossover_value and overbought_crossover_value.

if oversold_crossover_value== -1 and hist_data.iloc[-1]['close'] > ema_one.iloc[-1] and ema_one.iloc[-1] > ema_two.iloc[-1] and ema_two.iloc[-1] > ema_three.iloc[-1]:
            action = self.ActionConstants.ENTRY_BUY_OR_EXIT_SELL
            print(oversold_crossover_value)
        elif overbought_crossover_value == 1 and hist_data.iloc[-1]['close'] < ema_one.iloc[-1] and ema_one.iloc[-1] < ema_two.iloc[-1] and ema_two.iloc[-1] < ema_three.iloc[-1]:
            action = self.ActionConstants.ENTRY_SELL_OR_EXIT_BUY
            print(overbought_crossover_value)
        else:
            action = self.ActionConstants.NO_ACTION

[/quote]

Hi @shakthi_jagdish,

You can print values using the built-in logger.

Example:

self.logger.info(f'OVERSOLD VALUE - {oversold_crossover_value}')
self.logger.info(f'OVERBOUGHT VALUE - {overbought_crossover_value}') 

This will print the values using an f-string as shown above.

The latest completed candle is taken into account and the values are printed repeatedly at every candle.

Screenshot for your reference:

Let me know if this works.
Also, do reach out again in case of any further issues.

Cheers!
` aki

1 Like

I have resorted to other methods to check the crossover. Its working fine for me now. Will reach out, in case of any queries. Just in case, some one else might need it - I’m checking the crossover of RSI with a user-defined function better suiting my needs.

     def get_crossover(self, rsi_value):
        if((rsi_value.iloc[-1] >= self.overbought_value) & (rsi_value.iloc[-2] < self.overbought_value)):
            crossover_value = 1
        elif((rsi_value.iloc[-1] <= self.oversold_value) & (rsi_value.iloc[-2] > self.oversold_value)):
            crossover_value = -1            
        else:
            crossover_value = 0
        return crossover_value 

Hi @shakthi_jagdish,

Thanks for the code snippet.

I am sure it will help others also.

Cheers!
` aki