// Initialize variables for stop loss (SL) and take profit (TP) var float sl_level = na var float tp1_level = na var float tp2_level = na var float tp3_level = na var float entry_price = na
// Calculate position size based on risk calculate_position_size(entry_price, sl_level, risk_percent) => risk_amount = strategy.equity * risk_percent sl_distance = math.abs(entry_price - sl_level) position_size = risk_amount / sl_distance position_size
// Place Buy Stop Order if (breakout_up) sl_level := low - syminfo.mintick entry_price := high + syminfo.mintick tp1_level := high + (high - low) * rr1 tp2_level := high + (high - low) * rr2 tp3_level := high + (high - low) * rr3
position_size = calculate_position_size(entry_price, sl_level, risk_percent) strategy.entry("Buy Stop", strategy.long, qty=position_size, stop=entry_price, comment="Buy Stop") strategy.exit("TP1", from_entry="Buy Stop", limit=tp1_level, qty=position_size / 2, stop=sl_level) strategy.exit("TP2", from_entry="Buy Stop", limit=tp2_level, stop=entry_price) // Move SL to BE strategy.exit("TP3", from_entry="Buy Stop", limit=tp3_level, stop=entry_price) // Move SL to BE
position_size = calculate_position_size(entry_price, sl_level, risk_percent) strategy.entry("Sell Stop", strategy.short, qty=position_size, stop=entry_price, comment="Sell Stop") strategy.exit("TP1", from_entry="Sell Stop", limit=tp1_level, qty=position_size / 2, stop=sl_level) strategy.exit("TP2", from_entry="Sell Stop", limit=tp2_level, stop=entry_price) // Move SL to BE strategy.exit("TP3", from_entry="Sell Stop", limit=tp3_level, stop=entry_price) // Move SL to BE
// Move SL to Break Even when price reaches 2x SL if (strategy.opentrades.profit(strategy.opentrades - 1) >= 2 * (high - low)) sl_level := entry_price strategy.exit("Move to BE", from_entry="Buy Stop", stop=entry_price) strategy.exit("Move to BE", from_entry="Sell Stop", stop=entry_price)
// Close trade fully when price crosses SMA in the opposite direction if (strategy.position_size > 0) if (strategy.position_size > 0 and close < sma100) strategy.close("Buy Stop") if (strategy.position_size < 0 and close > sma100) strategy.close("Sell Stop")
// Plot SMA 100 plot(sma100, title="SMA 100", color=color.blue)