Trade reverse 3 commas error - Pinescript

Viewed 359

I came here for a little bit of help regarding a situation I cannot figure out for a trend reverse order. Overall the strategy is based on a PSAR and as a second exit rule, the bot should be closing the current position while opening a new one if the trend reverse.

The strategy is working well and everything is going smooth during backtesting, but when I've combined it with 3commas I realized that when a trend reverse occurred, the bot will be opening a new position but not close the previous one. The problem is that the bot will exit the short position with a long condition instead of an exit short position, which will send the wrong bot ID to 3 commas and not allow it to close the position.

The comment should basically return the bot order's information, but I've modified it to simplify your understanding.

As you can see on the attached screenshot :

  • When entering long position, the exit return correspond to what is written in the code : " Exit Long reverse". Simultaneously the bot will be opening a new Short position

  • When entering short position, the exit return doesn't correspond to the expect result : " Exit Short reverse" but instead the position exit return " Enter Long". Simultaneously the bot will be opening a new Long position

I would be very grateful if someone could help, thanks in advance !

Find the screenshot of the trades here


//===================================================================================================================
//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// STRATEGY ENTRY CONDITIONS ========================================================================================

bool startLongDeal =  (trend_bars ==  entry_bars) 
bool startShortDeal =  ( trend_bars == -entry_bars)
bool closeLongDeal_LF = crossunder(close2[1], MagicTrend2)
bool closeShortDeal_LF = crossover(close2[1], MagicTrend2)
bool closeLongDeal =   closeLongDeal_LF or startShortDeal
bool closeShortDeal = closeShortDeal_LF or startLongDeal


//===================================================================================================================
//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// STRATEGY BUY / SELLS TRAILING LOGIC ==============================================================================


bool longIsActive = startLongDeal or strategy.position_size > 0 and not startShortDeal
bool shortIsActive = startShortDeal or strategy.position_size < 0 and not startLongDeal

// take profit
var longTrailingTakeProfitExecuted = false
longTrailingTakeProfitExecuted := strategy.position_size > 0 and (longTrailingTakeProfitExecuted[1] or strategy.position_size < strategy.position_size[1])

float longTakeProfitPrice = na
longTakeProfitPrice := if (longTradesEnabled and longIsActive and not longTrailingTakeProfitExecuted)
    nz(longTakeProfitPrice[1], close * (1 + longTakeProfitPerc))
else
    na

var shortTrailingTakeProfitExecuted = false
shortTrailingTakeProfitExecuted := strategy.position_size < 0 and (shortTrailingTakeProfitExecuted[1] or strategy.position_size > strategy.position_size[1])

float shortTakeProfitPrice = na
shortTakeProfitPrice := if (shortTradesEnabled and shortIsActive and not shortTrailingTakeProfitExecuted)
    nz(shortTakeProfitPrice[1], close * (1 - shortTakeProfitPerc))
else
    na

longTrailingTakeProfitStepTicks = longTakeProfitPrice * trailingTakeProfitDeviationPerc / syminfo.mintick
shortTrailingTakeProfitStepTicks = shortTakeProfitPrice * trailingTakeProfitDeviationPerc / syminfo.mintick

// determine trailing stop loss price. Trailing starts when the take profit price is reached
bool enableLongTakeProfitTrailing = enableStopLossTrailing == "ON" or enableStopLossTrailing == "TP" and longTrailingTakeProfitExecuted

float longTrailingStopLossPrice = na
longTrailingStopLossPrice := if (longTradesEnabled and longIsActive)
    stopValue = (startLongDeal ? close : enableLongTakeProfitTrailing ? high : strategy.position_avg_price) * (1 - longTrailingStopLossPerc)
    max(stopValue, nz(longTrailingStopLossPrice[1]))
else
    na

bool enableShortTakeProfitTrailing = enableStopLossTrailing == "ON" or enableStopLossTrailing == "TP" and shortTrailingTakeProfitExecuted

