Reaches Profit Target || End of Day
Short Exit: Close <= 200 EMA || Reaches Profit Target || End of Day
The stop loss is 20% of the option premium.
II. Advantages
The main advantages of this strategy are:
III. Risks
The main risks of this strategy are:
The following aspects can be optimized to reduce the above risks:
IV. Optimization Directions
The main optimization directions for this strategy are:
V. Conclusion
This article analyzed in detail the logic, strengths, weaknesses and optimization directions of the trend following strategy based on the distance between price and 200-day moving average. This strategy judges medium-long term trend by tracking the price deviation from long-term moving average. Positions are established when the deviation exceeds a threshold and closed when hitting stop loss or take profit targets. This strategy can track medium-long term trend well but still has some parameter optimization space. Future improvements can be made from multiple perspectives to make the strategy more robust across different market conditions.
/*backtest start: 2024-02-22 00:00:00 end: 2024-02-24 06:00:00 period: 3h basePeriod: 15m exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=4 strategy("Intraday Price Away from 200 EMA Strategy", overlay=true) // Define inputs emaPeriod = input(200, title="EMA Period") thresholdPercent = input(0.75, title="Threshold Percent", minval=0) // Define the threshold percentage // Calculate 200 EMA ema = ema(close, emaPeriod) // Calculate distance from 200 EMA as a percentage distance_percent = ((close - ema) / ema) * 100 // Track average entry price var float avgEntryPrice = na // Buy conditions buy_condition = close < ema and abs(distance_percent) >= thresholdPercent and close[1] < close[2] // Exit conditions for buy exit_buy_condition = close >= ema or time_close(timeframe.period) or (avgEntryPrice * 1.5) <= close // Sell conditions sell_condition = close > ema and abs(distance_percent) >= thresholdPercent and close[1] > close[2] // Exit conditions for sell exit_sell_condition = close <= ema or time_close(timeframe.period) or (avgEntryPrice * 1.5) >= close // Execute buy and sell orders only if there are no open trades if strategy.opentrades == 0 strategy.entry("Buy", strategy.long, when=buy_condition) strategy.entry("Sell", strategy.short, when=sell_condition) // Update average entry price for buy condition if buy_condition avgEntryPrice := close // Update average entry price for sell condition if sell_condition avgEntryPrice := close // Close buy position if exit condition is met strategy.close("Buy", when=exit_buy_condition) // Close sell position if exit condition is met strategy.close("Sell", when=exit_sell_condition) // Plot 200 EMA plot(ema, color=color.blue, linewidth=2) // Plot buy and sell signals plotshape(buy_condition, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small) plotshape(sell_condition, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)