Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

¿is there any minimally credible breakeven stop strategy component for ninjatrader?

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

    ¿is there any minimally credible breakeven stop strategy component for ninjatrader?




    people with nt,



    regards.



    a breakeven stop is a widely used component for automated strategies, just like stop loss, trailing stop and profit target components. it should look something like the following:



    if profit of current position >= minimum number of ticks in one's favor, then create a stop market exit order at entry price +/- some ticks.


    i am quite surprised to see that nt does not natively include such an order. ¿does anyone have any reliable and credible code that would accomplish this?


    very well, thanks, regards.

    #2
    Hello rtwave,

    Thanks for your post.

    Correct, there is no such natively provided method in Ninjascript.

    You can create this yourself as you have described, "if profit of current position >= minimum number of ticks in one's favor, then create a stop market exit order at entry price +/- some ticks.".

    In order to react to the current price, I would suggest using Calculate.OnPriceChange or Calculate.OnEachTick as this will allow the Close price of the currently forming bar to represent the current price which you can then compare to the specific ticks in your favor value as the trigger to set the breakeven stop.

    For the current position, if using a single entry, you can use Position.AveragePrice as the actual entry price.

    You would want to add a bool so that once the breakeven stop has been set that it is not set again. Here is a quick example:

    if (setTheStopBool && Close[0] >= Position.AveragePrice + 20 * TickSize)
    {
    // your stop order here
    setTheStopBool = false; // Note you would set the bool true in your order entry code block.
    }


    You will, of course, want to test this using your sim101 account.

    References:
    https://ninjatrader.com/support/help.../?ticksize.htm
    https://ninjatrader.com/support/help...erageprice.htm (Note the example here).
    Paul H.NinjaTrader Customer Service

    Comment


      #3



      Paul H.,



      thanks.


      i read your reply and it generates more questions than it answers.


      - first of all, i'm using nt's stop loss component in several of my strategies. i suppose that it must be impossible to combine a stop loss component with other orders like a makeshift breakeven stop for one same position. ¿is this correct?


      - in fact, the structure i have in mind would be something like the following:

      for every position, 1) open position with associated stop loss (i assume this must be a stop market order) at stoplossnumberofticks ticks away, 2) if profit > profitnumberofticks then cancel the original stop loss order and 3) set a new stop market order at breakeven (entry price +/- slippagenumberofticks ticks).


      - ¿is it possible to take apart a stop loss component to be able to take a look at its structure and contents? that would be really helpful.


      - then you mention calculate onpricechange or oneachtick. ¿does this mean that i should incorporate one of nt's very peculiar override voids to define this breakeven mechanism? ¿does nt have any working examples of onpricechange or oneachtick voids and where should i place them?


      - it seems to me like i would i need to create one mechanism for short positions and another for long positions, ¿is this correct?


      - i use signal identifiers for all the orders in my strategies, ¿would i have to also use signal identifiers for these artisanal - makeshift - handmade breakeven orders?


      this kind of orders are crucial when creating automated strategies, if nt could provide working examples for these complicated mechanisms that would be great.



      very well, regards.
      Last edited by rtwave; 07-22-2020, 10:22 PM.

      Comment


        #4
        Hello rtwave,

        Thanks for your reply.

        The use of Calculate.OnPriceChange or Calculate.OnEachTick is used to set how often the OnBarUpdate() method is called. Calculate.OnBarClose calls OnbarUpdate() once per bar. The other calculate modes call OnBarUpdate() on each incoming tick or on each change in price. This relates then to how often your script runs. Reference: https://ninjatrader.com/support/help...?calculate.htm

        If you are using entry signal names then you can provide stops that are attached to the entry by the signal name and can then adjust them as you wish for each order. Please see the help guide here: https://ninjatrader.com/support/help...CloseAPosition

        You can use a Set method for a stop loss that you can move in your code. Please see the simple example here: https://ninjatrader.com/support/help...of_stop_lo.htm




        Paul H.NinjaTrader Customer Service

        Comment


          #5



          Paul H.,



          thanks.



          i have taken a look at the links you recommended and found some interesting code, but i still need help to put together a working breakeven order.


          - i have confirmed my intuition and learned that there is no point in using multiple kinds of stops for one same position as only one of the stop orders will be active.


          - nt makes the following sample code available:


          Code:
                      protected override void OnBarUpdate()
                  {
          
                      // Resets the stop loss to the original value when all positions are closed
                      if (Position.MarketPosition == MarketPosition.Flat)
                      {
                          SetStopLoss(CalculationMode.Ticks, StopLossTicks);
                      }
          
                      // If a long position is open, allow for stop loss modification to breakeven
                      else if (Position.MarketPosition == MarketPosition.Long)
                      {
                          // Once the price is greater than entry price+50 ticks, set stop loss to breakeven
                          if (Close[0] > Position.AveragePrice + 50 * TickSize)
                          {
                              SetStopLoss(CalculationMode.Price, Position.AveragePrice);
                          }
                      }
          
          
                  }

          - you recommended using onpricechange or oneachtick calculation. ¿is it possible to incorporate such calculations to this example?

          - you also recommended using a boolean variable but i get the impression that if one uses nt's native setstoploss command then the boolean variable would not be necessary.

          - the code above only deals with long positions, however my strategies also trade short positions and it would be interesting to have a piece of code that does include signal identifiers, so i have put together the code below:

          Code:
                      protected override void OnBarUpdate()
                  {
          
                      if (Position.MarketPosition == MarketPosition.Flat)
                      {
                          SetStopLoss(@"shortentry01", CalculationMode.Ticks, Stlositi, false);
                          SetStopLoss(@"longentry01", CalculationMode.Ticks, Stlositi, false);
                          SetStopLoss(@"shortentry02", CalculationMode.Ticks, Stlositi, false);
                          SetStopLoss(@"longentry02", CalculationMode.Ticks, Stlositi, false);
                      }        
                      else if (Position.MarketPosition == MarketPosition.Short)
                      {
                          if (Close[0] < Position.AveragePrice - 100 * TickSize)
                          {
                          SetStopLoss(@"shortentry01", CalculationMode.Price, Position.AveragePrice, false);
                          SetStopLoss(@"shortentry02", CalculationMode.Price, Position.AveragePrice, false);
                          }
                      }
                      else if (Position.MarketPosition == MarketPosition.Long)
                      {
                          if (Close[0] > Position.AveragePrice + 100 * TickSize)
                          {
                          SetStopLoss(@"longentry01", CalculationMode.Price, Position.AveragePrice, false);
                          SetStopLoss(@"longentry02", CalculationMode.Price, Position.AveragePrice, false);
                          }
                      }
          
          
                  }

          - i have two sections inside the onbarupdate override void, i figured that these breakeven stops should be included in the if(BarsInProgress == 1) section.

          - ¿will this code above work as intended? ¿are there any other recommendations that you would make?


          very well, thanks, regards.
          Last edited by rtwave; 07-23-2020, 09:16 AM.

          Comment


            #6
            Hello rtwave,

            Thanks for your reply.

            Your code example would work and please do run tests on it using your sim101 account.

            Concerning bool variables, those are used to control the logic.

            For example in your code:

            else if (Position.MarketPosition == MarketPosition.Short)
            {
            if (Close[0] < Position.AveragePrice - 100 * TickSize)
            {
            SetStopLoss(@"shortentry01", CalculationMode.Price, Position.AveragePrice, false);
            SetStopLoss(@"shortentry02", CalculationMode.Price, Position.AveragePrice, false);
            }
            }

            There is nothing to stop this code block from executing many times (assuming short position and Close < entry price). In this case of a single step change to breakeven price there would be no harm repeatedly issuing the SetStopLosses but it is also unnecessary. By using a bool variable you can have it execute only once which is really all you need to do.
            for example

            else if (Position.MarketPosition == MarketPosition.Short)
            {
            if (Close[0] < Position.AveragePrice - 100 * TickSize && doItOnce)
            {
            SetStopLoss(@"shortentry01", CalculationMode.Price, Position.AveragePrice, false);
            SetStopLoss(@"shortentry02", CalculationMode.Price, Position.AveragePrice, false);
            doItOnce = false;
            }
            }


            Note: You would want to set the bool to be true when you are in a flat position.



            Regarding, "you recommended using onpricechange or oneachtick calculation. ¿is it possible to incorporate such calculations to this example?" I have previously advised, "The use of Calculate.OnPriceChange or Calculate.OnEachTick is used to set how often the OnBarUpdate() method is called. Calculate.OnBarClose calls OnbarUpdate() once per bar. The other calculate modes call OnBarUpdate() on each incoming tick or on each change in price. This relates then to how often your script runs." You can set the Calculate when you apply the strategy in the user interface or you can set it in the OnStateChange() method inside of State.SetDefaults. It is also possible to have a script run some parts once per bar and other parts once a tick (or once a price change). Here is an example of that usage: https://ninjatrader.com/support/help...either_cal.htm
            Last edited by NinjaTrader_PaulH; 07-24-2020, 12:31 PM. Reason: 7-23-2020, Replaced incorrect link with link to example strategy page in Help guide.
            Paul H.NinjaTrader Customer Service

            Comment


              #7



              Paul H.,



              my breakeven stop is now looking a lot more credible. i will share my final versions for this code here in case it could be of use for someone else. and this is a widely used and very pedestrian component that nt should include natively, i will create a post on the suggestions section regarding this.



              - a lot of strategies will reverse positions from short to long and viceversa, so i have avoided the flat position requirement. i think this code below should work in all possible cases:


              Code:
              
                              if (Position.MarketPosition != MarketPosition.Short)
                              {
                                  SetStopLoss(@"shortentry01", CalculationMode.Ticks, Ticksforstoploss, false);
                                  SetStopLoss(@"shortentry02", CalculationMode.Ticks, Ticksforstoploss, false);
                                  Breakevenshort = true;
                              }
                              else if (Position.MarketPosition == MarketPosition.Short)
                              {
                                  if ( Breakevenshort == true && Close[0] < ( Position.AveragePrice - ( Ticksprofitforbreakeven * TickSize ) ) )
                                  {
                                  SetStopLoss(@"shortentry01", CalculationMode.Price, ( Position.AveragePrice - ( Ticksslippagecommision * TickSize ) ) , false);
                                  SetStopLoss(@"shortentry02, CalculationMode.Price, ( Position.AveragePrice - ( Ticksslippagecommision * TickSize ) ) , false);
                                  Breakevenshort = false;
                                  }
                              }
              
                              if (Position.MarketPosition != MarketPosition.Long)
                              {
                                  SetStopLoss(@"longentry01", CalculationMode.Ticks, Ticksforstoploss, false);
                                  SetStopLoss(@"longentry02", CalculationMode.Ticks, Ticksforstoploss, false);
                                  Breakevenlong = true;
                              }
                              else if (Position.MarketPosition == MarketPosition.Long)
                              {
                                  if ( Breakevenlong == true && Close[0] > ( Position.AveragePrice + ( Ticksprofitforbreakeven * TickSize ) ) )
                                  {
                                  SetStopLoss(@"longentry01", CalculationMode.Price, ( Position.AveragePrice + ( Ticksslippagecommision * TickSize ) ), false);
                                  SetStopLoss(@"longentry02", CalculationMode.Price, ( Position.AveragePrice + ( Ticksslippagecommision * TickSize ) ), false);
                                  Breakevenlong = false;
                                  }
                              }


              - i haven't been able to solve the issue of calculate onpricechange. the link you shared had nothing to do with having some part of a strategy calculate onbarclose and others onpricechange.

              however, i was able to find this example here:

              https://ninjatrader.com/support/foru...y-tick?t=19387

              following this example would require extensive changes to the logic in my strategies. and that sample also includes the if (State == State.Historical) return; command. i can't make up my mind as to whether i should implement the changes necessary to calculate onpricechange to my strategies. i'm not only interested in having a breakeven stop that does work as intended when trading live with real time data (which would require calculating onpricechange for that section of the strategy), i'm also greatly interested in a breakeven stop that would accurately evaluate the performance that my strategies which included with such a component would have generated on historical data (therefore i would not use the if (State == State.Historical) return; command).

              as things stand now, my strategies can sometimes take up to 3 or 4 hours to optimize, so if the nt platform would be overwhelmed and it would be impossible to run optimization processes for strategies that calculate onpricechange it would make no sense for me to make these changes.


              ¿what would nt recommend regarding this calculate onpricechange situation? ¿how could i tell if this breakeven stop is not generating credible results when evaluating on historical data? so far ordinary stop loss components seemingly have done a credible enough job.


              very well, thanks, regards.

              Comment


                #8
                Hello rtwave,

                Thanks for your reply.

                My advice, in general, would be to create a small sample strategy that you can test these concepts for yourself rather than modify your current strategy until you have validated what you need.

                Paul H.NinjaTrader Customer Service

                Comment


                  #9


                  Paul H.,



                  there are some bad news.



                  yesterday i tried to run one single optimization process with a strategy that included this latest version of my breakeven stop. i left the strategy as all my strategies up to now, to calculate onbarclose (i'm using tick bars), and even then, nt went completely nuts. one single optimization had made almost no progress after 3 hours and the platform indicated that it would take 6 days (more than 150 hours) to complete.


                  i cancelled the process, shut down and restarted nt and then tried again and the results were even worse, after 3 hours nt crashed.


                  this was the worst case scenario i could think of and unfortunately it is exactly what has been observed.


                  i think i will send a sample strategy to nt to check if you can replicate these crazy results. if nt could find a way to improve the platform to handle breakeven stops with efficiency that would be magnificent.


                  very well, regards.
                  Last edited by rtwave; 07-24-2020, 10:23 AM.

                  Comment


                    #10
                    Hello rtwave,

                    Thanks for your post.

                    You are welcome to attach a sample script that has been minimized to show the area of concern along with the steps to replicate your observations.

                    Paul H.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Georgee, Today, 04:43 AM
                    0 responses
                    11 views
                    0 likes
                    Last Post Georgee
                    by Georgee
                     
                    Started by tonynt, Today, 04:34 AM
                    0 responses
                    12 views
                    0 likes
                    Last Post tonynt
                    by tonynt
                     
                    Started by ballboy11, 10-04-2020, 02:53 PM
                    2 responses
                    25 views
                    0 likes
                    Last Post mav8rick  
                    Started by manueldecastro, Today, 01:37 AM
                    0 responses
                    9 views
                    0 likes
                    Last Post manueldecastro  
                    Started by memonolog, Today, 01:29 AM
                    0 responses
                    9 views
                    0 likes
                    Last Post memonolog  
                    Working...
                    X