Skip to content

Latest commit

 

History

History
334 lines (221 loc) · 11.9 KB

金叉死叉交易策略Trend-Trading-Strategy-Based-on-EMA-Crossover.md

File metadata and controls

334 lines (221 loc) · 11.9 KB

Name

金叉死叉交易策略Trend-Trading-Strategy-Based-on-EMA-Crossover

Author

ChaoZhang

Strategy Description

IMG [trans]

概述

这个策略的主要思想是利用EMA指标的金叉和死叉信号来进行买入和卖出操作。它同时绘制了多组快慢EMA,并利用其交叉进行交易信号的判断。

策略原理

这个策略首先定义了多组EMA均线,包括快速EMA均线ema1到ema6和慢速EMA均线ema7到ema12。其次定义了买入信号buy_signal和卖出信号sell_signal:

  • 买入信号buy_signal为ema1上穿ema3时生成
  • 卖出信号sell_signal为ema1下穿ema3时生成

这样,当短期EMA均线上穿长期EMA均线时,表示市场处于上升趋势,买入;当短期EMA均线下穿长期EMA均线时,表示市场处于下降趋势,卖出。

策略通过监测EMA均线的交叉来判断趋势方向,以此制定买入卖出决策。

优势分析

这个策略有以下几个优势:

  1. 使用EMA均线指标判断趋势,EMA均线对价格变动较为平滑,可以过滤掉短期市场噪音,使交易信号更可靠。

  2. 同时绘制多组EMA均线,可以更准确判断趋势的变化。快慢EMA线的交叉可以避免错过重要的趋势转折点。

  3. 策略简单清晰,通过EMA交叉发出交易信号,易于理解实现,适合量化交易。

  4. 可自定义EMA周期参数,可以根据不同品种和市场调整参数,灵活应对市场变化。

风险分析

该策略也存在以下风险:

  1. EMA均线具有滞后性,可能会延迟发出交易信号。

  2. 选取不当的EMA参数组合可能会产生错误的交易信号。

  3. EMA交叉无法有效过滤震荡区间造成的虚假信号。

  4. 存在过拟合风险,EMA参数可优化空间有限。

对策:

  1. 结合其他指标进行过滤,避免在震荡区间发出错误信号。

  2. 测试不同周期参数的稳定性,防止出现过拟合。

  3. 适当调整策略参数组合或增加Exit机制,控制风险。

优化方向

这个策略还可以从以下几个方面进行优化:

  1. 增加止损策略,在亏损达到一定幅度后止损退出。

  2. 加入重新进入市场的机制,即设置重新买入卖出的信号。

  3. 优化买入卖出的EMA交叉周期参数组合,寻找最优参数。

  4. 加入其他指标判断,进行多因子验证,提高信号质量。

  5. 测试不同品种参数优化,寻找最佳适用范围。

  6. 考虑实盘交易中的滑点因素,进行回测调优。

总结

这个策略利用EMA均线的快慢交叉判断趋势方向,根据交叉信号进行买入和卖出,是一个较简单的趋势跟踪策略。它有判断趋势变化的优势,但也存在滞后性和震荡区间的风险。通过止损、参数优化、多因子验证等方法可以进一步增强策略效果,在回测和实盘中不断改进。

||

Overview

The core idea of this strategy is to use the golden cross and dead cross signals of the EMA indicator to make buy and sell decisions. It plots multiple sets of fast and slow EMAs and generates trading signals based on their crossover.

Strategy Logic

The strategy first defines multiple EMA lines, including fast EMAs ema1 to ema6 and slow EMAs ema7 to ema12. It then defines the buy signal buy_signal and sell signal sell_signal:

  • The buy signal buy_signal is generated when ema1 crosses above ema3.

  • The sell signal sell_signal is generated when ema1 crosses below ema3.

So when the short-term EMA crosses above the long-term EMA, it indicates an upward trend in the market and a buy signal is triggered. When the short-term EMA crosses below the long-term EMA, it indicates a downward trend and a sell signal is triggered.

The strategy monitors the crossover of the EMA lines to determine the trend direction and make buy/sell decisions accordingly.

Advantage Analysis

The advantages of this strategy include:

  1. Using EMA lines to determine the trend can filter out short-term market noise and make trading signals more reliable.

  2. Plotting multiple EMAs can identify trend changes more accurately. Crossovers between fast and slow EMAs help capture important trend turning points.

  3. The strategy is simple and clear. Trading signals are generated by EMA crosses, making it easy to understand and implement for quant trading.

  4. The EMA period parameters are customizable to adapt to different products and markets.

Risk Analysis

The risks of this strategy include:

  1. EMA lines have lagging effects which may delay trading signals.

  2. Improper selection of EMA parameters may generate incorrect signals.

  3. EMA crosses fail to filter out false signals in ranging markets effectively.

  4. Overfitting risks exist due to limited optimizable space for EMA parameters.

Solutions:

  1. Add filters with other indicators to avoid wrong signals in ranging markets.

  2. Test stability of different period parameters to prevent overfitting.

  3. Adjust parameters or add exit mechanisms to control risks.

Optimization Directions

The strategy can be further optimized in the following aspects:

  1. Add stop loss to exit positions when losses reach a threshold.

  2. Implement re-entry logic with additional buy/sell signals.

  3. Optimize EMA crossover period parameters for best results.

  4. Incorporate other indicators for multifactorial validation to improve signal quality.

  5. Test parameter tuning on different products to find optimal applicability.

  6. Consider slippage in live trading and optimize via backtesting.

Summary

This is a simple trend following strategy based on EMA crossover. It can identify trend changes but also has risks like lagging effects and whipsaws. Enhancements with stop loss, parameter optimization, multifactorial validation etc. can further improve strategy performance in backtesting and live trading.

[/trans]

Strategy Arguments

Argument Default Description
v_input_1 2 Short EMA
v_input_2 7 Long EMA
v_input_3 2018 Start Year
v_input_4 true Start Month
v_input_5 18 Start Day
v_input_6 2018 Stop Year
v_input_7 12 Stop Month
v_input_8 26 Stop Day
v_input_9 3 Fast EMA 2
v_input_10 5 Fast EMA 4
v_input_11 8 Fast EMA 5
v_input_12 10 Fast EMA 6
v_input_13 30 Slow EMA 7
v_input_14 35 Slow EMA 8
v_input_15 40 Slow EMA 9
v_input_16 45 Slow EMA 10
v_input_17 50 Slow EMA 11
v_input_18 60 Slow EMA 12
v_input_19 2018 From Year
v_input_20 true From Month
v_input_21 true From Day
v_input_22 2019 To Year
v_input_23 true To Month
v_input_24 true To Day

Source (PineScript)

