Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Drawing ruler from addon

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

    Drawing ruler from addon

    Hi,

    I need draw the ruler clicking the mouse middle button.

    On my addon, I've been implemented this

    private DateTime startTime; // Para almacenar el tiempo inicial
    private double startY; // Para almacenar el precio inicial

    private void OnChartMouseDown(object sender, MouseButtonEventArgs e)
    {
    // Detecta si el botón del medio del ratón fue presionado
    if (e.ChangedButton == MouseButton.Middle && sender is Chart chartWindow)
    {
    //Draw.Ruler_Isra
    // chartControl te da el gráfico actual
    outputLog(" Evento MouseDown en gráfico: " + chartWindow.Title);
    //chartWindow. .AddDrawingTool<DrawingTools.Ruler>();

    // Obtén la posición del ratón en el gráfico
    Point mousePosition = e.GetPosition(chartWindow);

    // Guarda el tiempo y el precio inicial
    startTime = chartWindow.ActiveChartControl.GetTimeByX((int)mou sePosition.X); // Tiempo (DateTime)
    startY = (int)mousePosition.Y;

    outputLog($"Coordenadas MouseDown: Tiempo={startTime}, Precio={startY}");
    }
    }

    private void OnChartMouseUp(object sender, MouseButtonEventArgs e)
    {
    // Detecta si el botón del medio del ratón fue presionado
    if (e.ChangedButton == MouseButton.Middle && sender is Chart chartWindow)
    {
    outputLog("Evento MouseUp en gráfico: " + chartWindow.Title);

    // Obtén la posición del ratón en el gráfico
    Point mousePosition = e.GetPosition(chartWindow);

    // Guarda el tiempo y el precio final
    DateTime endTime = chartWindow.ActiveChartControl.GetTimeByX((int)mou sePosition.X); // Tiempo (DateTime)
    int endY = (int)mousePosition.Y; // Precio (double)

    outputLog($"Coordenadas MouseUp: Tiempo={endTime}, Precio={endY}");

    // Dibuja el Ruler entre el punto de inicio y el punto final
    Draw.Ruler(this, "RulerTool", true, startTime, startY, endTime, endY, endTime, endY);

    outputLog("Ruler dibujado");
    }


    But I get an error on the line

    Draw.Ruler(this, "RulerTool", true, startTime, startY, endTime, endY, endTime, endY);
    Addon.cs Argument 1: cannot convert from 'NinjaTrader.NinjaScript.AddOns.Addon' to 'NinjaTrader.NinjaScript.NinjaScriptBase' CS1503 789 22

    How can I fix it?

    #2
    Hello Powerbucker,

    Thank you for your post.

    Draw objects can only be called from indicators and strategies. If you want to use the ruler drawing tool from your AddOn script, you'll need to extract the OnRender code from the Ruler.cs DrawingTool script and reuse it.

    Please let us know if you have any further questions. ​

    Comment


      #3

      Hi,

      Since I can't call the indicator from a strategy, I've thought about making an indicator and capturing the click event to draw the ruler.

      But when I try to capture the event I get an error.

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Indicator here.";
      Name = "RulerIndicator";
      Calculate = Calculate.OnBarClose;
      IsOverlay = false;
      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;
      }
      else if (State == State.DataLoaded)
      {
      ChartControl.ChartPanels[0].MouseDown += OnChartMouseDown; //THIS LINE THROWS ERROR
      }
      else if (State == State.Terminated)
      {
      ChartControl.ChartPanels[0].MouseDown -= OnChartMouseDown;
      }
      }​


      private void OnChartMouseDown(object sender, MouseButtonEventArgs e)
      {
      // Detecta si el botón del medio del ratón fue presionado
      if (e.ChangedButton == MouseButton.Middle && sender is Chart chartWindow)
      {
      ...........
      }
      }



      The error is:
      12/09/2024 16:51:36 Default Indicator 'RulerIndicator': Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.


      How should I do it?​


      Comment


        #4
        Hello Powerbucker,

        The Help Guide states ChartControl should only be accessed when State is State.Historical. Likely it is null when you are trying to access it from State.DataLoaded.

        "Warning: The ChartControl and its methods and properties should ONLY be access once the State has reached State.Historical​​"

        https://ninjatrader.com/support/help...artcontrol.htm

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        557 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