Commodity Channel Index Indicator
Send Feedback

Glossary Item Box

Topic Overview

Purpose

This topic provides a conceptual overview of IGChartView™ control’s Commodity Channel Index indicator and provides a code example demonstrating how to add it to the chart view.

In this topic


Introduction

Commodity Channel Index indicator summary

The Commodity Channel Index indicator is the mean deviation of the daily average price from the moving average used to measure the price of a commodity, relative to its moving average. Conceptually similar to Bollinger Bands, but represented as an indicator line rather than as overbought/oversold levels where values greater than 100 identify overbought commodities, and values below -100 identify oversold commodities. To calculate the Commodity Channel Index, you will first need to know the Typical Price (TP), (High + Low + Close)/3 and the Simple Moving Average of the Typical Price (SMATP). Subtract the SMATP from TP divide the results by subtracting today’s SMATP from TP for each of the last n days sum the absolute values and divide by n, where n represents the number of days in the period under analysis.

Data requirements

While the IGChartView control allows easy binding to your own data model, make sure to supply the appropriate amounts and types of data required by the series. Failing to do so causes the IGChartView to appear blank.

Required – the data model must contain numeric fields for each, high, low and closing values in order to render data.


Adding a Commodity Channel Index Indicator to the IGChartView – Code Example

Description

The following code uses the IGOHLCSeriesDataSourceHelper to supply randomly generated data to a financial price series by first adding to the IGChartView instance, then next adding the financial indicator to the IGChartView instance as a series, and then finally adding the IGChartView as a subview of the current UIView.

Prerequisites

This code example requires the inclusion of the IGChartView framework, detail about how to add this framework can be found in the Adding the Chart Framework File topic.

Code

In C#:

List<NSObject> open = new List<NSObject>();
for (int i = 0; i < 25; i++) {
      open.Add(new NSNumber((new Random((int)DateTime.Now.Ticks).Next(100))));
}
List<NSObject> high = new List<NSObject>();
for (int i = 0; i < 25; i++) {
      high.Add(new NSNumber((new Random((int)DateTime.Now.Ticks).Next(130))));
}
List<NSObject> low = new List<NSObject>();
for (int i = 0; i < 25; i++) {
      low.Add(new NSNumber((new Random((int)DateTime.Now.Ticks).Next(100))));
}
List<NSObject> close = new List<NSObject>();
for (int i = 0; i < 25; i++) {
      close.Add(new NSNumber((new Random((int)DateTime.Now.Ticks).Next(110))));
}
List<NSObject> volume = new List<NSObject>();
for (int i = 0; i < 25; i++) {
      volume.Add(new NSNumber((new Random((int)DateTime.Now.Ticks).Next(1100000))));
}
IGOHLCSeriesDataSourceHelper source = new IGOHLCSeriesDataSourceHelper();
source.OpenValues = open.ToArray();
source.HighValues = high.ToArray();
source.LowValues = low.ToArray();
source.CloseValues = close.ToArray();
source.VolumeValues = volume.ToArray();
IGChartView infraChart = new IGChartView(this.View.Frame);
infraChart.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
IGCategoryXAxis xAxis = new IGCategoryXAxis("xAxis");
IGNumericYAxis yAxis = new IGNumericYAxis("yAxis");
infraChart.AddAxis(xAxis);
infraChart.AddAxis(yAxis);
IGFinancialPriceSeries financialPriceSeries = new IGFinancialPriceSeries("financialPriceSeries");
financialPriceSeries.XAxis = xAxis;
financialPriceSeries.YAxis = yAxis;
financialPriceSeries.DataSource = source;
financialPriceSeries.DisplayType = IGPriceDisplayType.IGPriceDisplayTypeCandlestick;
IGCommodityChannelIndexIndicator financialIndicator = new IGCommodityChannelIndexIndicator("financialIndicator");
financialIndicator.XAxis = xAxis;
financialIndicator.YAxis = yAxis;
financialIndicator.DataSource = source;
financialIndicator.Thickness = 3.0f;
infraChart.AddSeries(financialPriceSeries);
infraChart.AddSeries(financialIndicator);
(this.View).AddSubview(infraChart);

In Objective-C:

    NSMutableArray *open = [[NSMutableArray alloc] init];
    for (int i = 0; i < 25; i++) {
        [open addObject:[[NSNumber alloc] initWithDouble:(arc4random() % 100)]];
    }
    
    NSMutableArray *high = [[NSMutableArray alloc] init];
    for (int i = 0; i < 25; i++) {
        [high addObject:[[NSNumber alloc] initWithDouble:(arc4random() % 100 + 30)]];
    }
    
    NSMutableArray *low = [[NSMutableArray alloc] init];
    for (int i = 0; i < 25; i++) {
        [low addObject:[[NSNumber alloc] initWithDouble:(arc4random() % 100)]];
    }
    
    NSMutableArray *close = [[NSMutableArray alloc] init];
    for (int i = 0; i < 25; i++) {
        [close addObject:[[NSNumber alloc] initWithDouble:(arc4random() % 100 + 10)]];
    }
    
    NSMutableArray *volume = [[NSMutableArray alloc] init];
    for (int i = 0; i < 25; i++)
    {
        [volume addObject:[[NSNumber alloc] initWithDouble:(arc4random() % 1000000 + 100000)]];
    }
    
    IGOHLCSeriesDataSourceHelper *source = [[IGOHLCSeriesDataSourceHelper alloc] init];
    source.openValues = open;
    source.highValues = high;
    source.lowValues = low;
    source.closeValues = close;
    source.volumeValues = volume;
    
    IGChartView *infraChart = [[IGChartView alloc] initWithFrame:self.view.frame];
    [infraChart setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
    
    IGCategoryXAxis *xAxis = [[IGCategoryXAxis alloc] initWithKey:@"xAxis"];
    IGNumericYAxis *yAxis = [[IGNumericYAxis alloc] initWithKey:@"yAxis"];
    [infraChart addAxis:xAxis];
    [infraChart addAxis:yAxis];
    
    IGFinancialPriceSeries *financialPriceSeries = [[IGFinancialPriceSeries alloc] initWithKey:@"financialPriceSeries"];
    financialPriceSeries.xAxis = xAxis;
    financialPriceSeries.yAxis = yAxis;
    financialPriceSeries.dataSource = source;
    financialPriceSeries.displayType = IGPriceDisplayTypeCandlestick;
    IGCommodityChannelIndexIndicator *financialIndicator = [[IGCommodityChannelIndexIndicator alloc] initWithKey:@"financialIndicator"];
    financialIndicator.xAxis = xAxis;
    financialIndicator.yAxis = yAxis;
    financialIndicator.dataSource = source;
    financialIndicator.thickness = 3.0f;
    
    [infraChart addSeries:financialPriceSeries];
    [infraChart addSeries:financialIndicator];
    [self.view addSubview:infraChart];

Related Content

Topics

The following topics provide additional information related to this topic.

Topic

Purpose

This collection of topics explains each of the individual charts supported by the IGChartView control.