Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Request Breakeven Functions in Strategy Builder

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

    Hello Graci117,

    Thanks for your notes.

    This would require adding additional custom logic into your script that calls ExitLongLimit() (Order management folder > Exit long position by a limit order) to place a profit target order for a quantity of 1 and set the limit price to be 10 ticks above the Position.AveragePrice.

    You could set the limit price to be 10 ticks above the Position.Average price similar to how the stop loss order is set up in the BreakEvenBuilderExample shared by Chelsea in post # 2.

    The BreakEvenTrigger determines the number of ticks that you want the script to change the stop price to breakeven at. The TriggerPrice variable uses the BreakEvenTrigger to offset the Position.AveragePrice by a set number of ticks. Since you want to change the stop price to breakeven when the profit target order is filled 10 ticks above the Position.AveragePrice, you should set the BreakEvenTrigger to 10 so that the TriggerPrice is the same price as the profit target order's limit price (10 ticks above the Position.AveragePrice).
    Brandon H.NinjaTrader Customer Service

    Comment


      Originally posted by NinjaTrader_ChelseaB View Post
      Hello 123r34,

      I am happy to submit a feature request on your behalf for the NinjaTrader Development to consider this for a future version of NinjaTrader. Please let me know if you would like to do this.

      NinjaScript Strategies are meant to have the behavior customized by the logic of the programmer including any custom trailing or breakeven movements.

      Attached are three examples of a breakeven movement and a trailing action created with the Strategy Builder.

      (Update August 5th, 2020 - changed the CurrentTriggerPrice and CurrentStopPrice variables to doubles)
      (Update August 8th, 2023 - Added TrailLongShortBuilderExample_NT8, started tracking with a progressState variable)

      BreakEvenBuilderExample_NT8.zip
      TrailBuilderExample_NT8.zip
      TrailLongShortBuilderExample_NT8.zip
      Thanks Chelsea,
      For the "Trail builder example" that you posted, which is an updated example for us to use when looking at a blueprint for adding a trailing stop to a long entry, is very helpful. I understand part of it, but I was wondering if you could explain in further detail each of the 4 sets and what they do.
      For example;
      "Set 1 basically says if we are in a flat position, set the CurrentStopPrice variable to 0. which I assume allows us to take further trades after the initial trade"

      Comment


        Hello Mikey_,

        Thanks for your notes.

        In Set 1 of the TrailBuilderExample strategy, we check if we are in a Flat market position and reset the CurrentStopPrice variable to 0. This is so that anytime we go Flat, the CurrentStopPrice is reset.

        In Set 2, we check if we are in a Flat market position and check if the current State is State.Realtime to check that realtime data is processing. In this Set, we then call the EnterLong() method to enter a position. We also assign a value to the CurrentTriggerPrice and CurrentStopPrice.

        CurrentTriggerPrice is used to trail the stop order and CurrentStopPrice is used to update the price of the stop order.

        In Set 3, we check if we are in a Long market position and we check if the current Close price is greater than the CurrentTriggerPrice. In this Set, we update the CurrentTriggerPrice and CurrentStopPrice when the current close price trails above our CurrentTriggerPrice set in Set 2.

        In Set 4, we check if CurrentStopPrice is not equal to 0 (which also means we are not in a flat position. See Set 1) and submit our stop order to the CurrentStopPrice with ExitLongStopMarket().
        Brandon H.NinjaTrader Customer Service

        Comment


          Originally posted by NinjaTrader_BrandonH View Post
          Hello Mikey_,

          Thanks for your notes.

          In Set 1 of the TrailBuilderExample strategy, we check if we are in a Flat market position and reset the CurrentStopPrice variable to 0. This is so that anytime we go Flat, the CurrentStopPrice is reset.

          In Set 2, we check if we are in a Flat market position and check if the current State is State.Realtime to check that realtime data is processing. In this Set, we then call the EnterLong() method to enter a position. We also assign a value to the CurrentTriggerPrice and CurrentStopPrice.

          CurrentTriggerPrice is used to trail the stop order and CurrentStopPrice is used to update the price of the stop order.

          In Set 3, we check if we are in a Long market position and we check if the current Close price is greater than the CurrentTriggerPrice. In this Set, we update the CurrentTriggerPrice and CurrentStopPrice when the current close price trails above our CurrentTriggerPrice set in Set 2.

          In Set 4, we check if CurrentStopPrice is not equal to 0 (which also means we are not in a flat position. See Set 1) and submit our stop order to the CurrentStopPrice with ExitLongStopMarket().
          Thanks Brandon for your detailed explanation. Lastly, I just want to make sure I understand the inputs.

          "TrailStopDistance" would be how far the stop is from the entry?
          "TrailFrequency" would be how often the stop moves? Also for example if im trading ES, and my frequency is set to 4 (since 4 ticks in 1 point), every time the market moves in my favor 4 ticks would that move the stop 1 tick or would it move it the amount of ticks that the TrailStopDistance is set to? Thanks

          Comment


            Hello Mikey_,

            Thanks for your notes.

            TrailStopDistance is how far away the stop is placed from the Close price in ticks since the stop loss uses CurrentStopPrice and we assign (Close[0] + (TrailStopDistance * TickSize)) to the Exit method.

            For example, if TrailStopDistance is set to -4, this means that CurrentStopPrice will be assigned a value of the Close price minus 4 ticks. So the initial stop order would be placed 4 ticks below the entry and trail from there.

            TrailFrequency is used to offset the CurrentTriggerPrice by a set number of ticks. CurrentTriggerPrice is assigned (Close[0] + (TrailFrequency * TickSize)).

            If we set the TrailFrequency to 4, this means that CurrentTriggerPrice is assigned a value of the Close price plus 4 ticks. Then, anytime the price moves 4 ticks in your favor, the stop would move to the value assigned to CurrentStopPrice.

            Brandon H.NinjaTrader Customer Service

            Comment


              Originally posted by NinjaTrader_BrandonH View Post
              Hello Mikey_,

              Thanks for your notes.

              TrailStopDistance is how far away the stop is placed from the Close price in ticks since the stop loss uses CurrentStopPrice and we assign (Close[0] + (TrailStopDistance * TickSize)) to the Exit method.

              For example, if TrailStopDistance is set to -4, this means that CurrentStopPrice will be assigned a value of the Close price minus 4 ticks. So the initial stop order would be placed 4 ticks below the entry and trail from there.

              TrailFrequency is used to offset the CurrentTriggerPrice by a set number of ticks. CurrentTriggerPrice is assigned (Close[0] + (TrailFrequency * TickSize)).

              If we set the TrailFrequency to 4, this means that CurrentTriggerPrice is assigned a value of the Close price plus 4 ticks. Then, anytime the price moves 4 ticks in your favor, the stop would move to the value assigned to CurrentStopPrice.
              thanks for your reply. I believe I understand, but I want to be sure. The TrailStopDistance makes sense thanks for breaking that down.
              In the example of the TrailFrequency being set to 4, anytime the price moves in my favor 4 ticks, the stop moves to the value assigned to CurrentStopPrice.

              So lets say I am currently trading the ES and I enter at 4500 with my TrailStopDistance set to -4 and my TrailFrequency set to 4. Since entry is at 4500, my trailstop is sitting at 4499. If the price moves in my favor 4 ticks to 4501 my trailing stop then moves to 4500. If price then moves an additional 4 ticks in my favor to 4502, my stop then would move to 4501 (because CurrentStopPrice would be the value of the close minus 4 ticks) is that correct ?

              Comment


                Hello Mikey_,

                Thanks for your notes.

                Yes, that is correct.

                You have a correct understanding of how TrailStopDistance and TrailFrequency work in the TrailBuilderExample reference sample.

                Brandon H.NinjaTrader Customer Service

                Comment


                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hello 123r34,

                  I have received a tracking ID for your request.

                  Your request for the Strategy Builder to have breakeven settings is being tracked with ID #SFT-2212.

                  Please note it is up to the NinjaTrader Development to decide if and when a request will be implemented.

                  We appreciate your feedback. Please let us know of any other suggestions you have for the NinjaTrader Platform.​


                  I also vote for this feature in SB!

                  else if (State == State.Configure)
                  {
                  SetBreakeven(@"EnterLong", CalculationMode.Ticks, Breakeven, true);
                  }​

                  Comment


                    Hello Fbraun378,

                    Thanks for your notes.

                    I have added your vote to this feature request.

                    When/if the feature is implemented, it will be noted on the Release Notes section of the help guide.
                    Brandon H.NinjaTrader Customer Service

                    Comment


                      How would the code be adjusted so I have the all three parameters like on the Custom Stop Strategy parameters in the ATM strategy?
                      Last edited by Graci117; 11-25-2023, 09:30 AM.

                      Comment


                        In the example posted by Ninjatrader Paul, I would like it to advance only based on Frequency. How would I achieve that?

                        if (previousPrice == 0)
                        {
                        stopPlot = Position.AveragePrice - stopLossTicks * TickSize; // initial stop plot level
                        }

                        // Once the price is greater than entry price+ breakEvenTicks ticks, set stop loss to plusBreakeven ticks
                        if (Close[0] > Position.AveragePrice + breakEvenTicks * TickSize && previousPrice == 0)
                        {
                        initialBreakEven = Position.AveragePrice + plusBreakEven * TickSize;
                        SetStopLoss(CalculationMode.Price, initialBreakEven);
                        previousPrice = Position.AveragePrice; //I can add offset here for breakeven
                        stopPlot = initialBreakEven;
                        }
                        // Once at breakeven wait till trailProfitTrigger is reached before advancing stoploss by trailStepTicks size step
                        else if (previousPrice != 0 ////StopLoss is at breakeven
                        && GetCurrentAsk() > previousPrice + trailProfitTrigger * TickSize )
                        {
                        newPrice = previousPrice + trailStepTicks * TickSize; // Calculate trail stop adjustment
                        SetStopLoss(CalculationMode.Price, newPrice); // Readjust stoploss level
                        previousPrice = newPrice; // save for price adjust on next candle
                        stopPlot = newPrice; // save to adjust plot line
                        }​

                        Comment


                          Hello Graci117,

                          Thanks for your notes.

                          I do not see where NinjaTrader_Paul has shared a sample script on this forum thread.

                          To clarify, could you please share an link to the sample script from NinjaTrader_Paul that you are referring to?

                          The BreakEvenBuilderExample_NT8 script shared by NinjaTrader_ChelseaB on post # 2 of this forum thread demonstrates how to implement breakeven in a Strategy Builder strategy.

                          The TrailBuilderExample_NT8 script shared by NinjaTrader_ChelseaB on post # 2 of this forum thread demonstrates how to implement breakeven in a Strategy Builder strategy.

                          In the TrailBuilderExample_NT8 script, TrailStopDistance is how far away the stop is placed from the Close price in ticks since the stop loss uses CurrentStopPrice and we assign (Close[0] + (TrailStopDistance * TickSize)) to the Exit method.

                          For example, if TrailStopDistance is set to -4, this means that CurrentStopPrice will be assigned a value of the Close price minus 4 ticks. So the initial stop order would be placed 4 ticks below the entry and trail from there.

                          TrailFrequency is used to offset the CurrentTriggerPrice by a set number of ticks. CurrentTriggerPrice is assigned (Close[0] + (TrailFrequency * TickSize)).

                          If we set the TrailFrequency to 4, this means that CurrentTriggerPrice is assigned a value of the Close price plus 4 ticks. Then, anytime the price moves 4 ticks in your favor, the stop would move to the value assigned to CurrentStopPrice.
                          Brandon H.NinjaTrader Customer Service

                          Comment


                            Click image for larger version

