
A estratégia é uma estratégia de negociação quantitativa que combina a regressão do valor médio e o acompanhamento de tendências, principalmente através da combinação de três indicadores técnicos MA, MACD e ATR para gerar sinais de negociação e controle de risco. A idéia central da estratégia é capturar oportunidades de reversão do mercado quando os preços se desviam da linha de equilíbrio, combinando sinais cruzados do indicador MACD, enquanto usa o stop loss dinâmico do ATR para controlar o risco.
A estratégia utiliza um mecanismo de tripla verificação:
A estratégia permite um sistema de negociação relativamente robusto, através da combinação de regressão de média e acompanhamento de tendências. O mecanismo de verificação de múltiplos indicadores aumenta a confiabilidade dos sinais de negociação, enquanto o stop loss dinâmico do ATR controla bem o risco. Embora haja algum espaço para otimização, é, em geral, um quadro de estratégia lógicamente claro e prático.
/*backtest
start: 2024-10-01 00:00:00
end: 2024-10-31 23:59:59
period: 3h
basePeriod: 3h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Mean Reversion Strategy with ATR, MACD and MA", overlay=true)
// === Настройки для индикаторов ===
// Параметры скользящей средней (MA)
maLength = input.int(30, title="Период скользящей средней (MA)")
maType = input.string("EMA", title="Тип скользящей средней", options=["SMA", "EMA"])
// Параметры ATR
atrLength = input.int(10, title="Период ATR")
atrMultiplier = input.float(10, title="ATR множитель для стоп-лосса")
// Параметры MACD
macdFastLength = input.int(8, title="Период быстрой EMA для MACD")
macdSlowLength = input.int(26, title="Период медленной EMA для MACD")
macdSignalLength = input.int(5, title="Период сигнальной линии MACD")
// === Рассчёт индикаторов ===
// Скользящая средняя
ma = if maType == "SMA"
ta.sma(close, maLength)
else
ta.ema(close, maLength)
// ATR (Средний истинный диапазон)
atr = ta.atr(atrLength)
// MACD
[macdLine, signalLine, _] = ta.macd(close, macdFastLength, macdSlowLength, macdSignalLength)
// Условия для входа на покупку и продажу
longCondition = ta.crossover(macdLine, signalLine) and close < ma
shortCondition = ta.crossunder(macdLine, signalLine) and close > ma
// === Управление позициями ===
if (longCondition)
strategy.entry("Buy", strategy.long)
// Стоп-лосс на основе ATR
stopLossLevel = close - atr * atrMultiplier
strategy.exit("Take Profit/Stop Loss", "Buy", stop=stopLossLevel)
if (shortCondition)
strategy.entry("Sell", strategy.short)
// Стоп-лосс на основе ATR
stopLossLevel = close + atr * atrMultiplier
strategy.exit("Take Profit/Stop Loss", "Sell", stop=stopLossLevel)
// Визуализация
plot(ma, title="MA", color=color.blue, linewidth=2)
plot(macdLine, title="MACD Line", color=color.green)
plot(signalLine, title="Signal Line", color=color.red)
hline(0, "Zero Line", color=color.gray)