Strategy on rejection from support and resistance

@Tradehull_Imran
Need your input on this. Is there any change require in this code. Please feel free to modify and help me complete this for intraday strategy for Nifty 50 at TF 15 mins
Strike price should be at Delta = > 0.7

Calculate the 20-period Exponential Moving Average (EMA)

df[‘EMA_20’] = df[‘Close’].ewm(span=20, adjust=False).mean()

Calculate the Volume Weighted Average Price (VWAP)

df[‘VWAP’] = (df[‘Close’] * df[‘Volume’]).cumsum() / df[‘Volume’].cumsum()

Calculate previous day high and low

df[‘Prev_Day_High’] = df[‘High’].shift(1)
df[‘Prev_Day_Low’] = df[‘Low’].shift(1)

Determine bullish or bearish condition based on EMA, VWAP, and previous day levels

df[‘Bullish_Condition’] = (df[‘Close’] > df[‘EMA_20’]) & (df[‘Close’] > df[‘VWAP’]) & (df[‘Close’] > df[‘Prev_Day_High’])
df[‘Bearish_Condition’] = (df[‘Close’] < df[‘EMA_20’]) & (df[‘Close’] < df[‘VWAP’]) & (df[‘Close’] < df[‘Prev_Day_Low’])

Define rejection from support (EMA, VWAP, or previous day low)

df[‘Rejection_From_Support’] = ((df[‘Low’] <= df[‘EMA_20’]) & (df[‘Close’] > df[‘Open’])) |
((df[‘Low’] <= df[‘VWAP’]) & (df[‘Close’] > df[‘Open’])) |
((df[‘Low’] <= df[‘Prev_Day_Low’]) & (df[‘Close’] > df[‘Open’]))

Define rejection from resistance (EMA, VWAP, or previous day high)

df[‘Rejection_From_Resistance’] = ((df[‘High’] >= df[‘EMA_20’]) & (df[‘Close’] < df[‘Open’])) |
((df[‘High’] >= df[‘VWAP’]) & (df[‘Close’] < df[‘Open’])) |
((df[‘High’] >= df[‘Prev_Day_High’]) & (df[‘Close’] < df[‘Open’]))

Define confirmation candle conditions

df[‘Confirmation_Candle_Long’] = df[‘Close’] > df[‘High’].shift(1)
df[‘Confirmation_Candle_Short’] = df[‘Close’] < df[‘Low’].shift(1)

Generate buy and sell signals with confirmation candle

df[‘Buy_Signal’] = df[‘Rejection_From_Support’] & df[‘Bullish_Condition’] & df[‘Confirmation_Candle_Long’].shift(1)
df[‘Sell_Signal’] = df[‘Rejection_From_Resistance’] & df[‘Bearish_Condition’] & df[‘Confirmation_Candle_Short’].shift(1)

Calculate the Average True Range (ATR) for stop loss calculation

df[‘ATR_14’] = df[‘High’].rolling(window=14).max() - df[‘Low’].rolling(window=14).min()

for i in range(1, len(df)):
if df[‘Buy_Signal’].iloc[i]:
stop_loss_long = df[‘Low’].iloc[i] - df[‘ATR_14’].iloc[i]
target_long = df[‘Close’].iloc[i] + 2 * (df[‘Close’].iloc[i] - stop_loss_long)
df[‘Position’].iloc[i] = df[‘Cash’].iloc[i - 1] / df[‘Close’].iloc[i]
df[‘Cash’].iloc[i] = 0
df.loc[i, ‘Stop_Loss_Long’] = stop_loss_long
df.loc[i, ‘Target_Long’] = target_long
elif df[‘Sell_Signal’].iloc[i] and df[‘Position’].iloc[i - 1] > 0:
stop_loss_short = df[‘High’].iloc[i] + df[‘ATR_14’].iloc[i]
target_short = df[‘Close’].iloc[i] - 2 * (stop_loss_short - df[‘Close’].iloc[i])
df[‘Cash’].iloc[i] = df[‘Position’].iloc[i - 1] * df[‘Close’].iloc[i]
df[‘Position’].iloc[i] = 0
df.loc[i, ‘Stop_Loss_Short’] = stop_loss_short
df.loc[i, ‘Target_Short’] = target_short
else:
df[‘Position’].iloc[i] = df[‘Position’].iloc[i - 1]
df[‘Cash’].iloc[i] = df[‘Cash’].iloc[i - 1]

df['Portfolio_Value'].iloc[i] = df['Cash'].iloc[i] + df['Position'].iloc[i] * df['Close'].iloc[i]
1 Like

Hi @Altaf_Sheikh

The code for detecting rejection from S/R sees to be good,

However we also need to manage position information as well.
Also we need to manage multiple positions simultaneously.

These topics I will be covering in detail in upcoming advance algo series

1 Like

Also, do post all queries on this thread: Learn Algo Trading with Python | Codes | Youtube Series

This helps me keep track of questions and ensures other traders benefit as well, as they might be facing similar issues.

2 Likes

Can you please help me to complete the code with position or I will wait for you next session to complete it.

Thank you for reviewing the code.

1 Like