交易反向 3 个逗号错误 - Pinescript

时间:2021-07-02 09:29:43

标签: pine-script

我来这里是为了寻求帮助,因为我无法弄清楚趋势反转顺序。总体而言,该策略基于 PSAR,作为第二个退出规则,如果趋势反转,机器人应该关闭当前头寸,同时打开一个新头寸。

该策略运行良好,回测期间一切顺利,但是当我将它与 3commas 结合使用时,我意识到当趋势反转时,机器人将开新仓,但不会关闭前一个仓位。问题是机器人会以多头条件退出空头头寸,而不是退出空头头寸,这会将错误的机器人 ID 发送到 3 个逗号,并且不允许其平仓。

注释基本上应该返回机器人订单的信息,但我已经对其进行了修改以简化您的理解。

正如您在附加的屏幕截图中看到的:

  • 进入多头头寸时,退出返回对应于代码中所写的:“退出多头反向”。同时机器人将开设一个新的空头头寸

  • 进入空头头寸时,退出返回与预期结果不符:“退出空头反向”,而是头寸退出返回“进入多头”。同时机器人将开设一个新的多头头寸

如果有人能提供帮助,我将不胜感激,提前致谢!

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 个答案:

没有答案