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 NullPointStrategies, Today, 05:17 AM
                0 responses
                50 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                126 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                69 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                42 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                46 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X