The Stop and Reverse Strategy : Using the Parabolic SAR and ADX
Step 1
pandas
Step 2
ta
Step 3
import pandas as pd
import ta
# Assuming df is your DataFrame with 'Close' prices
df['parabolic_sar'] = ta.trend.PSARIndicator(high=df['High'], low=df['Low'], close=df['Close']).psar()
Step 4
# Using the same DataFrame 'df'
adx = ta.trend.ADXIndicator(high=df['High'], low=df['Low'], close=df['Close'])
df['adx'] = adx.adx()
df['+DI'] = adx.adx_pos()
df['-DI'] = adx.adx_neg()
Step 5
def parabolic_sar_strategy(df):
buy_signals = (df['Close'].shift(1) > df['parabolic_sar'].shift(1)) & (df['Close'] <= df['parabolic_sar'])
sell_signals = (df['Close'].shift(1) < df['parabolic_sar'].shift(1)) & (df['Close'] >= df['parabolic_sar'])
return buy_signals, sell_signals
Step 6
def adx_strategy(df):
buy_signals = (df['+DI'] > df['-DI']) & (df['adx'] > 20)
sell_signals = (df['-DI'] > df['+DI']) & (df['adx'] > 20)
return buy_signals, sell_signals
Step 7
def combined_strategy(df):
sar_buy, sar_sell = parabolic_sar_strategy(df)
adx_buy, adx_sell = adx_strategy(df)
buy_signals = sar_buy & adx_buy
sell_signals = sar_sell & adx_sell
return buy_signals, sell_signals
Step 8
pandas
Step 9
ta