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

Triggering strategy by movement of price up or down X number of points

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

    #61
    Hello Jeff,

    Thanks for your notes.

    The code in the example script attached in my previous post could be used directly in your strategy if you are wanting to get the highest price of the last 5 bars. This includes the CurrentBar (bar 0) and the previous 4 bars on the chart because HighestBar() will include the CurrentBar too.

    A loop would need to be used to accomplish the goal you described instead of using HighestBar(). The loop might look something like this:

    Code:
    double HighestHigh = -1;
    int HighestBarAgo = -1;
    for(int i = 1; i < 6; i++)
    {
        if(High[i] > HighestHigh)
        {
            HighestHigh = High[i];
            HighestBarAgo = i;
        }
    }​
    Please let me know if I may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #62
      Thanks. I am trying to build an indicator that draws a box around a supply or demand zone. I am not sure if its possible to draw a box and have it extend to the right until price goes through it. I found a form post that seems related to this but the sample code provided is too old. I have been unable to figure this one out. My box just goes to the currentbar (explosive move) used to create the zone and extends back 1 bar (basing candle) but it should be continuing on to the right until price goes through it.

      The part I think may have an issue is

      // Calculate the number of bars between the large movement bar and the first bar where the price touches the zone
      int barsToTouchZone = 0;
      for (int i = CurrentBar - 1; i >= 0; i--)
      {
      if (Low[i] <= demandZoneLow || High[i] >= demandZoneHigh)
      {
      barsToTouchZone = CurrentBar - i;
      break;
      }
      }​


      https://forum.ninjatrader.com/forum/...-price-crosses

      Here is code I have built so far.

      protected override void OnBarUpdate()
      {
      // Check if there are enough bars to calculate the range of the current bar and the previous bar
      if (CurrentBar < 2) return;

      // Calculate the range of the current bar and the previous bar
      double currentBarRange = Math.Abs(Close[0] - Open[0]);
      double previousBarRange = Math.Abs(Close[1] - Open[1]);

      // Check if the current bar is a large increase or a large decrease
      bool isLargeIncrease = currentBarRange >= 5 * previousBarRange && Close[0] > Open[0] && High[0] - Low[0] >= 12 * TickSize;
      bool isLargeDecrease = currentBarRange >= 5 * previousBarRange && Close[0] < Open[0] && Open[0] - Close[0] >= 12 * TickSize;

      // If the current bar is a large increase, draw a demand zone
      if (isLargeIncrease)
      {
      // Define the high and low of the demand zone
      double demandZoneHigh = High[1];
      double demandZoneLow = Low[1];

      // Add the low of the demand zone to the list of demand zones
      demandZones.Add(demandZoneLow);

      // Calculate the number of bars between the large movement bar and the first bar where the price touches the zone
      int barsToTouchZone = 0;
      for (int i = CurrentBar - 1; i >= 0; i--)
      {
      if (Low[i] <= demandZoneLow || High[i] >= demandZoneHigh)
      {
      barsToTouchZone = CurrentBar - i;
      break;
      }
      }


      // Draw the demand zone
      Draw.Rectangle(this, "demandZone" + CurrentBar, true, 1, demandZoneLow, -barsToTouchZone, demandZoneHigh, demandColor, demandColor, 30);
      }
      // If the current bar is a large decrease, draw a supply zone
      else if (isLargeDecrease)
      {
      // Define the high and low of the supply zone
      double supplyZoneHigh = High[1];
      double supplyZoneLow = Low[1];

      // Add the high of the supply zone to the list of supply zones
      supplyZones.Add(supplyZoneHigh);

      // Calculate the number of bars between the large movement bar and the first bar where the price touches the zone
      int barsToTouchZone = 0;
      for (int i = CurrentBar - 1; i >= 0; i--)
      {
      if (High[i] >= supplyZoneHigh || Low[i] <= supplyZoneLow)
      {
      barsToTouchZone = CurrentBar - i;
      break;
      }
      }


      // Draw the supply zone
      Draw.Rectangle(this, "supplyZone" + CurrentBar, true, 1, supplyZoneLow, -barsToTouchZone, supplyZoneHigh, supplyColor, supplyColor, 30);
      }
      }
      Last edited by Jdmtrader; 04-05-2023, 02:14 PM.
      Jdmtrader
      NinjaTrader Ecosystem Vendor - JDM Indicators

      Comment


        #63
        Hello Jeff,

        Thanks for your inquiry.

        The RectangleTest script you linked to in the forums was created for NinjaTrader 7 which is why it is not working with NinjaTrader 8.

        That said, I understand that you are wanting to draw a rectangle on the current bar on the chart and the previous bar. Then, as new bars form you are wanting the right side of the rectangle drawing object to extend until the price is greater than or less than the upper and lower values (startY and endY) of the rectangle. Is that correct?

        To modify a Drawing Object, you would need to call Draw.Rectangle() method and use the same exact tag name of the Drawing Object you want to modify and supply new values for the parameters.

        Fomr the Draw.Rectangle() help guide: "if you pass in a value of "myTag", each time this tag is used, the same draw object is modified. If unique tags are used each time, a new draw object will be created each time."

        Draw.Rectangle(): https://ninjatrader.com/support/help..._rectangle.htm

        A simple example of this is if you want to modify a Drawing Object that has the tag name of say "MyTag" and extend the right side to continue drawing with the current bar, you would call Draw.Rectangle() and supply the tagName parameter "MyTag" and supply and set the endBarsAgo parameter to 0. Setting the endBarsAgo value to 0 means that the right side of the rectangle will be drawn on the current bar on the chart.

        For example, say Draw.Rectangle() is called and the endY value is set to 0 and the CurrentBar is bar 10. The right side of the rectangle will be drawn on bar 10.

        When a new bar (bar 11) forms and the Draw.Rectangle() method is called using the same tagName, the right side of that rectangle drawing object will be drawn on bar 11.

        If you do not call Draw.Rectangle() again using the same tagName, that drawing object will no longer be modified.

        You may consider creating a bool (initially set to false) named something like 'stopDrawing'. Create a condition that checks if the bool is false and checks if the Close price is less than or equal to a specified price and call Draw.Rectangle(). Then create a condition that checks if the Close price (Close[0]) is greater than or equal to that same specified price value and flip the bool to true. Note that you would need to also create a condition that flips the bool back to false or it will remain true.

        Here is a reference sample called SampleDrawObject which you might find helpful: https://ninjatrader.com/support/help...aw_objects.htm

        Please let me know if I may assist further.
        Brandon H.NinjaTrader Customer Service

        Comment


          #64
          Im not sure if you got my latest script I posted, but in case you didn't see below. This is what I attempted with a bool. It does draw the zones as I wish but they are not extending out. It should work.

          protected override void OnBarUpdate()
          {
          // Check if there are enough bars to calculate the range of the current bar and the previous bar
          if (CurrentBar < 2) return;

          // Calculate the range of the current bar and the previous bar
          double currentBarRange = Math.Abs(Close[0] - Open[0]);
          double previousBarRange = Math.Abs(Close[1] - Open[1]);

          // Check if the current bar is a large increase or a large decrease
          bool isLargeIncrease = currentBarRange >= 5 * previousBarRange && Close[0] > Open[0] && High[0] - Low[0] >= 12 * TickSize;
          bool isLargeDecrease = currentBarRange >= 5 * previousBarRange && Close[0] < Open[0] && Open[0] - Close[0] >= 12 * TickSize;

          // If the current bar is a large increase, draw a demand zone
          if (isLargeIncrease)
          {
          // Define the high and low of the demand zone
          double demandZoneHigh = High[1];
          double demandZoneLow = Low[1];

          // Add the low of the demand zone to the list of demand zones
          demandZones.Add(demandZoneLow);

          // Calculate the number of bars between the large movement bar and the first bar where the price touches the zone
          int barsToTouchZone = 0;
          for (int i = CurrentBar - 1; i >= 0; i--)
          {
          if (Low[i] <= demandZoneLow || High[i] >= demandZoneHigh)
          {
          barsToTouchZone = CurrentBar - i;
          break;
          }
          }


          // Draw the demand zone
          Draw.Rectangle(this, "demandZone" + CurrentBar, true, 1, demandZoneLow, -barsToTouchZone, demandZoneHigh, demandColor, demandColor, 30);
          }
          // If the current bar is a large decrease, draw a supply zone
          else if (isLargeDecrease)
          {
          // Define the high and low of the supply zone
          double supplyZoneHigh = High[1];
          double supplyZoneLow = Low[1];

          // Add the high of the supply zone to the list of supply zones
          supplyZones.Add(supplyZoneHigh);

          // Calculate the number of bars between the large movement bar and the first bar where the price touches the zone
          int barsToTouchZone = 0;
          for (int i = CurrentBar - 1; i >= 0; i--)
          {
          if (High[i] >= supplyZoneHigh || Low[i] <= supplyZoneLow)
          {
          barsToTouchZone = CurrentBar - i;
          break;
          }
          }


          // Draw the supply zone
          Draw.Rectangle(this, "supplyZone" + CurrentBar, true, 1, supplyZoneLow, -barsToTouchZone, supplyZoneHigh, supplyColor, supplyColor, 30);
          }
          }​
          Jdmtrader
          NinjaTrader Ecosystem Vendor - JDM Indicators

          Comment


            #65
            Hello Jeff,

            Thanks for your note.

            Here is a script demonstrating extending the projection of a drawing object which you might find helpful: https://forum.ninjatrader.com/forum/...663#post828663

            On the NinjaTrader Ecosystem User App Share you could find an adaptation of the NinjaTrader Rectangle DrawingTool with the added feature of being able to extend/resize from the four sides in addition to the corners.

            Rectangle Tool - Extendable from all sides: https://ninjatraderecosystem.com/use...rom-all-sides/


            Please let me know if I may assist further.

            The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
            Brandon H.NinjaTrader Customer Service

            Comment


              #66
              Is there any way to add a condition in my strategy that would be able to know if any open position was without a stop? I have seen my strategy a few times get into a situation where somehow there is no longer a stop but an open position. Im trying to avoid that situation.

              What I would want is for a condition that could somehow tell if at anytime, an open position was without a stop and to then add a stop. Is that possible?
              Jdmtrader
              NinjaTrader Ecosystem Vendor - JDM Indicators

              Comment


                #67
                Hello Jdmtrader,

                There would not be a collection of active orders. But you could track this with custom logic.

                For example you could have a List<Order> list and .Add(order) each order from OnOrderUpdate() when the state is Submitted. Then you could loop through the list and check each order's OrderState.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #68
                  Thanks. Another question. When I attach a strategy to the chart, it doesnt have a setting for trading hours. When I add a strategy thats not attached to a chart it does. Why is that?
                  Jdmtrader
                  NinjaTrader Ecosystem Vendor - JDM Indicators

                  Comment


                    #69
                    Hello Jdmtrader,

                    Thanks for your inquiry.

                    When a strategy is attached to a chart, it uses the Trading Hours template of the data series that is on the chart. For example, if you have an ES 06-23 chart, the strategy will use the Trading Hours template that the ES 06-23 data series is set to use.

                    You could change the Trading Hours template by right-clicking on the chart, selecting Data Series, and using the Trading Hours drop-down menu.

                    How to edit chart's data series parameters: https://ninjatrader.com/support/help...tm#HowToEditDa taSeriesParameters

                    When you add a strategy to the Strategies tab of the Control Center, it is not being added to a specific data series so that data series and trading hours needs to be defined when adding the script.

                    Enabling a Strategy from the Control Center — https://ninjatrader.com/support/helpGuides/nt8/index.html?strategies_tab2.htm#UnderstandingTheStr ategiesTab

                    Enabling a Strategy in a Chart window — https://ninjatrader.com/support/help...rategyInAChart
                    Brandon H.NinjaTrader Customer Service

                    Comment


                      #70
                      Thanks. When I have an active position with a strategy attached to the chart, if I change time frames, it cancels out open positions. Is that normal for the strategy to be disabled when changing time frames?
                      Jdmtrader
                      NinjaTrader Ecosystem Vendor - JDM Indicators

                      Comment


                        #71
                        Hello Jdmtrader,

                        Thanks for your note.

                        Say you have a strategy enabled on say a 30-second ES 06-23 chart and the strategy places orders that are working and not filled. If you change the interval on the chart to say 1-minute, the working orders will be canceled.

                        Note that when you switch chart intervals when a strategy is running on a chart, the strategy will disable from the interval it was running on (30-second interval in the example above) and it will re-enable on the new interval (1-minute in the example above) that you changed to.

                        If you want to look at the chart with a different interval, you could add a tab to the chart for the new interval you want to view or you could open a new chart window for that interval.
                        Brandon H.NinjaTrader Customer Service

                        Comment


                          #72
                          I found an indicator that is using a custom settings menu to control the indicator settings. Is this possible? If so, how would I go about doing that?

                          Click image for larger version

