Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Place multiple stop exit orders for single entry

Tags:

In a strategy on tradingview, I enter an entry and have a condition to place a trailing stop. At the same time I want a stop-loss order at a fixed price, but when I place two strategy.exit() commands, only one is actually used, because both are of type "stop". But it is possible to replace an exit order with another one by calling strategy.exit() and giving it the same id. So my idea is to replace one exit with another and back, when it suits me, such that only one exit order is active at the same time.

Let's say I have a strategy with a condition to enter a position with a certain price, eg conditionEnter and enterPrice, at the time of entering I also know the stop-loss stopLossPrice, so I can set:

if conditionEnter
    strategy.entry(id="longEnter", long=true, qty=1, limit=enterPrice
strategy.exit(id="longExit", from_entry="longEnter", qty_percent=100, stop=stopLossPrice)

Now, if conditionTrailingStopLoss is met, I replace it accordingly:

if conditionTrailingStopLoss
    strategy.exit(id="longExit", from_entry="longEnter", qty=strategy.position_size * 0.2, trail_price=priceToTriggerTrailingStopLoss, trail_offset=100)

where priceToTriggerTrailingStopLoss is equal or below the close price, so that the trailing stop is placed immediately and trails the price by 100 ticks. Notice, that even if it is executed, only a fifth of the current position is exited.

As expected, the placement of the trailing stop loss exit order erases the original stop loss exit order - that is even true, when different ids are used. I would like to wait until the trailing stop loss has executed (is filled) and then place the stop loss exit order again. But how can I determine the time of execution?

I tried something along

if strategy.position_size < strategy.position_size[1]
    strategy.exit(id="longEnter", long=true, qty=1, limit=enterPrice
strategy.exit(id="longExit", from_entry="longEnter", qty_percent=100, stop=stopLossPrice)

to see that some exit has been executed, but I never saw both orders executed. Changing the order of commands around does influence which one of the two is ignored though. If in above piece of code I change the id to something completely different, none of the three exit orders is ever executed!

So can anyone help here? I mean wanting to have one fixed stop loss and one trailing stop loss is not very exotic to wish for, it has to work somehow, right?

Edit: If I scroll back in the chart, I actually do see sometimes one, sometimes the other exit order executed, but still never both for the same entry order.

like image 327
Paul Avatar asked Jul 03 '19 23:07

Paul


1 Answers

I found a solution, one can also have a stop loss order with strategy.order() command, like this:

if conditionEnter
    strategy.entry(id="longEnter", long=true, qty=1, limit=enterPrice
    strategy.order(id="stopLossLong", long=strategy.short,qty=strategy.position_size, limit=syminfo.mintick, stop=stopLossPrice)
like image 79
Paul Avatar answered Sep 30 '22 20:09

Paul