Trois moyennes mobiles exponentielles et stratégie de négociation de l'indice de force relative stochastique

Auteur:ChaoZhang est là., Date: 2024-01-30 16:52:48 Je suis désolé
Les étiquettes:

img

Résumé

Il s'agit d'une stratégie de suivi de tendance qui combine la moyenne mobile exponentielle triple (EMA) et l'indice de force relative stochastique (Stoch RSI) pour générer des signaux de trading.

Principaux

  1. Utilisez des EMA de 8, 14, 50 jours. Allez long lorsque l'EMA de 8 jours > 14 jours > 50 jours. Allez court quand c'est le contraire.

  2. Utilisez le RSI stochastique comme indicateur auxiliaire. Calculez d'abord le RSI de 14 jours, puis calculez le Stochastique sur le RSI, enfin calculez la SMA de 3 jours comme ligne K et la SMA de 3 jours sur la ligne K comme ligne D. Le croisement de K sur D donne un signal long.

  3. Enregistrer les transactions longues lorsque la clôture est supérieure à l'EMA de 8 jours sur le signal long.

  4. Le montant de l'impôt sur les sociétés est calculé à partir de la valeur de l'impôt sur les sociétés, qui correspond à la valeur de l'impôt sur les sociétés.

Points forts

  1. L'EMA, en tant qu'indicateur de base, permet de suivre efficacement les tendances.

  2. L'ajout de Stoch RSI peut filtrer les faux signaux et augmenter la précision d'entrée.

  3. L'opération de stop loss et de take profit basée sur ATR permet de suivre dynamiquement la volatilité du marché, en évitant un placement inapproprié.

  4. Cette stratégie a des paramètres bien ajustés et fonctionne très bien pendant les périodes de tendance.

Les risques

  1. La combinaison de plusieurs indicateurs augmente le risque de piqûre. Les signaux contradictoires entre EMA et Stoch RSI peuvent entraîner une entrée à de mauvais niveaux.

  2. Les paramètres de stop loss et de take profit conservateurs pourraient être violés par d'énormes fluctuations du marché, ce qui entraînerait des sorties prématurées en l'absence de tendances ultérieures.

  3. La mise en place de la triple EMA a un certain décalage lorsque les lignes rapides et moyennes s'inversent.

  4. Cette stratégie favorise la tendance du marché. Les marchés latéraux ne fonctionneraient pas bien.

Améliorations

  1. Ajouter des indicateurs comme MACD pour de meilleures entrées.

  2. Optimisation des paramètres de test long/courts sur ATR. Par exemple, ajuster le stop loss de 1 ATR à 1,5 ATR, prendre des bénéfices de 4 ATR à 3 ATR pour de meilleurs résultats.

  3. Supprimer le Stock RSI et garder uniquement les MAs pour filtrer les bruits et des bénéfices plus stables.

  4. Ajout d'autres critères pour juger de la tendance, comme le volume des transactions, à fonctionner en dessous de niveaux significatifs.

Conclusion

Cette stratégie combine la triple EMA et le Stoch RSI pour déterminer les tendances. Des signaux d'entrée stricts réduisent les transactions inutiles. SL dynamique et TP basés sur ATR rendent les paramètres adaptables.


