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 CarlTrading, 03-31-2026, 09:41 PM
    1 response
    129 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    74 views
    1 like
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    116 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    111 views
    1 like
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    89 views
    0 likes
    Last Post CarlTrading  
    Working...
    X