Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

NinjaTrader 8 update (trail) stop based on conditions

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

    NinjaTrader 8 update (trail) stop based on conditions

    Hi,

    I have just registered to this forum, please don't scream at me I am a software developer by a day job and joined the vendor program with NT. I see that this branch says NinjaTrader 7 ... is there actually NinjaTrader 8 section or they are similar and everything is here?

    I've messed around strategy builder to get familiar but that got old quickly. I see how working with actual code would be lot faster to do iterations and updates. Given it is only couple hundred lines and copy/paste is lot more effective than trying to navigate in the builder (well, easier for developers maybe).

    Could someone give me an example of the C# code for the following logic, so I can understand the syntax and the structure better.

    I've entered Long position on some conditions and set stop to say 100 points. Then I watch it and after it is 50 points in the profit I update my stop loss to the low of the bar 5 bars ago providing it is higher than my entry. Then repeat after every additional 50 points ... look 5 bars back from the current bar, find its low and set new stop to that low if it is higher than previously set stop.

    What would that look like for short trade (flip it, so I completely get it)

    I plan to be quite active here and willing to help other developers as I learn. I have several years as developer, mostly JS but some C# as well. I like to learn from examples.

    Thanks a lot
    xcoder
    NinjaTrader Ecosystem Vendor - EMS

    #2
    Hello Xcoder,

    While it is not within our supports scope to create fully custom samples like the one you asked for we can try to assist with questions that you have about those type of concepts. If you are new to NinjaScript I would suggest looking at the existing samples that are in the reference samples help guide section. One that would be relevant to what you are asking about would be the following sample which shows how to do a break even, that type of logic would be how you can change the price of an order based on a condition using the average entry price of the order and offsets.

    https://ninjatrader.com/support/help...and_onexec.htm

    The logic in OnBarUpdate is what controls the orders movement.

    You can also see the following strategy builder sample, the same concept could be used in manual coding to make a trailing stop. https://forum.ninjatrader.com/forum/...rategy-builder

    There is a section for NT8, that is called NinjaTrader desktop on the forum. I have moved this post into that section.
    Last edited by NinjaTrader_Jesse; 10-20-2023, 07:59 AM.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you, Jesse.

      I see that in example you have provided they're saving updated stop value and use it on trigger in ExitLongStopMarket() function. What I was trying to do is executing SetStopLoss() function within onBarUpdate(). Here's what it looks like. It compiles and runs but stop loss didn't move up when price went up.

      Code:
      // Update stop loss Long
      if (Position.MarketPosition == MarketPosition.Long && Close[0] >= Position.AveragePrice + 10)
      {
      SetStopLoss(CalculationMode.Pips, Position.AveragePrice + 5); // adjust to breakeven + 5
      
      }
      Do I use SetStopLoss() incorrectly or I can't actually update old stop value with this function? You know how I have my initial stop with exchange ​and I want to update it at exchange after position have moved in my favor, not to rely on local code to exit on market when it is time to stop.

      Click image for larger version  Name:	Screenshot 2023-10-21 222027.png Views:	0 Size:	148.4 KB ID:	1274047
      Last edited by Xcoder; 10-21-2023, 10:25 PM.
      xcoder
      NinjaTrader Ecosystem Vendor - EMS

      Comment


        #4
        Hello Xcoder,

        You should be abe to do that. Are you using SetStopLoss in OnStateChange? If so you need to remove it from there and use it before the entry instead.

        SetStopLoss(...)
        EnterLong(...)

        then when you want to update it later you would add a condition like you provided and call SetStopLoss again.

        One other note is that you are adding a price and a number not an amount of pips:

        Position.AveragePrice + 5 this is a price which is not what CalculationMode.Pips is asking for. you need to enter a number like 5 for 5 pips away from the entry average price. If you wanted to use position average price you need to use CalculationMode.Price instead and then calculate you price. Normally ticks are used so it would be like the following:

        SetStopLoss(CalculationMode.Price, Position.AveragePrice + (5 * TickSize)); // adjust to breakeven + 5 ticks
        JesseNinjaTrader Customer Service

        Comment


          #5
          Thank you, Jesse.

          My mistake was that I tried to add an integer to Position.AveragePrice vs (interger * TickSize). Although it might have worked and I didn't see it because markers on the chart do not move once stop gets updated. At least this is what I observe.

          For example, if I set trailing stop I can see how it actually trails with the price movement. Setting stop on bar update as described earlier does update the stop but won't move the maker on the chart. I was able to trace it by printing the label of the value and saw that I actually hit that stop eventually. So I guess it is working minus visual confirmation on the chart.

          Not sure if it by design, it it a bug or I'm missing something to move the stop marker ...
          Attached Files
          xcoder
          NinjaTrader Ecosystem Vendor - EMS

          Comment


            #6
            Hello Xcoder,

            To view the order trail you can open the chart trader and select the same account as the strategy, that will show the order as a line and it will move when the price changes.


            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by fx.practic, 10-15-2013, 12:53 AM
            5 responses
            5,406 views
            0 likes
            Last Post Bidder
            by Bidder
             
            Started by Shai Samuel, 07-02-2022, 02:46 PM
            4 responses
            98 views
            0 likes
            Last Post Bidder
            by Bidder
             
            Started by DJ888, Yesterday, 10:57 PM
            0 responses
            8 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by MacDad, 02-25-2024, 11:48 PM
            7 responses
            160 views
            0 likes
            Last Post loganjarosz123  
            Started by Belfortbucks, Yesterday, 09:29 PM
            0 responses
            9 views
            0 likes
            Last Post Belfortbucks  
            Working...
            X