user14554500
user14554500

Reputation: 15

Pine script strategy exit orders for one id being applied to a different id

OK I am sure I must be making a silly mistake but can't work out what's happening. Can anyone tell me where my understand it wrong or where the problem is? I'm happy to accept that I've simply misunderstood something.

My strategy is fairly complex and was working but I appear to have broken it. I am trying to develop a strategy with multiple "classes" of trade (ie TA, TB , TC) with different entry and exit conditions but also some emergency exit all type conditions. I want to have the configurable potential to have multiple active trades of each class so, for example, a max of 3 x TA, 4 x TB, 2 x TC.... For each class there may be an exit condition for that class that generates a close but normally the trade will be closed with an exit.

Anyway, I found that exit orders for TB were closing TA trades etc. After hours staring at the code I decided to just try to create a simple strategy to replicate just this problem. Here's the code:

@version=5
strategy(title = "Test Strategy", shorttitle="Test Strategy", overlay = true, max_bars_back = 100, pyramiding = 30, process_orders_on_close = true, calc_on_every_tick = true)
import TradingView/ta/5

float price_atr = ta.atr(10)
float st_upper = na
float st_lower = na
[supertrend10, st10_dir] = ta.supertrend(1.5, 5)
st_lower :=   (st10_dir == -1 ? supertrend10 : na)
st_upper :=   (st10_dir == 1 ? supertrend10 : na)
bool st_long_start  = (na(st_lower[1]) and st10_dir == -1)
bool st_upper_signal = (na(st_upper[1]) and st10_dir == 1)
bool st_long_sell = (not(na(st_lower[1])) and na(st_lower))

var int orders_TA = 0
var int orders_TB = 0
var float sell_TB = 0
orders_TA := 0
orders_TB := 0
for tradeNumber = 0 to (strategy.opentrades>0 ? strategy.opentrades - 1 : na)
    switch strategy.opentrades.entry_id(tradeNumber)
        "TA" => orders_TA += 1
        "TB" => orders_TB += 1
bool st_trade = not(na(st_lower)) and low<=st_lower
switch
    st_long_start and orders_TA<4 =>
        strategy.entry("TA", strategy.long, qty = 2)
        strategy.exit("A exit", "TA", qty = 1, limit = (na(st_lower) ? high + price_atr : st_lower + 2.5 *  price_atr))
    st_trade and orders_TB<4 =>
        strategy.entry("TB", strategy.long, qty = 2)
        sell_TB := st_lower + 3 * price_atr
    st_long_sell => strategy.close_all()
sell_TB := na(st_lower) ? low : math.max(sell_TB, st_lower + 1.5*price_atr)
strategy.exit("B exit", "TB", qty = 1, limit = sell_TB)
plot(st_lower, title = "Lower ST", style = plot.style_linebr, color = color.orange, linewidth = 3) 
plot(st_upper, title = "Upper ST", style = plot.style_linebr, color = color.fuchsia, linewidth = 2)

This only has two classes of trade TA and TC and each generates qty=2 entry on the entry condition. For TA trades two exits orders are created when the trade is opened. For TB trades there is a single limit order that is meant to be updated on each bar. It will sell half of the order when it first executes and the next bar there will be no "B exit" order so it will generate a new order to sell the second half of the trade. There is an emergency sell that simply closes_all trades (TA and TB).

So I apply this to MSFT with daily bars. Here is a screenshot of part of the trade history.

enter image description here

On my screen trade 1005 is opened with id TB but closed with a "A exit2" order. Trade 1004 is opened with a "TA" and closed with a "B exit". What am I doing wrong

Upvotes: 0

Views: 360

Answers (2)

user14554500
user14554500

Reputation: 15

After significantly more investigating I think the solution is to have

close_entries_rule = "ANY"

in the strategy declaration statement. At the moment that seems to work.

Upvotes: 1

Rakesh Poluri
Rakesh Poluri

Reputation: 357

So, this is weird, since the from_entry is specified, which should mean that it should not exit other signals.

before

However, I did notice that when I made the exit quantity the same as the entry quantity, all exits worked properly.

after

I am not sure why this is happening. It could be a bug.

Upvotes: 0

Related Questions