How To Calculate Commodity Channel Index (CCI) in Visual Basic?

by bria_metz , in category: Technology , 2 months ago

How To Calculate Commodity Channel Index (CCI) in Visual Basic?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by adolf , 2 months ago

@bria_metz 

To calculate the Commodity Channel Index (CCI) in Visual Basic, you can use the following formula:


CCI = (Typical Price - SMA of Typical Price) / (0.015 * Mean Deviation)


Here's a step-by-step guide on how to calculate CCI in Visual Basic:

  1. Define the variables for the inputs and outputs:
1
2
3
4
5
6
Dim ClosePrices() As Double ' Array of historical closing prices
Dim Period As Integer ' Number of periods for the CCI calculation
Dim TP As Double ' Typical Price for each period
Dim SMATP As Double ' Simple Moving Average of Typical Price
Dim MeanDev As Double ' Mean Deviation
Dim CCI As Double ' Commodity Channel Index


  1. Calculate the Typical Price for each period:
1
2
For i = 0 To UBound(ClosePrices) - Period
    TP = (ClosePrices(i) + ClosePrices(i + 1) + ClosePrices(i + 2)) / 3


  1. Calculate the Simple Moving Average of Typical Price:
1
SMATP = Application.WorksheetFunction.Average(Range("A1:A" & i))


  1. Calculate the Mean Deviation:
1
2
3
4
For j = i - Period To i
    MeanDev = MeanDev + Abs(TP - SMATP)
Next j
MeanDev = MeanDev / Period


  1. Calculate the Commodity Channel Index:
1
CCI = (TP - SMATP) / (0.015 * MeanDev)


  1. Display or use the CCI value as needed in your program.


This is a basic example of how to calculate the Commodity Channel Index using Visual Basic. You may need to modify the code based on your specific requirements and data structure.