金叉死叉交易策略

Author: ChaoZhang, Date: 2023-10-31 14:37:38
Tags:

金叉死叉交易策略

概述

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


/*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")

更多内容