swing-trading-dashboard/strategies/double_5s.py

22 lines
710 B
Python

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