Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

trail stop low/high of bar

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

    trail stop low/high of bar

    Hello
    is this section of the script correct if I want to move the trailstop to the low or high of the bar once the positions moves?

    if (Position.MarketPosition == MarketPosition.Long)
    {
    // For long positions, trail the stop below the low of the previous bar
    double low = Lows[0][1]; // The low of the previous bar
    SetTrailStop(CalculationMode.Price, low);
    }
    else if (Position.MarketPosition == MarketPosition.Short)
    {
    // For short positions, trail the stop above the high of the previous bar
    double high = Highs[0][1]; // The high of the previous bar
    SetTrailStop(CalculationMode.Price, high);

    ​Thank you for your help

    #2
    Hello Mathias79,

    Thank you for your post.

    This should work to set the stop loss to either the high or the low of the previous bar once the position is opened. If there is a certain profit trigger you'd like to use, you would need to include that in your condition. We have an example of modifying the price of stop and target orders here:


    Please let us know if we may be of further assistance.

    Comment


      #3
      I wasn't able to make it work... would it be ok to post the script here for your review?

      Comment


        #4
        Originally posted by Mathias79 View Post
        I wasn't able to make it work... would it be ok to post the script here for your review?
        You could post a snippet of what you have tried and explain what you were expecting it to do vs. the actual behavior when you tested it so we can help you identify the next steps in the debugging process to help you get your strategy behaving how you would like it to.

        Unfortunately, in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services or one-on-one educational support in our NinjaScript Support department. This is so that we can maintain a high level of service for all of our clients as well as our associates.

        That said, through email or on the forum we are happy to answer any questions you may have about NinjaScript if you decide to code this yourself. We are also happy to assist with finding resources in our help guide as well as simple examples, and we are happy to assist with guiding you through the debugging process to assist you with understanding unexpected behavior.

        You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our NinjaTrader Ecosystem team to follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request or provide one-on-one educational services.​

        Please let me know if I may be of further assistance.

        Comment


          #5
          Thank you... I am interested in contacting a consultant for help. Please send me the list.

          Thanks

          Mathias

          Comment


            #6
            Hello Mathias79,

            You can search our list of NinjaScript consultants through the link below. Simply enter a consultant name or search by using our filter categories. Once you have identified your consultants of choice, please visit each consultant's site for more information or contact them directly to learn more:
            You can locate the contact information for the consultants on their direct websites for any additional questions you may have. Since these consultants are third-party services for NinjaTrader, all pricing and support information will need to be obtained through the consultant.

            The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The companies and services listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem, LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.

            Let me know if I may be of further assistance.

            Comment


              #7
              I just realized that the script I provided has a "double" in it. please see script below.
              is that an input or variable? if it is, could you please share an example that could work with this script?


              if (Position.MarketPosition == MarketPosition.Long)
              {
              // For long positions, trail the stop below the low of the previous bar
              double low = Lows[0][1]; // The low of the previous bar
              SetTrailStop(CalculationMode.Price, low);
              }
              else if (Position.MarketPosition == MarketPosition.Short)
              {
              // For short positions, trail the stop above the high of the previous bar
              double high = Highs[0][1]; // The high of the previous bar
              SetTrailStop(CalculationMode.Price, high);​



              Thank you for your hep

              Comment


                #8
                Don't know if this helps, but I just figured this out today.
                If you want a script to "Overwrite" a previous stop loss (so as not to spam orders and possibly get rogue orders out there), you can set your stop loss by entry name

                For whatever your terms are for queueing your "EnterLong" code: put in " EnterLong("ANameThatMakesSenseToYou"); "

                Then in your coding above, use the "entryFromSignal" to tie the stop loss to the position you entered.

                ---Snip----
                (Position.MarketPosition == MarketPosition.Long)
                {
                // For long positions, trail the stop below the low of the previous bar
                double low = Lows[0][1]; // The low of the previous bar
                SetTrailStop("ANameThatMakesSenseToYou", CalculationMode.Price, low);
                }
                else if (Position.MarketPosition == MarketPosition.Short)
                {
                // For short positions, trail the stop above the high of the previous bar
                double high = Highs[0][1]; // The high of the previous bar
                SetTrailStop("(Do The same here for your short entry)", CalculationMode.Price, high);​

                This way it will reassign whatever stop loss was the Low/High of the previous bar to the next low/high on bar change. I believe I have that right. Anyone at NT is welcome to correct me with thanks in advance.

                Comment


                  #9
                  I will try that. Thank you for your help!!

                  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
                  68 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