Name:	image.png
Views:	56
Size:	40.5 KB
ID:	1248131
                          Jdmtrader
                          NinjaTrader Ecosystem Vendor - JDM Indicators

                          Comment


                            #73
                            Hello Jdmtrader,

                            Thanks for your note.

                            This would require implementing your own custom C# WPF modification logic to accomplish this.

                            You could view the SampleWPFModifications reference sample to get an idea of how to do C# WPF modifications.



                            For further information on this topic, you could do a quick Google search for something like 'learning C# WPF' to learn more about custom C# WPF programming.
                            Brandon H.NinjaTrader Customer Service

                            Comment


                              #74
                              Thanks. Another question. I am trying to add logic that will only be true if the close of the current bearish bar is lower than the lowest low of the previous 5 bars. And logic for the opposite as well. Close of the current bullish bar must be higher than the highest high of the previous 5 bars.
                              Jdmtrader
                              NinjaTrader Ecosystem Vendor - JDM Indicators

                              Comment


                                #75
                                Hello Jdmtrader,

                                Thanks for your post.

                                LowestBar() could be used to get the lowest Low price over a specified lookback period.

                                See the sample code on the LowestBar() help guide page for an example of how this would be done: https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?lowestbar.htm

                                To check if the current bar is a down bar, you could create a condition that checks if the Close[0] price is less than the Open[0] price.

                                Once you get the lowestPrice using the LowestBar() method you could check if the bar is a down bar and check if the Close[0] price is less than the lowestPrice that you got with LowestBar().

                                HighestBar() could be used to get the highest High price over a specified lookback period.

                                HighestBar(): https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?highestbar.htm

                                To check if the current bar is an up bar you could create a condition that checks if the Close[0] price is greater than the Open[0] price.

                                Once you get the highesPrice using the HighestBat() method you could check if the bar is an up bar and check if the Close[0] price is greater than the highestPrice that you got using HighestBar().

                                Let us know if you have further questions on this topic.

                                Last edited by NinjaTrader_BrandonH; 05-01-2023, 02:33 PM.
                                Brandon H.NinjaTrader Customer Service

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by DanielTynera, Today, 01:14 AM
                                0 responses
                                2 views
                                0 likes
                                Last Post DanielTynera  
                                Started by yertle, 04-18-2024, 08:38 AM
                                9 responses
                                40 views
                                0 likes
                                Last Post yertle
                                by yertle
                                 
                                Started by techgetgame, Yesterday, 11:42 PM
                                0 responses
                                11 views
                                0 likes
                                Last Post techgetgame  
                                Started by sephichapdson, Yesterday, 11:36 PM
                                0 responses
                                2 views
                                0 likes
                                Last Post sephichapdson  
                                Started by bortz, 11-06-2023, 08:04 AM
                                47 responses
                                1,615 views
                                0 likes
                                Last Post aligator  
                                Working...
                                X