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

trying to draw horizontal line between to points in time

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

    trying to draw horizontal line between to points in time

    Hi NinjaTrader_Jim- (or any expert) Newbie here. I am attempting to draw a line in code (as an indicator) on a chart between to dates using Labeled Lines Drawing Tool. I am strugling to even get it working on the standard Draw.Line object. Here is where I am and it doesnt work between the two dates in time:

    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
    // Define the start and end date for the drawing
    DateTime startDate = new DateTime(2017, 05, 01);
    DateTime endDate = new DateTime(2017, 05, 21);

    // Define the prices and labels for the horizontal lines
    double price1 = 141.50;

    double price2 = 142.50;

    double price3 = 143.50;

    // Loop through each bar in the chart and draw the horizontal lines if the bar falls within the date range
    for (int i = 0; i < Bars.Count; i++)
    {
    if (Bars.GetTime(i) >= startDate && Bars.GetTime(i) <= endDate)
    {
    Draw.HorizontalLine(this, "Line1", false, price1, Brushes.Red, DashStyleHelper.Solid, 1);

    Draw.HorizontalLine(this, "Line2", false, price2, Brushes.Green, DashStyleHelper.Solid, 1);

    Draw.HorizontalLine(this, "Line3", false, price3, Brushes.Blue, DashStyleHelper.Solid, 1);
    }
    }
    }
    ​Any Ideas on where I need to tweak the code?
    THANKS IN ADVANCE,
    MD


    #2
    Hello MD,

    Thank you for your post.

    The draw methods, such as Draw.HorizontalLine or even the methods from the Labeled Lines drawing tool, should not be called in OnRender(). The code for the drawing tools themselves contains the logic needed in OnRender(); you should be able to call the draw methods from within OnBarUpdate() and achieve your desired result. A horizontal line will extend indefinitely in both directions, so if you want a line to only be drawn between two dates you could use Draw.Line() with anoverload that allows you to add a startTime and endTime. More information regarding the Draw.Line() method may be found here:


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

    Comment


      #3
      Thank you Emily. Can you show an example of how to use the "Labeled Lines drawing tool" when the line is limited between two dates ?
      Just looking for a flat or horizontal line but I need to label it so when I look back at it I can remember what it is for.
      Thanks in advance! MD

      Comment


        #4
        You should be using Draw.Line. Look at the help example: https://ninjatrader.com/support/help...?draw_line.htm and you will see that there are overloads to the method to use DateTimes for the start/end.

        Draw.HorizontalLine, by definition, extends indefinitely in each direction. That is not compatible with the idea that it starts or ends at a time ergo that is the wrong tool.
        Bruce DeVault
        QuantKey Trading Vendor Services
        NinjaTrader Ecosystem Vendor - QuantKey

        Comment


          #5
          Hello DILLWEED,

          Thank you for you reply.

          The Labeled Lines Drawing Tool is not a default drawing tool that comes with NinjaTrader by default. This is a custom tool that is publicly available on our NinjaTrader Ecosystem website:Per the updates in the description, this is what it says about NinjaScript overloads:
          "10/08/2019: Fixed an issue with Multi Panel rendering, Added NinjaScript overloads. (Please use a template for customizing Label properties when using NS overloads.)"

          Looking at the script, the overloads were created as a class called DrawLL. This means that you can call DrawLL.<insert type of labeled line here>() in your script. You may use the NinjaScript Editor's intelliprompt feature to view the different method signatures available as described here:


          Please let me know if I can be of further assistance.

          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.
          Emily C.NinjaTrader Customer Service

          Comment


            #6
            Thank you Emily. Well I am part way there now. I am successful at drawing a line between two points in time. Where I am having an issue is my attempt to have text of a label or description of what the line represent (which is just user input into a variable). I am trying to use the chartControl object ... something like this is my latest failed attempt. Is there a best way to programatically write text to a line in a hover over way? Thanks in advance!


            double yCoord1 = chartScale.GetYByValue(priceLevel1);

            if (chartControl.IsMouseOverChartPanel)
            {
            if (Math.Abs(chartControl.GetYByValue(priceLevel1) - chartControl.LastMouseMoveEventArgs.Y) < 15)
            Draw.Text(this, "Label1", "141.50 is a SOM retracement level", 0, yCoord1 + 5, Brushes.White);​

            Comment


              #7
              Hello DILLWEED,

              Thank you for your reply.

              Are you trying to use Draw.Text() within OnRender()? As previously mentioned, the draw methods should not be used in OnRender(). You will need to look into custom rendering if you'd like to render text via a mouse hover. I suggest reviewing the following example to see how text could be rendered based on a mouse hover event:
              Hi, with the help of you guys, I created my first strategy. In my strategy, I have a risk management section, where I print the risk of a running trade. Which kind of look like this: Draw.TextFixed(this, &quot;Risk&quot;, &quot;Total Risk: $&quot; + RiskCash.ToString(), TextPosition.BottomRight); The problem is that it only


              There are more details about SharpDX text rendering here:


              Thank you for using NinjaTrader.
              Emily C.NinjaTrader Customer Service

              Comment


                #8
                Some relevant examples:

                (Time of given bar setting the line extremities)

                //Draw.Line(this, "BosUp" + CurrentBar, true, Time[crossBar], crossBar - (3 * TickSize), Time[0], crossBar - (3 * TickSize), BuyClr, eStyle, iWdth);
                //Draw.Line(this, "BosUp" + CurrentBar, true, Time[CurrentBar], CurrentBar + (3 * TickSize), Time[0], CurrentBar + (3 * TickSize), BuyClr, eStyle, iWdth);
                Draw.Line(this, "BosUp" + CurrentBar, true, dtUp, dUp, Time[0], dUp, BuyClr, eStyle, iWdth);​

                https://ninjatraderecosystem.com/use...ructures-lite/


                PHP Code:
                            if(CrossBelow(Values[0], Values[1], 1))
                            {
                                
                crossBarUp CurrentBar;
                                
                //Print("ax crossBarUp : " + crossBarUp);
                            
                }
                            
                //Print("ZZ :" +crossBarUp);


                            
                if(CrossAbove(Values[0], Values[1], 1))
                            {
                                
                crossBarDown CurrentBar;
                                
                //Print("ax crossBarDown : " + crossBarDown);
                            
                }
                            Print(
                "ZZ :" +crossBarDown);


                            if(
                CrossAbove(Values[0], Values[1], 1))    {

                                
                // Draws the line not till next crossover candle but with minus previous nb of candles ot previous crossover candle
                                //Draw.Line(this, "A1"+CurrentBar.ToString(), false, 1, High[0] + (1* TickSize), -(CurrentBar-crossBar), High[0] + (1* TickSize), Brushes.White, DashStyleHelper.Dot, 5);

                                
                Draw.Line(this,
                                    
                "A1"+CurrentBar.ToString(),
                                    
                false,
                                    
                Time[CurrentBar-crossBarUp],
                                    
                Low[CurrentBar-crossBarUp] - (TickSize),
                                    
                Time[0],
                                    
                Low[CurrentBar-crossBarUp] - (TickSize),
                                    
                Brushes.White,
                                    
                DashStyleHelper.Dot,
                                    
                5);

                                
                Draw.Dot(this"C"+CurrentBar.ToString(), true1High[0] + (1TickSize), Brushes.White);

                                
                #region PRINTS

                                    /*
                                        Print(" ##### ---");
                                        Print("Time[crossBarUp] : " + Time[crossBarUp]);
                                        Print("Time[CurrentBar-crossBarUp] : " + Time[CurrentBar-crossBarUp]);
                                        Print("crossBarUp : " + crossBarUp);
                                        Print("Low[crossBarUp] - (3 * TickSize) : " + (Low[crossBarUp] - (3 * TickSize)));
                                        Print("Low[CurrentBar-crossBarUp] - (3 * TickSize) : " + (Low[CurrentBar-crossBarUp] - (3 * TickSize)));
                                        Print("Time[0] : " + Time[0]);

                                    */

                                #endregion

                                #region CURRENTBAR VS CROSSBAR

                                    /*
                                    Draw.Text(this,
                                        "test1"+CurrentBar,
                                        false,
                                        CurrentBar.ToString() + " = \n CurrentBar",
                                        2,
                                        High[0] + (3 * TickSize),
                                        0,
                                        Brushes.White,
                                        myFont,
                                        TextAlignment.Left,
                                        Brushes.Transparent,
                                        null,
                                        1);

                                    Draw.Text(this,
                                        "test2"+CurrentBar,
                                        false,
                                        crossBarUp.ToString() + " = \n crossBarUp",
                                        2,
                                        High[0] + (9 * TickSize),
                                        0,
                                        Brushes.Magenta,
                                        myFont,
                                        TextAlignment.Left,
                                        Brushes.Transparent,
                                        null,
                                        1);
                                    */

                                #endregion

                            
                }
                            else if (
                CrossBelow(Values[0], Values[1], 1)) {

                                
                Draw.Line(this,
                                    
                "B1"+CurrentBar.ToString(),
                                    
                false,
                                    
                Time[CurrentBar-crossBarDown],
                                    
                High[CurrentBar-crossBarDown] + (TickSize),
                                    
                Time[0],
                                    
                High[CurrentBar-crossBarDown] + (TickSize),
                                    
                Brushes.Red,
                                    
                DashStyleHelper.Dot,
                                    
                5);

                                
                Draw.Dot(this"D"+CurrentBar.ToString(), true0Low[0] - (1*TickSize), Brushes.Red);

                                
                #region PRINTS

                                    /*
                                    Print(" ##### ---");
                                    Print("Time[0] : " + Time[0]);
                                    Print("Time[CurrentBar-crossBarDown] : " + Time[CurrentBar-crossBarDown]);
                                    Print("crossBarDown : " + crossBarDown);
                                    Print("High[crossBarDown] + (1 * TickSize) : " + (High[CurrentBar-crossBarDown] + (1 * TickSize)));
                                    Print("High[CurrentBar-crossBarDown] + (1 * TickSize) : " + (High[CurrentBar-crossBarDown] + (1 * TickSize)));
                                    Print("CurrentBar-crossBarDown : " + (CurrentBar-crossBarDown));
                                    Print("Time[0] : " + Time[0]);
                                    */

                                #endregion

                                #region CURRENTBAR VS CROSSBAR

                                    /*
                                    Draw.Text(this,
                                        "test3"+CurrentBar,
                                        false,
                                        CurrentBar.ToString() + " = \n CurrentBar",
                                        1,
                                        Low[0] - (3 * TickSize),
                                        0,
                                        Brushes.Red,
                                        myFont,
                                        TextAlignment.Left,
                                        Brushes.Transparent,
                                        null,
                                        1);

                                    Draw.Text(this,
                                        "test4"+CurrentBar,
                                        false,
                                        crossBarDown.ToString() + " = \n crossBarDown",
                                        1,
                                        Low[0] - (9 * TickSize),
                                        0,
                                        Brushes.Pink,
                                        myFont,
                                        TextAlignment.Left,
                                        Brushes.Transparent,
                                        null,
                                        1);
                                    */

                                #endregion
                            
                }  &#8203; 

                (Time based line extremities)

                Draw.Line(this, "1High", false, (barsCurrent - barsAtLine1), my1MinHigh, -1, my1MinHigh, min1Color, min1Dash, min1Width);
                Draw.Text(this, "1HighText", false, "Previous 1 High: " + my1MinHigh, -5, my1MinHigh, 5, min1Color, myFont, TextAlignment.Left, Brushes.Transparent, null, 1);

                https://ninjatraderecosystem.com/use...median-levels/
                Last edited by PaulMohn; 12-22-2023, 10:32 PM.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by ageeholdings, Today, 07:43 AM
                0 responses
                7 views
                0 likes
                Last Post ageeholdings  
                Started by pibrew, Today, 06:37 AM
                0 responses
                4 views
                0 likes
                Last Post pibrew
                by pibrew
                 
                Started by rbeckmann05, Yesterday, 06:48 PM
                1 response
                14 views
                0 likes
                Last Post bltdavid  
                Started by llanqui, Today, 03:53 AM
                0 responses
                6 views
                0 likes
                Last Post llanqui
                by llanqui
                 
                Started by burtoninlondon, Today, 12:38 AM
                0 responses
                12 views
                0 likes
                Last Post burtoninlondon  
                Working...
                X