'Why aren't these strategy.exit() calls with stop losses getting triggered?
I have 2 TPs and 2 SLs.
In each case one is based on ticks (loss,profit) and one is based on a price (stop,limit).
The TPs work more or less as intended...
However, currently my strategy.exit() calls involving my stop losses never get triggered.
I don't think there is any problem with the values; e.g. I've plotted long_tp to check it.
Particularly, note I set stop losses at the beginning, and then modify the long_sl variable (used in strategy.exit ID "EL-SL2") to the entry price once one TP has been reached.
Please see my script code (actual exit calls are at the very bottom):
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ddctv
//@version=5
strategy("Double Top/Bottom + EMAs",overlay=true)
////////////
// Inputs //
////////////
max_bars_double_top_bottom = input(10)
take_longs = input(true)
// Set TP1 and SL1 (based on ticks)
tp_ticks = input(3000)
sl_ticks = input(3000)
order_qty_contracts = input(2)
///////////////////
// TP & SL Logic //
///////////////////
// Quantity to exit on first TP to occur
exit_qty_pct = 50
// Initialize SL (to be based on nearest EMA below double bottom)
var long_sl = 0.0
plot(long_sl==0?na:long_sl,color=color.red)
// Initialize TP (based on inverse distance from nearest EMA below double bottom)
var float long_tp_ema = na
// Set TP2 (needs help)
// if strategy.position_size>0 and strategy.position_size[1]==0
// long_tp_ema := strategy.position_avg_price + (low[1]-ema800)
plot(long_tp_ema==0?na:long_tp_ema,color=color.green)
// Move SL to entry after 1 TP is taken, and update exit_qty_pct to 100
if (strategy.position_size>0 and strategy.position_size[1]>strategy.position_size) // or (strategy.position_size<0 and strategy.position_size[1]<strategy.position_size)
long_sl := strategy.position_avg_price
exit_qty_pct := 100
// Reset equity_qty_pct to 50 after position is flat
if strategy.position_size==0
exit_qty_pct := 50
//////////
// EMAs //
//////////
ema50 = ta.ema(close,50)
ema200 = ta.ema(close,200)
ema800 = ta.ema(close,800)
plot(ema50,"EMA 50",color=color.aqua,linewidth=1,transp=25)
plot(ema200,"EMA 200",color=color.gray,linewidth=2,transp=25)
plot(ema800,"EMA 800",color=color.purple,linewidth=3,transp=25)
/////////////////////////////////////
// W / Double-bottom Pattern Setup //
/////////////////////////////////////
//
// (Meant for use with Renko bricks, but ideally should also work for regular bars)
// Define simple double bottom pattern
double_bottom = ta.lowest(close,1)[1]==ta.lowest(close,max_bars_double_top_bottom)[2] and close>open
plotshape(double_bottom,style=shape.triangleup,location=location.belowbar,size=size.tiny)
// Check trending for entry condition
// Is the 50 EMA falling and are the 200 & 800 EMAs falling?
mismatch_w = double_bottom and ema50<ema50[1] and (ema200>ema200[1] or ema800>ema800[1])
plotshape(mismatch_w,location=location.belowbar,size=size.normal)
/////////////
// ENTRIES //
/////////////
// LONGS
if mismatch_w and take_longs and (low[1]>ema200 or low[1]>ema800) and strategy.position_size==0
strategy.entry("Long",strategy.long,qty=order_qty_contracts)
// Set SL2 according to position of setup in relation to EMAs (200 or 800)
if low[1]>ema200 and low[1]>ema800
long_sl := math.max(ema200,ema800)
else if low[1]>ema200 and low[1]<ema800
long_sl := ema200
else if low[1]<ema200 and low[1]>ema800
long_sl := ema800
long_tp_ema := close + (low[1]-long_sl)
///////////
// EXITS //
///////////
strategy.exit("EL-TP1", from_entry="Long", profit=tp_ticks, qty_percent=exit_qty_pct)
strategy.exit("EL-TP2", from_entry="Long", limit=long_tp_ema, qty_percent=exit_qty_pct)
strategy.exit("EL-SL1", from_entry="Long", loss=sl_ticks)
strategy.exit("EL-SL2", from_entry="Long", stop=long_sl)
Does anyone know why my last two exits (the stop losses) never get triggered?
Solution 1:[1]
Try to combine SL & TP in single strategy.exit call:
strategy.exit("EL-TP1", from_entry="Long", profit=tp_ticks, loss=sl_ticks, qty_percent=exit_qty_pct)
strategy.exit("EL-TP2", from_entry="Long", limit=long_tp_ema, stop=long_sl, qty_percent=exit_qty_pct)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrey D |
