
La stratégie est un système de trading automatisé basé sur les indicateurs MACD, combiné avec un mécanisme de stop-loss dynamique. Le cœur de la stratégie est de déterminer les signaux de trading par le croisement des lignes MACD et des lignes de signal, tout en intégrant des fonctions de gestion des risques telles que le pourcentage de stop-loss, le profit cible et le suivi des stop-loss, ce qui permet des transactions entièrement automatisées.
La logique fondamentale de la stratégie comprend les éléments clés suivants :
Grâce à la signalisation croisée des indicateurs MACD et à un système de gestion des risques bien développé, la stratégie a permis de construire un système de négociation automatisé robuste. Bien qu’il y ait un certain espace d’optimisation, le cadre de base est déjà bien développé. Grâce à l’optimisation et à l’amélioration continues, la stratégie devrait être capable de maintenir une performance stable dans différents environnements de marché.
/*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)