Jump to page sections
- Open-source PowerShell RSI Calculator (Relative Strength Index)
- Parameters for the script
- Download source link
- Example use
- Only RSI step one
- A practical application demo
Open-source PowerShell RSI Calculator (Relative Strength Index)
Initial date of publishing: 2022-11-05.
PowerShell Core runs on Windows, macOS and Linux. This RSI calculator is made in PowerShell. If you are using a Windows computer, you already have Windows PowerShell 5.1 installed, and can use that version.
The relative strength index (RSI) is a momentum indicator used in technical analysis that measures the magnitude of recent price changes to evaluate overbought or oversold conditions in the price of a stock or other asset.
The RSI is displayed as an oscillator (a line graph that moves between two extremes) and can have a reading from 0 to 100. The indicator was originally developed by J. Welles Wilder Jr. and introduced in his seminal 1978 book, “New Concepts in Technical Trading Systems.”The relative strength index (RSI) is a popular momentum indicator developed in 1978.
The RSI provides technical traders with signals about bullish and bearish price momentum, and it is often plotted beneath the graph of an asset's price. An asset is usually considered overbought when the RSI is above 70 % and oversold when it is below 30 %.
Parameters for the script
Parameter | Explanation |
---|---|
Numbers | The numbers to calculate an RSI value for, such as 15 (or 197) stock market close rates, hourly/whatever crypto currency data, or endless other types of data. |
FirstStepSampleCount | Sets the first step sample count to be used (default 14) for calculating RSI step one. If you want standard RSI calculations, leave this parameter out.
The traditional RSI formula uses 14 data points/numbers. This parameter allows you to specify a different count for your RSI calculation. The number of -Numbers you need is this number plus one (to get $FirstStepSampleCount percentages, the first calculated and used percentage requires a "starting number"). |
GetSecondStep | Switch parameter (takes no value) indicating you want the RSI second step on a larger set of data than without this (only the first RSI step is calculated without this parameter). |
SecondStepSampleCount |
Sets the second step sample count which should be evenly divisible by $FirstStepSampleCount, used for calculating the second step of the RSI formula. Default 14 * 14 (14 times the default $FirstStepSampleCount). 14*14 = 196. If you want standard RSI calculations, leave this parameter out. Only in use together with -GetSecondStep.
The traditional RSI formula uses 14*14 numbers. This parameter allows you to specify a different count for your RSI calculation. If you provide the switch parameter -GetSecondStep, the number of -Numbers you need is this number plus one (to get $SecondStepSampleCount percentages, the first calculated and used percentage requires a "starting number"). |
Download source link
Example use
PS C:\> $EthPrices = gci "$MyHome\coinmarketcapdata_5m\*.json" | sort-object Name | select-object -last 197 | %{ ((gc -raw $_.fullname) | ConvertFrom-Json).data.where({ $_.slug -eq 'ethereum'}).quote.usd.price } PS C:\> $EthPrices.Count 197 PS C:\> $EthPrices[0,-1] 1619.804828821538 1588.1437982934824 PS C:\> Get-RSI -Numbers $EthPrices -GetSecondStep RsiStepOnesCalulationsForStepTwo : {@{Numbers=System.Decimal[]; Percentages=System.Object[]; Gains=System.Object[]; AverageGain=0.0332195783966708; Losses=System.Object[]; AverageLoss=-0.0143569386067286; SampleCount=14; RSIStepOne=69.8234769777225; DateTime=10/30/2022 11:11:06 PM}, @{Numbers=System.Decimal[]; Percentages=System.Object[]; Gains=System.Object[]; AverageGain=0.0828497999703561; Losses=System.Object[]; AverageLoss=-0.0259971421649874; SampleCount=14; RSIStepOne=76.1158727521607; DateTime=10/30/2022 11:11:06 PM}, @{Numbers=System.Decimal[]; Percentages=System.Object[]; Gains=System.Object[]; AverageGain=0.0182880118722195; Losses=System.Object[]; AverageLoss=-0.0662359869957693; SampleCount=14; RSIStepOne=21.6364726197847; DateTime=10/30/2022 11:11:06 PM}, @{Numbers=System.Decimal[]; Percentages=System.Object[]; Gains=System.Object[]; AverageGain=0.0286163046781352; Losses=System.Object[]; AverageLoss=-0.0681690762665304; SampleCount=14; RSIStepOne=29.5667634913746; DateTime=10/30/2022 11:11:06 PM}...} RsiStepOnesString : 69.82 - 76.12 - 21.64 - 29.57 - 15.51 - 72.93 - 37.96 - 26.01 - 72.43 - 45.24 - 26.99 - 70.55 - 72.06 - 26.68 RsiStepOnesAverage : 47.39 SecondStepSampleCount : 196 PreviousXAverageGain : 0.0387438591514454 PreviousXAverageLoss : -0.0471697208659479 RSIStepTwo : 44.08 Numbers : {1619.804828821538, 1619.8921283607126, 1619.9369185512846, 16211149589714098...} DateTime : 10/30/2022 11:11:06 PM PS C:\>
I recommend including an ISO8601 timestamp in the filename (or a variant of ISO8601, rather) so you can sort on the filename if something happens to the files, they are moved to another disk, etc. My files are named e.g. crypto-json-coinmarketcap-top-100-2022-10-26_21.00.29.json, which technically isn't ISO8601.
The "RsiStepOnesCalulationsForStepTwo" property is there in case you want to perform math operations or similar on the 14 RSI step ones that are combined to get the second step RSI number.
The "RsiStepOnesString" is a convenience property for visual inspection of the 14 RSI step ones that form the RSI step two number, in chronological order.
The average of the RSI step ones is also calculated for potential elucidation.
The "PreviousXAverageGain" number by default is the average gain based on the previous 13 average gain percentages for the RSI step one periods, which are then again averaged to produce this number. The same logic applies to "PreviousXAverageLoss", only for losses.
In the example above, you have 0.0387 as the average gain and -0.04717 as the average loss. This is as expected when the RSI step two is below 50, so it also serves as a sanity-check of the calculations. If the gains are higher than the losses, the RSI step two will be higher than 50. At least typically.
Some other, self-explanatory properties are included for convenience.
If you want to look at periods longer back in time than the minimum ~16.4 hours required with 5-minute samples and 197 files, you can do it in a myriad of ways. One is by skipping every other file (experienced programmers immediately smell the modulus operator here).
Only RSI step one
PS /> $BitcoinPrices3 = gci "$MyHome/coinmarketcapdata_5m/*.json" | Sort-Object Name | Select-Object -Last 15 | %{(gc -raw $_.FullName | ConvertFrom-Json ).data.where({$_.slug -eq 'bitcoin'}).quote.usd.price} PS /> Get-RSI -Numbers $BitcoinPrices3 Numbers : {21336.7507475232, 21329.3592371492, 21328.3559122658, 21328.979918163…} Percentages : {-0.0346421555065408302976309700, -0.0047039616720061419873462200, 0.0029257102599321227143309500, -0.0209321620402393993940203300…} Gains : {0.0029257102599321227143309500, 0.013861648396240968074699100, 0.0173634991327144277054730400, 0.0388522788372299841613685200…} AverageGain : 0.00869109795628686 Losses : {-0.0346421555065408302976309700, -0.0047039616720061419873462200, -0.0209321620402393993940203300, -0.0229687896176396015299950700…} AverageLoss : -0.0105057203141827 SampleCount : 14 RSIStepOne : 45.2736377134766 DateTime : 11/5/2022 11:27:17 PM PS />
A practical application demo
An example use case is to set up push notifications (I use pushover.net) to your phone with hourly development. This can be supplemented with warnings that trigger on threshold values, for instance if there is a big change in percent, as you see fit in your code.
The end result can look something like this:
Blog articles in alphabetical order
A
- A Look at the KLP AksjeNorden Index Mutual Fund
- A primitive hex version of the seq gnu utility, written in perl
- Accessing the Bing Search API v5 using PowerShell
- Accessing the Google Custom Search API using PowerShell
- Active directory password expiration notification
- Aksje-, fonds- og ETF-utbytterapportgenerator for Nordnet-transaksjonslogg
- Ascii art characters powershell script
- Automatically delete old IIS logs with PowerShell
C
- Calculate and enumerate subnets with PSipcalc
- Calculate the trend for financial products based on close rates
- Check for open TCP ports using PowerShell
- Check if an AD user exists with Get-ADUser
- Check when servers were last patched with Windows Update via COM or WSUS
- Compiling or packaging an executable from perl code on windows
- Convert between Windows and Unix epoch with Python and Perl
- Convert file encoding using linux and iconv
- Convert from most encodings to utf8 with powershell
- ConvertTo-Json for PowerShell version 2
- Create cryptographically secure and pseudorandom data with PowerShell
- Crypto is here - and it is not going away
- Crypto logo analysis ftw
D
G
- Get rid of Psychology in the Stock Markets
- Get Folder Size with PowerShell, Blazingly Fast
- Get Linux disk space report in PowerShell
- Get-Weather cmdlet for PowerShell, using the OpenWeatherMap API
- Get-wmiobject wrapper
- Getting computer information using powershell
- Getting computer models in a domain using Powershell
- Getting computer names from AD using Powershell
- Getting usernames from active directory with powershell
- Gnu seq on steroids with hex support and descending ranges
- Gullpriser hos Gullbanken mot spotprisen til gull
H
- Have PowerShell trigger an action when CPU or memory usage reaches certain values
- Historical view of the SnP 500 Index since 1927, when corona is rampant in mid-March 2020
- How to check perl module version
- How to list all AD computer object properties
- Hva det innebærer at særkravet for lån til sekundærbolig bortfaller
I
L
M
P
- Parse openssl certificate date output into .NET DateTime objects
- Parse PsLoggedOn.exe Output with PowerShell
- Parse schtasks.exe Output with PowerShell
- Perl on windows
- Port scan subnets with PSnmap for PowerShell
- PowerShell Relative Strength Index (RSI) Calculator
- PowerShell .NET regex to validate IPv6 address (RFC-compliant)
- PowerShell benchmarking module built around Measure-Command
- Powershell change the wmi timeout value
- PowerShell check if file exists
- Powershell check if folder exists
- PowerShell Cmdlet for Splitting an Array
- PowerShell Executables File System Locations
- PowerShell foreach loops and ForEach-Object
- PowerShell Get-MountPointData Cmdlet
- PowerShell Java Auto-Update Script
- Powershell multi-line comments
- Powershell prompt for password convert securestring to plain text
- Powershell psexec wrapper
- PowerShell regex to accurately match IPv4 address (0-255 only)
- Powershell regular expressions
- Powershell split operator
- Powershell vs perl at text processing
- PS2CMD - embed PowerShell code in a batch file
R
- Recursively Remove Empty Folders, using PowerShell
- Remote control mom via PowerShell and TeamViewer
- Remove empty elements from an array in PowerShell
- Remove first or last n characters from a string in PowerShell
- Rename unix utility - windows port
- Renaming files using PowerShell
- Running perl one-liners and scripts from powershell
S
- Sammenlign gullpriser og sølvpriser hos norske forhandlere av edelmetall
- Self-contained batch file with perl code
- Simple Morningstar Fund Report Script
- Sort a list of computers by domain first and then name, using PowerShell
- Sort strings with numbers more humanely in PowerShell
- Sorting in ascending and descending order simultaneously in PowerShell
- Spar en slant med en optimalisert kredittkortportefølje
- Spre finansiell risiko på en skattesmart måte med flere Aksjesparekontoer
- SSH from PowerShell using the SSH.NET library
- SSH-Sessions Add-on with SCP SFTP Support
- Static Mutual Fund Portfolio the Last 2 Years Up 43 Percent
- STOXR - Currency Conversion Software - Open Exchange Rates API