Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Create a simple horizontal line

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

    Create a simple horizontal line

    Hi, I am trying to create a line, from the close of a bar.

    I just cant seem to create a line at all!

    I think it should be simple but I havent been able to work.

    Below is what I am trying but not sure what I am missing?

    Thank you

    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

    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class LineFrom20BarsBack : Indicator
    {
    private int lookBackPeriod = 20; // Number of bars to look back
    private Brush lineColor = Brushes.Blue; // Line color
    private double lineThickness = 2; // Line thickness

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Draws a line from 20 bars back (open value) to the right of the chart.";
    Name = "LineFrom20BarsBack";
    Calculate = Calculate.OnEachTick;
    IsOverlay = true;
    AddPlot(lineColor, "LineFrom20BarsBackPlot");
    }
    }

    protected override void OnBarUpdate()
    {
    // Ensure there are enough bars to calculate
    if (CurrentBar < lookBackPeriod)
    return;

    // Calculate the start point (open value of the bar 20 bars back)
    double startValue = Open[lookBackPeriod];

    // Get the x-coordinates for the start and end points of the line
    int startBarIndex = CurrentBar - lookBackPeriod;
    int endBarIndex = CurrentBar;

    // Draw the line on the chart
    Draw.Line(this, $"LineFrom20BarsBack-{CurrentBar}", false, startBarIndex, startValue, endBarIndex, startValue, lineColor, DashStyleHelper.Solid, lineThickness);
    }

    region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name = "LookBack Period", GroupName = "Parameters", Order = 0)]
    public int LookBackPeriod
    {
    get { return lookBackPeriod; }
    set { lookBackPeriod = value; }
    }

    [XmlIgnore]
    [Display(Name = "Line Color", GroupName = "Appearance", Order = 1)]
    public Brush LineColor
    {
    get { return lineColor; }
    set { lineColor = value; }
    }

    [Range(0.1, 10)]
    [Display(Name = "Line Thickness", GroupName = "Appearance", Order = 2)]
    public double LineThickness
    {
    get { return lineThickness; }
    set { lineThickness = value; }
    }
    #endregion
    }
    }

    region NinjaScript generated code. Neither change nor remove.

    namespace NinjaTrader.NinjaScript.Indicators
    {
    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    {
    private LineFrom20BarsBack[] cacheLineFrom20BarsBack;
    public LineFrom20BarsBack LineFrom20BarsBack(int lookBackPeriod)
    {
    return LineFrom20BarsBack(Input, lookBackPeriod);
    }

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

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

    public Indicators.LineFrom20BarsBack LineFrom20BarsBack(ISeries<double> input , int lookBackPeriod)
    {
    return indicator.LineFrom20BarsBack(input, lookBackPeriod);
    }
    }
    }

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

    public Indicators.LineFrom20BarsBack LineFrom20BarsBack(ISeries<double> input , int lookBackPeriod)
    {
    return indicator.LineFrom20BarsBack(input, lookBackPeriod);
    }
    }
    }

    #endregion

    #2
    Hello sweeper2024,

    Thank you for your post.

    Are there any errors in the Log tab of the Control Center?

    To understand why the script is behaving as it is, such as placing or not placing drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In particular print out the time of the bar, and all values you are passing into the Draw object call.

    Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    From the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

    Below is a link to a support article that demonstrates using informative prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.

    https://support.ninjatrader.com/s/ar...nd-TraceOrders

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    558 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    324 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    545 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    547 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X