移动平均线金叉死叉交易策略通过计算快线EMA(fastLength)和慢线EMA(slowLength)的交叉来生成买入和卖出信号。当快线上穿慢线时,生成买入信号;当快线下穿慢线时,生成卖出信号。该策略简单实用,适用于中短线交易。
该策略使用两个移动平均线,快线和慢线。快线参数EMAfastLength默认为9日线,慢线参数EMAslowLength默认为26日线。计算两个EMA线的交叉来判断市场买卖信号:
具体交易信号和策略规则如下:
所以该策略就是在两条移动平均线发生金叉和死叉时进行交易操作的策略。
针对风险,可以优化的参数有移动平均线周期、交易品种、止盈止损比例等,需要大量测试来减少风险。
该策略的移动平均线交叉思路简单实用,可以通过下列方式进行优化:
通过这些优化测试,可以大幅度提高策略的实战效果和稳定性。
移动平均线交叉策略思路简单,实际应用需要不断优化。本策略给出了其交易信号生成逻辑和基本交易规则,在此基础上可以大力度优化,使之成为可实战的量化策略。移动平均线的应用也为我们提供了策略思路,我们可以在此基础上进行创新和改进。
/*backtest start: 2023-12-01 00:00:00 end: 2023-12-31 23:59:59 period: 1h basePeriod: 15m exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=4 strategy("EMA Cross by MarketAlpha", overlay=true) EMAfastLength = input(defval = 9, minval = 2) EMAslowLength = input(defval = 26, minval = 2) Targetpercentage = input(defval = 0.15, title = "Profit Target in percentage", minval = 0.05) StopLosspercentage = input(defval = 0.20, title = "Stop Loss in percentage", minval = 0.05) profitpoints = close*Targetpercentage stoplosspoints = close*StopLosspercentage price = close FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12) FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31) FromYear = input(defval = 2018, title = "From Year", minval = 2000) ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12) ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31) ToYear = input(defval = 9999, title = "To Year", minval = 2017) start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window window() => true // create function "within window of time" emafast = ema(price, EMAfastLength) emaslow = sma(price, EMAslowLength) plot(emafast,color=green) plot(emaslow,color=red) enterLong() => crossover(emafast, emaslow) strategy.entry(id = "MarketAlpha Long", long = true, when = window() and enterLong()) strategy.exit("Exit Long", from_entry = "MarketAlpha Long", profit = profitpoints,loss = stoplosspoints) enterShort() => crossunder(emafast, emaslow) strategy.entry(id = "MarketAlpha Short", long = false, when = window() and enterShort()) strategy.exit("Exit Short", from_entry = "MarketAlpha Short", profit = profitpoints,loss = stoplosspoints)