
La estrategia combina dos indicadores técnicos, el MACD y el RSI, para determinar la hora de negociación utilizando la señal de cruce del MACD y la señal de sobreventa y sobreventa del RSI. Al mismo tiempo, la estrategia también introduce el promedio móvil ponderado (WMA) como un criterio auxiliar para aumentar la fiabilidad de la estrategia. La estrategia funciona en un marco de tiempo de 1 hora, cuando el MACD tiene un tenedor y el RSI es mayor que 50 para abrir más posiciones, y cuando el MACD tiene un tenedor y el RSI es menor que 50 para abrir más posiciones.
El MACD está compuesto por diferencias entre la línea rápida (medios móviles a corto plazo) y la línea lenta (medios móviles a largo plazo), que reflejan los cambios en la tendencia del mercado. Cuando la línea rápida atraviesa la línea lenta, se forma un tenedor de oro, que indica una tendencia alcista, y viceversa, se forma un tenedor muerto, que indica una tendencia descendente. El RSI es un indicador que mide el estado de sobreventa de la compra en el mercado.
La estrategia combina el MACD y el RSI para aprovechar el juicio de tendencia del MACD y el juicio de sobreventa y sobreventa del RSI para capturar el momento de negociación con mayor precisión. Al mismo tiempo, la estrategia también introduce un promedio móvil ponderado (WMA) como un juicio auxiliar, que da más importancia a los precios recientes que el promedio móvil ordinario y puede reflejar los cambios de precios de manera más sensible.
Además, la estrategia también establece variables de varios marcos de tiempo (como 15 minutos, 30 minutos, 1 hora, 2 horas, etc.) para juzgar los cambios de tendencia en diferentes escalas de tiempo. Este método de análisis de varios marcos de tiempo puede ayudar a la estrategia a capturar las tendencias del mercado de manera más completa y mejorar la precisión de las decisiones.
La estrategia combina dos indicadores técnicos efectivos, el MACD y el RSI, y introduce WMA como juicio auxiliar, para tomar decisiones comerciales en un marco de tiempo de 1 hora. La lógica de la estrategia es clara, fácil de entender e implementar, puede capturar mejor las tendencias del mercado y el estado de sobreventa y sobreventa, y tiene cierta viabilidad. Sin embargo, la estrategia también tiene algunas limitaciones y riesgos, como la demora, un solo marco de tiempo, la falta de control de riesgo, etc.
/*backtest
start: 2024-05-01 00:00:00
end: 2024-05-31 23:59:59
period: 2h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Improved MACD and RSI Trading Strategy", overlay=true, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.01, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// MACD 設置
fast_length = input(12, title="MACD Fast Length")
slow_length = input(26, title="MACD Slow Length")
signal_smoothing = input(9, title="MACD Signal Smoothing")
// RSI 設置
input_rsi_length = input.int(14, title="RSI Length")
input_rsi_source = input(close, "RSI Source")
RSI = ta.rsi(input_rsi_source, input_rsi_length)
// 計算MACD和信號線
[macdLine, signalLine, _] = ta.macd(close, fast_length, slow_length, signal_smoothing)
// 自然交易理論:利用MACD和RSI的結合
ma(source, length, type) =>
switch type
"SMA" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)
maTypeInput = input.string("SMA", title="Moving Average Type", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(14, title="Moving Average Length", group="MA Settings")
macdMA = ma(macdLine, maLengthInput, maTypeInput)
// 設置交易信號
longCondition = ta.crossover(macdLine, signalLine) and macdLine > macdMA and RSI < 70
shortCondition = ta.crossunder(macdLine, signalLine) and macdLine < macdMA and RSI > 30
// 定義時間框架
tf_15m = ta.change(RSI, 15) > 0 ? 1 : 0
tf_30m = ta.change(RSI, 30) > 0 ? 1 : 0
tf_1h = ta.change(RSI, 60) > 0 ? 1 : 0
tf_2h = ta.change(RSI, 120) > 0 ? 1 : 0
tf_4h = ta.change(RSI, 240) > 0 ? 1 : 0
tf_6h = ta.change(RSI, 360) > 0 ? 1 : 0
tf_8h = ta.change(RSI, 480) > 0 ? 1 : 0
tf_12h = ta.change(RSI, 720) > 0 ? 1 : 0
tf_1d = ta.change(RSI, 1440) > 0 ? 1 : 0
// 設置開倉、平倉和空倉條件
if (longCondition and tf_1h and RSI > 50)
strategy.entry("Long", strategy.long)
if (shortCondition and tf_1h and RSI < 50)
strategy.entry("Short", strategy.short)
if (tf_1h and RSI > 70)
strategy.close("Long")
if (tf_1h and RSI < 30)
strategy.close("Short")
// 加入其他策略
// 定義加權平均價格
wma(source, length) =>
wma = 0.0
sum = 0.0
sum_wts = 0.0
for i = 0 to length - 1
wts = (length - i) * (length - i)
sum := sum + source[i] * wts
sum_wts := sum_wts + wts
wma := sum / sum_wts
wmaLength = input.int(20, title="WMA Length", group="Other Strategies")
wmaValue = wma(close, wmaLength)
// 設置交易信號
longWMACondition = close > wmaValue
shortWMACondition = close < wmaValue
if (longWMACondition and tf_1h and RSI > 50)
strategy.entry("Long WMA", strategy.long)
if (shortWMACondition and tf_1h and RSI < 50)
strategy.entry("Short WMA", strategy.short)
if (tf_1h and RSI > 70)
strategy.close("Long WMA")
if (tf_1h and RSI < 30)
strategy.close("Short WMA")
// 繪製MACD和RSI
plot(macdLine, color=color.blue, title="MACD Line")
plot(signalLine, color=color.red, title="Signal Line")