
Esta estratégia é um sistema de negociação automatizado baseado em indicadores MACD, combinado com um mecanismo de stop-loss dinâmico. O núcleo da estratégia é determinar os sinais de negociação através da intersecção das linhas MACD com as linhas de sinal, além de integrar funções de gerenciamento de risco, como percentual de stop-loss, ganho alvo e rastreamento de stop-loss, permitindo negociações totalmente automatizadas.
A lógica central da estratégia inclui as seguintes partes principais:
A estratégia, através de sinais cruzados dos indicadores MACD e um sistema de gerenciamento de risco perfeito, constrói um sistema de negociação automatizado robusto. Embora haja algum espaço para otimização, a estrutura básica já está bastante perfeita. Com otimização e melhoria contínuas, a estratégia deve manter um desempenho estável em diferentes ambientes de mercado.
/*backtest
start: 2024-01-01 00:00:00
end: 2024-11-01 00:00:00
period: 12h
basePeriod: 12h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © traderhub
//@version=5
strategy("MACD Strategy with Settings", overlay=true)
// Параметры MACD в контрольной панели
fastLength = input.int(12, title="Fast Length", minval=1, maxval=50)
slowLength = input.int(26, title="Slow Length", minval=1, maxval=50)
signalSmoothing = input.int(9, title="Signal Smoothing", minval=1, maxval=50)
// Параметры риска
stopLossPerc = input.float(1, title="Stop Loss (%)", step=0.1) // Стоп-лосс в процентах
takeProfitPerc = input.float(2, title="Take Profit (%)", step=0.1) // Тейк-профит в процентах
trailStopPerc = input.float(1.5, title="Trailing Stop (%)", step=0.1) // Трейлинг-стоп в процентах
// Вычисляем MACD
[macdLine, signalLine, _] = ta.macd(close, fastLength, slowLength, signalSmoothing)
// Показываем MACD и сигнальную линию на графике
plot(macdLine, color=color.blue, title="MACD Line")
plot(signalLine, color=color.red, title="Signal Line")
hline(0, "Zero Line", color=color.gray)
// Условия для покупки и продажи
longCondition = ta.crossover(macdLine, signalLine) // Покупка при пересечении MACD вверх сигнальной линии
shortCondition = ta.crossunder(macdLine, signalLine) // Продажа при пересечении MACD вниз сигнальной линии
// Расчет стоп-лосса и тейк-профита
var float longStopLevel = na
var float longTakeProfitLevel = na
if (longCondition)
longStopLevel := strategy.position_avg_price * (1 - stopLossPerc / 100)
longTakeProfitLevel := strategy.position_avg_price * (1 + takeProfitPerc / 100)
strategy.entry("Long", strategy.long)
if (strategy.position_size > 0)
// Установка стоп-лосса и тейк-профита
strategy.exit("Take Profit/Stop Loss", "Long", stop=longStopLevel, limit=longTakeProfitLevel, trail_offset=trailStopPerc)
// Закрытие позиции при медвежьем сигнале
if (shortCondition)
strategy.close("Long")
strategy.entry("Short", strategy.short)