采用两条不同长度的移动平均线来区分定制平均均线上的买入/卖出颜色。
回测测试
/*backtest start: 2021-12-01 00:00:00 end: 2022-05-30 00:00:00 period: 1h basePeriod: 15m exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}] args: [["ContractType","hc2210",360008]] */ // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © dannhau2 //@version=5 indicator(title="Moving Average Buy-Sell", shorttitle="MABS", overlay=true, timeframe="", timeframe_gaps=true) Wave = ta.ema(input(close, title="数据源"), input.int(9, minval=1, title="快线周期")) Tide = ta.ema(input(close, title="数据源"), input.int(30, minval=1, title="慢线周期")) AVG = (Wave + Tide)/2 Up = ta.crossover(Wave, Tide) Down = ta.crossover(Tide,Wave) var col = color.red if Up col := color.lime if Down col := color.red P1=plot(AVG, title="MABS", color=col, linewidth = 3, display=display.none) P2=plot(Wave, title="MA-Wave", color=col, linewidth = 2) P3=plot(Tide, title="MA-Tide", color=col, linewidth = 3) fill(P2, P3, color= Wave > Tide ? color.new(color.lime,70):color.new(color.red, 70)) alertcondition(Up, title="Trend Changed Positive", message="Trend Changed Positive") alertcondition(Down, title="Trend Changed Negative", message="Trend Changed Negative") if Up strategy.entry("Enter Long", strategy.long) else if Down strategy.entry("Enter Short", strategy.short)