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

Big ask / bid data detection with horizontial line

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

    Big ask / bid data detection with horizontial line

    Hello,

    I'm trying to create an indicator that draws a horizontal line (on all the charts with the same name) starting from the POC / highest volume of the current bar and doesn't stop showing until disabled or 2 days later.
    The plot is considerably simple; 2 combined bids / asks at the same price, from 2 different bars (See attached picture) that are greater than a certain volume figure (8000).
    I've added the code for your review. I tried changing from MaxSeenDelta to GetMaximumPositiveDelta (and negative delta) but it doesn't seem to work.

    Thank you


    }
    else if (State == State.Configure)
    {
    AddVolumetric("",BarsPeriodType.Volume, 10000, VolumetricDeltaType.BidAsk, 1);
    }
    }
    protected override void OnBarUpdate()
    {
    if(BarsInProgress == 0)
    {
    NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe barsType = BarsArray[1].BarsType as
    NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe;

    if(barsType.Volumes[CurrentBar].GetMaximumPositiveDelta() + barsType.Volumes[CurrentBar -1].GetMaximumPositiveDelta() > 8000) { // Big Ask
    Draw.HorizontalLine(this, "BigAsk", Open[0], Brushes.Magenta);
    // Draw.HorizontalLine(this, "BigAsk", Open[0], Brushes.Magenta);
    {
    }
    }
    if(barsType.Volumes[CurrentBar].GetMaximumNegativeDelta() - barsType.Volumes[CurrentBar -1].GetMaximumNegativeDelta() < -8000){ // Big Bid
    Draw.HorizontalLine(this, "BigBid", Close[0], Brushes.OrangeRed);
    // Draw.HorizontalLine(this, "BigBid", Close[0], Brushes.OrangeRed);

    #2
    Hello nzag555,

    Thank you for your post.

    I recommend adding Print() statements to your script to double-check the values being used in your conditions. By placing a print outside of your condition, you can see what the values are calculated as. When putting a print inside of your condition, if the print appears in the NinjaScript Output window this means that the condition evaluated to true and the code inside was triggered. I have created a modified version of your script with added print statements to help debug. Please see the attached:
    BigAskBigBidPrints_NT8.zip

    I set the maximum and minimum positive deltas into variables to make it easier to reference each value in the conditions and print statements repeatedly. I also added a check for "if (CurrentBar <1)" to make sure there are enough bars on the chart, as described in the help guide page here: https://ninjatrader.com/support/help...nough_bars.htm

    Please test out the script and see if it helps you to understand better what the script is doing. If the script is still not behaving as expected after reviewing the prints, please provide a description of the behavior you are seeing vs. the behavior you desire so we may better assist you. For more details on using prints to debug your scripts:


    Please let me know if I may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Hey Emily,


      Thank you for your response.

      The drawings of the script are very confusing. It keeps adding new horizontal lines while erasing the old ones.
      I'd like the new lines to be added to the script without erasing the older lines.
      I also tried to change the horizontal line to a draw.line with a starting point of the current bar (from the max vol point of the triggered bar) and end point is infinity. While doing that, the actual calculation of the script got scrambled and I don't get any lines to draw whatsoever.
      Draw.line would be easier to spot and identified as the trigger point to the plot.

      Thank you

      Comment


        #4
        Hello nzag555,

        Thank you for your reply.

        If you would like a new horizontal line to be added rather than re-drawing the same line, you will need to use unique tag names such as "Big Ask" + CurrentBar.ToString() for example. Based on what you are looking to accomplish, Draw.Line() is likely not the only option you have. Please clarify the following information regarding what you are wanting to see on your chart so I may more accurately assist you:

        You originally stated you want a line, "starting from the POC / highest volume of the current bar and doesn't stop showing until disabled or 2 days later." Your snippet includes the Maximum Positive and Maximum Negative deltas. Are you looking to have two lines drawn or only one per bar?

        You also mentioned, "I'd like the new lines to be added to the script without erasing the older lines." Additionally, you are trying to Draw.Line() from the current bar to infinity. Do you want each line to remain on the chart and add new lines with each bar that all extend into the future? Or, like your first post, are you wanting the lines to remain on the chart until a certain point? What is the maximum amount of lines you want total on your chart?

        I ask these questions because depending on your answers, it may be better to use Draw.Ray() although if you are planning to have many lines drawn on your chart at once, it may be even better to utilize custom rendering rather than drawing tools due to better performance. For more information, please see "Using DrawObjects vs custom graphics in OnRender()" from the NinjaScript Best Practices page here:


        I appreciate your patience and clarification. I look forward to resolving your inquiry.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Hey,


          Regarding your questions:
          - "starting from the POC / highest volume of the current bar and doesn't stop showing until disabled or 2 days later." - Yes, that's correct. I'd like to plot a new line (even multiple lines at each bar) as long as the criteria is correct (over 8K vol etc.).
          - "Do you want each line to remain on the chart and add new lines with each bar that all extend into the future?" - Yes. Like you said, I'd like the lines to remain on the charts until 2 days later.

          * The type of drawing (line, ray, rectangle etc.) doesn't matter that much. What does matter is the beginning of the drawing (POC of the current triggered bar) and the fact it'd be active on all charts simultaneously when triggered (global drawing?).

          Thank you

          Comment


            #6
            Hello nzag555,

            Thank you for your response.

            I have modified my previous sample to be more in line with what you have described. Please feel free to modify it as needed. Here is the file:

            BigAskBigBidRays_NT8.zip

            I commented out the previous lines that draw horizontal lines and instead have drawn rays. In order to check if a ray has been plotted for two days, I created a list of the drawn rays called myRays. I used a method overload for Draw.Ray() that uses a startTime and endTime rather than startBarsAgo and endBarsAgo as well as a bool for isGlobal to make it a global drawing object. I left the drawing template string null so it will just use the default template for Ray drawing objects, although you could create your own template if desired or assign the properties directly since the rays are assigned as either bidRay or askRay. This was done for adding them to the list, but could double for accessing and modifying their properties (such as the Stroke if you want to change the brush color).

            At the end of OnBarUpdate(), there is a for loop that loops through the list of myRays to see if the EndAnchor time is less than the current time. I purposefully set the endAnchor time when drawing the rays to the time when the condition is triggered plus two days to make this check in the loop simple. If the EndAnchor is over two days ago from Time[0], the ray drawing object is removed with RemoveDrawObject() as well as removed from the list of myRays.

            Here are some relevant links related to the example:
            Draw.Ray() - https://ninjatrader.com/support/help...8/draw_ray.htm
            Ray - https://ninjatrader.com/support/helpGuides/nt8/ray.htm
            RemoveDrawObject() - https://ninjatrader.com/support/help...drawobject.htm
            OnDataPoint() which shows time is a DateTime value - https://ninjatrader.com/support/help...ndatapoint.htm
            Publicly available Microsoft Documentation about DateTime.AddDays() - https://docs.microsoft.com/en-us/dot...tframework-4.8

            Please let me know if I may be of further assistance.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              Hey Emily,


              The Draw.ray doesn't always start from the POC of the triggered bar. Some instances yes but many times it started from a random spot on the bar.
              I wasn't able to find a way to change the Draw.ray color for ask or bid. Is it possible?

              If we change the Draw.ray to a regular draw.line - what difference does it make?

              On another note; I tried to alternate GetMaximumDelta with GetMaximumVolume(true, out price) + " at price: " + price); and GetMaximumVolume(False, out price) + " at price: " + price);
              For max bid and max ask intra bar comparison. It scrambled the script and did not calculate anything. Is it possible to change the calculation method based on that without creating a whole new script?
              Sorry for being all over the place with this.

              thank you​

              Comment


                #8
                Hello nzag555,

                Thank you for your reply.

                I used rays in the example because you had mentioned drawing a line with the end point set to infinity; a ray is a line drawn with an infinite end point in one direction and would suit this need more appropriately than a line. You could draw a line instead of a ray if you have a specific idea in mind for where the line should end, otherwise drawing lines into the future can get a little bit complicated. Please see the following posts for more information about drawing lines into the future:

                How would I do this please? currently this is not working myLine = Draw.Line(this,&quot;hfdlsfs&quot;+CurrentBar,true,0,Close[0],endtime,Close[0],dotBrush,DashStyleHelper.DashDot,2); where endtime = ToTime(11, 59, 00); which I think is 1 min before midnight today? Also does the following instantiate a line?

                Hello, Is there a way how to extend a line (I mean line provided by an indicator)? E.g. there is an indicator PriorDayOHLC. When I create its copy, how can


                Here are the help guide sections for Draw.Ray() and Draw.Line() if you would like to compare the two (you could even makea test script that draws a ray and draws a line to see how the two behave differently):



                The rays in the example script are set to draw at either the open[0] or close[0] price - when you see the ray drawn at a seemingly random spot on the bar is it in line with the open or close, or something different altogether? Adding print statements could also help with determining what price the ray is being drawn at so you can figure out where adjustments may be needed to suit your needs.

                Regarding the Maximum Ask and Maximum Bid, what do you mean that it scrambled the script? Do you receive any error messages in the NinjaScript Editor/Log/NinjaScript Output window? If you take this snipped from the help guide and try to print these values, what is the result?

                Print("Maximum Ask: " + barsType.Volumes[CurrentBar].GetMaximumVolume(true, out price) + " at price: " + price);
                Print("Maximum Bid: " + barsType.Volumes[CurrentBar].GetMaximumVolume(false, out price) + " at price: " + price);

                You should be able to add this into the current script you are working on without having to create a new script.

                I look forward to your reply.​
                Emily C.NinjaTrader Customer Service

                Comment


                  #9
                  Hey Emily,



                  Whenever the draw.line is drawn it gives an error message in the script. The examples you provided helped, but still whenever the "end" time is changed - the script doesn't accept it.




                  I tried changing the conditions:
                  currentBarMaxPos = barsType.Volumes[CurrentBar].GetMaximumVolume(true, out price) + " at price: " + price);
                  prevBarMaxPos = barsType.Volumes[CurrentBar -1].GetMaximumVolume(true, out price) + " at price: " + price);


                  Without any luck.




                  Also, you said the script is set to draw at either the open[0] or close[0] price. Is it possible to draw it from the max vol of the triggered bar?




                  Thank you for your response

                  Comment


                    #10
                    Hello nzag555,

                    Thank you for your reply.

                    What is the change you are making in the script and what is the resulting error? This information would assist in identifying the potential cause and solution. If it is easier to provide a screenshot of the error(s) rather than typing out the message, please follow these steps:
                    • To send a screenshot with Windows 10 or newer I would recommend using the Windows Snipping Tool.
                    • Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save it as a jpeg file and send the file as an attachment in your forum post or paste it into the body of your message.
                    ​To answer your next question, yes you would be able to draw at the maximum volume of a bar. GetMaximumVolume returns the price at which the max volume occurred. I will work on creating a code snippet that works to demonstrate this that you should be able to add into your script and modify as needed. In the meantime, please provide the other information I requested and I will follow up as soon as I have that snippet ready.

                    I appreciate your patience and look forward to your reply.
                    Emily C.NinjaTrader Customer Service

                    Comment


                      #11
                      Hey Emily,

                      The screenshots you requested are attached.
                      Looking forward for your response.

                      Thank you


                      Attached Files

                      Comment


                        #12
                        Hello nzag555,

                        Thank you for your patience.

                        Here is a snipped I was able to use in OnBarUpdate() and test successfully:

                        HTML Code:
                        protected override void OnBarUpdate()
                        {
                        if (Bars == null)
                        return;
                        
                        NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe barsType = BarsArray[1].BarsType as NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe;
                        
                        if (barsType == null)
                        return;
                        
                        try
                        {
                        double askPrice;
                        double bidPrice;
                        double combinedPrice;
                        Print("Maximum Ask: " + barsType.Volumes[CurrentBar].GetMaximumVolume(true, out askPrice) + " at price: " + askPrice);
                        Draw.HorizontalLine(this, "askLine", (askPrice), Brushes.Blue);
                        Print("Maximum Bid: " + barsType.Volumes[CurrentBar].GetMaximumVolume(false, out bidPrice) + " at price: " + bidPrice);
                        Draw.HorizontalLine(this, "bidLine", (bidPrice), Brushes.Green);
                        Print("Maximum Combined: " + barsType.Volumes[CurrentBar].GetMaximumVolume(null, out combinedPrice) + " at price: " + combinedPrice);
                        Draw.HorizontalLine(this, "combinedLine", (combinedPrice), Brushes.Pink);
                        }
                        catch{}
                        }
                        ​

                        The Print statements and drawing objects could definitely be modified to meet your needs, and you will need to adjust them accordingly. For example, in Draw.Line or Draw.Ray you would use the askPrice/bidPrice/combinedPrice for the start Y and end Y arguments to set the price where the line or ray should start and end on the y-axis. For more information about how the out parameter modifier works in C#, you could use the following publicly available Microsoft documentation as a resource:



                        As for the end time, based on your screenshot the Endtime variable has not yet been declared. The following page has links to useful resources related to DateTime as well as an indicator sample that manipulates DateTime objects:



                        line 31 is where the DateTime variable is declared at the class level:
                        private DateTime myDateTime;

                        If you do not have a declaration for Endtime in your code, it is expected that you would receive the error in your screenshot. With these items in mind, please adjust your code and see if you are able to resolve any remaining errors so that you may test out your updated script.

                        Please let me know if I may be of further assistance.
                        Emily C.NinjaTrader Customer Service

                        Comment


                          #13
                          Hey Emily,


                          Not sure if this required a new post or not, as it relates to the current info on this topic:
                          Is it possible to create couple instances of GetMaximumPositiveDelta / GetMaximumNegativeDelta within the same bar?
                          I.E get the data from the top 4 occurrences that happened in the same bar?
                          I've uploaded a picture to make it easier to understand.

                          Attached Files

                          Comment


                            #14
                            Hello nzag555,

                            Thanks for your reply.

                            In order to get the top 4 max positive and max negative deltas, you could loop through each price in the bar and use GetDeltaForPrice. This would give you the delta for each price. Next, you could save each price value and delta for that price to a Dictionary<double, int>. Finally, you could loop through the dictionary in order to find the highest and lowest values. Here is an example of how to loop through and create the dictionary:

                            private double deltaForPrice
                            private Dictionary<double, double> deltas = new Dictionary<double, double>();

                            protected override void OnBarUpdate()
                            {
                            if (Bars == null)
                            return;
                            NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe barsType = BarsArray[1].BarsType as NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe;
                            if (barsType == null)
                            return;​

                            // loop through the prices of the bar from highest to lowest. get the delta for each price and save the price and delta to the dictionary deltas
                            for (double i = High[0]; i >= Low[0]; i -= TickSize)
                            {
                            deltaForPrice = barsType.Volumes[CurrentBar].GetDeltaForPrice(i);
                            deltas.Add(i, deltaForPrice);
                            }​
                            }​

                            From here, you would have options to sort the dictionary from lowest delta to highest or vice versa. If you don't need the price that corresponds with the deltas, you could just save the deltaForPrice value to a list instead and sort the list. This will be up to your preference and based on your needs. For more information about dictionaries and lists, please see the publicly available Microsoft documentation at these links:

                            Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.


                            Please let us know if we may be of further assistance.
                            Emily C.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by mjairg, 07-20-2023, 11:57 PM
                            3 responses
                            213 views
                            1 like
                            Last Post PaulMohn  
                            Started by TheWhiteDragon, 01-21-2019, 12:44 PM
                            4 responses
                            544 views
                            0 likes
                            Last Post PaulMohn  
                            Started by GLFX005, Today, 03:23 AM
                            0 responses
                            3 views
                            0 likes
                            Last Post GLFX005
                            by GLFX005
                             
                            Started by XXtrader, Yesterday, 11:30 PM
                            2 responses
                            12 views
                            0 likes
                            Last Post XXtrader  
                            Started by Waxavi, Today, 02:10 AM
                            0 responses
                            7 views
                            0 likes
                            Last Post Waxavi
                            by Waxavi
                             
                            Working...
                            X