Name:	Please check the order price.png
Views:	156
Size:	21.5 KB
ID:	1283380
                            I apologize if this was covered already here,
                            I am just testing the raw strategy example provided in post 2 and it seems that when stop losses are hit the strategy is having issues cancelling orders and resetting. Breakeven Example Strategy.


                            Last edited by Viking272; 12-22-2023, 12:26 PM.

                            Comment


                              Hello Viking272,

                              Thanks for your notes.

                              Please write in to support[at]ninjatrader[dot]com with a brief description of your inquiry and attach your Log and Trace files to the email so we may look into this matter further.

                              Follow the steps below to manually attach your log and trace files to your response so I may investigate this matter further.
                              • Open your NinjaTrader folder under, "Documents" (sometimes called, "My Documents")
                              • Right click on the 'log' and 'trace' folders and select Send To> Compressed (zipped) Folder.
                              • Send the 2 compressed folders as attachments to this email.
                              • Once complete, you can delete these compressed folders.
                              ​We look forward to assisting further.
                              Brandon H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by f.saeidi, Yesterday, 02:09 PM
                              3 responses
                              18 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by Jltarrau, Today, 05:57 AM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by kujista, Today, 06:23 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post kujista
                              by kujista
                               
                              Started by traderqz, Yesterday, 04:32 PM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by f.saeidi, Today, 05:56 AM
                              1 response
                              5 views
                              0 likes
                              Last Post Jltarrau  
                              Working...
                              X