feat: Add trading logic for new technical indicators in DynamicStrategy

This commit is contained in:
Bobby (aider) 2025-02-14 00:34:40 -08:00
parent 0033d433c4
commit 071559a8e1

View File

@ -176,7 +176,6 @@ class DynamicStrategy(Strategy):
current_sma = self.indicators[ind_name][-1] current_sma = self.indicators[ind_name][-1]
print(f"SMA - Price: {price:.2f}, SMA: {current_sma:.2f}") print(f"SMA - Price: {price:.2f}, SMA: {current_sma:.2f}")
if price > current_sma and not self.position: if price > current_sma and not self.position:
# Set stop loss at 7% below the entry price
stop_loss = price * 0.93 stop_loss = price * 0.93
print(f"BUY signal - Price above SMA with stop loss at {stop_loss:.2f}") print(f"BUY signal - Price above SMA with stop loss at {stop_loss:.2f}")
self.buy(sl=stop_loss) self.buy(sl=stop_loss)
@ -235,6 +234,120 @@ class DynamicStrategy(Strategy):
print(f"SELL signal - Price below EMA") print(f"SELL signal - Price below EMA")
self.position.close() self.position.close()
elif ind_config['type'] == 'WMA':
current_wma = self.indicators[ind_name][-1]
print(f"WMA - Price: {price:.2f}, WMA: {current_wma:.2f}")
if price > current_wma and not self.position:
stop_loss = price * 0.93
print(f"BUY signal - Price above WMA with stop loss at {stop_loss:.2f}")
self.buy(sl=stop_loss)
elif price < current_wma and self.position:
print(f"SELL signal - Price below WMA")
self.position.close()
elif ind_config['type'] == 'DEMA':
current_dema = self.indicators[ind_name][-1]
print(f"DEMA - Price: {price:.2f}, DEMA: {current_dema:.2f}")
if price > current_dema and not self.position:
stop_loss = price * 0.93
print(f"BUY signal - Price above DEMA with stop loss at {stop_loss:.2f}")
self.buy(sl=stop_loss)
elif price < current_dema and self.position:
print(f"SELL signal - Price below DEMA")
self.position.close()
elif ind_config['type'] == 'TEMA':
current_tema = self.indicators[ind_name][-1]
print(f"TEMA - Price: {price:.2f}, TEMA: {current_tema:.2f}")
if price > current_tema and not self.position:
stop_loss = price * 0.93
print(f"BUY signal - Price above TEMA with stop loss at {stop_loss:.2f}")
self.buy(sl=stop_loss)
elif price < current_tema and self.position:
print(f"SELL signal - Price below TEMA")
self.position.close()
elif ind_config['type'] == 'HMA':
current_hma = self.indicators[ind_name][-1]
print(f"HMA - Price: {price:.2f}, HMA: {current_hma:.2f}")
if price > current_hma and not self.position:
stop_loss = price * 0.93
print(f"BUY signal - Price above HMA with stop loss at {stop_loss:.2f}")
self.buy(sl=stop_loss)
elif price < current_hma and self.position:
print(f"SELL signal - Price below HMA")
self.position.close()
elif ind_config['type'] == 'VWAP':
current_vwap = self.indicators[ind_name][-1]
print(f"VWAP - Price: {price:.2f}, VWAP: {current_vwap:.2f}")
if price > current_vwap and not self.position:
stop_loss = price * 0.93
print(f"BUY signal - Price above VWAP with stop loss at {stop_loss:.2f}")
self.buy(sl=stop_loss)
elif price < current_vwap and self.position:
print(f"SELL signal - Price below VWAP")
self.position.close()
elif ind_config['type'] == 'Stochastic':
k = self.indicators[f"{ind_name}_k"][-1]
d = self.indicators[f"{ind_name}_d"][-1]
prev_k = self.indicators[f"{ind_name}_k"][-2]
prev_d = self.indicators[f"{ind_name}_d"][-2]
print(f"Stochastic - K: {k:.2f}, D: {d:.2f}")
if k > d and prev_k <= prev_d and k < 20 and not self.position:
stop_loss = price * 0.93
print(f"BUY signal - Stochastic crossover in oversold territory with stop loss at {stop_loss:.2f}")
self.buy(sl=stop_loss)
elif k < d and prev_k >= prev_d and k > 80 and self.position:
print(f"SELL signal - Stochastic crossunder in overbought territory")
self.position.close()
elif ind_config['type'] == 'ADX':
adx = self.indicators[ind_name][-1]
print(f"ADX - Value: {adx:.2f}")
if adx > 25 and not self.position:
stop_loss = price * 0.93
print(f"BUY signal - Strong trend detected with stop loss at {stop_loss:.2f}")
self.buy(sl=stop_loss)
elif adx < 20 and self.position:
print(f"SELL signal - Weak trend")
self.position.close()
elif ind_config['type'] == 'CCI':
cci = self.indicators[ind_name][-1]
print(f"CCI - Value: {cci:.2f}")
if cci < -100 and not self.position:
stop_loss = price * 0.93
print(f"BUY signal - CCI oversold with stop loss at {stop_loss:.2f}")
self.buy(sl=stop_loss)
elif cci > 100 and self.position:
print(f"SELL signal - CCI overbought")
self.position.close()
elif ind_config['type'] == 'MFI':
mfi = self.indicators[ind_name][-1]
print(f"MFI - Value: {mfi:.2f}")
if mfi < 20 and not self.position:
stop_loss = price * 0.93
print(f"BUY signal - MFI oversold with stop loss at {stop_loss:.2f}")
self.buy(sl=stop_loss)
elif mfi > 80 and self.position:
print(f"SELL signal - MFI overbought")
self.position.close()
elif ind_config['type'] == 'Williams%R':
willr = self.indicators[ind_name][-1]
print(f"Williams%R - Value: {willr:.2f}")
if willr < -80 and not self.position:
stop_loss = price * 0.93
print(f"BUY signal - Williams%R oversold with stop loss at {stop_loss:.2f}")
self.buy(sl=stop_loss)
elif willr > -20 and self.position:
print(f"SELL signal - Williams%R overbought")
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 {