
该策略通过计算股票的快速30日简单移动平均线和慢速33日简单移动平均线,在它们发生金叉或死叉时进行 LONG 或 SHORT 入场。在相反信号出现时立即止损。这可以有效捕捉趋势的变化。
该策略的核心在于计算快速30日均线和慢速33日均线。快速线能更快响应价格变化而慢速线则有更好的滤波效果。当快速线从下方突破慢速线而上时产生买入信号。这表示价格开始上涨而快速线已响应但慢速线仍落后。当快速线从上方跌破慢速线而下时产生卖出信号。这表示价格开始下跌而快速线已响应但慢速线仍落后。
通过这样的快慢均线交叉设计,可以在趋势开始的时候产生交易信号,在相反信号出现时止损,有效捕捉了中长线的价格趋势。同时也避免被过多的市场波动所迷惑。
该策略具有以下几点优势:
该策略也存在一些风险:
可以通过参数优化、止损点设置、仅在趋势明确时交易等方法来控制和减少这些风险。
该策略可从以下几个方面进行优化:
通过测试和优化,可以持续改进策略规则,在不同市场环境下获得更可靠的交易信号。
该双均线交叉突破策略整体来说较为简单实用,通过快速均线和慢速均线的结合,可以有效识别中长线趋势的开始,生成较为可靠的交易信号。同时其止损规则也易于实现。通过进一步优化,该策略可以成为一个值得长期持有的量化系统。
/*backtest
start: 2022-11-20 00:00:00
end: 2023-11-26 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=3
//future strategy
//strategy(title = "es1!_1minute_hull", default_qty_type = strategy.fixed, initial_capital=250000, overlay = true, commission_type=strategy.commission.cash_per_contract,commission_value=2, calc_on_order_fills=false, calc_on_every_tick=false,pyramiding=0)
//strategy.risk.max_position_size(2)
//stock strategy
strategy(title = "stub", default_qty_type = strategy.percent_of_equity, default_qty_value = 100, initial_capital=1000000, overlay = false)//, calc_on_order_fills=true, calc_on_every_tick=true)
//forex strategy
//strategy(title = "stub", default_qty_type = strategy.percent_of_equity, default_qty_value = 100, overlay = true,initial_capital=250000, default_qty_type = strategy.percent_of_equity)
//crypto strategy
//strategy(title = "stub", default_qty_type = strategy.percent_of_equity, default_qty_value = 100, overlay = true, commission_type=strategy.commission.percent,commission_value=.005,default_qty_value=10000)
//strategy.risk.allow_entry_in(strategy.direction.long) // There will be no short entries, only exits from long.
testStartYear = 2010
testStartMonth = 1
testStartDay = 1
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
testEndYear = 2039
testEndMonth = 1
testEndDay = 1
testPeriodEnd = timestamp(testEndYear,testEndMonth,testEndDay,0,0)
testPeriod() =>
//true
time >= testPeriodStart and time <= testPeriodEnd ? true : false
fast_length = 30
slow_length = 33
ema1 = 0.0
ema2 = 0.0
volumeSum1 = sum(volume, fast_length)
volumeSum2 = sum(volume, slow_length)
//ema1 := (((volumeSum1 - volume) * nz(ema1[1]) + volume * close) / volumeSum1)
ema1 := ema(close,fast_length)
//ema2 := (((volumeSum2 - volume) * nz(ema2[1]) + volume * close) / volumeSum2)
ema2 := ema(close,slow_length)
plot(ema1,color=#00ff00, linewidth=3)
plot(ema2, color=#ffff00, linewidth=3)
go_long = crossover(ema1,ema2)
go_short = crossunder(ema1,ema2)
if testPeriod()
strategy.entry("long_ride", strategy.long, when=go_long)
strategy.entry("short_ride", strategy.short,when=go_short)
strategy.close("long_ride",when=go_short)
strategy.close("short_ride",when=go_long)