This strategy adopts the crossover of 20-day moving average and 60-day moving average to generate trading signals. It goes long when price breaks above 20-day MA and closes position when price breaks below 20-day MA. Similarly, it forms trading signals when price crosses 60-day MA. This strategy belongs to a typical trend following system.
The above rules define the trading signals and logic for this strategy. When price crosses over the MA line, it shows a new trend is emerging and we can follow the trend to go long. When price drops below the MA line, it shows the trend is ending so we close position.
Risk Solutions:
This is a typical dual moving average crossover strategy. The core idea is to follow trends by establishing position when price crosses over MA line. The strategy is simple and practical to implement. Meanwhile, there is room for further optimization, by parameter tuning, stop loss, position sizing etc. to achieve better results.
/*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)