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

How to draw a line into the future without it drawing past the current bar?

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

    How to draw a line into the future without it drawing past the current bar?

    I want to draw a one hour line and I have the start datetime and end datetime,
    that works fine. It doesn't draw until the bar is at the correct time, and ends correctly,
    BUT. I don't want the line to draw past the current bar. I want it to stop at the current bar and keep updating with each bar
    to stay out of the margin area where I have important levels.

    Here's the original code:
    Code:
    Draw.Line(this, "Open" + dt.Month + dt.Day + dt.Year, false, dtStartTime, OpenPrice, dtEndTime, OpenPrice, Brushes.DodgerBlue, OpenDashStyleHelper, OpenLineWidth);

    Thank you to anyone and everyone who replies!

    #2
    Hello davydhnz,

    Use the Time[0] of the current bar on as the endTime parameter and call the draw method on each new bar update.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Chelsea, any example of calling the draw method on each new bar update?

      My goal is to extend the draw line one bar at a time on each new bar, until a next crossover/below signal bar occurs.

      But the problem is the endBarsAgo cannot be known in advance because
      we don't know when/on what bar in the future the signal will occur.

      I have this Draw.Line method.

      Draw.Line(
      this,
      "A2"+CurrentBar.ToString(),
      false,
      0, // startBarsAgo
      High[0], // startY
      -10, // endBarsAgo
      High[0], // endY
      Brushes.Cyan,
      DashStyleHelper.Dot,
      10);

      How to get the ​endBarsAgo to plot the line one new bar at a time, and stop plotting on the new crossover/below bar?
      How do you capture the value of the next signal/listen for it and store it in a variable to use as endBarsAgo value?

      I thought of this pseudocode:

      // Next Signal bar CurrentBar capture
      if(CrossBelow(Values[0], Values[1], 1))
      {
      FutureSignalendBarsAgo​ = CurrentBar;
      }​​

      if( CrossAbove(Values[0], Values[1], 1)
      {​
      Draw.Line(
      this,
      "A2"+CurrentBar.ToString(),
      false,
      0, // startBarsAgo
      High[0], // startY
      FutureSignalendBarsAgo, // endBarsAgo
      High[0], // endY
      Brushes.Cyan,
      DashStyleHelper.Dot,
      10);​
      }​​​


      I looked at the endTime syntax method but it's the same issue.

      // Next Signal bar CurrentBar capture
      if(CrossBelow(Values[0], Values[1], 1))
      {
      crossBarUp = CurrentBar;
      }​


      if( CrossAbove(Values[0], Values[1], 1)
      {
      Draw.Line(this,
      "A1"+CurrentBar.ToString(),
      false,
      Time[CurrentBar-crossBarUp],
      Low[CurrentBar-crossBarUp],
      Time[0],
      Low[CurrentBar-crossBarUp],
      HLinesCUpColor,
      DashStyleHelper.Dot,
      2);​
      }

      Comment


        #4
        Hello PaulMohn,

        The object would be drawn to barsAgo 0 on each bar update.

        Attached is a simple example that updates the drawing object to set the endBarsAgo to 0 on each bar update as long as the SMA(7) is above the SMA(14).
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello Chelsea,

          Thanks for the sample.

          I tested this on your sample:
          Code:
           Draw.Line(this, "myLine1" + startBar, CurrentBar - startBar, Close[CurrentBar - startBar], 0, Close[CurrentBar - startBar], Brushes.Green);​
          The problem is it plots only once on the new signal bar, not on each new bar until the next signal bar (dynamically).
          How to make it plot dynamically?

          In the below screenshot, hot to make it plot as the magenta horizontal line on each new bar until the next cross above bar?

          Comment


            #6
            Hello PaulMohn,

            The line is dynamically drawn to the close of the most recent bar on every bar update.

            Meaning the line continuously gets longer until the condition to draw is no longer true.

            Below is a link to a video demonstrating.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello Chelsea,

              I just figured it does plot dynamically (sorry I couldn't spot the thin lines before).

              A last problem. I get this error :
              NinjaScript File Error Code Line Column
              Operator '<' cannot be applied to operands of type 'NinjaTrader.NinjaScript.Series<double>' and 'NinjaTrader.NinjaScript.Series<double>' CS0019 394 12

              with this code
              Code:
                                          if (Values[0] < Values[1])
                                          {
                                              Draw.Line(
                                                  this,
                                                  "A3"+CurrentBar.ToString(),
                                                  false,
                                                  CurrentBar - crossBarDown, // startBarsAgo
                                                  High[CurrentBar - crossBarDown], // startY
                                                  0, // endBarsAgo
                                                  High[CurrentBar - crossBarDown], // endY
                                                  Brushes.Cyan,
                                                  DashStyleHelper.Dot,
                                                  5);
                                          }​
              Why doesn't it compile for those double values?

              Comment


                #8
                Hello PaulMohn,

                Values[0] is a plot series.
                Values[0][0] is a double and is the most recent bar of that plot series.

                You are asking if an entire series is greater than another entire series, you are not checking a specific value.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Ok thanks it works now with
                  if (Values[0][0] < Values[1][0])

                  Comment


                    #10
                    The script for later reference:

                    region Using declarations
                    using System;
                    using System.Collections.Generic;
                    using System.ComponentModel;
                    using System.ComponentModel.DataAnnotations;
                    using System.Linq;
                    using System.Text;
                    using System.Threading.Tasks;
                    using System.Windows;
                    using System.Windows.Input;
                    using System.Windows.Media;
                    using System.Xml.Serialization;
                    using NinjaTrader.Cbi;
                    using NinjaTrader.Gui;
                    using NinjaTrader.Gui.Chart;
                    using NinjaTrader.Gui.SuperDom;
                    using NinjaTrader.Gui.Tools;
                    using NinjaTrader.Data;
                    using NinjaTrader.NinjaScript;
                    using NinjaTrader.Core.FloatingPoint;
                    using NinjaTrader.NinjaScript.DrawingTools;
                    #endregion

                    //This namespace holds Indicators in this folder and is required. Do not change it.
                    namespace NinjaTrader.NinjaScript.Indicators
                    {
                    public class DrawToCurrentBarExample : Indicator
                    {
                    private SMA SMA1, SMA2;
                    private int startBar, startBarDwn;

                    protected override void OnStateChange()
                    {
                    if (State == State.SetDefaults)
                    {
                    Name = "DrawToCurrentBarExample";
                    Calculate = Calculate.OnPriceChange;
                    IsOverlay = true;
                    }
                    else if (State == State.DataLoaded)
                    {
                    SMA1 = SMA(7);
                    SMA2 = SMA(14);

                    SMA1.Plots[0].Brush = Brushes.Red;
                    SMA2.Plots[0].Brush = Brushes.Blue;
                    }
                    }

                    protected override void OnBarUpdate()
                    {
                    if (CrossAbove(SMA1, SMA2, 1))
                    startBar = CurrentBar;

                    if (SMA1[0] > SMA2[0])
                    {

                    Draw.Line(
                    this,
                    "A3"+CurrentBar.ToString(),
                    false,
                    CurrentBar - startBar, // startBarsAgo
                    High[CurrentBar - startBar], // startY
                    0, // endBarsAgo
                    High[CurrentBar - startBar], // endY
                    Brushes.Magenta,
                    DashStyleHelper.Dot,
                    5);

                    // Draw.Line(this, "myLine0" + startBar, CurrentBar - startBar, Close[CurrentBar - startBar], 0, Close[0], Brushes.Green);
                    // Draw.Line(this, "myLine1" + startBar, CurrentBar - startBar, Close[CurrentBar - startBar], 0, Close[CurrentBar - startBar], Brushes.Green);
                    }

                    if (CrossBelow(SMA1, SMA2, 1))
                    startBar = CurrentBar;

                    if (SMA1[0] < SMA2[0])
                    {
                    Draw.Line(
                    this,
                    "A3"+CurrentBar.ToString(),
                    false,
                    CurrentBar - startBar, // startBarsAgo
                    Low[CurrentBar - startBar], // startY
                    0, // endBarsAgo
                    Low[CurrentBar - startBar], // endY
                    Brushes.Cyan,
                    DashStyleHelper.Dot,
                    5);

                    // Draw.Line(this, "myLine0" + startBar, CurrentBar - startBar, Close[CurrentBar - startBar], 0, Close[0], Brushes.Green);
                    // Draw.Line(this, "myLine1" + startBar, CurrentBar - startBar, Close[CurrentBar - startBar], 0, Close[CurrentBar - startBar], Brushes.Green);
                    }
                    }
                    }
                    }

                    region NinjaScript generated code. Neither change nor remove.

                    namespace NinjaTrader.NinjaScript.Indicators
                    {
                    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
                    {
                    private DrawToCurrentBarExample[] cacheDrawToCurrentBarExample;
                    public DrawToCurrentBarExample DrawToCurrentBarExample()
                    {
                    return DrawToCurrentBarExample(Input);
                    }

                    public DrawToCurrentBarExample DrawToCurrentBarExample(ISeries<double> input)
                    {
                    if (cacheDrawToCurrentBarExample != null)
                    for (int idx = 0; idx < cacheDrawToCurrentBarExample.Length; idx++)
                    if (cacheDrawToCurrentBarExample[idx] != null && cacheDrawToCurrentBarExample[idx].EqualsInput(input))
                    return cacheDrawToCurrentBarExample[idx];
                    return CacheIndicator<DrawToCurrentBarExample>(new DrawToCurrentBarExample(), input, ref cacheDrawToCurrentBarExample);
                    }
                    }
                    }

                    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
                    {
                    public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
                    {
                    public Indicators.DrawToCurrentBarExample DrawToCurrentBarExample()
                    {
                    return indicator.DrawToCurrentBarExample(Input);
                    }

                    public Indicators.DrawToCurrentBarExample DrawToCurrentBarExample(ISeries<double> input )
                    {
                    return indicator.DrawToCurrentBarExample(input);
                    }
                    }
                    }

                    namespace NinjaTrader.NinjaScript.Strategies
                    {
                    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
                    {
                    public Indicators.DrawToCurrentBarExample DrawToCurrentBarExample()
                    {
                    return indicator.DrawToCurrentBarExample(Input);
                    }

                    public Indicators.DrawToCurrentBarExample DrawToCurrentBarExample(ISeries<double> input )
                    {
                    return indicator.DrawToCurrentBarExample(input);
                    }
                    }
                    }

                    #endregion
                    Attached Files

                    Comment


                      #11
                      PaulMoan, I'm glad you guys got it ironed out

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Radano, 06-10-2021, 01:40 AM
                      19 responses
                      605 views
                      0 likes
                      Last Post Radano
                      by Radano
                       
                      Started by KenneGaray, Today, 03:48 AM
                      0 responses
                      4 views
                      0 likes
                      Last Post KenneGaray  
                      Started by thanajo, 05-04-2021, 02:11 AM
                      4 responses
                      470 views
                      0 likes
                      Last Post tradingnasdaqprueba  
                      Started by aa731, Today, 02:54 AM
                      0 responses
                      5 views
                      0 likes
                      Last Post aa731
                      by aa731
                       
                      Started by Christopher_R, Today, 12:29 AM
                      0 responses
                      11 views
                      0 likes
                      Last Post Christopher_R  
                      Working...
                      X