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

Let user select an area (draw a box) so that I can get the limits the box

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

    Let user select an area (draw a box) so that I can get the limits the box

    Hi,

    I would like to set up an indicator which allows the user to select a custom area in the chart and then color the candles within the selected area with a certain brush. I would like to know if you can give advice on how to, once the indicator is loaded, let the user select a rectangular area in the chart in a way internally - from the indicator - I can detect the limits of the box (x and y ranges) so that I can apply the internal logic to color the candles.

    Thank you so much in advance!
    Cheers

    #2
    Hello VFI26,

    The best way to accomplish this would be to make a drawing tool. drawing objects have native support for mouse interaction and anchors which are where the user clicks. Once you draw the tool you could use those points from within the drawing objects OnRender to do custom drawing or paint the chart in a custom way. From an indicator it would be difficult to allow for mouse selection because the chart already has control of the mouse for its original actions like dragging the chart. You can see the following forum post which has some code that can be used for detecting mouse position in a chart and getting the X/Y coordinates of the mouse. You would still need to come up with your own logic for click events and storing those datapoints but this would show you how to use those points once found and convert them to valid X/Y positions that can be used for conversions to prices/bars. https://forum.ninjatrader.com/forum/...868#post820868
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      sounds great! do you have some example at hand which shows how to create a custom region x highlight box? Thank you so much!

      Comment


        #4
        Hello VFI26,

        I am not aware of any prebuilt sample indicator that creates a region based on mouse clicks. You can see the existing drawing tools in the NinjaScript editor if you wanted to make a drawing object.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi Jesse, thank you for your answer, I am following that approach. I have a question: from within the onrender method of the drawingtool I do have the indexes of the bars associated with the left and right borders of the highlighted selection. How can I get the highest & lowest price of the asset between those bars? I have not managed to use the MAX or MIN methods within the onRender method.
          Thank you for your help

          Comment


            #6
            Hello VFI26,

            In that type of use case you won't be able to use MIN or MAX indicators but would instead need to just use variables and basic conditions. A loop can also be used in that case because you have a starting index and an ending index.

            Code:
            double high = 0;
            double low = double.MaxValue;
            ChartBars chartBars = (AttachedTo.ChartObject as Gui.NinjaScript.IChartBars).ChartBars;
            int yourStartIndex = 0;
            int yourEndIndex = 10;
            
            for (int i= yourStartIndex; i <= yourEndIndex; i++)
            {
               double close = chartBars.Bars.GetClose(i);
               if(close < low) low = close;
               if(close > high) high= close;
            }
            JesseNinjaTrader Customer Service

            Comment


              #7
              Hi, I've tried the code but at line (AttachedTo.ChartObject as Gui.NinjaScript.IChartBars).ChartBars I get a null reference (AttachedTo.ChartObject as Gui.NinjaScript.IChartBars).ChartBars = '(AttachedTo.ChartObject as Gui.NinjaScript.IChartBars).ChartBars' -> exception is raised 'System.NullReferenceException'

              do you have any idea why this happens?

              Comment


                #8
                Hi, I solved using the GetAttachedToChartBars() method

                Comment


                  #9
                  Hello VFI26,

                  That line is copied from the Region tool which apparently in this case works a little differently because you don't manually draw that, please use the following instead:

                  Code:
                  ChartBars chartBars = (AttachedTo.ChartObject as ChartBars);
                  GetAttachedToChartBars will also work for that purpose.
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Hi,

                    I am at a point where now I am able to use my logic for highlighting a certain x range on the price chart and display a rectangle whose Y-limits are actually the high and low of the price within that time range. The code which I began with is the RegionHighlight.cs drawing tool. Now my question is: even though I can move the whole selection and even only the left or rigth borders I have not understood yet how to move those 3 points which are always plot at a price which is in the middle of the chart in Y direction. My goal is to have them exactly at a Y coordinate which equals the line which I already plot in the miidle of the selection. See image attached. How can I move the drawing of those points?

                    Click image for larger version

Name:	image.png
Views:	80
Size:	29.5 KB
ID:	1279754

                    Comment


                      #11
                      In addition to the information reported so far, I attach here an image which clearly shows that the properties of the drawing tool are set correctly but still those 3 points are drawn always with a Y coordinate which is in the middle of the chart and not linked to the anchor line. Can you help here? Thank you!

                      Click image for larger version

Name:	image.png
Views:	65
Size:	40.8 KB
ID:	1279759​​
                      Attached Files

                      Comment


                        #12
                        Hi NinjaTrader_Jesse

                        do you have any suggestion on how to solve this issue?

                        Thank you so much in advance

                        Comment


                          #13
                          Hello VFI26,

                          The 3 dots you pictured are the anchors. Most drawing tools use the anchors where the mouse was clicked, the region highlight discards the X/Y depending on which tool you are using because it places them in the center. I would suggest looking at the Rectangle drawing object and how it uses anchors, the anchors should correspond to the 4 corners of your rectangle so you would need to add 1 additional anchor. The default rectangle tool can be found in the Shapes drawing object file. You can see the ShapeBase class which has the anchors and mouse handling code along with rendering code, the Rectangle class only defines what code in ShapeBase is to be used.

                          JesseNinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by fx.practic, 10-15-2013, 12:53 AM
                          5 responses
                          5,404 views
                          0 likes
                          Last Post Bidder
                          by Bidder
                           
                          Started by Shai Samuel, 07-02-2022, 02:46 PM
                          4 responses
                          95 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
                          159 views
                          0 likes
                          Last Post loganjarosz123  
                          Started by Belfortbucks, Yesterday, 09:29 PM
                          0 responses
                          8 views
                          0 likes
                          Last Post Belfortbucks  
                          Working...
                          X