Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Conditionally Call SetStopLoss() and SetTrailStop()?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Conditionally Call SetStopLoss() and SetTrailStop()?

    Is it possible to conditionally set up a SetStopLoss() and SetTrailStop()?
    I have the following code in my strategy, but the TrailStop is executing as soon as a position is entered.
    I would like it to start trailing only once break even has been hit, as I have another initial StopLoss that is much tighter.

    if ((Position.MarketPosition == MarketPosition.Long)
    && (BEBool == true))
    {
    SetTrailStop(@"Long", CalculationMode.Ticks, 10, true);
    }​

    BEBool is a boolean set to true once breakeven has been hit. The SetTrailStop() is seemingly ignoring this and sets immediately on entry.

    #2
    Hello DZ469,

    Thank you for your post.

    It is possible to call SetStopLoss() or SetTrailStop() conditionally from OnBarUpdate(). However, are you using both at the same time?

    Please note that SetStopLoss() and SetTrailStop() cannot be used at the same time.

    From the Help Guide:

    "•The SetStopLoss() method can NOT be used concurrently with the SetTrailStop() or SetParabolicStop() method for the same position, if any methods are called for the same position (fromEntrySignal) the SetStopLoss() will always take precedence. You can however, use all three methods in the same strategy if they reference different signal names.​"



    Please let us know if you have any further questions.

    Comment


      #3
      So you can't use them both for the same EntrySignal, even if the conditions for their activation are different?

      Also, according to the snippet of code I included in my first post, the SetTrailStop should not be active until the boolean is set to true, but it begins trailing as soon as the long position is taken. Why is that?

      Comment


        #4
        Hello DX469,

        No, as detailed in the Help Guide you cannot use SetTrailStop() and SetStopLoss() together. You would only be able to do so if each references different signal names.

        Also, according to the snippet of code I included in my first post, the SetTrailStop should not be active until the boolean is set to true, but it begins trailing as soon as the long position is taken. Why is that?
        I recommend using prints to debug the script to determine why the SetTrailStop() condition is triggering when you aren't expecting it to. In order to better understand how the code is working, it will be necessary to use Print to see how the conditions are evaluating and enable TraceOrders to see if orders are being submitted, ignored, rejected, or cancelled.

        I'm also including a link to a forum post with further suggestions on debugging a script.

        Enable TraceOrders, print the time of the bar and all values used in the conditions that submit entry orders. Include labels for all values and comparison operators.

        Let me know if you need any assistance creating a print or enabling TraceOrders.

        Save the output from the output window to a text file and provide this with your reply.

        I'll be happy to assist with analyzing the output.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        20 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        119 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        63 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        41 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        45 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X