Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

trailing stop code

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

    trailing stop code

    Greetings,
    I am trying to add to my strategy a trailing stop. The below code I was playing around with is not working well.
    When a trade is entered. A preset stop loss is set. After a bar candle move I would like the trailing stop start based on the high/low (high if a sell, low if a buy) of the previous candle.
    The code is partially working for me, except if after several candles there's an inside bar it stops and exit the strategy do to an error in putting the new SL. I would like the SL to remain with the past candle if there is a new inside bar candle that does not take out the high or low the previous candle.

    // Set 7
    if ((Position.MarketPosition != MarketPosition.Flat) && (BarsInProgress >= 1))
    {
    if ((Position.MarketPosition == MarketPosition.Short)
    && (Open[0] < High[1]) && ((High[1] - Open[0])>3))
    {
    SetStopLoss(CalculationMode.Price, High[1]);
    }
    else if ((Position.MarketPosition == MarketPosition.Long)
    && (Open[0] > Low[1]) && ((Open[0] - Low[1])>3))
    {
    SetStopLoss(CalculationMode.Price, Low[1]);
    }
    else return;
    }

    #2
    Are you intentionally using BarsInProgress to run the code on all additional dataseries added except for the primary one? It just seems like BarsInProgress might not be being used as its designed:

    Last edited by markdshark; 09-18-2022, 09:21 AM.

    Comment


      #3
      No, I was just trying things to hopefully get it to work right.

      Comment


        #4
        Seems like it should work without this bit "&& (BarsInProgress >= 1)". Whenever you get into onBarUpdate(), you're in a bar. BarsInProgress only returns an int that tells you which dataseries you are processing. 0 for primary, 1 for the first additional one added and so on... So I wouldn't use BarsInProgress the way it appears you might be. Rather try
        Code:
        if (CurrentBar < 1) return;
        Last edited by markdshark; 09-18-2022, 09:42 AM.

        Comment


          #5
          I think I used that before or similar, I will go back to it. However, something is occurring that is shutting down the strategy when an inside bar happens.

          Comment


            #6
            might be your else return; statement. Without looking too closely it seems that might be your branch of code for when you encounter an inside bar. You are telling the program to exit if that's the case

            Comment


              #7
              Hello allwyn,

              I can provide an example with sample logic you can learn from.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                okay, I'll look into that. It's still not working right.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Mindset, 04-21-2026, 06:46 AM
                0 responses
                90 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by M4ndoo, 04-20-2026, 05:21 PM
                0 responses
                137 views
                0 likes
                Last Post M4ndoo
                by M4ndoo
                 
                Started by M4ndoo, 04-19-2026, 05:54 PM
                0 responses
                68 views
                0 likes
                Last Post M4ndoo
                by M4ndoo
                 
                Started by cmoran13, 04-16-2026, 01:02 PM
                0 responses
                120 views
                0 likes
                Last Post cmoran13  
                Started by PaulMohn, 04-10-2026, 11:11 AM
                0 responses
                71 views
                0 likes
                Last Post PaulMohn  
                Working...
                X