/*backtest
start: 2023-12-01 00:00:00
end: 2023-12-31 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//              3ESRA
//              v0.2a

// Coded by Vaida Bogdan

// 3ESRA consists of a 3 EMA cross + a close above (for longs) the quickest EMA
// or below (for shorts). Note that I've deactivated the RSI Cross Over/Under
// (you can modify the code and activate it). The strategy also uses a stop loss
// that's at 1 ATR distance from the entry price and a take profit that's at
// 4 times the ATR distance from the entry price.

// Feedback:
// Tested BTCUSDT Daily
// 1. Stoch-RSI makes you miss opportunities.
// 2. Changing RR to 4:1 times ATR works better.

//@version=4
strategy(title="3 EMA + Stochastic RSI + ATR", shorttitle="3ESRA", overlay=true, pyramiding=1,
     process_orders_on_close=true, calc_on_every_tick=true,
     initial_capital=1000, currency = currency.USD, default_qty_value=10, 
     default_qty_type=strategy.percent_of_equity,
     commission_type=strategy.commission.percent, commission_value=0.1, slippage=2)

startDate = input(title="Start Date", type=input.integer,
     defval=1, minval=1, maxval=31, group="Backtesting range")
startMonth = input(title="Start Month", type=input.integer,
     defval=1, minval=1, maxval=12, group="Backtesting range")
startYear = input(title="Start Year", type=input.integer,
     defval=1900, minval=1800, maxval=2100, group="Backtesting range")
endDate = input(title="End Date", type=input.integer,
     defval=1, minval=1, maxval=31, group="Backtesting range")
endMonth = input(title="End Month", type=input.integer,
     defval=1, minval=1, maxval=12, group="Backtesting range")
endYear = input(title="End Year", type=input.integer,
     defval=2040, minval=1800, maxval=2100, group="Backtesting range")

// Date range filtering
inDateRange = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and
     (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 23, 59))
     
fast = input(8, minval=8, title="Fast EMA", group="EMAs")
medium = input(14, minval=8, title="Medium EMA", group="EMAs")
slow = input(50, minval=8, title="Slow EMA", group="EMAs")
src = input(close, title="Source")

smoothK = input(3, "K", minval=1, group="Stoch-RSI", inline="K&D")
smoothD = input(3, "D", minval=1, group="Stoch-RSI", inline="K&D")
lengthRSI = input(14, "RSI Length", minval=1, group="Stoch-RSI", inline="length")
lengthStoch = input(14, "Stochastic Length", minval=1, group="Stoch-RSI", inline="length")
rsiSrc = input(close, title="RSI Source", group="Stoch-RSI")

length = input(title="Length", defval=14, minval=1, group="ATR")
smoothing = input(title="Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"], group="ATR")

// EMAs
fastema = ema(src, fast)
mediumema = ema(src, medium)
slowema = ema(src, slow)

// S-RSI
rsi1 = rsi(rsiSrc, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(k, smoothD)
sRsiCrossOver = k[1] < d[1] and k > d
sRsiCrossUnder = k[1] > d[1] and k < d

// ATR
ma_function(source, length) =>
	if smoothing == "RMA"
		rma(source, length)
	else
		if smoothing == "SMA"
			sma(source, length)
		else
			if smoothing == "EMA"
				ema(source, length)
			else
				wma(source, length)
atr = ma_function(tr(true), length)

// Trading Logic
longCond1 = (fastema > mediumema) and (mediumema > slowema)
longCond2 = true
// longCond2 = sRsiCrossOver
longCond3 = close > fastema
longCond4 = strategy.position_size <= 0
longCond = longCond1 and longCond2 and longCond3 and longCond4 and inDateRange

shortCond1 = (fastema < mediumema) and (mediumema < slowema)
shortCond2 = true 
// shortCond2 = sRsiCrossUnder
shortCond3 = close < fastema
shortCond4 = strategy.position_size >= 0
shortCond = shortCond1 and shortCond2 and shortCond3 and shortCond4 and inDateRange

var takeProfit = float(na), var stopLoss = float(na)
if longCond and strategy.position_size <= 0
    takeProfit := close + 4*atr
    stopLoss := close - 1*atr
    // takeProfit := close + 2*atr
    // stopLoss := close - 3*atr

else if shortCond and strategy.position_size >= 0
    takeProfit := close - 4*atr
    stopLoss := close + 1*atr
    // takeProfit := close - 2*atr
    // stopLoss := close + 3*atr
    
// Strategy calls
strategy.entry("3ESRA", strategy.long, comment="Long", when=longCond and strategy.position_size <= 0)
strategy.entry("3ESRA", strategy.short, comment="Short", when=shortCond and strategy.position_size >= 0)
strategy.exit(id="TP-SL", from_entry="3ESRA", limit=takeProfit, stop=stopLoss)
if (not inDateRange)
    strategy.close_all()
    
// Plot EMAs
plot(fastema, color=color.purple, linewidth=2, title="Fast EMA")
plot(mediumema, color=color.teal, linewidth=2, title="Medium EMA")
plot(slowema, color=color.yellow, linewidth=2, title="Slow EMA")
// Plot S-RSI
// plotshape((strategy.position_size > 0) ? na : sRsiCrossOver, title="StochRSI Cross Over", style=shape.triangleup, location=location.belowbar, color=color.teal, text="SRSI", size=size.small)
// Plot trade
bgcolor(strategy.position_size > 0 ? color.new(color.green, 75) : strategy.position_size < 0 ? color.new(color.red,75) : color(na))
// Plot Strategy
plot((strategy.position_size != 0) ? takeProfit : na, style=plot.style_linebr, color=color.green, title="TP")
plot((strategy.position_size != 0) ? stopLoss : na, style=plot.style_linebr, color=color.red, title="SL")



Plus de