This strategy is based on the golden cross and death cross principles of simple moving averages, making buy and sell decisions based on the crossover of 7-day and 14-day moving averages. It generates a buy signal when the 7-day MA crosses above the 14-day MA from below, and a sell signal when the 7-day MA crosses below the 14-day MA from above. The strategy also features stop loss, take profit, and trailing stop functions to lock in profits and control risks.
The core trading logic of this strategy is based on the crossover principles of the 7-day and 14-day moving averages. The 7-day MA reflects short-term price trends, while the 14-day MA reflects medium-term trends. When the short-term MA crosses above the medium-term MA from below, it signals that the short-term trend is strengthening, making it a good time to go long. Conversely, when the short-term MA crosses below the medium-term MA from above, it signals that the short-term trend is weakening, so one should close positions or go short.
Specifcally, this strategy calculates the 7-day and 14-day simple moving averages using the SMA indicator. After each candlestick forms, it compares the current values of the 7-day line and the 14-day line. If the 7-day line crosses above the 14-day line, a long signal is generated to go long. If the 7-day line crosses below the 14-day line, a short signal is generated to go short.
In addition, the strategy also sets stop loss, take profit, and trailing stop functions to lock in profits and control risks. The parameters can be optimized based on backtest results.
This strategy has the following advantages:
There are also some risks with this strategy:
To address these risks, the following countermeasures can be considered:
This strategy can be optimized in the following aspects:
In conclusion, this strategy is very suitable for beginners to learn. The logic is simple and easy to understand and implement. It also has relatively good market adaptiveness, with ample room for parameter adjustment and optimization to achieve steady profits. It is worthwhile for quantitative trading beginners to use it for getting started and learning.
/*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"}] */ //@version=4 // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © bensonsuntw strategy("Strategy Template[Benson]", pyramiding=1, default_qty_type=strategy.percent_of_equity, default_qty_value=100) backtest_year = input(2019, type=input.integer, title='backtest_year') backtest_month = input(01, type=input.integer, title='backtest_month', minval=1, maxval=12) backtest_day = input(01, type=input.integer, title='backtest_day', minval=1, maxval=31) start_time = timestamp(backtest_year, backtest_month, backtest_day, 00, 00) stop_loss_and_tp = input(title="Enable Stop Loss and Take Profit", type=input.bool, defval=true) trail_stop = input(title="Enable Trail Stop", type=input.bool, defval=true) buy_stop_loss = input(0.2, type=input.float, title='buy_stop_loss') sell_stop_loss = input(0.1, type=input.float, title='sell_stop_loss') buy_tp = input(0.4, type=input.float, title='buy_tp') sell_tp =input(0.2, type=input.float, title='sell_tp') trail_stop_long = input(1.1, type=input.float, title='trail_stop_long') trail_stop_short = input(0.9, type=input.float, title='trail_stop_short') trail_stop_long_offset = input(0.05, type=input.float, title='trail_stop_long_offset') trail_stop_short_offset = input(0.05, type=input.float, title='trail_stop_short_offset') // you can set your own logic here shortCondition = crossunder(sma(close,7),sma(close,14)) longCondition = crossover(sma(close,7),sma(close,14)) strategy.entry("Buy", strategy.long, when=longCondition ) strategy.close("Buy", when=shortCondition) strategy.exit("Close Buy","Buy", limit= stop_loss_and_tp?strategy.position_avg_price * (1+buy_tp):na, stop = stop_loss_and_tp?strategy.position_avg_price * (1-buy_stop_loss):na,trail_price=trail_stop?strategy.position_avg_price *trail_stop_long:na,trail_offset=trail_stop?-strategy.position_avg_price *trail_stop_long_offset:na) strategy.entry("Sell", strategy.short, when=shortCondition) strategy.close("Sell", when=longCondition) strategy.exit("Close Sell","Sell", limit= stop_loss_and_tp?strategy.position_avg_price * (1-sell_tp):na, stop = stop_loss_and_tp?strategy.position_avg_price * (1+sell_stop_loss):na,trail_price=trail_stop?strategy.position_avg_price *trail_stop_short:na,trail_offset=trail_stop?strategy.position_avg_price *trail_stop_short_offset:na) net_profit = strategy.netprofit + strategy.openprofit plot(net_profit, title="Net Profit", linewidth=2, style=plot.style_area, transp=50, color=net_profit >= 0 ? #26A69A : color.red)