1

I'm writing a strategy that goes long 2 futures contracts at a time. I'm having an issue exiting positions. Sometimes the script sends two 1 lot sells at the same time instead of sending a single alert for both contracts if the stop loss is hit.

Setting stop value and take profit values:

if (strategy.position_size > 0)
    stopValue = long_stop_src * (1 - longTrailPerc)
    longStopPrice := math.max(stopValue, longStopPrice[1])
    tpValue = strategy.opentrades.entry_price(strategy.opentrades - 1) * (1 + longTPPerc)
    longTPPrice := math.max(tpValue, longTPPrice[1])

Exit code:

if (strategy.position_size == 2)
    strategy.exit(id="Long TP", stop=longStopPrice, limit=longTPPrice, qty=1)

if (strategy.position_size > 0)
    strategy.exit(id="Long SL", stop=longStopPrice)

This does trade at correct times and prices - the issue is that when I never hit the target profit and hit the trailing stop, it gets sent as two orders.

If I'm long two contracts, there is a take profit level. If this level is hit, I am trying to send a single alert to sell 1 contract at that price. Then, if the trailing stop is hit, we get stopped out of the other contract. This functionality works as expected. Both contracts have the same trailing stop price. In the case where we never hit a take profit, and get stopped out of both contracts, the script sends two different signals, each with a different trade ID. In this case, I want the script to sell both in one alert "Long SL" instead of two.

progains
  • 31
  • 1

0 Answers0