float shortTrailingStopLossPrice = na
shortTrailingStopLossPrice := if (shortTradesEnabled and shortIsActive)
    stopValue = (startShortDeal ? close : enableShortTakeProfitTrailing ? low : strategy.position_avg_price) * (1 + shortTrailingStopLossPerc)
    min(stopValue, nz(shortTrailingStopLossPrice[1], 999999.9))
else
    na

//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// STRATEGY EXECUTION ===============================================================================================


enter_B  = '{"message_type": "bot", "bot_id": ' + bot_idB + ' , "email_token": "3273efba-b2d9-4534-9281-88e78b219629", "delay_seconds": 0 }'
exit_B   = '{"message_type": "bot", "bot_id": ' + bot_idB + ' , "email_token": "3273efba-b2d9-4534-9281-88e78b219629", "delay_seconds": 0, "action": "close_at_market_price" }' 
enter_S  = '{"message_type": "bot", "bot_id": ' + bot_idS + ' , "email_token": "3273efba-b2d9-4534-9281-88e78b219629", "delay_seconds": 0 }'
exit_S   = '{"message_type": "bot", "bot_id": ' + bot_idS + ' , "email_token": "3273efba-b2d9-4534-9281-88e78b219629", "delay_seconds": 0, "action": "close_at_market_price" }' 


//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// STRATEGY EXECUTION ===============================================================================================

if (longTradesEnabled and isWithinBacktestPeriod())
    // getting into LONG position
    strategy.entry(id = "Long Entry", long = strategy.long, oca_name = 'Exit Long', when = startLongDeal, comment = "Enter Long")
    // close on trend reversal
    strategy.close(id = "Long Entry", when = startShortDeal, comment = "Exit Long Reverse" )
    // submit exit order for trailing take profit price
    strategy.exit(id = "Long Take Profit", from_entry = "Long Entry", qty_percent = profitQuantityPerc, limit = enableTakeProfitTrailing ? na : longTakeProfitPrice, trail_price = enableTakeProfitTrailing ? longTakeProfitPrice : na, trail_offset = enableTakeProfitTrailing ? longTrailingTakeProfitStepTicks : na, oca_name = 'Exit Long ', when = longIsActive, comment = "Exit Long TP" )
    strategy.cancel(id = "Long Take Profit", when = not longIsActive)
    // submit exit orders for trailing stop loss price
    strategy.exit(id = "Long Stop Loss", from_entry = "Long Entry", qty_percent = 100, stop = longTrailingStopLossPrice, oca_name = 'Exit Long', when = longIsActive, comment = "Exit Long SL" )
    strategy.cancel(id = "Long Stop Loss", when = not longIsActive)


if (shortTradesEnabled and isWithinBacktestPeriod())
    // getting into SHORT position
    strategy.entry(id = "Short Entry", long = strategy.short, oca_name = 'Exit Short', when = startShortDeal, comment= "Enter Short" )
    strategy.close(id = "Short Entry", when = startLongDeal, comment = "Exit Short Reverse")
    strategy.exit(id = "Short Take Profit", from_entry = "Short Entry", qty_percent = profitQuantityPerc, limit = enableTakeProfitTrailing ? na : shortTakeProfitPrice, trail_price = enableTakeProfitTrailing ? shortTakeProfitPrice : na, trail_offset = enableTakeProfitTrailing ? shortTrailingTakeProfitStepTicks : na, oca_name = 'Exit Short', when = shortIsActive, comment = "Exit Short TP")
    strategy.cancel(id = "Short Take Profit", when = not shortIsActive)
    strategy.order(id = "Short Stop Loss", long = strategy.long, qty = -1 * strategy.position_size, stop = shortTrailingStopLossPrice, oca_name = 'Exit Short', oca_type = strategy.oca.cancel, when = strategy.position_size < 0, comment = "Exit Short SL" )
    strategy.cancel(id = "Short Stop Loss", when = not shortIsActive)


0 Answers
Related