简体   繁体   中英

TradingView help using multiple strategy.exit calls

I'm having an odd issue modifying an open position in TradingView's strategy tester. Let me explain the context first:
The following line opens an order based on my entry condition, entryLong .

strategy.entry("ID", strategy.long, comment="L_Entry", when = entryLong)

This works great, however, you can't make money until you close an order;) thus:

strategy.exit( "L_STOP", "ID", loss = fixedSL * 10)

This line modifies the open order to add a stop loss at a price fixedSL below the entry position. At this point, my only exit condition is price hitting my stop loss, which will always result in a losing strategy. To address this, I include:

if (exitLong) strategy.exit("L_TRAIL", "ID", trail_points = fixedTP * 10, trail_offset = trailSL * 10)

Which then adds a trailing stop loss reaching a set profit.添加追踪止损。 This way, I can safely lock in profit while still leaving room for growth. Here lies the problem. Each exit condition has an ID - L_STOP and L_Trail (L stands for Long, bc this is a buy). I reference these IDs on my charts to help in debugging and only the L_STOP ever appears to close the order. This leads me to believe that the L_TRAIL exit condition is either never met (unlikely) or is never set. I know that the bool, exitLong , is set to True and the line should be executing.

I could avoid this issue entirely by setting the trailing stop and stop loss in a single strategy.exit call, but it is extremely helpful to see L_STOP or L_TRAIL printed on the screen to tell what caused the exit of a trade. Only the ID of an order is printed when its condition is met, so with one call it would only be L_STOP for instance, which doesn't give much information on the trigger to exiting.

Any and all feedback is helpful. I can also include screencaps of the charts if necessary.

strategy.exit( "L_STOP", "ID", loss = fixedSL * 10)

...

strategy.exit("L_TRAIL", "ID", trail_points = fixedTP * 10, trail_offset = trailSL * 10)

...

I reference these IDs on my charts to help in debugging and only the L_STOP ever appears to close the order. This leads me to believe that the L_TRAIL exit condition is either never met (unlikely) or is never set.

The problem here is that you use the strategy.exit() function twice, both times to set a stop loss (fixed stop and a trailing stop loss).

But that is not how strategy.exit() can work. TradingView's reference says:

"If you use a stop loss and a trailing stop, their order type is 'stop', so only one of them is placed (the one that is supposed to be filled first)."

This would explain why your second stop loss (the trail one) is not set.

What you'll need to do instead is rewrite your strategy code so that there's one stop send by strategy.exit() . Without seeing the full code I can't offer much practical advice. But perhaps you can give your trailing stop loss the trail_offset value that your normal stop would be set to?

I can't tell from your question what values the variables hold, but perhaps this would work:

stopPrice = exitLong ? strategy.position_avg_price - (trailSL * 10) :
     strategy.position_avg_price - (fixedSL * 10)
     
strategy.exit("L_STOP", "ID", stop=stopPrice)

This worked for me. Do a "strategy.cancel()" before the "strategy.exit()".

Now i get the id correctly plotted on the chart.

Example:

if (strategy.position_size > 0) //long position exists
    if strategy.position_avg_price < open //price is lower than my entry_price
        strategy.cancel(id="StopLoss")
        strategy.exit(id="TakePrft", limit= longLimit, stop = stopPrice)
    else if strategy.position_avg_price > open //price is higher than my entry_price
        strategy.cancel(id="TakePrft")
        strategy.exit(id="StopLoss", limit= longLimit, stop = stopPrice)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM