This repository was archived by the owner on Mar 28, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGet-CCI.ps1
More file actions
111 lines (89 loc) · 3.42 KB
/
Copy pathGet-CCI.ps1
File metadata and controls
111 lines (89 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
function Get-CCI {
<#
.SYNOPSIS
Calculate the Commodity Channel Index (CCI) based on high, low, and close prices.
.DESCRIPTION
This function calculates the Commodity Channel Index (CCI) using the typical prices (average of high, low, and close) and a specified period.
.PARAMETER HighPrices
An array of high prices for the specified period.
.PARAMETER LowPrices
An array of low prices for the specified period.
.PARAMETER ClosePrices
An array of close prices for the specified period.
.PARAMETER Period
The number of periods to calculate the CCI.
.EXAMPLE
Example usage with historical data from Coingecko API
$baseUrl = "https://api.coingecko.com/api/v3"
$coinId = "bitcoin"
$days = 30
$url = "$baseUrl/coins/$coinId/market_chart?vs_currency=usd&days=$days"
$response = Invoke-RestMethod -Uri $url
$prices = $response.prices
$highPrices = $prices | ForEach-Object { $_[1] }
$lowPrices = $prices | ForEach-Object { $_[2] }
$closePrices = $prices | ForEach-Object { $_[1] }
$period = 20
$cci = Get-CCI -HighPrices $highPrices -LowPrices $lowPrices -ClosePrices $closePrices -Period $period
Write-Output $cci
.NOTES
Author: Wojciech Napierała
Date: 04.07.2023
Version: 1.1
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[double[]]$HighPrices,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[double[]]$LowPrices,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[double[]]$ClosePrices,
[Parameter(Mandatory = $true)]
[ValidateRange(1, [int]::MaxValue)]
[int]$Period
)
# Input validation
if ($HighPrices.Length -ne $LowPrices.Length -or $LowPrices.Length -ne $ClosePrices.Length) {
throw "Input arrays must have the same length."
}
# Calculate typical prices
$typicalPrices = for ($i = 0; $i -lt $ClosePrices.Length; $i++) {
($HighPrices[$i] + $LowPrices[$i] + $ClosePrices[$i]) / 3
}
# Calculate Simple Moving Average (SMA)
$sma = Get-SMA -DataPoints $typicalPrices -Period $Period
# Calculate Mean Deviation
$meanDeviation = New-Object 'System.Collections.Generic.List[Double]'
for ($i = $Period - 1; $i -lt $typicalPrices.Length; $i++) {
$sum = 0.0
for ($j = $i - $Period + 1; $j -le $i; $j++) {
$sum += [Math]::Abs($typicalPrices[$j] - $sma[$i - $Period + 1])
}
$meanDeviation.Add($sum / $Period)
}
# Calculate Commodity Channel Index (CCI)
$cci = New-Object 'System.Collections.Generic.List[Double]'
for ($i = $Period - 1; $i -lt $typicalPrices.Length; $i++) {
$cci.Add(($typicalPrices[$i] - $sma[$i - $Period + 1]) / (0.015 * [Math]::Abs($meanDeviation[$i - $Period + 1])))
}
return $cci
}
<#
# Example usage with historical data from Coingecko API
$baseUrl = "https://api.coingecko.com/api/v3"
$coinId = "bitcoin"
$days = 30
$url = "$baseUrl/coins/$coinId/market_chart?vs_currency=usd&days=$days"
$response = Invoke-RestMethod -Uri $url
$prices = $response.prices
$highPrices = $prices | ForEach-Object { $_[1] }
$lowPrices = $prices | ForEach-Object { $_[2] }
$closePrices = $prices | ForEach-Object { $_[1] }
$period = 20
$cci = Get-CCI -HighPrices $highPrices -LowPrices $lowPrices -ClosePrices $closePrices -Period $period
Write-Output $cci
#>