swing-trading-dashboard/strategies/internal_bar_strength_band.py

22 lines
759 B
Python
Raw Normal View History

import pandas as pd
def signals(data: pd.DataFrame) -> pd.Series:
"""
Calculate swing trading signals based on Internal Bar Strength Band strategy.
Returns a Series with 'L' for long signals and 'N' otherwise.
"""
ma_200 = data['Close'].rolling(window = 200).mean()
rolling_high_minus_low = (data['High'] - data['Low']).rolling(window = 25).mean()
rolling_high = data['High'].rolling(window = 10).max()
lower_band = rolling_high - 2.5 * rolling_high_minus_low
ibs = (data['Close'] - data['Low']) / (data['High'] - data['Low'])
signals = (
(data['Close'] > ma_200) &
(data['Close'] < lower_band) &
(ibs < 0.3)
).apply(lambda condition: 'L' if condition else 'N')
return signals