Stratégie de négociation de rupture de l'indicateur MACD

Auteur:ChaoZhang est là., Date: 12 septembre 2023 à 15h32h12
Les étiquettes:

Cette stratégie consiste à échanger des signaux de croisement MACD pour les décisions d'entrée et de sortie.

La logique de la stratégie:

  1. Calculez l'EMA rapide et l'EMA lente, leur différence forme la ligne MACD.

  2. Lisser la ligne MACD en utilisant une autre EMA pour dériver la ligne de signal.

  3. Allez long sur le MACD qui traverse au-dessus du signal, et court sur celui qui traverse en dessous.

  4. Définissez un pourcentage de stop loss et de profit pour la gestion des risques.

Les avantages:

  1. Le MACD s'améliore par rapport à l'EMA unique pour une identification plus claire de la tendance.

  2. Le trading de rupture capte les points tournants en temps opportun.

  3. Les mécanismes de stop loss/take profit aident à contrôler les risques commerciaux.

Les risques:

  1. Plus de fausses éruptions près de la ligne zéro du MACD.

  2. Paramètres de réglage nécessaires pour différents instruments de négociation.

  3. Le trading de tendance est sujet aux risques d'événements, nécessitant des arrêts.

En résumé, cette stratégie est basée sur le MACD et le croisement des lignes de signal. Les forces du MACD bénéficient de la performance mais les risques de fausse rupture demeurent. Des contrôles prudents des risques sont toujours nécessaires pour des gains stables à long terme.


/*backtest
start: 2023-01-01 00:00:00
end: 2023-09-11 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=3 
strategy("uray MACD", overlay=false, pyramiding = 0, calc_on_every_tick=true, precision=2, currency="USD", default_qty_value=10, default_qty_type=strategy.cash,initial_capital=100,commission_type=strategy.commission.percent,commission_value=0.1) 

// === INPUT BACKTEST RANGE ===
FromMonth = input(defval = 6, title = "From Month", minval = 1, maxval = 12)
FromDay   = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromYear  = input(defval = 2018, title = "From Year", minval = 2017)
ToMonth   = input(defval = 6, title = "To Month", minval = 1, maxval = 12)
ToDay     = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear    = input(defval = 2020, title = "To Year", minval = 2017)

// === FUNCTION EXAMPLE ===
start     = timestamp(FromYear, FromMonth, FromDay, 00, 00)  // backtest start window
finish    = timestamp(ToYear, ToMonth, ToDay, 23, 59)        // backtest finish window
inTimeframe()  => true


isPosLong    = strategy.position_size > 0
isPosShort   = strategy.position_size < 0
isNoMarginPos= strategy.position_size == 0

fastLength    = input(12, minval = 1, title = "MACD fast")
slowlength    = input(26, minval = 1, title = "MACD slow")
MACDLength    = input( 9, minval = 1, title = "MACD length")
stopLoss      = input( 10, minval = 1, title = "Stop Loss (price %)", type=float)
takeProfit    = input( 50, minval = 1, title = "Take Profit (price %)", type=float)
src           = close   // Source of Calculations (Close of Bar)

MACD  = ta.ema(src, fastLength) - ta.ema(src, slowlength)
aMACD = ta.ema(MACD, MACDLength)
delta = MACD - aMACD
stopLossValue      = close*(stopLoss/100)/syminfo.mintick
takeProfitValue    = close*(takeProfit/100)/syminfo.mintick
switchLongTrigger  = ta.crossover(delta, 0)
switchShortTrigger = ta.crossunder(delta, 0)
closeLongTrigger   = ta.crossunder(delta, 0)
closeShortTrigger  = ta.crossover(delta, 0)
entryLongTrigger   = ta.crossover(delta, 0)
entryShortTrigger  = ta.crossunder(delta, 0)

// if inTimeframe()
//     if isNoMarginPos
//         if entryLongTrigger
//             strategy.entry("Long", strategy.long,  comment="Entry Long")
//             strategy.exit("Stop (long SL/TP)", loss=stopLossValue, profit=takeProfitValue)   
//         if entryShortTrigger
//             strategy.entry("Short", strategy.short, comment="Entry Short")  
//             strategy.exit("Stop (short SL/TP)", loss=stopLossValue, profit=takeProfitValue)   
//     if isPosShort
//         if switchLongTrigger
//             strategy.close_all()    
//             strategy.entry("Long", strategy.long, comment="switch Long")
//             strategy.exit("Stop (long SL/TP)", loss=stopLossValue, profit=takeProfitValue)   
//         if closeLongTrigger
//             strategy.close_all()    
//     if isPosLong 
//         if switchShortTrigger
//             strategy.close_all()   
//             strategy.entry("Short", strategy.short, comment="switch Short")
//             strategy.exit("Stop (short SL/TP)", loss=stopLossValue, profit=takeProfitValue) 
//         if closeShortTrigger
//             strategy.close_all()   

if inTimeframe()
    strategy.entry("Long", strategy.long,  comment="Entry Long", when=entryLongTrigger)
    strategy.close("Long", when=entryShortTrigger)
    strategy.entry("Short", strategy.short,  comment="Entry Short", when=entryShortTrigger)
    strategy.close("Short", when=entryLongTrigger)
    strategy.exit("Stop (short SL/TP)", loss=stopLossValue, profit=takeProfitValue, when=entryShortTrigger) 
    strategy.exit("Stop (long SL/TP)", loss=stopLossValue, profit=takeProfitValue, when=entryLongTrigger) 




Plus de