Stratégie de tendance à double EMA croisée

Auteur:ChaoZhang est là., Date: 2023-12-29 à 15h46
Les étiquettes:

img

Résumé

Cette stratégie utilise la croix d'or et la croix de la mort des indicateurs EMA doubles pour déterminer la direction de la tendance actuelle, et combine l'indicateur RSI pour éviter de manquer des opportunités d'achat et de vente.

Principe de stratégie

  1. Calculer les lignes EMA à 10 périodes et à 20 périodes, nommées respectivement ma00 et ma01
  2. Un signal d'achat est généré lorsque ma00 dépasse ma01
  3. Un signal de vente est généré lorsque ma00 passe sous ma01
  4. En même temps, lorsque le prix dépasse ma00, si ma00 est supérieur à ma01, un signal d'achat sera également généré.
  5. De même, lorsque le prix dépasse ma00, si ma00 est inférieur à ma01, un signal de vente sera également généré.
  6. Grâce à cette double validation, certains points d'achat et de vente peuvent être évités
  7. Définir les prix stop loss et profit pour contrôler les risques

Analyse des avantages

  1. L' utilisation d' une double EMA pour déterminer les faux écarts peut filtrer efficacement
  2. La validation des deux conditions évite les commandes manquantes
  3. Les paramètres Stop Loss et Take Profit sont bénéfiques pour la gestion des risques

Analyse des risques

  1. La stratégie EMA double appartient à la stratégie de suivi de tendance.
  2. Il ne peut pas déterminer avec précision les points d'inversion de tendance, ce qui peut entraîner des pertes.
  3. Des réglages incorrects du point d'arrêt des pertes peuvent amplifier les pertes

Directions d'optimisation

  1. Les cycles EMA peuvent être correctement optimisés pour trouver la meilleure combinaison de paramètres
  2. D'autres indicateurs peuvent être ajoutés pour améliorer la stabilité de la stratégie
  3. Les arrêts dynamiques peuvent être réglés pour ajuster les points de stop loss en temps réel en fonction des fluctuations du marché

/*backtest
start: 2023-12-21 00:00:00
end: 2023-12-28 00:00:00
period: 2h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4

strategy(title='[STRATEGY][RS]MicuRobert EMA cross V1', shorttitle='S', overlay=true, pyramiding=0, initial_capital=100000)
USE_TRADESESSION = input(title='Use Trading Session?', type=bool, defval=true)
USE_TRAILINGSTOP = input(title='Use Trailing Stop?', type=bool, defval=true)
trade_session = input(title='Trade Session:',  defval='0400-1500', confirm=false)
istradingsession = not USE_TRADESESSION ? false : not na(time('1', trade_session))
bgcolor(istradingsession?color.gray:na)
trade_size = input(title='Trade Size:', type=float, defval=1)
tp = input(title='Take profit in pips:', type=float, defval=55.0) * (syminfo.mintick*10)
sl = input(title='Stop loss in pips:', type=float, defval=11.0) * (syminfo.mintick*10)
ma_length00 = input(title='EMA length:',  defval=10)
ma_length01 = input(title='DEMA length:',  defval=20)
price = input(title='Price source:', defval=open)

//  ||--- NO LAG EMA, Credit LazyBear:  ---||
f_LB_zlema(_src, _length)=>
    _ema1=ema(_src, _length)
    _ema2=ema(_ema1, _length)
    _d=_ema1-_ema2
    _zlema=_ema1+_d
//  ||-------------------------------------||

ma00 = f_LB_zlema(price, ma_length00)
ma01 = f_LB_zlema(price, ma_length01)
plot(title='M0', series=ma00, color=black)
plot(title='M1', series=ma01, color=black)

isnewbuy = change(strategy.position_size)>0 and change(strategy.opentrades)>0
isnewsel = change(strategy.position_size)<0 and change(strategy.opentrades)>0

buy_entry_price = isnewbuy ? price : buy_entry_price[1]
sel_entry_price = isnewsel ? price : sel_entry_price[1]
plot(title='BE', series=buy_entry_price, style=circles, color=strategy.position_size <= 0 ? na : aqua)
plot(title='SE', series=sel_entry_price, style=circles, color=strategy.position_size >= 0 ? na : aqua)
buy_appex = na(buy_appex[1]) ? price : isnewbuy ? high : high >= buy_appex[1] ? high : buy_appex[1]
sel_appex = na(sel_appex[1]) ? price : isnewsel ? low : low <= sel_appex[1] ? low : sel_appex[1]
plot(title='BA', series=buy_appex, style=circles, color=strategy.position_size <= 0 ? na : teal)
plot(title='SA', series=sel_appex, style=circles, color=strategy.position_size >= 0 ? na : teal)
buy_ts = buy_appex - sl
sel_ts = sel_appex + sl
plot(title='Bts', series=buy_ts, style=circles, color=strategy.position_size <= 0 ? na : red)
plot(title='Sts', series=sel_ts, style=circles, color=strategy.position_size >= 0 ? na : red)

buy_cond1 = crossover(ma00, ma01) and (USE_TRADESESSION ? istradingsession : true)
buy_cond0 = crossover(price, ma00) and ma00 > ma01 and (USE_TRADESESSION ? istradingsession : true)
buy_entry = buy_cond1 or buy_cond0
buy_close = (not USE_TRAILINGSTOP ? false : low <= buy_ts) or high>=buy_entry_price+tp//high>=last_traded_price + tp or low<=last_traded_price - sl //high >= hh or 
sel_cond1 = crossunder(ma00, ma01) and (USE_TRADESESSION ? istradingsession : true)
sel_cond0 = crossunder(price, ma00) and ma00 < ma01 and (USE_TRADESESSION ? istradingsession : true)
sel_entry = sel_cond1 or sel_cond0
sel_close = (not USE_TRAILINGSTOP ? false : high >= sel_ts) or low<=sel_entry_price-tp//low<=last_traded_price - tp or high>=last_traded_price + sl //low <= ll or 

strategy.entry('buy', long=strategy.long, qty=trade_size, comment='buy', when=buy_entry)
strategy.close('buy', when=buy_close)
strategy.entry('sell', long=strategy.short, qty=trade_size, comment='sell', when=sel_entry)
strategy.close('sell', when=sel_close)

//What i add .!
pos = iff(ma01 < ma00 , 1,
	    iff(ma01 > ma00 , -1, nz(pos[1], 0))) 
barcolor(pos == -1 ? red: pos == 1 ? green : blue)
plot(ma00, color=red, title="MA")
plot(ma01, color=blue, title="EMA")

Plus de