首页 > 解决方案 > 在 pine 脚本开始做空时关闭多头交易不起作用

问题描述

我试图在开始多头订单时关闭我的空头订单,反之亦然,但它仅适用于多头头寸。它让我的短裤敞开。

bool startLongDeal = crossover(closeSeriesAlt, openSeriesAlt)
bool startShortDeal = crossunder(closeSeriesAlt, openSeriesAlt)

bool closeLongDeal = startShortDeal
bool closeShortDeal = startLongDeal

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

// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// STRATEGY EXECUTION ===============================================================================================
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//
if (longTradesEnabled and isWithinBacktestPeriod())
    // getting into LONG position
    strategy.entry(id = "Long Entry", long = strategy.long, when = startLongDeal and barstate.isconfirmed, comment = "Start Long", alert_message = Long)
    // close on trend reversal
    strategy.close(id = "Long Entry", when = closeLongDeal, comment = "Close Long", alert_message = ExitLong)
    // submit exit order for trailing take profit price also set the stop loss for the take profit percentage in case that stop loss it reached first
    strategy.exit(id = "Long Take Profit / Stop Loss", from_entry = "Long Entry", qty_percent = profitQuantityPerc, limit = enableTakeProfitTrailing ? na : longTakeProfitPrice, stop = longTrailingStopLossPrice, trail_price = enableTakeProfitTrailing ? longTakeProfitPrice : na, trail_offset = enableTakeProfitTrailing ? longTrailingTakeProfitStepTicks : na, when = longIsActive, alert_message = ExitLong)
    // submit exit order for trailing stop loss price for the remaining percent of the quantity not reserved by the take profit order
    strategy.exit(id = "Long Stop Loss", from_entry = "Long Entry", qty_percent = 100, stop = longTrailingStopLossPrice, when = longIsActive, alert_message = ExitLong)

if (shortTradesEnabled and isWithinBacktestPeriod())
    // getting into SHORT position
    strategy.entry(id = "Short Entry", long = strategy.short, when = startShortDeal and barstate.isconfirmed, comment = "Start Short", alert_message = Goshort)
    // close on trend reversal
    strategy.close(id = "Short Entry", when = closeShortDeal, comment = "Close Short", alert_message = ExitShort)
    // submit exit order for trailing take profit price also set the stop loss for the take profit percentage in case that stop loss it reached first
    strategy.exit(id = "Short Take Profit / Stop Loss", from_entry = "Short Entry", qty_percent = profitQuantityPerc, limit = enableTakeProfitTrailing ? na : shortTakeProfitPrice, stop = shortTrailingStopLossPrice, trail_price = enableTakeProfitTrailing ? shortTakeProfitPrice : na, trail_offset = enableTakeProfitTrailing ? shortTrailingTakeProfitStepTicks : na, when = shortIsActive, alert_message = ExitShort)
    // submit exit order for trailing stop loss price for the remaining percent of the quantity not reserved by the take profit order
    strategy.exit(id = "Short Stop Loss", from_entry = "Short Entry", qty_percent = 100, stop = shortTrailingStopLossPrice, when = shortIsActive, alert_message = ExitShort)

预览 1 预览 2

我错过了什么?

谢谢

标签: pine-scriptalgorithmic-tradingtradingview-api

解决方案


推荐阅读