移动平均线交叉金叉优化交易策略


创建日期: 2023-12-19 13:37:33 最后修改: 2023-12-19 13:37:33
复制: 0 点击次数: 682
avatar of ChaoZhang ChaoZhang
1
关注
1621
关注者

移动平均线交叉金叉优化交易策略

概述

该策略通过优化常规的移动平均线交叉策略,设定三条不同周期的移动平均线,采用9周期、50周期和100周期的移动平均线构建金叉形态,在中长线均线处于上升趋势的条件下,短期均线上穿中线均线形成金叉买入信号。策略名称为“移动平均线交叉金叉优化交易策略”。

策略原理

该策略使用9周期、50周期和100周期三条移动平均线。其中,9周期移动平均线为短期均线,50周期移动平均线为中线均线,100周期移动平均线为长期均线。策略的交易信号来自短期均线与中线均线的交叉。具体逻辑是,在长期均线处于上升趋势(长期均线价高于中线均线)的条件下,短期均线上穿中线均线时产生买入信号;短期均线下穿中线均线时产生卖出信号,实现交易。

优势分析

相比常规的双移动平均线交叉策略,该策略在产生交易信号前增加了中长期趋势判断的条件,可以有效过滤掉部分无效信号。在长期趋势不明朗的情况下,策略将不会产生信号,可以避免被套。同时,该策略适合在短中期内捕捉趋势性行情,降低了激进入场的可能性。

风险分析

该策略在设定参数时需要调整均线的周期组合,不同的周期组合对策略的效果会产生影响。如果周期参数设置不当,将面临产生过多虚假信号的风险。此外,交易者需要警惕潜在的系统性风险,及时止损来规避风险。

优化方向

可以考虑结合其他指标来辅助判断市场趋势,如MACD、BOLL等,设置更加严格的入场条件,或者结合波动率指标构建自适应移动平均线,使得参数可以根据市场环境自动调整,进一步优化策略。

总结

该策略在常规双移动平均线交叉的基础上,添加长期均线判断和 filter 条件,可有效过滤虚假信号,适合捕捉短中期趋势行情,是一种简单实用的趋势跟踪策略。但交易者仍需关注参数优化和系统性风险,制定 scient 的资金管理策略。

策略源码
/*backtest
start: 2022-12-12 00:00:00
end: 2023-12-18 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
strategy("Golden Cross, SMA 100, Moving Average Strategy (by Coinrule)", shorttitle="Golden_Cross_Strat_MA100_optimized", overlay=true, initial_capital = 1000,process_orders_on_close=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)

// Input
switch1=input(true, title="Enable Bar Color?")
switch2=input(false, title="Show Fast Moving Average")
switch3=input(true, title="Show Slow Moving Average")

//Calculate Moving Averages
movingaverage_fast = sma(close, input(9))
movingaverage_slow = sma(close, input(100))
movingaverage_normal= sma(close, input(50))

//Backtest dates
fromMonth = input(defval = 1,    title = "From Month",      type = input.integer, minval = 1, maxval = 12)
fromDay   = input(defval = 1,    title = "From Day",        type = input.integer, minval = 1, maxval = 31)
fromYear  = input(defval = 2020, title = "From Year",       type = input.integer, minval = 1970)
thruMonth = input(defval = 1,    title = "Thru Month",      type = input.integer, minval = 1, maxval = 12)
thruDay   = input(defval = 1,    title = "Thru Day",        type = input.integer, minval = 1, maxval = 31)
thruYear  = input(defval = 2112, title = "Thru Year",       type = input.integer, minval = 1970)
showDate  = input(defval = true, title = "Show Date Range", type = input.bool)

start     = timestamp(fromYear, fromMonth, fromDay, 00, 00)        // backtest start window
finish    = timestamp(thruYear, thruMonth, thruDay, 23, 59)        // backtest finish window
window()  => time >= start and time <= finish ? true : false       // create function "within window of time"


// Calculation
bullish_cross = crossover(movingaverage_fast, movingaverage_normal)
bearish_cross = crossunder(movingaverage_fast, movingaverage_normal)

//Entry and Exit
if bullish_cross and window() and movingaverage_slow > movingaverage_normal
    strategy.entry("long", strategy.long)

strategy.close("long", when = bearish_cross and window())

// Colors
bartrendcolor = close > movingaverage_fast and close > movingaverage_slow and change(movingaverage_slow) > 0 ? color.green : close < movingaverage_fast and close < movingaverage_slow and change(movingaverage_slow) < 0 ? color.red : color.blue
barcolor(switch1?bartrendcolor:na)

// Output
plot(movingaverage_fast, color=color.orange, linewidth=2)
plot(movingaverage_slow, color=color.purple, linewidth=3)
plot(movingaverage_normal, color=color.blue, linewidth=2)

bgcolor(color = showDate and window() ? color.gray : na, transp = 90)