feat: Add trading logic for BB, RSI, and EMA indicators in strategy
This commit is contained in:
parent
a18bc66b19
commit
e48fcfafbd
@ -69,7 +69,6 @@ class DynamicStrategy(Strategy):
|
|||||||
self.indicators[f"{ind_name}_lower"] = bb_vals[2]
|
self.indicators[f"{ind_name}_lower"] = bb_vals[2]
|
||||||
|
|
||||||
def next(self):
|
def next(self):
|
||||||
# Simple example strategy - should be customized based on user input
|
|
||||||
price = self.data.Close[-1]
|
price = self.data.Close[-1]
|
||||||
|
|
||||||
# Example trading logic using indicators
|
# Example trading logic using indicators
|
||||||
@ -91,6 +90,33 @@ class DynamicStrategy(Strategy):
|
|||||||
elif macd < signal and prev_macd >= prev_signal and self.position:
|
elif macd < signal and prev_macd >= prev_signal and self.position:
|
||||||
self.position.close()
|
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:
|
def get_available_indicators() -> Dict:
|
||||||
"""Returns available indicators and their parameters"""
|
"""Returns available indicators and their parameters"""
|
||||||
return {
|
return {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user