
移动平均交叉策略是一个基于移动平均线的量化交易策略。该策略通过计算一段时间内的证券平均价格,利用价格的移动平均线的交叉来产生交易信号,实现盈利。
该策略主要利用快速移动平均线和慢速移动平均线的交叉来判断价格趋势和产生交易信号。具体来说,是运用两个不同周期长度的移动平均线,例如10日线和20日线。
当快速移动平均线从下方向上突破慢速移动平均线时,认为行情由跌转为涨,产生买入信号。当快速移动平均线从上方向下跌破慢速移动平均线时,认为行情由涨转为跌,产生卖出信号。
通过捕捉价格趋势的转折点,该策略可以在行情转好时买入,行情转坏时卖出,实现盈利。
该策略具有以下优势:
该策略也存在以下风险:
可通过适当优化来减轻这些风险。
该策略可以从以下几个方向进行优化:
通过以上优化,可以大大提高策略的实盘效果。
移动平均交叉策略整体来说是一种易于掌握和实施的量化交易策略。它利用价格平均线的交叉原理,简单且直观地判断市场走势和产生交易信号。通过参数调优和与其他技术指标的配合,可以强化该策略的实盘效果,使其成为可靠的量化盈利工具。
/*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"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HPotter
// Simple SMA strategy
//
// WARNING:
// - For purpose educate only
// - This script to change bars colors
//@version=4
strategy(title="Simple SMA Strategy Backtest", shorttitle="SMA Backtest", precision=6, overlay=true)
Resolution = input(title="Resolution", type=input.resolution, defval="D")
Source = input(title="Source", type=input.source, defval=close)
xSeries = security(syminfo.tickerid, Resolution, Source)
Length = input(title="Length", type=input.integer, defval=14, minval=2)
TriggerPrice = input(title="Trigger Price", type=input.source, defval=close)
TakeProfit = input(50, title="Take Profit", step=0.01)
StopLoss = input(20, title="Stop Loss", step=0.01)
UseTPSL = input(title="Use Take\Stop", type=input.bool, defval=false)
BarColors = input(title="Painting bars", type=input.bool, defval=true)
ShowLine = input(title="Show Line", type=input.bool, defval=true)
UseAlerts = input(title="Use Alerts", type=input.bool, defval=false)
reverse = input(title="Trade Reverse", type=input.bool, defval=false)
pos = 0
xSMA = sma(xSeries, Length)
pos := iff(TriggerPrice > xSMA, 1,
iff(TriggerPrice < xSMA, -1, nz(pos[1], 0)))
nRes = ShowLine ? xSMA : na
alertcondition(UseAlerts == true and pos != pos[1] and pos == 1, title='Signal Buy', message='Strategy to change to BUY')
alertcondition(UseAlerts == true and pos != pos[1] and pos == -1, title='Signal Sell', message='Strategy to change to SELL')
alertcondition(UseAlerts == true and pos != pos[1] and pos == 0, title='FLAT', message='Strategy get out from position')
possig =iff(pos[1] != pos,
iff(reverse and pos == 1, -1,
iff(reverse and pos == -1, 1, pos)), 0)
if (possig == 1)
strategy.entry("Long", strategy.long)
if (possig == -1)
strategy.entry("Short", strategy.short)
if (UseTPSL)
strategy.close("Long", when = high > strategy.position_avg_price + TakeProfit, comment = "close buy take profit")
strategy.close("Long", when = low < strategy.position_avg_price - StopLoss, comment = "close buy stop loss")
strategy.close("Short", when = low < strategy.position_avg_price - TakeProfit, comment = "close buy take profit")
strategy.close("Short", when = high > strategy.position_avg_price + StopLoss, comment = "close buy stop loss")
nColor = BarColors ? strategy.position_avg_price != 0 and pos == 1 ? color.green :strategy.position_avg_price != 0 and pos == -1 ? color.red : color.blue : na
barcolor(nColor)
plot(nRes, title='SMA', color=#00ffaa, linewidth=2, style=plot.style_line)