简体   繁体   中英

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

I am trying to get the strategy.exit to exit the trade on either a trailing take profit or stop loss condition. With the code below it only ever executes on one of the strategy.exit commands.`

// 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 I have tried to combine the statement into one strategy.exit (using both limit and stop) and limit twice I have also tried to switch the statements around so that the stop loss is first and trailing take profit is second, also I have tried to combine with a strategy.close statement for the trailing take profit. I have also tried to refine the if statement to allow for each condition with and/or statements. I have also tried to have separate if statements as well.

I am using a % of the previous close to calculate the trailing profit price, and am not using ticks. I am also using a percentage of the initial buy price for the stop loss.

The solution to my issue was:

strategy.exit - Stop Loss

  1. I was not capturing the entry price correctly using: strategy.opentrades.entry_price(strategy.opentrades - 1)

I updated the code to the below:

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

In the above I have added some conditions to the criteria to executing the strategy.close.

  1. ta.barssince(long_entry_cond) > 1 // this makes sure that you are not on the same bar as the open.
  2. Used a strategy.close // this is so that I could use the 'immediately' option.
  3. Used the strategy.cancel to remove the stop loss if not used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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