Search code examples
pine-scriptpine-script-v5

Pine: long order exits triggered but strategy.opentrades on the next bar is 1 (should be 0)


In my Pine strategy, I noticed that the value of strategy.opentrades was still to 1 even after the two long order exits (comprising 50% of the trade value each) triggered:

enter image description here

Here is the code of my strategy:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © me
//@version=5
strategy("Just Trend V2", process_orders_on_close=false, overlay=true, initial_capital=100, default_qty_value=1000, currency='USDT', default_qty_type=strategy.percent_of_equity, calc_on_every_tick = true, max_labels_count=500, use_bar_magnifier = true, margin_long = 1., commission_value = 0.07)
backtestStartDate = input.time(timestamp("12 Mar 2023 GMT+01:00"), title="Start Date", group="Backtest Time Period")
backtestEndDate = input.time(timestamp("13 Mar 2023 GMT+01:00"), title="End Date", group="Backtest Time Period")



// ### Four Smoothed Moving Averages

len1 = 21
//input(21, minval=1, title="Length 1", group = "Smoothed MA Inputs")
src1 = close
//input(close, title="Source 1", group = "Smoothed MA Inputs")
smma1 = 0.0
sma_1 = ta.sma(src1, len1)
smma1 := na(smma1[1]) ? sma_1 : (smma1[1] * (len1 - 1) + src1) / len1
plot(smma1, color=color.white, linewidth=2, title="21 SMMA")

len2 = 50
//input(50, minval=1, title="Length 2", group = "Smoothed MA Inputs")
src2 = close
//input(close, title="Source 2", group = "Smoothed MA Inputs")
smma2 = 0.0
sma_2 = ta.sma(src2, len2)
smma2 := na(smma2[1]) ? sma_2 : (smma2[1] * (len2 - 1) + src2) / len2
plot(smma2, color=color.new(#6aff00,0), linewidth=2, title="50 SMMA")

h100 = input.bool(title="Show 100 Line", defval=true, group = "Smoothed MA Inputs")
len3 = 100
//input(100, minval=1, title="Length 3", group = "Smoothed MA Inputs")
src3 = close
//input(close, title="Source 3", group = "Smoothed MA Inputs")
smma3 = 0.0
sma_3 = ta.sma(src3, len3)
smma3 := na(smma3[1]) ? sma_3 : (smma3[1] * (len3 - 1) + src3) / len3
sma3plot = plot(h100 ? smma3 : na, color=color.new(color.yellow,0), linewidth=2, title="100 SMMA")

len4 = 200
//input(200, minval=1, title="Length 4", group = "Smoothed MA Inputs")
src4 = close
//input(close, title="Source 4", group = "Smoothed MA Inputs")
smma4 = 0.0
sma_4 = ta.sma(src4, len4)
smma4 := na(smma4[1]) ? sma_4 : (smma4[1] * (len4 - 1) + src4) / len4
sma4plot = plot(smma4, color=color.new(#ff0500,0), linewidth=2, title="200 SMMA")

// Trend Fill

trendFill = input.bool(title="Show Trend Fill", defval=true, group = "Smoothed MA Inputs") 
ema2 = ta.ema(close, 2)
ema2plot = plot(ema2, color=color.new(#2ecc71, 50), style=plot.style_line, linewidth=1, title="EMA(2)", editable = false)

//bool condBuy = (ema2 > smma4 and trendFill) and close > smma1 and smma1 > smma2 and smma2 > smma3 and smma3 > smma4
bool condBuy = (ema2 > smma4 and trendFill) and close > smma1 and close > smma2 and close > smma3 and close > smma4

bool condSell = close < smma1 or close < smma2 or close < smma3 or close < smma4


fill(ema2plot, sma4plot, color=  ema2 > smma4 and trendFill ? color.new(color.green, 50) : ema2 < smma4 and trendFill ? color.new(color.red, 50) : na, title = "Trend Fill")

// End ###



inTradeWindow = time > backtestStartDate and time <= backtestEndDate




var Limit_Below_Entry_Price = 0.0

// LONG TRADES
if inTradeWindow and condBuy and strategy.opentrades > 0 == false // and not inLong[1] // 
   
    strategy.entry("My Long Entry ", strategy.long, limit = close)

    strategy.exit("My Exit 1", from_entry = "My Long Entry ", profit=15000, qty_percent = 50) // 50*syminfo.mintick

    strategy.exit("My Exit 2", from_entry = "My Long Entry ", qty_percent = 50, trail_offset=10000, trail_points=15000)


Limit_Below_Entry_Price := float(strategy.opentrades.entry_price(strategy.opentrades - 1)) - 100

if close < Limit_Below_Entry_Price
    strategy.close_all()



if inTradeWindow
    a = str.tostring(strategy.opentrades)
    label.new(bar_index, na, a, yloc = yloc.abovebar, size = size.normal,textcolor = color.white)

Anyone understand why the number of trade is 1 even after the two exits triggered ?

I noticed that this does not happen when the trade is closed with the strategy.close_all() function.

Thanks!


Solution

  • Your "My exit 2" ask to exit 50% of the on going trade.
    As your "My exit 1" already triggered, you have still 50% of your order in place.
    The your "My exit 2" is triggered, exit 50% of the remaining position.
    So after your "My exit2", you still have 25% of your initial position.
    You should try :

    strategy.exit("My Exit 2", from_entry = "My Long Entry ", qty_percent = 100, trail_offset=10000, trail_points=15000)