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

Decrease the size of the RegionHIghLightY on each tick when the price touches it

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

    Decrease the size of the RegionHIghLightY on each tick when the price touches it

    Hello I'm developing an indicator, from now I've drawn a RegionHIghLightY as :

    ```
    protected override void OnBarUpdate()
    {
    if (CurrentBar == 0)
    return;

    // Define the coordinates for the rectangle
    double top = SomeCalculationTop;
    double bot= SomeCalculationBot;

    // Draw the rectangle
    Draw.RegionHighlightY(this, "Example", false, top, bot, Brushes.Blue, Brushes.DodgerBlue, 25);


    // Remove the rectangle on each new bar to avoid multiple rectangles being drawn
    if (Bars.IsFirstBarOfSession)
    RemoveDrawObject("Example");
    }​
    ```

    From now it draws the RegionHighlighY as it should, but now I want to onEachTick detects if the price touches the object decrease (from top or from bottom depending on the price touching part).

    I've tried to get the High[0] and Low[0] but it doesn't react.

    Note: If the price pierces the object it should be deleted.

    Any hints? I'm drawing from now one RegionHighlightY, but it should draw N depending on the days loaded.

    Thanks in advantage.

    #2
    Hello cmtjoancolmenero,

    In the code you provided you are removing the drawing tool and re drawing it for every bar which will take a lot of resources, you just need to call the drawing method again to update the existing object which won't take much resources. If you want to later remove it when the price crosses below you will need to make logic that checks if the price is below the price point where you want to remove it and then call RemoveDrawObject. You would additionally need to add a condition for drawing the rectangle otherwise it will just redraw it as soon as the next tick comes in.

    You likely will need to save those values as class level variables so you can reference them later and check if the Close has touched or gone below the stored price.


    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello Jesse,

      No the thing is, first I want to draw the RegionHighlightY and then I guess it would be onBarUpdate because I'd like to update the size of this Region everytime the price pierces the object and in case it pierce completely the object just delete it, but the thing is, where should I draw first the Region, and then should I check if the price touches the rectangle onBarUpdate?

      In case it works, I'd like to add this logic to all the days loaded, and to the same as the indicator of volumen profile and leave the "naked poc" until touched, can you guide or do some pseudo to guide me how can I start developing it.

      Thanks in advance.

      Comment


        #4
        Hello cmtjoancolmenero,

        The problem with the code you provided is that you are always drawing an object, it will never be removed. The tick after the first tick of the bar it will re appear again. What you have coded does not line up with what you want to do.

        To do what you are asking you need two unique conditions. A first condition would be needed to initially draw the object, that would be based on whatever goals you have for its positioning.

        Your code would end up looking something like this:
        Code:
        protected override void OnBarUpdate()
        {
           if (CurrentBar == 0)
           return;
        
           // Define the coordinates for the rectangle
           double top = SomeCalculationTop;
           double bot= SomeCalculationBot;
        
        
           if(TheConditionToDrawTheRegion)
           {
           // Draw the rectangle when the condition is true once
           Draw.RegionHighlightY(this, "Example", false, top, bot, Brushes.Blue, Brushes.DodgerBlue, 25);
           }
        
           if(Close[0] <= bot)
           {
               RemoveDrawObject("Example");
           }
        
        
           // Remove the rectangle on each new bar to avoid multiple rectangles being drawn
           // The tag "Example" is used so no additional rectangles will ever be drawn, if you have a rectangle already drawn with the tag "Example" and call the draw method again with that tag the already    drawn object is updated, no additional object is added.
        
            if (Bars.IsFirstBarOfSession)
               RemoveDrawObject("Example");​
        }​​
        For the other items you are asking about I would suggest first working on this concept, get it finalized and then make a new forum posts for each of your additional questions so we can focus on a single concept here.

        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello Jesse,

          Yes, the thing is to setup this, I guess the onBarUpdate is not the correct method because its rendering on each bar update, what I'm looking for is for instance setup, and get the value top and bot (because is not going to change), let's say PriorDayOHLC.Open[0] and PriorDayOHLC.Close[0], so I want to draw a rectangle using this points, then yes, on each tick or when the price is changing yes, I want to reduce the rectangle when the price is piercing the object but fristly I'd like to know how to setup the rectangle and then onBarUpdate check from DrawingObjects if contains for instance "Example" and reduce the height somehow.

          Comment


            #6
            Hello cmtjoancolmenero,

            OnBarUpdate would be the correct method for that, drawing objects need to be drawn while bar data is processing because they use bars ago or bar times to be positioned.

            If the values of top and bottom do not change then you could just draw the object once at the session open instead of using RemoveDrawObject as you are now.

            Your next comment seems to conflict with your previous statement, you noted the values will not change but then you mention that the rectangle should reduce when the price is piecing. That means the top and bottom variables will not be constant and are instead variable and will change. Your two variables would need to be made into class level variables so they can maintain a value over multiple bars, you can then reset the variables to new values when your condition becomes true.

            Code:
            private double top ;
            private double bot;​
            
            protected override void OnBarUpdate()
            ​{
            }

            The sample that I previously provided is doing what you are asking. The drawing object is submitted once based on a condition. A second condition checks if the price is less than or equal to the rectangle bottom value. If you wanted to reduce the rectangle when the price is coming down from above it you could change what I provided to the top variable so you know the price has moved inside the rectangle. As an action in that condition you can call the drawing method again to update the object with new prices so it reduces.

            JesseNinjaTrader Customer Service

            Comment


              #7
              Hello Jesse,

              Sorry if I've missexplained what I'm trying to do, the goal is to draw a region on each day loaded from two points, let's instead of say two points are the Close[0] Open[0] from PriorDayOHLC, ok?

              So, that's it, I want to draw a region for each day loaded so I guess this is creating an Id for the day, but yes, this I'm going to open a different thread for this, for this case I'd like to do the reduce the size of the region depending on the price, but first draw the region getting these two values.

              And since I'm printing the values, I'm seeing that the values of the Close[0] and Open[0] from PriorDayOHLC are changing which that what I meant before, these values should be static.
              Last edited by cmtjoancolmenero; 05-01-2024, 11:46 AM.

              Comment


                #8
                Hello cmtjoancolmenero,

                so I guess this is creating an Id for the day,
                The Tag name you used is "Example" which means there will only be 1 rectangle ever drawn over all sessions, this does not change daily. The first time you call Draw.RegionHighlightY it will draw the object, every time you call that same Draw.RegionHighlightY with "Example" its going to check if an existing object is drawn and if so update to the new values. If you want 1 unique rectangle per day your tag of "Example" needs to change to "Example" + CurrentBar and you would draw the object 1 time only at the start of the session.




                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Hello Jesse,

                  To check if the price have pierced or is inside the region is ok to use the GetCurrentBid()? or you recommend me to use another way? I want to check if the actual price have peirced either from the top or from the bot to reduce the top or bot to be the currentBid.
                  Last edited by cmtjoancolmenero; 05-01-2024, 01:29 PM.

                  Comment


                    #10
                    Hello cmtjoancolmenero,

                    If that is the price you want to use that would be up to you, the sample I provided shows using Close[0] which could be substituted with any other price you wanted to compare against the value you calculated for the rectangle. Keep in mind that in historical data the GetCurrentAsk/Bid methods just return the Close price.

                    I want to check if the actual price have peirced either from the top or from the bot to reduce the top or bot to be the currentBid

                    That would be this part of the sample, you need to make a condition to check if the price crossed the price you calculated. If you want to know if the price went from above to below the top of the rectangle you would need to compare against the top price and use <=. If you wanted to know the price crossed the bottom price you would use >=.

                    Code:
                    if(Close[0] <= top)
                    {
                        //crossed the top price from outside to inside the rectangle.
                    }
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Hello Jesse,

                      But doing this is going to update it on bar close, right? Is there any way to update this region real time, means on every tick?

                      Logic should be :
                      price = Close[0];

                      if price outside the region do nothing
                      if price <= top crossed from top, so now top = price
                      if price >= bot crossed from bot, so now bot = price

                      update the region (do I have to remove and print it again???)

                      Thanks.

                      Comment


                        #12
                        Hello cmtjoancolmenero,

                        You need to set the scripts Calculate setting to Calculate.OnEachTick in order to use each tick event. OnBarUpdate is still used in that situation.

                        To update a drawing tool you can just call the drawing method with the same tag, I had explained that earlier:

                        The Tag name you used is "Example" which means there will only be 1 rectangle ever drawn over all sessions, this does not change daily. The first time you call Draw.RegionHighlightY it will draw the object, every time you call that same Draw.RegionHighlightY with "Example" its going to check if an existing object is drawn and if so update to the new values.

                        The only time you need to call RemoveDrawObject is when you want a specific named object removed completely.
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Hello Jesse,

                          Yep, definetely it would work using OnEachTick, but the thing is, imagine I load the market and the price have pierced it, but at the moment (when checking Close[0]) is outside, I need also to update this to decrease the size.

                          Comment


                            #14
                            Hello cmtjoancolmenero,

                            I am not sure I understand this comment, it wouldn't matter if the price has already pierced it. If the price you calculate for the bottom and top leads to the Close price being within the rectangle then your condition to reduce the rectangle would take care of that and reduce the rectangle. If you only want to reduce the rectangle when the price crosses one of those values you can use a CrossAbove or CrossBelow condition instead of >= or <=
                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              Hello Jesse,

                              Let me try to explain it better, now, using OnEachTick will get the realtime price, right? So, the thing I'm looking for is to draw a region, it's already done this, but now I'm fighting with the price because if the price enters the region but when the indicator was not loaded then it won't count.

                              Here's an example

                              I'm starting to validate it at 15:30 let's say,

                              TOP 1700
                              BOT 1400

                              Price 1350

                              This is at 15:30 but imagine I open Ninja at 17:30 and start the indicator and the price is now 1380 but at 16:30 the price went until 1650, I'd like to be the region 1700 to 1650, you got me now?

                              PD : Close, won't work for my scenario since is getting the close value but I want the tail, I mean the lower or the higher price (tail) don't need the body, I want to check the lowest or the higghest it went.
                              Last edited by cmtjoancolmenero; 05-01-2024, 03:17 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by bortz, 11-06-2023, 08:04 AM
                              53 responses
                              2,023 views
                              0 likes
                              Last Post muchacha  
                              Started by NickyD, 10-26-2023, 04:41 PM
                              5 responses
                              191 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by janio973, Yesterday, 07:24 PM
                              4 responses
                              25 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by Epiphany79, Today, 05:00 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post Epiphany79  
                              Started by punkiy2111, 08-31-2021, 10:42 AM
                              16 responses
                              952 views
                              0 likes
                              Last Post muchacha  
                              Working...
                              X