该策略通过计算通道均线,在价格突破通道均线时建立多头或空头头寸,追踪股票价格趋势,属于趋势追踪类策略。
该策略首先计算20日高点平均值作为通道上轨,20日低点平均值作为通道下轨,并计算通道中线。通道中线代表近期价格平均趋势。当价格上穿通道中线时,建立多头头寸;当价格下穿通道中线时,建立空头头寸。追踪价格趋势,直到价格重新回落到通道区间反向时,平仓头寸。
该策略整体来说较为简单直接,通过基本的价格通道来判断股票价格趋势,属于趋势追踪类型策略。优点是容易操作,充分利用价格趋势带来的投资机会,避免资金被锁定。缺点是参数设置不当可能影响效果,且存在一定回调测试的风险。通过合理优化,可以提高策略稳定性,增强实盘表现。
/*backtest start: 2024-01-01 00:00:00 end: 2024-01-31 23:59:59 period: 4h basePeriod: 15m exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=3 //future strategy //strategy(title = "stub", default_qty_type = strategy.fixed, default_qty_value = 1, overlay = true, commission_type=strategy.commission.cash_per_contract,commission_value=2) //stock strategy strategy(title = "dc", default_qty_type = strategy.percent_of_equity, default_qty_value = 20, overlay = true, commission_type=strategy.commission.cash_per_contract,commission_value=.005) //forex strategy //strategy(title = "stub", default_qty_type = strategy.percent_of_equity, default_qty_value = 20, overlay = true) //crypto strategy //strategy(title = "stub", default_qty_type = strategy.percent_of_equity, default_qty_value = 20, overlay = true, commission_type=strategy.commission.percent,commission_value=.25,default_qty_value=20) testStartYear = input(2000, "Backtest Start Year") testStartMonth = input(1, "Backtest Start Month") testStartDay = input(1, "Backtest Start Day") testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0) testEndYear = input(2019, "Backtest Start Year") testEndMonth = input(3) testEndDay = input(31, "Backtest Start Day") testPeriodEnd = timestamp(testStartYear,testStartMonth,testStartDay,0,0) testPeriod() => true //time >= testPeriodStart ? true : false dcPeriod = 20 dcUpper = highest(close, dcPeriod)[1] dcLower = lowest(close, dcPeriod)[1] dcAverage = (dcUpper + dcLower) / 2 plot(dcLower, style=line, linewidth=3, color=red, offset=1) plot(dcUpper, style=line, linewidth=3, color=aqua, offset=1) plot(dcAverage, color=black, style=line, linewidth=3, title="Mid-Line Average") strategy.entry("simpleBuy", strategy.long, when=close > dcAverage) strategy.close("simpleBuy",when=close < dcLower) strategy.entry("simpleSell", strategy.short,when=close < dcAverage) strategy.close("simpleSell",when=close > dcAverage)