> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.ninjatrader.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.ninjatrader.com/_mcp/server.

# ChartBars

The **ChartBars** class provides GUI access related methods and properties to the primary bars series configured on the Chart through the [Data Series](https://ninjatrader.com/support/helpGuides/nt8/?working_with_price_data.htm) menu. For data access information related to the NinjaScript input's bars series, please use the [Bars Series](/developer/desktop-sdk/references/common/bars) object (or the [BarsArray](/developer/desktop-sdk/references/common/adddataseries/barsarray) for multi-series input).

A ChartBars object will ONLY exist should the hosting NinjaScript type be loaded through a [Chart](/developer/unsorted/charts). For example, a Strategy would have access to a ChartBars property when running on a Chart, but would NOT when loaded through the [Strategies Grid](https://ninjatrader.com/support/helpGuides/nt8/?strategies_tab2.htm) or [Strategy analyzer](https://ninjatrader.com/support/helpGuides/nt8/?strategy_analyzer.htm).

![ChartBars](https://fdr-prod-docs-files-public.s3.us-east-1.amazonaws.com/ninjatrader-public.docs.buildwithfern.com/c8f21a0353150f772a7198fcfa586f1eb094dc57d622f02da3f2815d9b41b3a6/docs/assets/developer/desktop-sdk/references/chartbars-chartbars.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIA6KXJSKKNFOCF7G4B%2F20260729%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20260729T070351Z&X-Amz-Expires=604800&X-Amz-Signature=ca0a1cc01aa1d60ff83baa636709dd8a6b9d81508828f1089a47be9252318e07&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject)

It is crucial to check for object references before accessing the ChartBars otherwise possible null reference errors can be expected depending on where the NinjaScript object was started. See example below.

## Methods and Properties

| Method/Property                                                                                | Description                                                                                                                                                             |
| ---------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Bars](/developer/unsorted/chartbars-bars)                                                     | Data returned from the historical data repository.                                                                                                                      |
| [Count](/developer/desktop-sdk/references/common/charts/chartbars/chartbars-count)             | The total number of ChartBars that exist on the chart.                                                                                                                  |
| [FromIndex](/developer/desktop-sdk/references/common/charts/chartbars/fromindex)               | An index value representing the first bar painted on the chart.                                                                                                         |
| [GetBarIdxByTime()](/developer/desktop-sdk/references/common/charts/chartbars/getbaridxbytime) | An ChartBar index value calculated from a time value on the chart.                                                                                                      |
| [GetBarIdxByX()](/developer/desktop-sdk/references/common/charts/chartbars/getbaridxbyx)       | Returns the ChartBar index value at a specified x-coordinate relative to the ChartControl.                                                                              |
| [GetTimeByBarIdx()](/developer/desktop-sdk/references/common/charts/chartbars/gettimebybaridx) | The ChartBars time value calculated from a bar index value on the chart.                                                                                                |
| [Panel](/developer/desktop-sdk/references/common/charts/chartbars/panel)                       | The Panel index value that the ChartBars reside.                                                                                                                        |
| [Properties](/developer/desktop-sdk/references/common/charts/chartbars/chartbars-properties)   | Various ChartBar properties that have been configured from the Chart's [Data Series](https://ninjatrader.com/support/helpGuides/nt8/?working_with_price_data.htm) menu. |
| [ToChartString()](/developer/desktop-sdk/references/common/bars/tochartstring)                 | A string formatted for the Chart's Data Series Label as well as the period.                                                                                             |
| [ToIndex](/developer/desktop-sdk/references/common/charts/chartbars/toindex)                   | An index value representing the last bar painted on the chart.                                                                                                          |

## Examples

```csharp
protected override void OnStateChange()
{         
   if (State == State.DataLoaded)
   {
     if(ChartBars != null)
     {
         Print("The starting number of bars on the chart is " + ChartBars.Bars.Count);
     }
     else 
     {
         Print("Strategy was not loaded from a chart, exiting strategy...");
         return;
     }
   }
}
```