Add script for generating Double 5's swing trading signals

This commit is contained in:
moshferatu 2024-10-29 06:58:14 -07:00
parent c09d469894
commit 0f5335cfe2

22
strategies/double_5s.py Normal file
View File

@ -0,0 +1,22 @@
import pandas as pd
def signals(data: pd.DataFrame) -> pd.Series:
"""
Generate trading signals based on the following rules:
1. SPY is above its 200-day moving average.
2. Enter a long position if SPY closes at a 5-day low.
This strategy is based on the Double 7's strategy by Larry Connors.
Returns a Series with 'L' for long signals and 'N' for no signal.
"""
ma_200 = data['Close'].rolling(window = 200).mean()
low_5_day = data['Close'].rolling(window = 5).min()
above_200_ma = data['Close'] > ma_200
at_5_day_low = data['Close'] == low_5_day
signals = pd.Series('N', index = data.index)
signals[above_200_ma & at_5_day_low] = 'L'
return signals