strategy.exit 订单未执行

问题描述 投票:0回答:1

当满足入场标准时,脚本会下达根据当前买入价格的百分比计算的止损订单。该策略是在开盘时下达止损订单,这样,如果达到该价格,订单就会被执行。对于多头和空头头寸条件都应该这样做。

即使在策略测试器中看到了止损条件,strategy.exit也永远不会执行。

// 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 don't stop loss and trailing profit properly.
pine-script
1个回答
0
投票

在你的strategy.exit中,你应该尝试使用 limit 而不是 stop :
应该使用限制以指定价格(或更好)退出市场头寸
你的代码应该是:

// 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)

// ...
© www.soinside.com 2019 - 2024. All rights reserved.