Reputation: 5080
This is my code:
for tradeIndex = 0 to strategy.opentrades - 1
entryPrice = math.round(strategy.opentrades.entry_price(tradeIndex))
tradeQty = strategy.opentrades.size(tradeIndex)
tradeId = strategy.opentrades.entry_id(tradeIndex)
stopLossCond = close < entryPrice * (100 - slThreshold) / 100
if (stopLossCond)
//strategy.close_all()
strategy.exit("SL", from_entry = tradeId, stop = entryPrice)
//strategy.close(tradeId, comment="SL hit", immediately = true)
label.new(x=bar_index, y = low*(1-tradeIndex/150), text="ID: "+ str.tostring(tradeId)+" Qty: "+str.tostring(tradeQty)+" EP: "+str.tostring(entryPrice)+" SL: "+str.tostring(entryPrice * (100 - slThreshold) / 100)+"\n", color=color.black, style=label.style_label_down, textcolor=color.white, size=size.small)
As you may see the commented part, I tried close()
and close_all()
, the latter worked just fine.
I know for sure, that stopLossCond
is triggering, because I see labels printing out on every bar with all the details. So it's beyond me, WHY doesn't my code close/exit the trade.
EDIT1:
My stopLossCond
is at fault. When I place the close outside the condition, it closes every trade as expected. Now I just can't figure out, why is it an issue, given I see the black labels on every single candle.
EDIT2: I tried a different approach and created a stop loss order right at the time of creating a limit order long. Still wouldn't work. It closes some trades at some random price points, sometimes -15%, some are still not closed and are in negative.
strategy.order(orderId, strategy.long, limit=limitPrice, qty=posSize, comment="Buy multi POC: " + str.tostring(j+1)) // TODO: Fix QTY here, so ROI is compounding... // qty=(orderSize / numLimits),
strategy.exit("SL", from_entry=orderId, stop=limitPrice*0.98)
EDIT3:
Using exit right after opening the order seems works a bit better, but then resulted in many small fragment orders being open.
I fixed it by rounding my quantities:
posSize = math.round(orderSize / close / numLimits)
Unfortunately my problem still remains unsolved. Some trades still remain open despite being way below my SL.
EDIT4: It seems like my issue is very much related to the order size. When in strategy I switch from 1 contract to a set number of USDT, more unclosed trades pop up. Super strange, can't figure it out yet.
EDIT5: I added order ID (represented by an entry price of the long order) to the exit order (triggering stop loss) and look what I found. It's not matching. The order triggering SL, is from a different trade, not corresponding to the entry price. Now I don't know if this is just TradingView glitch, or something worse.
strategy.order(orderId, strategy.long, limit=upperPOC, comment="Buy: Single POC", qty = posSize)
strategy.exit("SL"+str.tostring(orderId), from_entry=orderId, stop=upperPOC*0.98)
Upvotes: 0
Views: 46
Reputation: 5080
Thanks to the SO algorithm of finding related questions, I've stumbled upon this answer: Pine script strategy exit orders for one id being applied to a different id which solved my issue.
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.
Seems like default behavior is FIFO and with this setting I allow to close any trade in any order. See here for more: https://www.tradingcode.net/tradingview/exit-order-sequence/
Upvotes: 0