/*backtest
start: 2023-09-30 00:00:00
end: 2023-10-30 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=3
//Companion Strategy script to my Cloud Study. Enjoy! -MP
// study("MP's Cloud Study", overlay=true)
strategy(title="MP's Cloud Strat'", shorttitle="MP's Cloud Strat", overlay=true, precision=6, pyramiding=0, initial_capital=10000, currency="USD", default_qty_type=strategy.percent_of_equity,calc_on_order_fills= false, calc_on_every_tick=false, default_qty_value=100.0, commission_type=strategy.commission.percent, commission_value=0.05)

//bgcolor ( color=black, transp=20, title='Blackground', editable=true)

src = close, len1 = input(2, minval=1, title="Short EMA")
src2 = close, len3 = input(7, minval=1, title="Long EMA")
emaShort = ema(src, len1)
emaLong = ema(src2, len3)


StartYear = input(2018, "Start Year")
StartMonth = input(01, "Start Month")
StartDay = input(18, "Start Day")

 
StopYear = input(2018, "Stop Year")
StopMonth = input(12, "Stop Month")
StopDay = input(26, "Stop Day")
tradeStop = timestamp(StopYear,StopMonth,StopDay,0,0)

//src = close, 
//len1 = input(3, minval=1, title="Fast EMA 1")
len2 = input(3, minval=1, title="Fast EMA 2")
//len3 = input(8, minval=1, title="Fast EMA 3")
len4 = input(5, minval=1, title="Fast EMA 4")
len5 = input(8, minval=1, title="Fast EMA 5")
len6 = input(10, minval=1, title="Fast EMA 6")
//Slow EMA
len7 = input(30, minval=1, title="Slow EMA 7")
len8 = input(35, minval=1, title="Slow EMA 8")
len9 = input(40, minval=1, title="Slow EMA 9")
len10 = input(45, minval=1, title="Slow EMA 10")
len11 = input(50, minval=1, title="Slow EMA 11")
len12 = input(60, minval=1, title="Slow EMA 12")

//Fast EMA
ema1 = ema(src, len1)
ema2 = ema(src, len2)
ema3 = ema(src, len3)
ema4 = ema(src, len4)
ema5 = ema(src, len5)
ema6 = ema(src, len6)
//Slow EMA
ema7 = ema(src, len7)
ema8 = ema(src, len8)
ema9 = ema(src, len9)
ema10 = ema(src, len10)
ema11 = ema(src, len11)
ema12 = ema(src, len12)

//Fast EMA Color Rules
//colfastL = (ema1 > ema2 and ema2 > ema3 and ema3 > ema4 and ema4 > ema5 and ema5 > ema6)
colfastS = (ema1 < ema2 and ema2 < ema3 and ema3 < ema4 and ema4 < ema5 and ema5 < ema6)
//Slow EMA Color Rules
//colslowL = ema7 > ema8 and ema8 > ema9 and ema9 > ema10 and ema10 > ema11 and ema11 > ema12 
//colslowS = ema7 < ema8 and ema8 < ema9 and ema9 < ema10 and ema10 < ema11 and ema11 < ema12 
//Fast EMA Final Color Rules
//colFinal = colfastL and colslowL? aqua : colfastS and colslowS? orange : gray
//Slow EMA Final Color Rules
//colFinal2 = colslowL  ? lime : colslowS ? red : gray
//Fast EMA Plots
p1=plot(ema1, title="Fast EMA 1", style=line, linewidth=2, color=silver)
plot(ema2, title="Fast EMA 2", style=line, linewidth=1, color=silver)
plot(ema3, title="Fast EMA 3", style=line, linewidth=1, color=silver)
plot(ema4, title="Fast EMA 4", style=line, linewidth=1, color=silver)
plot(ema5, title="Fast EMA 5", style=line, linewidth=1, color=silver)
p2=plot(ema6, title="Fast EMA 6", style=line, linewidth=2, color=silver)
fill(p1,p2,color=silver, transp=60)
//Slow EMA Plots
//p3=plot(ema7, title="Slow EMA 7", style=line, linewidth=4, color=colFinal2)
//plot(ema8, title="Slow EMA 8", style=line, linewidth=3, color=colFinal2)
//plot(ema9, title="Slow EMA 9", style=line, linewidth=3, color=colFinal2)
//plot(ema10, title="Slow EMA 10", style=line, linewidth=3, color=colFinal2)
//plot(ema11, title="Slow EMA 11", style=line, linewidth=3, color=colFinal2)
//p4=plot(ema12, title="Slow EMA 12", style=line, linewidth=4, color=colFinal2)
//fill(p3,p4, color=silver, transp=60)



//Plot the Ribbon
ma1=plot( emaShort,color=rising(emaShort,2)?green:red,linewidth=1,join=true,transp=20,title="Short")
ma2=plot( emaLong,color=rising(emaLong,2)?green:red,linewidth=1,join=true,transp=20,title="Long")
fcolor = emaShort>emaLong?green:red
fill(ma1,ma2,color=fcolor,transp=80,title="Ribbon Fill")


//fast = 4, slow = 16
//fastMA = ema(close, fast)
//slowMA = ema(close, slow)
//plot(fastMA, color=green, title = "buy/sell")
//plot(slowMA, color=red, title = "base")


//Conditions
buy_signal = crossover(ema1,ema3)
sell_signal = crossunder(ema1,ema3)

plotshape(sell_signal, style=shape.triangleup, color = red, text="Start Short")
plotshape(buy_signal, style=shape.triangledown, color = green, text="Start Long")

alertcondition(sell_signal, title = 'Sell/Short', message = 'e= s= c=position b=long t=market l= | delay=30 | e= s= b=short l= t=market q=0.01')
alertcondition(buy_signal, title = 'Buy/Long', message =  'e= s= c=position b=short t=market l= | delay=30 | e= s= b=long l= t=market q=0.01')

//alertcondition(sell_signal, title = 'Sell/Short', message = 'e= s= c=order b=buy | delay=3 | e= b=sell q=99% p=0.70% u=currency')
//alertcondition(buy_signal, title = 'Buy/Long', message =  'e= s= c=order b=sell | delay=30 | e= b=buy q=80 p=0.1% u=currency')


testStartYear = input(2018, "From Year") 
testStartMonth = input(1, "From Month")
testStartDay = input(1, "From Day")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)

testStopYear = input(2019, "To Year")
testStopMonth = input(1, "To Month")
testStopDay = input(1, "To Day")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)

testPeriod() => true

if testPeriod()
    if buy_signal
        strategy.entry("Long", true)
    

    if sell_signal
        strategy.close("Long")

Detail

https://www.fmz.com/strategy/430667

Last Modified

2023-10-31 14:37:38