Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Difficulty with Horizontal Line

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

    Difficulty with Horizontal Line

    I was attempting something that I thought would be simple, but ran into issues. I have the following code that draws or shows no lines.
    I have tried both Draw.Line() and Draw.HorizontalLine(), neither providing an line output. For emphasis, I am trying to just plot a "threshold level", in a separate chart Pane, not on Price (which I was advised somewhere that I needed to use Draw.Line() when in a Pane, but uncertain if that was correct advice).
    Intent was to be able to have an "Indicator" to add threshold levels to another Indicator (i.e. think RSI, Stochastic levels, etc.).

    Any clues as to how to fix this would be much appreciated.

    [Is there a better/preferred way to insert Code into these posts?]

    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 tst_LevelLine_V1 : Indicator
    {
    region Properties

    [NinjaScriptProperty]
    [Browsable(true)]
    [Range(-10, 10)]
    [Display(Name = "Zero Value Override", Order = 1, GroupName = "Level Values")]
    public double ZeroValue { get; set; } = 0; // Set to Zero

    [NinjaScriptProperty]
    [Browsable(true)]
    [Range(0, double.MaxValue)]
    [Display(Name = "Level 1", Order = 2, GroupName = "Level Values")]
    public double Level1Value { get; set; } = 10; // Set to 10

    #endregion

    [NinjaScriptProperty]
    [Display(Name = "Zero Line Dash Style", Order = 1, GroupName = "Line Styles")]
    public DashStyleHelper ZeroLineDashStyle { get; set; } = DashStyleHelper.Dot;

    [NinjaScriptProperty]
    [Display(Name = "Threshold Line Dash Style", Order = 2, GroupName = "Line Styles")]
    public DashStyleHelper ThresholdLineDashStyle { get; set; } = DashStyleHelper.Dash;

    private DashStyle ConvertToDashStyle(DashStyleHelper dashStyleHelper)
    {
    switch (dashStyleHelper)
    {
    case DashStyleHelper.Dash:
    return DashStyles.Dash;
    case DashStyleHelper.Dot:
    return DashStyles.Dot;
    case DashStyleHelper.Solid:
    return DashStyles.Solid;
    case DashStyleHelper.DashDot:
    return DashStyles.DashDot;
    case DashStyleHelper.DashDotDot:
    return DashStyles.DashDotDot;
    default:
    return DashStyles.Solid;
    }
    }

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Horizontal Line Level in Pane";
    Name = "tst_LevelLine_V1";
    Calculate = NinjaTrader.NinjaScript.Calculate.OnEachTick;
    IsOverlay = false; // Do not plot on price chart
    IsAutoScale = true;
    AddPlot(Brushes.Transparent, "ForcePanelScale");
    }
    else if (State == State.DataLoaded)
    {
    // Draw.Line(this, "ZeroLine", ZeroValue, Brushes.Silver)
    // .Stroke.DashStyleHelper = ZeroLineDashStyle;

    // Draw.Line(this, "Level1Line", Level1Value, Brushes.Lime)
    // .Stroke.DashStyleHelper = ThresholdLineDashStyle;
    }
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar < Count - 1)
    return;

    int barsBack = 100; // How far back to draw the line

    // Draw Zero Line
    Draw.Line(this, "ZeroLine",
    false,
    barsBack, ZeroValue,
    0, ZeroValue,
    Brushes.Silver,
    DashStyleHelper.Dot,
    1);

    // Draw Level 1 Line
    Draw.Line(this, "Level1Line",
    false,
    barsBack, Level1Value,
    0, Level1Value,
    Brushes.Lime,
    DashStyleHelper.Dash,
    1);

    Values[0][0] = ZeroValue; // or Just to keep the pane scaled
    }
    }
    }​

    #2


    To see drawing objects in a separate panel you need to use DrawOnPricePanel false. After making that change you need to remove and re apply the indicator to the chart.

    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.

    Comment


      #3
      Thank you, that finally did get some lines drawn. I have two follow-on questions:

      I noticed that even though I had IsAutoScale = true; that only the ZeroLine showed. I had to collapse the Panel scale with the mouse to see the LevelLine (but it was there).
      Subsequently today, changing Values[0][0] = 20; did not seem to affect the outcome and force the LevelLine to be revealed.
      Is there a way to force the Scale, so that the LevelLine is always displayed? (i.e. whatever value it is set at)

      Next, I have int barsBack = 100; (which it does). However, is there a method to have these lines draw starting at the first bar of the Chart, and just continuing? (i.e. so that they can be seen even if scrolling back in time)

      Thank you again for your assistance on this.

      Comment


        #4
        Hello tradeologist,

        As long as you are using Plots the AutoScale should apply. After making that true make sure to re apply the indicator to see the changes.

        For drawing objects you need to specify autoscale true in their overload to make sure those are scaled as well.

        To use the first bar of the chart you can use CurrentBar as the bars ago.



        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        581 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        338 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        103 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        554 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        552 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X