该策略的主要思想是利用移动平均线的金叉死叉作为买卖信号,结合价格突破双均线的方式进行建仓和止损。当短期均线上穿长期均线时生成买入信号;当短期均线下穿长期均线时生成卖出信号。该策略同时具有趋势跟踪和反转交易的特点。
策略详细运作原理如下:
计算短期简单移动平均线和长期简单移动平均线。
比较价格是否高于或低于移动平均线,以价格在移动平均线之上为持多头,价格在移动平均线之下为空头的判断依据。
在短均线上穿长均线时做多;在短均线下穿长均线时做空。
这样来回切换多空仓位。
该策略的主要优势有:
双均线策略同时结合趋势跟踪和反转交易,兼顾了追踪市场趋势和捕捉反转机会。
均线的金叉死叉具有一定的持久性,可以有效滤除假突破。
利用均线理论,有利于在趋势震荡中锁定盈利。
该策略的主要风险有:
双均线策略对参数敏感,移动平均线参数设置不当可能导致交易频繁或漏掉机会。
突破失败可能造成亏损,需要有效止损来控制风险。
趋势反转不一定成功,可能继续原趋势而亏损。
该策略的主要优化方向:
对移动平均线参数进行测试和优化,找到最佳参数组合。
添加趋势判断指标,区分趋势和震荡市场。
增加有效止损来控制风险,如追踪止损、挂单止损等。
结合其他指标进行组合,提高策略的稳定性。
总之,本策略作为一种双均线反转跟踪策略,同时考虑了趋势跟踪和反转交易,在参数优化和风险控制到位的情况下,能够获得较好的效果。但任何策略都可能面临方向判断错误、止损失败等风险,需要不断测试和优化来适应市场的变化。
/*backtest
start: 2023-11-29 00:00:00
end: 2023-12-06 00:00:00
period: 10m
basePeriod: 1m
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)
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(reverse and pos == 1, -1,
iff(reverse and pos == -1, 1, pos))
if (possig == 1)
strategy.entry("Long", strategy.long)
if (possig == -1)
strategy.entry("Short", strategy.short)
if (possig == 0)
strategy.close_all()
nColor = BarColors ? possig == -1 ? color.red : possig == 1 ? color.green : color.blue : na
barcolor(nColor)
plot(nRes, title='SMA', color=#00ffaa, linewidth=2, style=plot.style_line)