Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Zoom a Chart programmatically

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

    Zoom a Chart programmatically

    After spending days looking at forums the best way to zoom a chart I found was to send key strokes to simulate pressing the bar spacing +/- hotkeys, finally after much trial and error I found out how to programmatically change any chart zoom, note that you need to set the BarWidth like I do at the end of the function (I have yet to find how to set a good ratio to make the look good with any BarDistance (this is the preset for distance = 44, bar width = 13) , I hope this helps some devs, I also hope ninjatrader will start documenting better their code

    Code:
    private void ZoomChart(Chart chartWindow, double newBarDistance){
    
    
        chartWindow.Dispatcher.InvokeAsync(() =>
    
        {
    
            try
    
            {
    
              
                if (chartWindow == null)
    
                {
    
                    Print("[UpdateBarDistanceUsingProperties] Could not retrieve the Chart window.");
    
                    return;
    
                }
    
    
    
    
                // Access ActiveChartControl
    
                var activeChartControl = chartWindow.GetType().GetProperty("ActiveChartControl", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(chartWindow);
    
                if (activeChartControl == null)
    
                {
    
                    Print("[UpdateBarDistanceUsingProperties] ActiveChartControl is null.");
    
                    return;
    
                }
    
    
    
    
                // Access Properties field in ActiveChartControl
    
                var propertiesField = activeChartControl.GetType().GetProperty("Properties", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
    
                var properties = propertiesField?.GetValue(activeChartControl);
    
                if (properties == null)
    
                {
    
                    Print("[UpdateBarDistanceUsingProperties] Properties is null.");
    
                    return;
    
                }
    
    
    
    
                // Access and update BarDistance
    
                var barDistanceProperty = properties.GetType().GetProperty("BarDistance", BindingFlags.Public | BindingFlags.Instance);
    
                if (barDistanceProperty != null && barDistanceProperty.CanWrite)
    
                {
    
                    // Convert to float before setting
    
                    barDistanceProperty.SetValue(properties, (float)newBarDistance);
    
    
    
                    Print($"[UpdateBarDistanceUsingProperties] BarDistance updated to {newBarDistance}.");
    
                }
    
                else
    
                {
    
                    Print("[UpdateBarDistanceUsingProperties] BarDistance property not found or not writable.");
    
                }
    
            }
    
            catch (Exception ex)
    
            {
    
                Print($"[UpdateBarDistanceUsingProperties] Exception: {ex.Message}");
    
            }
    
    chartWindow.ActiveChartControl.BarWidth=13;
    
    
    
        });
    
    
    
    }
    ​

    you can call it for all charts like:
    Code:
    foreach (var window in Core.Globals.AllWindows)
    
                    {
    
                        // check if the found window is a Chart window, if not continue looking
    
                        if (!(window is Chart))
    
                            continue;
    
                    UpdateBarDistanceFromChart(window as Chart);
    
    }
    ​
    Last edited by Pole123; 01-23-2025, 04:50 AM.

    #2
    Hello Pole123,

    This is not supported to do in NinjaScript and would not be something our staff can assist with.

    This thread will remain open for any community members that would like to provide unsupported or direction.

    You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.
    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
    553 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
    100 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    543 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    546 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X