Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Place Order After Price Drops 20 Ticks Below Previous High

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

    Place Order After Price Drops 20 Ticks Below Previous High

    Hello NinjaTrader Community,

    I’m looking to develop a strategy where a long position is entered when the price drops by 20 ticks below the previous high. I’m not sure how to set up the conditions for this in NinjaTrader.

    Could someone please guide me on how to achieve this? Specifically, I need to know:
    1. How to track the previous high price.
    2. How to monitor if the current price has dropped by 20 ticks from that previous high.
    3. How to place a long order once this condition is met.

    Any code snippets, tips, or guidance would be greatly appreciated!

    Thank you in advance for your help!

    #2
    Hello maaxk,

    Below is a link to a support article with helpful resources on getting started with NinjaScript and C#.
    Please be sure to start by watching the 'Automate Your Trading with NinjaTrader's Strategy Builder​' and 'NinjaScript Editor 401​' training videos.


    In the Strategy Builder add a double custom series to track the current saved high and use the offset to do math.
    In the first condition set, set the current high series to the value of the previous bar (to carry it forward). Don't add any conditions to this set.
    In the second condition set, add a condition comparing Misc > Current bar, select Equals as the center comparison operator, on the right select Misc > Numeric value > 0.
    In the actions, select Misc > Set CurrentHighSeries, click 'set' in the CurrentHighSeries field, select Price > High.
    In the third condition set, add a condition comparing the Price > Close, select Less as the center comparison operator, on the right select Misc > Custom Series > CurrentHighSeries, and set the Offset Type to Ticks and use -20 as the Value.
    In the actions, select Misc > Set CurrentHighSeries, click 'set' in the CurrentHighSeries field, select Price > High.
    Add an action, select Order management > Enter long position with a market order

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello NinjaTrader Community,

      I’m looking to develop a strategy where a long position is entered when the price drops by 20 ticks below the previous high. I’m not sure how to set up the conditions for this in NinjaTrader.

      Could someone please guide me on how to achieve this? Specifically, I need to know:
      1. How to track the previous high price.
      2. How to monitor if the current price has dropped by 20 ticks from that previous high.
      3. How to place a long order once this condition is met.

      Any code snippets, tips, or guidance would be greatly appreciated!

      Thank you in advance for your help!​

      Comment


        #4
        Hello Chelsea,

        Below is my code:

        protected override void OnStateChange()
        {
        if (State == State.SetDefaults)
        {
        Description = @"enter a long position after a previous high";
        Name = "prevhilongpos11p";
        Calculate = Calculate.OnEachTick;
        EntriesPerDirection = 1;
        EntryHandling = EntryHandling.AllEntries;
        IsExitOnSessionCloseStrategy = true;
        ExitOnSessionCloseSeconds = 30;
        IsFillLimitOnTouch = false;
        MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
        OrderFillResolution = OrderFillResolution.Standard;
        Slippage = 0;
        StartBehavior = StartBehavior.WaitUntilFlat;
        TimeInForce = TimeInForce.Gtc;
        TraceOrders = false;
        RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
        StopTargetHandling = StopTargetHandling.PerEntryExecution;
        BarsRequiredToTrade = 20;
        // Disable this property for performance gains in Strategy Analyzer optimizations
        // See the Help Guide for additional information
        IsInstantiatedOnEachOptimizationIteration = true;
        }
        else if (State == State.Configure)
        {
        AddDataSeries(Data.BarsPeriodType.Tick, 1);
        }
        else if (State == State.DataLoaded)
        {
        CurrentHighSeries = new Series<double>(this);
        SetProfitTarget(CalculationMode.Ticks, 30);
        SetStopLoss(CalculationMode.Ticks, 20);
        }
        }

        protected override void OnBarUpdate()
        {
        if (BarsInProgress != 0)
        return;

        if (CurrentBars[0] < 1)
        return;

        // Set 1
        if (GetCurrentAsk(0) == GetCurrentAsk(0))
        {
        CurrentHighSeries[0] = High[0];
        }

        // Set 2
        if (Close[0] < (CurrentHighSeries[0] + (-40 * TickSize)) )
        {
        EnterLong(3, "");
        }

        }
        }
        }


        I done it as per your directions.

        Please let me know!

        Thanks
        Last edited by maaxk; 08-21-2024, 02:27 PM.

        Comment


          #5
          Hello Chelsea,

          Here's the snapshot of the chart and control center and the historical data analysis:
          Click image for larger version

