pine-script - 在 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)
我错过了什么?
谢谢
解决方案
推荐阅读
- graphviz - Graphviz 布局从左到右,然后从上到下
- javascript - Javascript 滚动文本函数 - array.shift() 不是函数
- google-cloud-functions - 如何从 firebase 函数发送图像文件?
- swift - 如果 String 是可选的,如何将 String 转换为 Double?
- excel - Autifil 列基于另一列数据
- java - 当二叉树中的一个节点被触发时,Java 程序查找所有未燃烧的节点
- c# - 正确执行工厂
- typescript - 如何在 TypeScript 中扩展 Error 类
- android-studio-3.0 - 在 Android Studio 中,java 文件显示不同的图标并出现错误
- .net - 执行分批/解码时如何消除对集成帐户的依赖