feat: Add trading logic for BB, RSI, and EMA indicators in strategy

This commit is contained in:
Bobby (aider) 2025-02-13 23:16:58 -08:00
parent a18bc66b19
commit e48fcfafbd

View File

@ -69,7 +69,6 @@ class DynamicStrategy(Strategy):
self.indicators[f"{ind_name}_lower"] = bb_vals[2]
def next(self):
# Simple example strategy - should be customized based on user input
price = self.data.Close[-1]
# Example trading logic using indicators
@ -91,6 +90,33 @@ class DynamicStrategy(Strategy):
elif macd < signal and prev_macd >= prev_signal and self.position:
self.position.close()
elif ind_config['type'] == 'BB':
upper = self.indicators[f"{ind_name}_upper"][-1]
lower = self.indicators[f"{ind_name}_lower"][-1]
# Buy when price touches lower band
if price <= lower and not self.position:
self.buy()
# Sell when price touches upper band
elif price >= upper and self.position:
self.position.close()
elif ind_config['type'] == 'RSI':
rsi = self.indicators[ind_name][-1]
# Buy when RSI is below 30 (oversold)
if rsi < 30 and not self.position:
self.buy()
# Sell when RSI is above 70 (overbought)
elif rsi > 70 and self.position:
self.position.close()
elif ind_config['type'] == 'EMA':
if price > self.indicators[ind_name][-1] and not self.position:
self.buy()
elif price < self.indicators[ind_name][-1] and self.position:
self.position.close()
def get_available_indicators() -> Dict:
"""Returns available indicators and their parameters"""
return {