繁体   English   中英

Strategy.exit/s 无法执行止损或尾随获利条件仅在满足第一个条件时执行

[英]Strategy.exit/s fails to executes for both stop loss or trailing take profit condition only exuecutes for the first condition met

我正在尝试让 strategy.exit 在追踪止盈或止损条件下退出交易。 使用下面的代码,它只会在 strategy.exit 命令之一上执行。`

// Submit entry orders
if enterLong and entry_date_constraint and strategy.position_size==0
    strategy.entry('el', strategy.long)

if enterShort and entry_date_constraint and strategy.position_size==0
    strategy.entry('es', strategy.short)

//===================================================================================================================//

// Submit exit orders based on calculated stop loss price
if strategy.position_size > 0
    strategy.exit('el', stop=longStopLossPrice, comment = long_stop_loss_comment)
    strategy.exit('el', limit=longStopTrailingPrice, comment=long_trailing_profit_comment)
if strategy.position_size < 0
    strategy.exit('es', stop=shortStopLossPrice, comment = short_stop_loss_comment)
    strategy.exit('es', limit=shortStopTrailingPrice, comment=short_trailing_profit_comment)

your text我试图将声明合并为一个策略。退出(同时使用限价和止损)和限价两次 我还尝试切换声明,以便止损在前,尾随止盈在后,我也有试图结合尾随止盈的 strategy.close 语句。 我还尝试改进 if 语句以允许使用和/或语句的每个条件。 我也尝试过使用单独的 if 语句。

我使用先前收盘价的 % 来计算追踪利润价格,而不是使用报价。 我还使用初始买入价的百分比作为止损。

我的问题的解决方案是:

strategy.exit - 止损

  1. 我没有使用以下方法正确捕获入场价: strategy.opentrades.entry_price(strategy.opentrades - 1)

我将代码更新为以下内容:

//---------------------------------------
var float long_entry_price = na // setting the long_entry_price to na
 
if long_entry_cond // checking for the entry condition
    long_entry_price := long_entry_cond ? close : long_entry_price  // updating the entry price
strategy.entry("eS", strategy.short, comment = short_enter_comment) //entering a position
    strategy.exit(id="short_stop loss", stop = short_entry_price * short_stop_loss_multiplier, comment = short_stop_loss_comment) // setting the stop loss with the updated entry price in the 'stop' option.

//----------------strategy.exit trailing take profit:

if strategy.position_size>0 and close < long_stop_trailing_price and ta.barssince(long_entry_cond) > 1 //long_stop_trailing_price
    strategy.close(id="eL", immediately=true, comment = long_trailing_profit_comment)
    strategy.cancel(id="long_stop loss")

在上面,我已经为执行 strategy.close 的标准添加了一些条件。

  1. ta.barssince(long_entry_cond) > 1 // 这确保您与开盘价不在同一个柱线上。
  2. 使用了strategy.close // 这样我就可以使用“立即”选项。
  3. 如果不使用,则使用strategy.cancel删除止损。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM