Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Creating an indicator that generates horizontal lines at multiples of a given value.

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

    Creating an indicator that generates horizontal lines at multiples of a given value.

    Hello Ninjatrader Support team.

    I want to create horizontal lines at multiples, 10 for example. At any given starting and ending point. I also do not want to be able to select these horizontal lines. I have solved both problems but there is likely a better way of achieving this.

    The main problem I have now and where I need assistance is that I can't change the styling of the horizontal lines in the chart indicator menu.
    In the second picture I have shown what the indicator menu in the chart looks like. So I could manually go in and alter all the stylings individually but this really isn't feasible of course. So in my current situation I would have to save multiple versions of this indicator with the different stylings when I would like to be able to just use templates. The first picture shows basically what I want with the horizontal line on 4000 and the horizontal lines on multiples of 10 with different stylings. I just would like to be able to style these directly through the indicators chart menu and not only through code.

    I have stripped down the code but it is very basic.

    Code:
    public static int NumberOfLines;
    
    protected override void OnStateChange()
    {
    
    if (State == State.SetDefaults)
    {
      Multiple = 10;
      Start = 10;
      End = 1000;
      NumberOfLines = (int) ((End - Start) / Multiple);
    
      // Generate Hori zontal line plots
      for(int i = 0; i <= NumberOfLines; i++) {
      AddPlot(new Stroke(Brushes.DimGray, DashStyleHelper.Solid, 1, 40), PlotStyle.HLine, "myplot" + i.ToString());
    }
    }
    
    else if (State == State.Configure)
    {
    }
    }
    
    // Makes the horizontal bars generated unselectable
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
      if(!IsInHitTest)
      base.OnRender(chartControl, chartScale);
    }
    
    //gives the horizontal bars their y axis values
    protected override void OnBarUpdate()
    {
      for(int i = 0; i <= NumberOfLines; i++) {
      Values[i][0] = Start + Multiple*i;
    }
    }
    Attached Files
    Last edited by okrsfinance; 07-27-2022, 10:39 AM.

    #2
    Hello okrsfinance,

    Thank you for your post.

    I'd probably just use horizontal line drawing tools rather than plots. I've created a pretty simple example that will draw lines at the specified multiple from a defined center point down to 0. This example allows you to style the upper lines, center line, and lower lines separately if you like.

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

    Comment


      #3
      Thank you for the response. This does work and I went with horizontal lines in my first iterations of this indicator but I was unable to prevent being able to click the lines on the chart. I guess that is my real question. I want to be able to click anywhere in my chart and scroll up and down and side to side without having to dodge clicking the horizontal lines. I want to have lines drawn at 100s, 10s, 5s, 1s. I can use the built in Ninjatrader lines to draw the lines on the 1s but then having lines on 5s and 10s and 100s clicking in between those tight spaces to scroll the chart is not really doable.

      So is there anyway to make horizontal lines not selectable? Or maybe a hotkey I could hold down to ensure I am going to scroll the chart and not select objects? I suppose that would work.

      Example in the picture below to scroll left right or up and down i have to be very careful to not select one of the horizontal lines.
      Attached Files
      Last edited by okrsfinance; 07-27-2022, 07:56 PM.

      Comment


        #4
        Hello okrsfinance,

        Thank you for your reply.

        You'd need to use custom rendering in OnRender in order to keep the lines from being clicked. I've reworked my prior example to demonstrate doing this with custom rendering with SharpDX.

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

        Comment


          #5
          Thank you very much for your time and the example.

          So experimenting with this it has the same problem when trying to scroll the chart if you click and hold on one of the lines they are not selected but instead what happens is the same thing as when you click and hold to drag a plot like a moving average from one panel to another. This new indicator cannot be selected like a traditional plot or drawing object but it can be dragged and dropped to new panels and copy and pasted around. So I still have the issue of not being able to quickly click on a zoomed out chart to resize and move the chart.


          My solution works well enough I just can't change the stylizing through the GUI but I might as well just get used to doing it in the code it seems.


          Seems like I found the code snippet for my solution in this thread:

          https://ninjatrader.com/support/foru...ndered-objects

          Is it possible to use the same solution from that thread?

          Code:
          protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
          {
              if(!IsInHitTest)                                                         // Use IsInHitTest to avoid rendering for hit test passes
                   base.OnRender(chartControl, chartScale);      // Call base OnRender() method to paint defined Plots.
          }
          Last edited by okrsfinance; 07-29-2022, 12:07 PM.

          Comment


            #6
            Hello okrsfinance,

            Thank you for your reply.

            Yes, you could try that method to see if that meets your needs.

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

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by argusthome, Yesterday, 10:06 AM
            0 responses
            17 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            16 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            14 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            9 views
            0 likes
            Last Post TheRealMorford  
            Started by Mindset, 02-28-2026, 06:16 AM
            0 responses
            36 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Working...
            X