首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >strategy.exit命令不执行

strategy.exit命令不执行
EN

Stack Overflow用户
提问于 2022-11-18 07:57:39
回答 1查看 16关注 0票数 0

当符合进入条件时,脚本按当前买入价格的百分比计算止损订单。策略是在开盘时为止损下订单,这样如果价格满足,订单就会被填满。在多头和空头两种情况下都应该这样做。

即使在策略测试器中看到了停止丢失的条件,strategy.exit也不会执行。

脚本//

代码语言:javascript
复制
// Long Stop %
long_stop_percentage    = input.float(title ="Long Stop Loss (%)", minval=0.0, step=0.001, defval=1.0)
// this gets the last trade open price
long_current_buy_price     = strategy.opentrades.entry_price(strategy.opentrades - 1)
// this calculates the value that a stop loss is activated from the initial buy price
long_stop_loss_price = long_current_buy_price - (long_current_buy_price * (long_stop_percentage/100))
 
// Short Stop %
short_stop_percentage   = input.float(title="Short Stop Loss (%)", minval=0.0, step=0.001, defval=1.0)
// this gets the last trade open price
short_last_buy_price    = strategy.opentrades.entry_price(strategy.opentrades - 1)
// this calculates the value that a stop loss is activated from the initial buy price
short_stop_loss_price   = short_last_buy_price + (short_last_buy_price * (short_stop_percentage/100))
  
//===========================================================================================================================
//=============================================== Script Trade Entry  =======================================================
//===========================================================================================================================
    // Long Position
// the condition that needs to be met to enter a trade position
if ta.crossover(bb_src,bb_lower) and strategy.position_size==0 and entry_date_constraint
    strategy.entry("EL", strategy.long, comment = long_enter_comment)
    strategy.exit("xSL", from_entry="EL", stop = long_stop_loss_price)
    

     // Short Position
// the condition that needs to be met to enter a trade position
if ta.crossunder(bb_src,bb_upper) and strategy.position_size==0 and entry_date_constraint
    strategy.entry("ES", strategy.short, comment = short_enter_comment)
    strategy.exit("xSS", from_entry="ES", stop = short_stop_loss_price)
 
.....

I have tried to adjust the percentages for the stop loss and the trailing profit take.
Even though I have done that the orders dont stop loss and trailing profit properly.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-19 03:29:11

在您的strategy.exit中,您应该尝试使用限制而不是停止:

限价应用于按规定价格(或更好)退出市场头寸。

您的代码应该是:

代码语言:javascript
复制
// Long Stop %
// ...

// Long Position
// the condition that needs to be met to enter a trade position
if ta.crossover(bb_src,bb_lower) and strategy.position_size==0 and entry_date_constraint
    strategy.entry("EL", strategy.long, comment = long_enter_comment)
    strategy.exit("xSL", from_entry="EL", limit = long_stop_loss_price)


// Short Position
// the condition that needs to be met to enter a trade position
if ta.crossunder(bb_src,bb_upper) and strategy.position_size==0 and entry_date_constraint
    strategy.entry("ES", strategy.short, comment = short_enter_comment)
    strategy.exit("xSS", from_entry="ES", limit = short_stop_loss_price)

// ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74486523

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档