Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need simple example of how to draw a horizontal line/price when chart/NT is booted up

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

    Need simple example of how to draw a horizontal line/price when chart/NT is booted up

    Hi - looked around, found much more complex examples that what I need, so am hoping someone here on the forum can post a chunk of code for what I need. While I have an extensive programming background, C# is not part of that and I find that to do anything in C# one needs 50+ lines of setup code just to add 2+2. I do not have time/interest to become a C# expert; rather, just need a little sumpin' here...

    What I need:

    * Pick a stock; say, AAPL.
    * 1m chart, lookback 180 days.
    * at NT bootup, I want a horizontal line to be plotted, with the following values:
    - price = $124.69
    - includes price bubble
    - line shape = dashes
    - line width/thickness = '3'
    - line color = Red.

    I don't need this recalculated on bar close or anything like that. It just needs to be plotted on the chart at initialization only and remain on the chart until I close out NT.

    That's it. Once I have an example, I can modify it for future use, specifying diff prices, line shapes/colors, etc.

    #2
    Hello AMATX,

    Thank you for your post.

    The simplest way that wouldn't require drawing a new line periodically on a chart manually would be to create an indicator and use a plot that plots a line at the required value - this would give you a price flag at the price the line is set at. You can then apply the indicator to the chart of your choosing, and if you save your workspace with the chart open and the indicator applied, next time you open the workspace the chart will open with the indicator still applied and the line there automatically.

    Here's a very simple example:

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class DrawLineAtPriceExample : Indicator
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "DrawLineAtPriceExample";
    Calculate = Calculate.OnBarClose;
    // make sure IsOverlay = true so the line is plotted over the price
    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;
    
    // Assign default value for the line
    DrawAtPrice = 124.69;
    
    // Create a Stroke we can use for our plot
    Stroke MyStroke = new Stroke(Brushes.Red, DashStyleHelper.Dash, 3);
    
    // Here we use a plot that we set values for in OnBarUpdate (See value assignment in OBU Below)
    AddPlot(MyStroke, PlotStyle.Line, "MyLine");
    
    
    
    }
    else if (State == State.Configure)
    {
    }
    }
    
    protected override void OnBarUpdate()
    {
    // Assign our price to the plot every time so it plots a horizontal line
    Value[0] = DrawAtPrice;
    }
    
    // This creates a user input for the price so you can adjust the value of the line if you like
    [NinjaScriptProperty]
    [Range(1, double.MaxValue)]
    [Display(Name="Draw At Price", Order=1, GroupName="Parameters")]
    public double DrawAtPrice
    { get; set; }
    
    }
    }
    I've attached a working version of the above example below.

    Please let us know if we may be of further assistance to you.
    Attached Files

    Comment


      #3
      Hi Kate - thanks for the quick reply. I will be plotting multiple price levels, so could you add in statements to create a second price level; say, $130.00 ? That way, I'll be able to more easily modify your code to add as many price levels as I want. I'd assume the next level variable name might be something like DrawAtPrice2 and could you show the addplot(?) statement to make the line similar to the first line, but with a color of Blue?

      Thanks a bunch...

      Comment


        #4
        Oh, one more thing(this is IT, I promise ) - I'm looking to migrate from NT7 to NT8 this summer, so would your example work on both NT7 and NT8???

        I'll download your code and check it out this weekend...

        Comment


          #5
          Hello AMATX,

          Thank you for your replies.

          We in the support department cannot write your scripts for you, but since this is quite a simple example and quick to modify, I've added a second plot in blue with a second input for a second line to the example.

          As you have posted in the NinjaTrader 8 Indicator Development forum, the above example would be for NinjaTrader 8 only and would be incompatible with NinjaTrader 7 due to major changes in NinjaScript between the two versions. However, I've reproduced the example in a form that will work for NinjaTrader 7 as well. Both versions may be found attached.

          Please let us know if we may be of further assistance to you.


          Attached Files

          Comment


            #6
            Thanks for the additional help, Kate. Your example is kind of what I thought it might be. However, not knowing C#, getting the syntax/etc. just right would be a real PIA.

            This should allow me to do EXACTLY what I had in mind, so good to go there

            Some of what I'm migrating comes form Thinkorswim Thinkscript, which btw, is significantly easier to code with. I can plot the same sort of horizontal line with about 4 simple assignment statements vs. all of this C# stuff. However, since NT offers quite a bit of good stuff that is -not- available in Thinkscript(!), here I am...score one for NT, in spite of C# coding

            Comment


              #7
              Originally posted by NinjaTrader_Kate View Post
              Hello AMATX,

              Thank you for your replies.

              We in the support department cannot write your scripts for you, but since this is quite a simple example and quick to modify, I've added a second plot in blue with a second input for a second line to the example.

              As you have posted in the NinjaTrader 8 Indicator Development forum, the above example would be for NinjaTrader 8 only and would be incompatible with NinjaTrader 7 due to major changes in NinjaScript between the two versions. However, I've reproduced the example in a form that will work for NinjaTrader 7 as well. Both versions may be found attached.

              Please let us know if we may be of further assistance to you.

              Can you update this with an offset value that can be manually input that will draw the line that distance away from current price? So if price is currently trading at 5300, then you can make an offset of X points above or below that e.g., 28.70 Points. I would greatly appreciate it!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              597 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              343 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
              556 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              555 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X