本策略采用20日线和60日线的移动平均线交叉形成买卖信号。当价格上涨突破20日线时,做多;当价格下跌突破20日线时,平仓。同理,价格突破60日线时也形成买卖信号。该策略属于典型的趋势跟踪策略。
以上形成该策略的交易信号和规则。当价格突破平均线时,表明趋势开始,可以跟踪趋势做多;当价格跌破平均线时,表明趋势结束,此时平仓是正确选择。
风险解决方法:
1. 可以添加其他指标组合,如KDJ、MACD等,形成多重验证,避免误交易。
2. 可以根据市值、波动率等因素优化头寸和交易资金的利用效率。
3. 可以根据大盘指数不同阶段采用非对称操作,在震荡调整中减少交易,在明确趋势中加大仓位。
本策略整体是一个典型的双移动平均线交叉策略。核心思路是跟踪趋势,当价格突破平均线时建立趋势位置。策略简单实用,容易实现。同时也存在一些可以优化的空间,通过参数优化、止损规避、仓位管理等手段,可以获得更好的策略效果。
/*backtest
start: 2022-12-01 00:00:00
end: 2023-12-07 00:00:00
period: 1d
basePeriod: 1h
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/
// © Astorhsu
//@version=5
strategy("Astor SMA20/60 TW", overlay=true, margin_long=100, margin_short=100)
backtest_year = input(2018, title='backtest_year') //回測開始年分
backtest_month = input.int(01, title='backtest_month', minval=1, maxval=12) //回測開始月份
backtest_day = input.int(01, title='backtest_day', minval=1, maxval=31) //回測開始日期
start_time = timestamp(backtest_year, backtest_month, backtest_day, 00, 00) //回測開始的時間函數
//Indicators
sma20 = ta.sma(close,20)
sma60 = ta.sma(close,60)
plot(sma20, color=color.green, title="sma(20)")
plot(sma60, color=color.red, title="sma(60)")
//進場條件
longCondition = ta.crossover(close, ta.sma(close, 20))
if (longCondition) and time >= start_time
strategy.entry("open long20", strategy.long, qty=1, comment="站上m20做多")
shortCondition = ta.crossunder(close, ta.sma(close, 20))
if (shortCondition) and time >= start_time
strategy.close("open long20",comment="跌破m20平倉", qty=1)
longCondition1 = ta.crossover(close, ta.sma(close, 60))
if (longCondition1) and time >= start_time
strategy.entry("open long60", strategy.long, qty=1, comment="站上m60做多")
shortCondition1 = ta.crossunder(close, ta.sma(close, 60))
if (shortCondition1) and time >= start_time
strategy.close("open long60",comment="跌破m60平倉", qty=1)