-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-BollingerBands.ps1
67 lines (60 loc) · 2.57 KB
/
Get-BollingerBands.ps1
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
<#
.SYNOPSIS
Calculates Bollinger Bands for a given symbol and closing prices.
.DESCRIPTION
This script defines a PowerShell function, Get-BollingerBands, which calculates Bollinger Bands
based on the provided symbol and closing prices within a specified period and number of standard deviations.
.PARAMETER symbol
The symbol for which Bollinger Bands are calculated.
.PARAMETER prices
An array of objects containing date and close price properties.
.PARAMETER period
The period for calculating the moving average and standard deviation. Default is 20.
.PARAMETER num_std_devs
The number of standard deviations for calculating upper and lower bands. Default is 2.
.EXAMPLE
$symbol = "BTC/USD"
$prices = @(
[pscustomobject]@{ date = "2022-01-01"; close = 50000 },
[pscustomobject]@{ date = "2022-01-02"; close = 55000 },
[pscustomobject]@{ date = "2022-01-03"; close = 55000 },
[pscustomobject]@{ date = "2022-01-04"; close = 55033 },
[pscustomobject]@{ date = "2022-01-05"; close = 53000 },
[pscustomobject]@{ date = "2022-01-06"; close = 52000 },
[pscustomobject]@{ date = "2022-01-07"; close = 45000 },
[pscustomobject]@{ date = "2022-01-08"; close = 43000 },
[pscustomobject]@{ date = "2022-01-09"; close = 42900 },
[pscustomobject]@{ date = "2022-01-10"; close = 45000 }
)
Get-BollingerBands -symbol $symbol -prices $prices -period 10 -num_std_devs 2
.NOTES
Author: Wojciech Napierała
Date: 04.07.2023
Version: 1.1
#>
function Get-BollingerBands {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$symbol,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[array]$prices,
[Parameter(Mandatory = $false)]
[ValidateRange(1, [int]::MaxValue)]
[int]$period = 20,
[Parameter(Mandatory = $false)]
[ValidateRange(0, [int]::MaxValue)]
[int]$num_std_devs = 2
)
# Calculate moving average and standard deviation
$ma_values = $prices[-$period..-1].close
$ma_value = ($ma_values | Measure-Object -Average).Average
$stdev = [Math]::Sqrt(($ma_values | ForEach-Object { [Math]::Pow($_ - $ma_value, 2) } | Measure-Object -Average).Average)
# Calculate upper and lower bands
$upper_band = $ma_value + ($num_std_devs * $stdev)
$lower_band = $ma_value - ($num_std_devs * $stdev)
# Output Bollinger Bands
Write-Output "The Bollinger Bands for $symbol are: Upper Band = $upper_band, Lower Band = $lower_band."
}