Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

A complicated Trailing Stop to a simpler one

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

    A complicated Trailing Stop to a simpler one

    I am using a strategy from the the ecosystem, it has a trailing stop with certain logic parameters, but sometimes the trailing stop doesn't execute and the market end up reversing on me.

    I would like to add a simple trailing stop instead, when the profit reach a certain amount in currency (like $100) the trailing stop begins, so if the market reversed the trade get closed before I lose the $100 profit.

    Can anyone please walk me through how can I achieve that with my current strategy?

    Here down is the code for the original trailing stop on my strategy

    Code:
    if (PriceBreakoutRectangularBase(upwardBreakout,VolumeMultiple).Plot0[0] == 1 &&
                    Position.MarketPosition == MarketPosition.Flat && PositionAccount.MarketPosition == MarketPosition.Flat)
                {
                    EnterLong(6, "Long");
                    justEntered = true;
                }
                if (Position.MarketPosition == MarketPosition.Long && justEntered == true)
                {
                    if (trailingStopPct == 0)
                    {
                        trailStop = Position.AveragePrice - (chandelierRangeMultiple * ATR(atrPeriod)[0]);
                    }
                    else
                    {
                        trailStop = Position.AveragePrice - (trailingStopPct * Position.AveragePrice);    
                    }
    
                    StopLoss = Position.AveragePrice - (stopLossPct * Position.AveragePrice);
    
                    prevValue = Position.AveragePrice;
    
                    justEntered = false;
    
                    //Print(" TRAIL STOP TRACKING: " + trailStop + " symbol " + Instrument.FullName);
                }    
    
                if (Position.MarketPosition == MarketPosition.Long)
                {
                    if (High[0] > Position.AveragePrice && High[0] > prevValue)
                    {
                        if (trailingStopPct == 0)
                        {
                            trailStop = High[0] - chandelierRangeMultiple * ATR(atrPeriod)[0];
                        }
                        else
                        {
                        trailStop = trailStop + (High[0] - prevValue);    
                        }
    
                           prevValue = High[0];
    
                           //Print(" TRAIL STOP RAISED: " + trailStop + " PrevValue " + prevValue + " symbol " + Instrument.FullName);
                    }
    
                    //Print(Time[0] + " High " + High[0] + " PrevValue " + prevValue + " symbol " + Instrument.FullName);
                }
    
                if (Low[0] <= trailStop && Position.MarketPosition == MarketPosition.Long && Position.AveragePrice < trailStop)
                {
                    Print(" TRAIL STOP HIT: " + trailStop + " " + Close[0] + " symbol " + Instrument.FullName);
                    // Trailing stop has been hit; do whatever you want here
                    ExitLong(100,"Trailing Stop" ,"Long");
                       trailStop = 0;
                    StopLoss = 0;
                    prevValue = 0;
                }​

    #2
    Hello onlinebusiness,

    Thank you for your post.

    For a more simple example of modifying the price of a stop loss, please see the following reference sample:


    That sample demonstrates how to move the stop loss to breakeven. The logic could be expanded upon to modify the stop loss as a trailing stop as well. My colleague Chelsea has put together a few different trailing stop examples that may be found at the following link:


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

    Comment


      #3
      Hi Emily, thank you for your reply,

      I am not very an advanced programmer and the code above from my strategy is very different from the code in those example by Chelsea.
      I was unable to understand what modifications I should do exactly.

      Would you be kind enough to to show me exactly what I need to modify from my code above with the code from Chelsea examples?
      This is what I need:
      1. an input box where I specify the stop profit
      2. an input box where I specify the percentage of the trailing stop
      3. when the UnRealized PnL reach my specified stop profit, the trailing stop activates.
      4. once the market reverse back on the trailing stop, exit trade should activate.
      5. I think there should be another parameter that specify how much the market can reverse on trailing spot before the exit trade activates.

      I would really appreciate your help on this. thank you

      Comment


        #4
        Hello onlinebusiness,

        Thank you for your reply.

        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 help 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 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.

        When it comes to resources to create what you are asking for, I recommend reading the commented lines in the reference sample script I provided that shows how to modify the price of stop loss and profit target orders. I feel like the comments do a pretty good job explaining what each part of the script is doing. We also have an example of using OnOrderUpdate() and OnExecutionUpdate() methods to submit protective orders with helpful comments throughout the script:


        Using the ideas in those scripts should help to understand how to modify your script so that it behaves as expected. For an idea of adding user input boxes in the indicator parameters, you could open up a strategy in the strategy builder at Control Center > New > Strategy Builder then fill out the inputs you would be looking for. Next, click the View Code button to see how this would be coded in NinjaScript. You can usually even copy and paste the code from that page into your own strategy after understanding how it was set up from the Strategy Builder.

        For anyone looking to enhance their NinjaScript skills, I like to recommend the following link which contains many useful resources for C# and NinjaScript development:


        I sincerely appreciate your patience and understanding. Please let me know if you have any specific questions or if you would like me to get you in touch with another team member for more information regarding consulting services.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        44 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        124 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        65 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