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

Drawing line using datetime

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

    Drawing line using datetime

    Hi,

    I have this code and compiling is okay, print shows all values needed, but using datetime seems to be a problem.
    No lines are drawn and in many cases it shows: you are accessing an index with a value that is invalid since it is out-of-range
    (When in replace the start and end with barnumber like 0 and -30 it works.)
    Hope you can help me out so it drawn simple lines from the candle to the max right of the chart. (later on I can add code so endtime is changed when it broken by price)

    Code:
    // Define a class to represent a line
    public class CustomMLine
    {
        public string Label { get; set; }
        public double Price { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public Brush Color { get; set; }
    }
    
    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class MLine : Indicator
        {
            // Declare an array to store your lines
            private List<CustomMLine> lines = new List<CustomMLine>();
            
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Every candle";
                    Name                                        = "MLine";
                    Calculate                                    = Calculate.OnBarClose;
                    IsOverlay                                    = true;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
                    AddPlot(Brushes.DarkRed, "MidLineColor");
                }
                else if (State == State.Configure)
                {
    
                }
            }
    
            protected override void OnBarUpdate()
            {
                // Check if there are enough bars on the chart
                if (CurrentBar < 50)
                    return;
                if (BarsInProgress != 0)
                    return;
                
                double currentMidpoint = (High[0] + Low[0]) / 2.0;
                Print(currentMidpoint);
                
                    var newCustomMLine = new CustomMLine
                    {
                        Label = "Mid_"+CurrentBar+currentMidpoint,
                        Price = currentMidpoint,
                        StartTime = Time[0],
                        EndTime = DateTime.MaxValue,
                        Color = Plots[0].Brush
                    };
    
                    lines.Add(newCustomMLine);
                    //This print is showing everything right
                    Print(newCustomMLine.Label +" "+ newCustomMLine.StartTime + " - " + newCustomMLine.EndTime + "    Price" + newCustomMLine.Price);            
                    Draw.Line(this, newCustomMLine.Label, false, newCustomMLine.StartTime, newCustomMLine.Price, newCustomMLine.EndTime, newCustomMLine.Price, Plots[0].Brush, DashStyleHelper.Dot, 3);
                }
    
    
            #region Properties
    
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> MidLineColor
            {
                get { return Values[0]; }
            }
            #endregion
    
        }
    }​
    Last edited by Creamers; 02-24-2024, 04:47 PM.

    #2
    Think I solved it / workaround using

    Code:
    DateTime futureEndTime = Bars.GetTime(CurrentBar + 100);

    Comment


      #3
      Hello Creamers,

      Thanks for your post.

      I am happy to hear you found a solution.

      When drawing a line on the chart using DateTime parameters, you would need to assign the DateTime to you want to draw the bar at to a DateTime variable in your script. Then, you could pass that variable into the DateTime endTime argument when calling Draw.Line().

      See the help guide documentation below for more information.

      Draw.Line(): https://ninjatrader.com/support/help.../draw_line.htm
      GetTime(): https://ninjatrader.com/support/help...t8/gettime.htm

      Brandon H.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_BrandonH View Post
        Hello Creamers,

        Thanks for your post.

        I am happy to hear you found a solution.

        When drawing a line on the chart using DateTime parameters, you would need to assign the DateTime to you want to draw the bar at to a DateTime variable in your script. Then, you could pass that variable into the DateTime endTime argument when calling Draw.Line().

        See the help guide documentation below for more information.

        Draw.Line(): https://ninjatrader.com/support/help.../draw_line.htm
        GetTime(): https://ninjatrader.com/support/help...t8/gettime.htm
        Just curious, cause that's what I did. I you run that code you'll see that ninjatrader almost hangs and nothing is drawn.
        If you change those to barnumbers and leave the code the same it works .So why does ninjatrader fail using this:

        StartTime = Time[0],
        EndTime = DateTime.MaxValue,​

        Comment


          #5
          Hello Creamers,

          Thanks for your notes.

          I am testing the code below on my end using Time[0] for the startTime argument and DateTime.MaxValue for the endTime argument when calling Draw.Line() and I am not able to reproduce the behavior you are reporting. The script is working without and issues when testing the code below.

          Code:
          Draw.Line(this, "line", false, Time[0], High[0], DateTime.MaxValue, High[0], Brushes.Blue, DashStyleHelper.Solid, 2);
          See this demonstration video showing this is working as expected: https://brandonh-ninjatrader.tinytak...NV8yMjg5NzQxNw

          I also attached the test script used to test this on my end.
          Attached Files
          Brandon H.NinjaTrader Customer Service

          Comment


            #6
            Hi Brandon,

            Thank you for your extended reaction to help me out.

            I used your code and it works........except, when you switch to daily candles it goes totally wrong.!
            When the screen finally is reactive again you'll not see the drawn line.
            This was/is my problem. I've only done this on daily charts therefor I had this problem continuesly.



            Think its a bug in Ninja or what is wrong, any idea?

            Comment


              #7
              Hello Creamers,

              Thanks for your notes.

              I am seeing an error message occur on the Log tab of the Control Center when testing the script on a Daily chart.

              To draw a line from the current time to a future time, you could create a DateTime variable, assign Time[0].AddYears(1) or something similar to the variable, and then use that DateTime variable for the endTime argument when calling Draw.Line().

              This would draw the line from the startTime (Time[0]) to the time one year in the future.

              I have tested this on a Daily chart, Minute chart, Weekly chart, Monthly chart, and Yearly chart and the script is behaving as expected.

              Attached is the sample script used to test this.
              Attached Files
              Brandon H.NinjaTrader Customer Service

              Comment


                #8
                Hi Brandon,

                You are the man! It's nice to have support that really reads the questions and even replies with code or prove! Works like a charm and thanks for your help!!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by JoMoon2024, Today, 06:56 AM
                0 responses
                6 views
                0 likes
                Last Post JoMoon2024  
                Started by Haiasi, 04-25-2024, 06:53 PM
                2 responses
                17 views
                0 likes
                Last Post Massinisa  
                Started by Creamers, Today, 05:32 AM
                0 responses
                5 views
                0 likes
                Last Post Creamers  
                Started by Segwin, 05-07-2018, 02:15 PM
                12 responses
                1,786 views
                0 likes
                Last Post Leafcutter  
                Started by poplagelu, Today, 05:00 AM
                0 responses
                3 views
                0 likes
                Last Post poplagelu  
                Working...
                X