Name:	image.png
Views:	199
Size:	284.6 KB
ID:	1315087
          The Ph1 and Ph2 are the two previous highs, the Lp's are where it was supposed to enter a long position, but it didn't.

          And Here's the snapshot of the strategy performance historical data, it didn't enter any long position in actual or historical data.


          Click image for larger version

Name:	image.png
Views:	194
Size:	60.6 KB
ID:	1315088

          Please let me know what should i do differently to make it work?

          Thanks!

          Comment


            #6
            Hello maaxk,

            In Set 1, of comparing Price > Ask to Price > Ask, Compare Misc > CurrentBar to Misc > Numeric value.
            Add a new set and drag this to be Set 2.
            In the new Set 2, assign CurrentHighSeries the value of Misc > Custom series > CurrentHighSeries with Bars ago set 1.
            In Set 3 (formerly Set 2), also assign CurrentHighSeries the value of Price > High (as you did in Set 1).
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello Chelsea,

              As i am a beginner and have no expert knowledge of the domain, can you explain this in a little detail.
              I tried the steps but its only confusing me.

              Thanks!

              Comment


                #8
                Hello Chelsea,

                I have updated the strategy exactly as you directed:

                namespace NinjaTrader.NinjaScript.Strategies
                {
                public class prevhilongpos11p : Strategy
                {
                private double CurrentHighSeries;


                protected override void OnStateChange()
                {
                if (State == State.SetDefaults)
                {
                Description = @"enter a long position after a previous high";
                Name = "prevhilongpos11p";
                Calculate = Calculate.OnBarClose;
                EntriesPerDirection = 1;
                EntryHandling = EntryHandling.AllEntries;
                IsExitOnSessionCloseStrategy = true;
                ExitOnSessionCloseSeconds = 30;
                IsFillLimitOnTouch = false;
                MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                OrderFillResolution = OrderFillResolution.Standard;
                Slippage = 0;
                StartBehavior = StartBehavior.WaitUntilFlat;
                TimeInForce = TimeInForce.Gtc;
                TraceOrders = false;
                RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                StopTargetHandling = StopTargetHandling.PerEntryExecution;
                BarsRequiredToTrade = 20;
                // Disable this property for performance gains in Strategy Analyzer optimizations
                // See the Help Guide for additional information
                IsInstantiatedOnEachOptimizationIteration = true;
                CurrentHighSeries = 1;
                }
                else if (State == State.Configure)
                {
                AddDataSeries(Data.BarsPeriodType.Tick, 1);
                SetProfitTarget(CalculationMode.Ticks, 30);
                SetStopLoss(CalculationMode.Ticks, 20);
                }
                }

                protected override void OnBarUpdate()
                {
                if (BarsInProgress != 0)
                return;

                if (CurrentBars[0] < 1)
                return;

                CurrentHighSeries = High[0];
                // Set 2
                if (CurrentBars[0] == 0)
                {
                CurrentHighSeries = High[0];
                }

                // Set 3
                if ((Close[0] + (-20 * TickSize)) < CurrentHighSeries)
                {
                CurrentHighSeries = High[0];
                }

                }
                }
                }​


                But its still not entering any long position.

                Please guide and let me know what should i do differently to make it work.

                Thanks!

                Comment


                  #9
                  Hello Chelsea,

                  This is a kind reminder.

                  Thanks!

                  Comment


                    #10
                    Hello maaxk,

                    CurrentHighSeries needs to be added as a custom series on the Additional data page of the Strategy Builder, not at a variable.

                    In Set 3, this should be

                    if (Close[0] < (CurrentHighSeries[0] + (-20 * TickSize)) )
                    {
                    EnterLong();
                    CurrentHighSeries[0] = High[0];
                    }

                    On the left choose Price > Close, in the center select Less, on the right select Misc > Custom series and select CurrentHighSeries from the drop-down, set the Offset to Ticks and set the Value to -20.

                    In the actions select Misc > Set CurrentHighSeries, and set the value to Price > High.


                    May I also confirm you have watched the 'Automate Your Trading with NinjaTrader's Strategy Builder​'​ so you understand where everything is in the Strategy Builder?
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello Chelsea,

                      I have updated as per your direction but can you please let me know where should i put the action of entering a long position:

                      namespace NinjaTrader.NinjaScript.Strategies
                      {
                      public class prevhilongpos11p : Strategy
                      {

                      private Series<double> currenthighseries;

                      protected override void OnStateChange()
                      {
                      if (State == State.SetDefaults)
                      {
                      Description = @"enter a long position after a previous high";
                      Name = "prevhilongpos11p";
                      Calculate = Calculate.OnBarClose;
                      EntriesPerDirection = 1;
                      EntryHandling = EntryHandling.AllEntries;
                      IsExitOnSessionCloseStrategy = true;
                      ExitOnSessionCloseSeconds = 30;
                      IsFillLimitOnTouch = false;
                      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                      OrderFillResolution = OrderFillResolution.Standard;
                      Slippage = 0;
                      StartBehavior = StartBehavior.WaitUntilFlat;
                      TimeInForce = TimeInForce.Gtc;
                      TraceOrders = false;
                      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                      StopTargetHandling = StopTargetHandling.PerEntryExecution;
                      BarsRequiredToTrade = 20;
                      // Disable this property for performance gains in Strategy Analyzer optimizations
                      // See the Help Guide for additional information
                      IsInstantiatedOnEachOptimizationIteration = true;
                      }
                      else if (State == State.Configure)
                      {
                      AddDataSeries(Data.BarsPeriodType.Tick, 1);
                      }
                      else if (State == State.DataLoaded)
                      {
                      currenthighseries = new Series<double>(this);
                      SetProfitTarget(CalculationMode.Ticks, 30);
                      SetStopLoss(CalculationMode.Ticks, 20);
                      }
                      }

                      protected override void OnBarUpdate()
                      {
                      if (BarsInProgress != 0)
                      return;

                      currenthighseries[0] = 0;
                      if (CurrentBars[0] < 0)
                      return;

                      // Set 2
                      if (Close[0] < (currenthighseries[0] + (-20 * TickSize)) )
                      {
                      currenthighseries[0] = 0;
                      }

                      // Set 3
                      if (CurrentBars[0] == 0)
                      {
                      currenthighseries[0] = 0;
                      }

                      }
                      }
                      }​

                      Please let me know where it will enter the long position?

                      Thanks!

                      Comment


                        #12
                        Also Let me know, how exactly will i do this step:
                        In the first condition set, set the current high series to the value of the previous bar (to carry it forward). Don't add any conditions to this set.

                        Comment


                          #13
                          One more thing, when i try to enable the strategy from the control center, its disables by itself.

                          Comment


                            #14
                            Hello maaxk,

                            In the actions select Misc > Set CurrentHighSeries > in the CustomHighSeries value click set > select CustomHighSeries and set Bars ago to 1.

                            With the code you have suggeted:

                            protected override void OnBarUpdate()
                            {
                            if (BarsInProgress != 0)
                            return;

                            currenthighseries[0] = 0;
                            if (CurrentBars[0] < 0)
                            return;

                            // Set 2
                            if (Close[0] < (currenthighseries[0] + (-20 * TickSize)) )
                            {
                            currenthighseries[0] = 0;
                            }

                            // Set 3
                            if (CurrentBars[0] == 0)
                            {
                            currenthighseries[0] = 0;
                            }

                            }
                            }
                            }​​

                            Set 1 should be setting CurrentHighSeries to itself from 1 bar go.

                            Set 2 should be assigning CurrentHighSeries to Price > High (not 0).

                            Set 3 should be calling the EnterLong() call AND setting CurrentHighSeries to Price > High (not 0)


                            "One more thing, when i try to enable the strategy from the control center, its disables by itself."

                            Likely a run-time error is occurring. Are there errors on the Log tab of the Control Center when the strategy becomes disabled?​
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              I updated as per your directions and run the strategy again:

                              protected override void OnBarUpdate()
                              {
                              if (BarsInProgress != 0)
                              return;

                              currenthighseries[0] = currenthighseries[1];
                              if (CurrentBars[0] < 1)
                              return;

                              // Set 2
                              if (CurrentBars[0] == High[0])
                              {
                              currenthighseries[0] = High[0];
                              }

                              // Set 3
                              if (Close[0] < (currenthighseries[0] + (-20 * TickSize)) )
                              {
                              EnterLong(2, "");
                              currenthighseries[0] = High[0];
                              }

                              }​

                              Its being disabled by itself and giving this error in the logs:
                              8/22/2024 9:23:54 AM Default Strategy 'prevhilongpos11p': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
                              Please let me know what to do differently.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              558 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              324 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              101 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              545 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              547 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X