Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Mouseclick wrong time and price values

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

    Mouseclick wrong time and price values

    Hi,

    I need get the time of a bar and price on an indicator when user clicks.

    I'm triyng with this code

    Code:
    protected override void OnStateChange()
    {
        if (State == State.SetDefaults)
        {
            Description                                    = @"Enter the description for your new custom Indicator here.";
            Name                                        = "RulerIsraIndicator";
            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.Realtime)
        {
            if (ChartControl != null)
            {
                foreach (ChartScale scale in ChartPanel.Scales)
                {
                    outputLog(scale.ScaleJustification.ToString());
                    if (scale.ScaleJustification == ScaleJustification)
                        chartScale = scale;
                }
                ChartControl.MouseDown += OnChartMouseDown;
                ChartControl.MouseUp += OnChartMouseUp;
            }
        }
        else if (State == State.Terminated)
        {
            if (ChartControl != null)
            {
                ChartControl.MouseDown -= OnChartMouseDown;
                ChartControl.MouseUp -= OnChartMouseUp;
            }
        }
    }
    
    private void OnChartMouseDown(object sender, MouseButtonEventArgs e)
    {
    // Detecta si el botón del medio del ratón fue presionado
        if (e.ChangedButton == MouseButton.Left && sender is ChartControl chartControl)
        {
            if (chartControl != null)
            {
                // Obtén la posición del ratón relativa al ChartPanel
                Point mousePosition = e.GetPosition(chartControl);
    
                // Obtén el tiempo en función de la coordenada X
                startTime = chartControl.GetTimeByX((int)mousePosition.X);
    
                // Obtén el precio en función de la coordenada Y (necesita la escala de precios)
                double startY = chartScale.GetValueByY( (int)mousePosition.Y);
    
                // Registra la información de tiempo y precio
                outputLog($"Coordenadas MouseDown: X={(int)mousePosition.X} Tiempo={startTime}, Precio={startY}");
            }
        }
    }        ​
    ​
    The values for startTime and startY always are wrong.

    Any idea?

    Thanks

    #2
    Hello Powerbucker,

    The coordinates need to be converted and you need to use TriggerCusteEvent before using Series values.

    See the example below.
    Chelsea B.NinjaTrader Customer Service

    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