双均线反转交易策略通过计算短期和长期两个不同周期的简单移动平均线,并根据价格与移动平均线的关系来产生交易信号。当短期均线上穿长期均线时,做多;当短期均线下穿长期均线时,做空。该策略属于趋势跟踪策略。
该策略通过输入参数设置两条不同周期长度的简单移动平均线,短周期线被称为快线,长周期线被称为慢线。快线响应价格变化更迅速,能捕捉短期趋势;慢线 响应价格变化更缓慢,能过滤掉短期市场噪音,捕捉主要趋势。当快线上穿慢线时,表明价格上涨势头增强,做多;当快线下穿慢线时,表明价格下跌势头增强,做空。
具体来说,策略通过sma()函数计算两条均线,并将计算结果赋值给xSMA(慢线)和快线。策略使用close价格计算均线。当close价格上穿xSMA时,做多;当close价格下穿xSMA时,做空。策略还设置了交易时间范围限制,只有在指定的时间范围内才会发出交易信号。
对每笔交易设置止盈止损点,并在到达止盈止损点时立即止盈止损。同时,策略通过barcolor函数将价格与慢线的关系显示在K线上:当做多时,K线为绿色;当做空时,K线为红色;当平仓时,K线为蓝色。
可以通过调整均线参数,优化止盈止损策略,取消时间限制或设置更合理的交易时间段来降低风险。也可以考虑结合其他指标作为过滤条件,避免出现太多虚假信号。
双均线反转交易策略overall是一种简单实用的趋势跟踪策略。它充分利用均线的平滑作用识别趋势方向,并配合快慢均线产生交易信号。该策略易于实现,思路清晰,适合初学者掌握。但其可能产生较多虚假信号和时滞问题。可以通过参数优化、引入辅助指标等方法来改进,使策略更稳定可靠。如果使用得当,该策略可以产生稳定的盈利,值得进行全面测试和优化。
/*backtest start: 2023-09-15 00:00:00 end: 2023-10-15 00:00:00 period: 4h 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 timeinrange(res, sess) => not na(time(res, sess)) ? 1 : 0 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) timeframe = input(title="Time Frame", defval="15") timerange = input(title="Time Range", defval="2300-0800") 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 and timeinrange(timeframe, timerange)) strategy.entry("Long", strategy.long) if (possig == -1 and timeinrange(timeframe, timerange)) strategy.entry("Short", strategy.short) if (timeinrange(timeframe, timerange) == 0) strategy.close_all() 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)