这是一个基于双均线交叉信号的量化交易策略。策略采用了两条移动平均线,一条作为主信号线,另一条作为平滑信号线。通过监测价格与平滑信号线的交叉情况来生成交易信号,实现市场趋势的把握和动量跟踪。策略的核心优势在于其简单而有效的信号生成机制,以及灵活的参数配置选项。
策略使用了两个层次的移动平均线计算。首先计算一条基础移动平均线(默认周期为9),然后对这条均线进行二次平滑处理(默认周期为5)。策略提供了多种均线计算方法的选择,包括简单移动平均线(SMA)、指数移动平均线(EMA)、平滑移动平均线(SMMA)、加权移动平均线(WMA)和成交量加权移动平均线(VWMA)。当收盘价向上穿越平滑信号线时,生成做多信号;当收盘价向下穿越平滑信号线时,生成做空信号。
这是一个经典的趋势跟踪策略的改进版本,通过双层移动平均线的设计,在保持策略简单性的同时提高了稳定性。策略具有良好的可扩展性和灵活性,通过参数优化和功能扩展,可以适应不同的市场环境。然而,使用者需要注意控制交易成本和管理风险,建议在实盘交易前进行充分的回测验证。
/*backtest
start: 2019-12-23 08:00:00
end: 2024-11-25 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Moving Average 1.0 Strategy", overlay=true)
// Input for Moving Average Length
len = input.int(9, minval=1, title="Length")
src = input(close, title="Source")
offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)
// Calculate the Moving Average
out = ta.sma(src, len)
// Plot the Moving Average
plot(out, color=color.blue, title="MA", offset=offset)
// Function to choose the type of moving average
ma(source, length, type) =>
switch type
"SMA" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)
// Input for Smoothing Method and Length
typeMA = input.string(title="Method", defval="SMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Smoothing")
smoothingLength = input.int(title="Smoothing Length", defval=5, minval=1, maxval=100, group="Smoothing")
// Calculate the Smoothing Line
smoothingLine = ma(out, smoothingLength, typeMA)
// Plot the Smoothing Line
plot(smoothingLine, title="Smoothing Line", color=color.rgb(120, 66, 134, 35), offset=offset)
// Strategy Logic
if (ta.crossover(close, smoothingLine))
strategy.entry("Buy", strategy.long)
if (ta.crossunder(close, smoothingLine))
strategy.entry("Sell", strategy.short)