
双移动均线金叉死叉反转策略是一种典型的跟踪趋势的量化交易策略。该策略运用双移动均线指标中的9日线和14日线构建买入和卖出信号。当9日线从下方突破14日线而形成金叉时买入,当9日线从上方突破14日线而形成死叉时卖出。为过滤假信号,策略还引入50日线指标判断价格是否突破。
该策略主要基于双移动均线指标的金叉和死叉信号进行交易。双移动均线中,9日线代表短期趋势,14日线代表中期趋势,它们的交叉为判断市场趋势转折的有效技术指标。当短期趋势线从下方突破中期趋势线而形成金叉时,代表短期趋势线走强,属于买入信号;当从上方突破而形成死叉时,代表短期趋势线走弱,属于卖出信号。
另外,策略还引入50日线来过滤误导信号。只有当价格高于50日线时,才产生买入;只有当价格低于50日线时,才产生卖出。50日线代表了中长期趋势,只有中长期趋势同意,才进行短期操作。
核心代码逻辑如下:
// 买入条件:9日线上穿14日线 且 当前价格高于50日线
buyCondition = ta.crossover(sma9, sma14) and close > sma50
// 卖出条件:9日线下穿14日线 且 当前价格低于50日线
sellCondition = ta.crossunder(sma9, sma14) and close < sma50
双移动均线策略优势明显:
双移动均线策略也存在一定的风险:
针对风险,可做如下优化: 1. 进一步引入其他指标组合,在崩盘行情中快速止损。 2. 增加开仓过滤条件,避免震荡行情的金叉死叉交替。
双移动均线策略可从以下几个方面进行优化:
双移动均线策略整体而言是一种有效率盈利的策略。它可以顺势而为,持续盈利;同时也存在一定风险,需要进一步完善。通过参数优化、止损方式以及策略组合,可以进一步增强该策略的效果。
/*backtest
start: 2022-11-24 00:00:00
end: 2023-11-30 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("smaCrossReverse", shorttitle="smaCrossReverse", overlay=true)
// Define the length for the SMAs
sma9Length = input(9, title="SMA 9 Length")
sma14Length = input(14, title="SMA 14 Length")
sma50Length = input(50, title="SMA 50 Length") // Add input for SMA 50
// Calculate SMAs
sma9 = ta.sma(close, sma9Length)
sma14 = ta.sma(close, sma14Length)
sma50 = ta.sma(close, sma50Length) // Calculate SMA 50
// Buy condition: SMA 9 crosses above SMA 14 and current price is above SMA 50
buyCondition = ta.crossover(sma9, sma14) and close > sma50
// Sell condition: SMA 9 crosses below SMA 14 and current price is below SMA 50
sellCondition = ta.crossunder(sma9, sma14) and close < sma50
// Track the time since position was opened
var float timeElapsed = na
if (buyCondition)
timeElapsed := 0
else
timeElapsed := na(timeElapsed[1]) ? timeElapsed[1] : timeElapsed[1] + 1
// Close the buy position after 5 minutes
if (timeElapsed >= 5)
strategy.close("Buy")
// Track the time since position was opened
var float timeElapsedSell = na
if (sellCondition)
timeElapsedSell := 0
else
timeElapsedSell := na(timeElapsedSell[1]) ? timeElapsedSell[1] : timeElapsedSell[1] + 1
// Close the sell position after 5 minutes
if (timeElapsedSell >= 5)
strategy.close("Sell")
// Plot the SMAs on the chart
plot(sma9, title="SMA 9", color=color.blue)
plot(sma14, title="SMA 14", color=color.red)
plot(sma50, title="SMA 50", color=color.green) // Plot SMA 50 on the chart
// Strategy entry and exit conditions using if statements
if (buyCondition)
strategy.entry("Buy", strategy.long)
if (sellCondition)
strategy.entry("Sell", strategy.short)