该策略是一个简单的SMA均线交叉策略。它使用两条不同周期的简单移动平均线(SMA),当快线从下向上穿过慢线时开仓做多,当快线从上向下穿过慢线时平仓。该策略可以自定义两条均线的长度,以及回测的起始和结束日期。
该策略的主要思路是利用均线的趋势特性和均线交叉的信号特性来进行交易。当快线在慢线之上时,说明当前处于上升趋势,应该持有多头头寸;当快线在慢线之下时,说明当前处于下降趋势,应该空仓观望。
SMA均线交叉策略是一个简单易懂、经典实用的趋势追踪策略,适合初学者学习和使用。它利用了均线的趋势特性和均线交叉的信号特性,可以快速捕捉到市场趋势的变化。但是该策略也存在一些局限性和风险,如滞后性、频繁交易、缺乏止损等。因此在实际应用中,需要根据具体情况进行适当的优化和改进,以提高策略的稳定性和盈利能力。
/*backtest start: 2023-03-22 00:00:00 end: 2024-03-27 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/ // © j0secyn //@version=5 strategy("MA Cross", overlay=true, margin_long=100, margin_short=100, default_qty_value=100, default_qty_type=strategy.percent_of_equity, initial_capital=10000) // === INPUT BACKTEST RANGE === fromDay = input.int(defval = 1, title = "From Day", minval = 1, maxval = 31) fromMonth = input.int(defval = 1, title = "From Month", minval = 1, maxval = 12) fromYear = input.int(defval = 2018,title = "From Year", minval = 1970) thruDay = input.int(defval = 30, title = "Thru Day", minval = 1, maxval = 31) thruMonth = input.int(defval = 9, title = "Thru Month", minval = 1, maxval = 12) thruYear = input.int(defval = 2024, title = "Thru Year", minval = 1970) slow_ma_length = input.int(defval = 100, title = "Slow MA lenght") fast_ma_length = input.int(defval = 30, title = "Fast MA lenght") // === FUNCTION EXAMPLE === start = timestamp(fromYear, fromMonth, fromDay, 00, 00) // backtest start window finish = timestamp(thruYear, thruMonth, thruDay, 23, 59) // backtest finish window window() => true // === LOGIC === crossOv = ta.crossover(ta.sma(close, fast_ma_length), ta.sma(close, slow_ma_length)) crossUn = ta.crossunder(ta.sma(close, fast_ma_length), ta.sma(close, slow_ma_length)) // === EXECUTION === // strategy.entry("L", strategy.long, when = window() and crossOv) // enter long when "within window of time" AND crossover // strategy.close("L", when = window() and crossUn) // exits long when "within window of time" AND crossunder strategy.entry("L", strategy.long, when = window() and crossOv) // enter long when "within window of time" AND crossover strategy.close("L", when = window() and crossUn) // exits long when "within window of time" AND crossunder