Pinescript – Tradingview strategy – Fixed stoploss & Dynamic take profit

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`

New contributor

Jochen Fostie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Pinescript – Tradingview strategy – Fixed stoploss & Dynamic take profit

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`

New contributor

Jochen Fostie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật