I want to make a (weekly) pullback trading strategy, and test it in the strategy tester. I’m struggling however to set the initial stoploss at a fixed value so it doesn’t update as the trade progresses.
I can’t code myself, so I’ve been using ChatGPT for the code below, but it doesn’t seem to understand what i’m trying to do.
In short:
I want to enter on pullbacks to the weekly 10 ema (or 30 ema), but only after price has confirmed a bounce. For this, I’ll look at the daily chart for a close above the 4ema(high).
My initial stoploss would be below the lowest low of this bounce (swing low), and my take profit would be 2 ATR below the most recent high on the weekly chart.
To simplify this in the code, I’ll only look at the daily chart =>
50ema touch in the last x candles
4ema(high) close
Stoploss: most recent low (or potentially 1 ATR below the 50ema (keltner channel)
Take profit: 4 ATR below the most recent high (this more or less translates to 2 ATR on the weekly on some spot checks I did.
It’s the initial stoploss that is the problem. I can ask ChatGPT to set the stoploss on the 50ema keltner channel, but I don’t want this stoploss to move up (or down) as the trade progresses. Once the take profit level exceeds the stoploss, this should be where the trade is exited.
Thanks for the help!
I tried this code from ChatGPT but it doesn’t do exactly what is expected:
//@version=5
strategy("EMA and Chandelier Strategy (Fixed Stop-Loss)", overlay=true)
// Input Parameters
ema_length50 = input.int(50, title="50 EMA Length")
ema_length4 = input.int(4, title="4 EMA Length (Highs)")
atr_length = input.int(14, minval=1, title="ATR Length")
chandelier_length = input.int(22, minval=1, title="Chandelier Highest High Length")
atr_coefficient = input.float(4.0, title="Chandelier ATR Coefficient")
lookback_candles = input.int(5, minval=1, title="Lookback Candles for Cross and Stop Loss")
// Weekly EMA Parameters
ema_length8 = 8
ema_length21 = 21
ema_length34 = 34
ema_length55 = 55
// EMA Calculations
ema50 = ta.ema(close, ema_length50)
ema4_high = ta.ema(high, ema_length4)
ema8 = request.security(syminfo.tickerid, "W", ta.ema(close, ema_length8))
ema21 = request.security(syminfo.tickerid, "W", ta.ema(close, ema_length21))
ema34 = request.security(syminfo.tickerid, "W", ta.ema(close, ema_length34))
ema55 = request.security(syminfo.tickerid, "W", ta.ema(close, ema_length55))
// Chandelier Exit Calculation
atr = ta.rma(ta.tr, atr_length)
highest_high = ta.highest(high, chandelier_length)
chandelier_exit = highest_high - (atr * atr_coefficient)
// Entry Conditions
crossed_ema50 = ta.lowest(low, lookback_candles) < ema50 and close > ema50
stacked_weekly_emas = ema8 > ema21 and ema21 > ema34 and ema34 > ema55
price_above_ema4 = close > ema4_high
price_above_previous_high = close > high[1]
long_condition = crossed_ema50 and stacked_weekly_emas and price_above_previous_high
// Variables for Stop-Loss
var float stop_loss_level = na
var int entry_bar_index = na
// Entry Logic
if (long_condition and strategy.position_size == 0) // New trade
strategy.entry("Long", strategy.long, comment="Long Entry")
stop_loss_level := ema50 - (1 * atr) // Set stop-loss to 1 ATR below EMA50
entry_bar_index := bar_index // Track the bar index of the entry
// Exit Conditions
stop_condition = strategy.position_size > 0 and low < stop_loss_level
exit_condition = strategy.position_size > 0 and close < chandelier_exit
// Manage Position
if stop_condition
strategy.close("Long", comment="Stop Loss Hit")
if exit_condition
strategy.close("Long", comment="Chandelier Exit")
// Reset Stop-Loss Level After Exit
if (strategy.position_size == 0)
stop_loss_level := na
entry_bar_index := na`
Jochen Fostie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.