Issue
I'm experiencing an issue with my TradingView Pine Script strategy where Take Profit (TP) and Stop Loss (SL) orders are not executing properly in historical data. The strategy enters trades correctly, but even when the price clearly hits TP or SL levels, no exit orders are executed.
Strategy Settings
- Key Value (Sensitivity): 2
- ATR Period: 11
- Signals from Heikin Ashi Candles: Yes
TradingView Strategy Settings
- Recalculate After order is filled: Yes
- Calculate On every tick: Yes
- Using bar magnifier: No
- Calculate On bar close: Yes
- Using standard OHLC: Yes
Example Scenario
Recent trade example:
- Entry Candle:
- Open: 2,897.10
- High: 2,905.07
- Low: 2,896.75
- Close: 2,903.19
- Entry Price: 2,903.19
The price later reached 2,914.05 (which should have triggered TP1), but no TP execution or alert was generated.
Code
//@version=6
strategy(title = '2 UT Bot Strategy', overlay = true, default_qty_type = strategy.cash, default_qty_value = 1000, initial_capital = 1000, currency = currency.USDT)
//================= INPUT Time WINDOW RANGE
//===================================================================================
//=====
FromYear = input(defval = 2024, title = 'From Year', inline = '05', group = 'INPUT Time WINDOW RANGE')
FromMonth = input.int(defval = 1, title = 'From Month', minval = 1, maxval = 12, inline = '06', group = 'INPUT Time WINDOW RANGE')
FromDay = input.int(defval = 1, title = 'From Day', minval = 1, maxval = 31, inline = '06', group = 'INPUT Time WINDOW RANGE')
ToDay = 1
ToMonth = 1
ToYear = 9999
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // start of window
finish = timestamp(ToYear, ToMonth, ToDay, 00, 00) // end of window
window() => // creates the function "within window of time"
time >= start and time <= finish ? true : false
//
//===================================================================================
//===============================================
// ===================== Inputs
//===================================================================================
//================
a = input.int(defval = 2, title = 'Key Vaule. \'This changes the sensitivity\'', minval = 1)
c = input.int(defval = 11, title = 'ATR Period', minval = 1)
h = input.bool(defval = false, title = 'Signals from Heikin Ashi Candles')
LongTP1 = input.float(defval = 1, title = 'LongTP1', minval = 0, step = 0.1, inline = '01', group = 'Long TP/SL/Qty')
qtyLong1 = input.float(defval = 30, title = 'QtyTP1', minval = 0, maxval = 100, step = 2, inline = '01', group = 'Long TP/SL/Qty')
LongTP2 = input.float(defval = 3.7, title = 'LongTP2', minval = 0, step = 0.2, inline = '02', group = 'Long TP/SL/Qty')
qtyLong2 = input.float(defval = 10, title = 'QtyTP2', minval = 0, maxval = 100, step = 2, inline = '02', group = 'Long TP/SL/Qty')
LongTP3 = input.float(defval = 5.7, title = 'LongTP3', minval = 0, maxval = 100, step = 0.2, inline = '03', group = 'Long TP/SL/Qty')
qtyLong3 = input.float(defval = 10, title = 'QtyTP3', minval = 0, maxval = 100, step = 2, inline = '03', group = 'Long TP/SL/Qty')
LongSL = input.float(defval = 4.8, title = 'LongSL', minval = 0, step = 0.1, inline = '04', group = 'Long TP/SL/Qty')
ShortTP1 = input.float(defval = 1, title = 'ShortTP1', minval = 0, step = 0.1, inline = '01', group = 'Short TP/SL/Qty')
qtyShort1 = input.float(defval = 40, title = 'QtyTP1', minval = 0, maxval = 100, step = 2, inline = '01', group = 'Short TP/SL/Qty')
ShortTP2 = input.float(defval = 3.2, title = 'ShortTP2', minval = 0, step = 0.2, inline = '02', group = 'Short TP/SL/Qty')
qtyShort2 = input.float(defval = 40, title = 'QtyTP2', minval = 0, maxval = 100, step = 2, inline = '02', group = 'Short TP/SL/Qty')
ShortTP3 = input.float(defval = 5, title = 'ShortTP3', minval = 0, maxval = 100, step = 0.2, inline = '03', group = 'Short TP/SL/Qty')
qtyShort3 = input.float(defval = 20, title = 'QtyTP3', minval = 0, maxval = 100, step = 2, inline = '03', group = 'Short TP/SL/Qty')
ShortSL = input.float(defval = 4, title = 'ShortSL', minval = 0, step = 0.1, inline = '04', group = 'Short TP/SL/Qty')
//
//===================================================================================
//================================================
// =================== UT Bot Strategy
//===================================================================================
//==========
xATR = ta.atr(c)
nLoss = a * xATR
src = h ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close, lookahead = barmerge.lookahead_off) : close
xATRTrailingStop = 0.0
iff_1 = src > nz(xATRTrailingStop[1], 0) ? src - nLoss : src + nLoss
iff_2 = src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), src + nLoss) : iff_1
xATRTrailingStop := src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), src - nLoss) : iff_2
pos = 0
iff_3 = src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0) ? -1 : nz(pos[1], 0)
pos := src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0) ? 1 : iff_3
xcolor = pos == -1 ? color.red : pos == 1 ? color.green : color.blue
ema = ta.ema(src, 1)
above = ta.crossover(ema, xATRTrailingStop) // Cross
below = ta.crossover(xATRTrailingStop, ema) // Cross
buy = src > xATRTrailingStop and above and window() // buy logic
sell = src < xATRTrailingStop and below and window() // sell logic
barbuy = src > xATRTrailingStop
barsell = src < xATRTrailingStop
plotshape(buy, title = 'Buy', text = 'Buy', style = shape.labelup, location = location.belowbar, color = color.new(color.green, 0), textcolor = color.new(color.white, 0), size = size.tiny)
plotshape(sell, title = 'Sell', text = 'Sell', style = shape.labeldown, location = location.abovebar, color = color.new(color.red, 0), textcolor = color.new(color.white, 0), size = size.tiny)
barcolor(barbuy ? color.green : na)
barcolor(barsell ? color.red : na)
// =================== UT Bot Strategy
//===================================================================================
//=======
//
//===================================================================================
//============================================
// = It may not be good in this form / Ebben a formában nem biztos, hogy jó!
//=====================================================
i_alert_txt_start_long = input.text_area(defval = '', title = 'Long Start Message', group = 'Alerts')
i_alert_txt_longTP1 = input.text_area(defval = '', title = 'LongTP1 Message', group = 'Alerts')
i_alert_txt_longTP2 = input.text_area(defval = '', title = 'LongTP2 Message', group = 'Alerts')
i_alert_txt_longTP3 = input.text_area(defval = '', title = 'LongTP3 Message', group = 'Alerts')
i_alert_txt_start_short = input.text_area(defval = '', title = 'Short Start Message', group = 'Alerts')
i_alert_txt_shortTP1 = input.text_area(defval = '', title = 'ShortTP1 Message', group = 'Alerts')
i_alert_txt_shortTP2 = input.text_area(defval = '', title = 'ShortTP2 Message', group = 'Alerts')
i_alert_txt_shortTP3 = input.text_area(defval = '', title = 'ShortTP3 Message', group = 'Alerts')
i_alert_txt_close_long = input.text_area(defval = '', title = 'Long Close Message', group = 'Alerts')
i_alert_txt_close_short = input.text_area(defval = '', title = 'Short Close Message', group = 'Alerts')
//
//===================================================================================
//============================================
//=========================== TP/SL/QTY
//===================================================================================
//======
LONGTP1 = strategy.position_avg_price * (1 + LongTP1 / 100) // Long TP1 price
LONGTP2 = strategy.position_avg_price * (1 + LongTP2 / 100) // Long TP2 price
LONGTP3 = strategy.position_avg_price * (1 + LongTP3 / 100) // Long TP3 price
LONGSL = strategy.position_avg_price * (1 - LongSL / 100) // Long SL price
SHORTTP1 = strategy.position_avg_price * (1 - ShortTP1 / 100) // Short TP1 price
SHORTTP2 = strategy.position_avg_price * (1 - ShortTP2 / 100) // Short TP2 proce
SHORTTP3 = strategy.position_avg_price * (1 - ShortTP3 / 100) // Short TP3 proce
SHORTSL = strategy.position_avg_price * (1 + ShortSL / 100) // Short SL proce
//
//===================================================================================
//===========================================
// ================================== Buy/Sell orders
//==========================================================================
if buy
strategy.entry(id = 'Long', direction = strategy.long, comment = 'Long') // Long Start
alert(i_alert_txt_start_long)
strategy.close(id = 'Short', comment = 'Sx') // Sx Short Close
alert(i_alert_txt_close_short)
if sell
strategy.entry(id = 'Short', direction = strategy.short, comment = 'SS') // Short Start
alert(i_alert_txt_start_short)
strategy.close(id = 'Long', comment = 'Lx') // Lx Long Close
alert(i_alert_txt_close_long)
//================================== Long / Stop trail
//=========================================================================
//longStopPrice = 0.0
//longStopPrice := if strategy.position_size > 0
// stopValue = low * (1 - LTrail/100)
// math.max(stopValue, longStopPrice[1])
//else
// 0
//shortStopPrice = 0.0
//shortStopPrice := if strategy.position_size < 0
// stopValue = close * (1 + Strail/100)
// math.min(stopValue, shortStopPrice[1])
//else
// 999999
// ================================= if Long position open
//====================================================================
if strategy.position_size > 0
strategy.exit(id = 'LongTP1', from_entry = 'Long', qty_percent = qtyLong1, limit = LONGTP1, comment_profit = 'LongTP1')
strategy.exit(id = 'LongTP2', from_entry = 'Long', qty_percent = qtyLong2, limit = LONGTP2, comment_profit = 'LongTP2')
strategy.exit(id = 'LongTP3', from_entry = 'Long', qty_percent = qtyLong3, limit = LONGTP3, comment_profit = 'LongTP3')
strategy.exit(id = 'LongSL', from_entry = 'Long', stop = LONGSL, comment_loss = 'LongSL')
//================================= if Short position open
//====================================================================
if strategy.position_size < 0
strategy.exit(id = 'ShortTP1', from_entry = 'Short', qty_percent = qtyShort1, limit = SHORTTP1, comment_profit = 'ShortTP1')
strategy.exit(id = 'ShortTP2', from_entry = 'Short', qty_percent = qtyShort2, limit = SHORTTP2, comment_profit = 'ShortTP2')
strategy.exit(id = 'ShortTP3', from_entry = 'Short', qty_percent = qtyShort3, limit = SHORTTP3, comment_profit = 'ShortTP3')
strategy.exit(id = 'ShortSL', from_entry = 'Short', stop = SHORTSL, comment_loss = 'ShortSL')
//
//===================================================================================
//==========================================
//....................................
//Table .............................................................................
//........
exitStats() =>
int LTP1Count = 0
int LTP2Count = 0
int LTP3Count = 0
int LSLCount = 0
int STP1Count = 0
int STP2Count = 0
int STP3Count = 0
int SSLCount = 0
if strategy.closedtrades > 0
for i = 0 to strategy.closedtrades - 1 by 1
switch_var_0 = strategy.closedtrades.exit_comment(i)
switch switch_var_0
'LongTP1' =>
LTP1Count := LTP1Count + 1
LTP1Count
'LongTP2' =>
LTP2Count := LTP2Count + 1
LTP2Count
'LongTP3' =>
LTP3Count := LTP3Count + 1
LTP3Count
'LongSL' =>
LSLCount := LSLCount + 1
LSLCount
'ShortTP1' =>
STP1Count := STP1Count + 1
STP1Count
'ShortTP2' =>
STP2Count := STP2Count + 1
STP2Count
'ShortTP3' =>
STP3Count := STP3Count + 1
STP3Count
'ShortSL' =>
SSLCount := SSLCount + 1
SSLCount
[LSLCount, SSLCount, STP1Count, STP2Count, STP3Count, LTP1Count, LTP2Count, LTP3Count]
//---
var testTable = table.new(position.bottom_right, 1, 9, color.orange, border_width = 1)
if barstate.islastconfirmedhistory
[LSLCount, SSLCount, STP1Count, STP2Count, STP3Count, LTP1Count, LTP2Count, LTP3Count] = exitStats()
table.cell(testTable, 0, 0, 'Closed trades (' + str.tostring(strategy.closedtrades) + ') stats:')
table.cell(testTable, 0, 1, 'Long Stop: ' + str.tostring(LSLCount))
table.cell(testTable, 0, 2, 'Short Stop: ' + str.tostring(SSLCount))
table.cell(testTable, 0, 3, 'Short TP1: ' + str.tostring(STP1Count))
table.cell(testTable, 0, 4, 'Short TP2: ' + str.tostring(STP2Count))
table.cell(testTable, 0, 5, 'Short TP3: ' + str.tostring(STP3Count))
table.cell(testTable, 0, 6, 'Long TP1: ' + str.tostring(LTP1Count))
table.cell(testTable, 0, 7, 'Long TP2: ' + str.tostring(LTP2Count))
table.cell(testTable, 0, 8, 'Long TP3: ' + str.tostring(LTP3Count))
Expected Behavior
- Strategy should execute TP orders when price hits the target levels
- Strategy should execute SL orders when price hits stop loss level
- Trade markers should appear on the chart for both entries and exits
- The strategy table should show TP/SL hits in the statistics
Actual Behavior
- Strategy enters trades correctly
- No TP or SL executions are visible on the chart
- The strategy table doesn't show any TP/SL hits
- No trade markers appear for exits
Questions
- Why aren't the TP/SL orders executing even when price clearly hits the levels?
- Why aren't trade markers appearing on the chart for exits?
- What could be causing this issue with historical data processing?
- Are there any specific settings or code modifications needed to make TP/SL work properly?
Any help or suggestions would be greatly appreciated!
Issue
I'm experiencing an issue with my TradingView Pine Script strategy where Take Profit (TP) and Stop Loss (SL) orders are not executing properly in historical data. The strategy enters trades correctly, but even when the price clearly hits TP or SL levels, no exit orders are executed.
Strategy Settings
- Key Value (Sensitivity): 2
- ATR Period: 11
- Signals from Heikin Ashi Candles: Yes
TradingView Strategy Settings
- Recalculate After order is filled: Yes
- Calculate On every tick: Yes
- Using bar magnifier: No
- Calculate On bar close: Yes
- Using standard OHLC: Yes
Example Scenario
Recent trade example:
- Entry Candle:
- Open: 2,897.10
- High: 2,905.07
- Low: 2,896.75
- Close: 2,903.19
- Entry Price: 2,903.19
The price later reached 2,914.05 (which should have triggered TP1), but no TP execution or alert was generated.
Code
//@version=6
strategy(title = '2 UT Bot Strategy', overlay = true, default_qty_type = strategy.cash, default_qty_value = 1000, initial_capital = 1000, currency = currency.USDT)
//================= INPUT Time WINDOW RANGE
//===================================================================================
//=====
FromYear = input(defval = 2024, title = 'From Year', inline = '05', group = 'INPUT Time WINDOW RANGE')
FromMonth = input.int(defval = 1, title = 'From Month', minval = 1, maxval = 12, inline = '06', group = 'INPUT Time WINDOW RANGE')
FromDay = input.int(defval = 1, title = 'From Day', minval = 1, maxval = 31, inline = '06', group = 'INPUT Time WINDOW RANGE')
ToDay = 1
ToMonth = 1
ToYear = 9999
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // start of window
finish = timestamp(ToYear, ToMonth, ToDay, 00, 00) // end of window
window() => // creates the function "within window of time"
time >= start and time <= finish ? true : false
//
//===================================================================================
//===============================================
// ===================== Inputs
//===================================================================================
//================
a = input.int(defval = 2, title = 'Key Vaule. \'This changes the sensitivity\'', minval = 1)
c = input.int(defval = 11, title = 'ATR Period', minval = 1)
h = input.bool(defval = false, title = 'Signals from Heikin Ashi Candles')
LongTP1 = input.float(defval = 1, title = 'LongTP1', minval = 0, step = 0.1, inline = '01', group = 'Long TP/SL/Qty')
qtyLong1 = input.float(defval = 30, title = 'QtyTP1', minval = 0, maxval = 100, step = 2, inline = '01', group = 'Long TP/SL/Qty')
LongTP2 = input.float(defval = 3.7, title = 'LongTP2', minval = 0, step = 0.2, inline = '02', group = 'Long TP/SL/Qty')
qtyLong2 = input.float(defval = 10, title = 'QtyTP2', minval = 0, maxval = 100, step = 2, inline = '02', group = 'Long TP/SL/Qty')
LongTP3 = input.float(defval = 5.7, title = 'LongTP3', minval = 0, maxval = 100, step = 0.2, inline = '03', group = 'Long TP/SL/Qty')
qtyLong3 = input.float(defval = 10, title = 'QtyTP3', minval = 0, maxval = 100, step = 2, inline = '03', group = 'Long TP/SL/Qty')
LongSL = input.float(defval = 4.8, title = 'LongSL', minval = 0, step = 0.1, inline = '04', group = 'Long TP/SL/Qty')
ShortTP1 = input.float(defval = 1, title = 'ShortTP1', minval = 0, step = 0.1, inline = '01', group = 'Short TP/SL/Qty')
qtyShort1 = input.float(defval = 40, title = 'QtyTP1', minval = 0, maxval = 100, step = 2, inline = '01', group = 'Short TP/SL/Qty')
ShortTP2 = input.float(defval = 3.2, title = 'ShortTP2', minval = 0, step = 0.2, inline = '02', group = 'Short TP/SL/Qty')
qtyShort2 = input.float(defval = 40, title = 'QtyTP2', minval = 0, maxval = 100, step = 2, inline = '02', group = 'Short TP/SL/Qty')
ShortTP3 = input.float(defval = 5, title = 'ShortTP3', minval = 0, maxval = 100, step = 0.2, inline = '03', group = 'Short TP/SL/Qty')
qtyShort3 = input.float(defval = 20, title = 'QtyTP3', minval = 0, maxval = 100, step = 2, inline = '03', group = 'Short TP/SL/Qty')
ShortSL = input.float(defval = 4, title = 'ShortSL', minval = 0, step = 0.1, inline = '04', group = 'Short TP/SL/Qty')
//
//===================================================================================
//================================================
// =================== UT Bot Strategy
//===================================================================================
//==========
xATR = ta.atr(c)
nLoss = a * xATR
src = h ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close, lookahead = barmerge.lookahead_off) : close
xATRTrailingStop = 0.0
iff_1 = src > nz(xATRTrailingStop[1], 0) ? src - nLoss : src + nLoss
iff_2 = src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), src + nLoss) : iff_1
xATRTrailingStop := src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), src - nLoss) : iff_2
pos = 0
iff_3 = src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0) ? -1 : nz(pos[1], 0)
pos := src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0) ? 1 : iff_3
xcolor = pos == -1 ? color.red : pos == 1 ? color.green : color.blue
ema = ta.ema(src, 1)
above = ta.crossover(ema, xATRTrailingStop) // Cross
below = ta.crossover(xATRTrailingStop, ema) // Cross
buy = src > xATRTrailingStop and above and window() // buy logic
sell = src < xATRTrailingStop and below and window() // sell logic
barbuy = src > xATRTrailingStop
barsell = src < xATRTrailingStop
plotshape(buy, title = 'Buy', text = 'Buy', style = shape.labelup, location = location.belowbar, color = color.new(color.green, 0), textcolor = color.new(color.white, 0), size = size.tiny)
plotshape(sell, title = 'Sell', text = 'Sell', style = shape.labeldown, location = location.abovebar, color = color.new(color.red, 0), textcolor = color.new(color.white, 0), size = size.tiny)
barcolor(barbuy ? color.green : na)
barcolor(barsell ? color.red : na)
// =================== UT Bot Strategy
//===================================================================================
//=======
//
//===================================================================================
//============================================
// = It may not be good in this form / Ebben a formában nem biztos, hogy jó!
//=====================================================
i_alert_txt_start_long = input.text_area(defval = '', title = 'Long Start Message', group = 'Alerts')
i_alert_txt_longTP1 = input.text_area(defval = '', title = 'LongTP1 Message', group = 'Alerts')
i_alert_txt_longTP2 = input.text_area(defval = '', title = 'LongTP2 Message', group = 'Alerts')
i_alert_txt_longTP3 = input.text_area(defval = '', title = 'LongTP3 Message', group = 'Alerts')
i_alert_txt_start_short = input.text_area(defval = '', title = 'Short Start Message', group = 'Alerts')
i_alert_txt_shortTP1 = input.text_area(defval = '', title = 'ShortTP1 Message', group = 'Alerts')
i_alert_txt_shortTP2 = input.text_area(defval = '', title = 'ShortTP2 Message', group = 'Alerts')
i_alert_txt_shortTP3 = input.text_area(defval = '', title = 'ShortTP3 Message', group = 'Alerts')
i_alert_txt_close_long = input.text_area(defval = '', title = 'Long Close Message', group = 'Alerts')
i_alert_txt_close_short = input.text_area(defval = '', title = 'Short Close Message', group = 'Alerts')
//
//===================================================================================
//============================================
//=========================== TP/SL/QTY
//===================================================================================
//======
LONGTP1 = strategy.position_avg_price * (1 + LongTP1 / 100) // Long TP1 price
LONGTP2 = strategy.position_avg_price * (1 + LongTP2 / 100) // Long TP2 price
LONGTP3 = strategy.position_avg_price * (1 + LongTP3 / 100) // Long TP3 price
LONGSL = strategy.position_avg_price * (1 - LongSL / 100) // Long SL price
SHORTTP1 = strategy.position_avg_price * (1 - ShortTP1 / 100) // Short TP1 price
SHORTTP2 = strategy.position_avg_price * (1 - ShortTP2 / 100) // Short TP2 proce
SHORTTP3 = strategy.position_avg_price * (1 - ShortTP3 / 100) // Short TP3 proce
SHORTSL = strategy.position_avg_price * (1 + ShortSL / 100) // Short SL proce
//
//===================================================================================
//===========================================
// ================================== Buy/Sell orders
//==========================================================================
if buy
strategy.entry(id = 'Long', direction = strategy.long, comment = 'Long') // Long Start
alert(i_alert_txt_start_long)
strategy.close(id = 'Short', comment = 'Sx') // Sx Short Close
alert(i_alert_txt_close_short)
if sell
strategy.entry(id = 'Short', direction = strategy.short, comment = 'SS') // Short Start
alert(i_alert_txt_start_short)
strategy.close(id = 'Long', comment = 'Lx') // Lx Long Close
alert(i_alert_txt_close_long)
//================================== Long / Stop trail
//=========================================================================
//longStopPrice = 0.0
//longStopPrice := if strategy.position_size > 0
// stopValue = low * (1 - LTrail/100)
// math.max(stopValue, longStopPrice[1])
//else
// 0
//shortStopPrice = 0.0
//shortStopPrice := if strategy.position_size < 0
// stopValue = close * (1 + Strail/100)
// math.min(stopValue, shortStopPrice[1])
//else
// 999999
// ================================= if Long position open
//====================================================================
if strategy.position_size > 0
strategy.exit(id = 'LongTP1', from_entry = 'Long', qty_percent = qtyLong1, limit = LONGTP1, comment_profit = 'LongTP1')
strategy.exit(id = 'LongTP2', from_entry = 'Long', qty_percent = qtyLong2, limit = LONGTP2, comment_profit = 'LongTP2')
strategy.exit(id = 'LongTP3', from_entry = 'Long', qty_percent = qtyLong3, limit = LONGTP3, comment_profit = 'LongTP3')
strategy.exit(id = 'LongSL', from_entry = 'Long', stop = LONGSL, comment_loss = 'LongSL')
//================================= if Short position open
//====================================================================
if strategy.position_size < 0
strategy.exit(id = 'ShortTP1', from_entry = 'Short', qty_percent = qtyShort1, limit = SHORTTP1, comment_profit = 'ShortTP1')
strategy.exit(id = 'ShortTP2', from_entry = 'Short', qty_percent = qtyShort2, limit = SHORTTP2, comment_profit = 'ShortTP2')
strategy.exit(id = 'ShortTP3', from_entry = 'Short', qty_percent = qtyShort3, limit = SHORTTP3, comment_profit = 'ShortTP3')
strategy.exit(id = 'ShortSL', from_entry = 'Short', stop = SHORTSL, comment_loss = 'ShortSL')
//
//===================================================================================
//==========================================
//....................................
//Table .............................................................................
//........
exitStats() =>
int LTP1Count = 0
int LTP2Count = 0
int LTP3Count = 0
int LSLCount = 0
int STP1Count = 0
int STP2Count = 0
int STP3Count = 0
int SSLCount = 0
if strategy.closedtrades > 0
for i = 0 to strategy.closedtrades - 1 by 1
switch_var_0 = strategy.closedtrades.exit_comment(i)
switch switch_var_0
'LongTP1' =>
LTP1Count := LTP1Count + 1
LTP1Count
'LongTP2' =>
LTP2Count := LTP2Count + 1
LTP2Count
'LongTP3' =>
LTP3Count := LTP3Count + 1
LTP3Count
'LongSL' =>
LSLCount := LSLCount + 1
LSLCount
'ShortTP1' =>
STP1Count := STP1Count + 1
STP1Count
'ShortTP2' =>
STP2Count := STP2Count + 1
STP2Count
'ShortTP3' =>
STP3Count := STP3Count + 1
STP3Count
'ShortSL' =>
SSLCount := SSLCount + 1
SSLCount
[LSLCount, SSLCount, STP1Count, STP2Count, STP3Count, LTP1Count, LTP2Count, LTP3Count]
//---
var testTable = table.new(position.bottom_right, 1, 9, color.orange, border_width = 1)
if barstate.islastconfirmedhistory
[LSLCount, SSLCount, STP1Count, STP2Count, STP3Count, LTP1Count, LTP2Count, LTP3Count] = exitStats()
table.cell(testTable, 0, 0, 'Closed trades (' + str.tostring(strategy.closedtrades) + ') stats:')
table.cell(testTable, 0, 1, 'Long Stop: ' + str.tostring(LSLCount))
table.cell(testTable, 0, 2, 'Short Stop: ' + str.tostring(SSLCount))
table.cell(testTable, 0, 3, 'Short TP1: ' + str.tostring(STP1Count))
table.cell(testTable, 0, 4, 'Short TP2: ' + str.tostring(STP2Count))
table.cell(testTable, 0, 5, 'Short TP3: ' + str.tostring(STP3Count))
table.cell(testTable, 0, 6, 'Long TP1: ' + str.tostring(LTP1Count))
table.cell(testTable, 0, 7, 'Long TP2: ' + str.tostring(LTP2Count))
table.cell(testTable, 0, 8, 'Long TP3: ' + str.tostring(LTP3Count))
Expected Behavior
- Strategy should execute TP orders when price hits the target levels
- Strategy should execute SL orders when price hits stop loss level
- Trade markers should appear on the chart for both entries and exits
- The strategy table should show TP/SL hits in the statistics
Actual Behavior
- Strategy enters trades correctly
- No TP or SL executions are visible on the chart
- The strategy table doesn't show any TP/SL hits
- No trade markers appear for exits
Questions
- Why aren't the TP/SL orders executing even when price clearly hits the levels?
- Why aren't trade markers appearing on the chart for exits?
- What could be causing this issue with historical data processing?
- Are there any specific settings or code modifications needed to make TP/SL work properly?
Any help or suggestions would be greatly appreciated!
Share Improve this question asked 2 days ago user1402259user1402259 111 silver badge6 bronze badges1 Answer
Reset to default 0The strategy appears to be running fine, at least with sufficient capital.
Otherwise, it starts with an initial_capital = 1000
and opens positions of default_qty_value = 1000
. Also, the optional margin_*
properties are not specified, and the default requires 100% of the funds from the equity.
/@version=6
strategy(title = '2 UT Bot Strategy', overlay = true, default_qty_type = strategy.cash, default_qty_value = 1000, initial_capital = 1000, currency = currency.USDT)