Golang : Calculate Relative Strength Index(RSI) example
Alright, below is a simple example on how to calculate the Relative Strength Index(RSI) using data from Binance.com and Golang. This example should be able to work with little modification if you're pulling your data from exchange other than Binance. All you need is a slice with Close
price values.
UPDATE : Please use the RSI function from "github.com/markcheno/go-talib" It matches the TradingView.com result.
Here you go!
package main
import (
"context"
"fmt"
"math"
"strconv"
"time"
"github.com/adshao/go-binance"
)
var (
//Binance
binanceAPIKey = "<your binance api key>"
binanceSecretKey = "<your binance secret key>"
)
func humanTimeToUnixNanoTime(input time.Time) int64 {
return int64(time.Nanosecond) * input.UnixNano() / int64(time.Millisecond)
}
func main() {
client := binance.NewClient(binanceAPIKey, binanceSecretKey)
targetSymbol := "BNBUSDT"
// reference https://golang.org/pkg/time/#Date
// func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
// NOTE : Binance only accepts unix nano timestamp, not unix time stamp
// For example
// 1527616800 <----- will return empty result or wrong date range
// 1527616899999 <--- ok
//startTimeHumanReadable := time.Date(2018, time.October, 10, 0, 0, 0, 0, time.UTC)
//start := humanTimeToUnixNanoTime(startTimeHumanReadable)
//endTimeHumanReadable := time.Date(2018, time.October, 24, 0, 0, 0, 0, time.UTC)
//end := humanTimeToUnixNanoTime(endTimeHumanReadable)
targetInterval := "1d" /// case sensitive, 1D != 1d or 30M != 30m
data, err := client.NewKlinesService().Symbol(targetSymbol).Interval(targetInterval).Do(context.Background())
if err != nil {
fmt.Println(err)
return
}
totalGain := 0.0
totalLoss := 0.0
for i := 1; i < len(data); i++ {
previous := data[i].Close
current := data[i-1].Close
// convert string to float64
previousClose, _ := strconv.ParseFloat(previous, 64)
currentClose, _ := strconv.ParseFloat(current, 64)
difference := currentClose - previousClose
if difference >= 0 {
totalGain += difference
} else {
totalLoss -= difference
}
}
rs := totalGain / math.Abs(totalLoss)
rsi := 100 - 100/(1+rs)
fmt.Println("RSI for "+targetSymbol+" : ", rsi)
}
Example output:
Thu Oct 25 11:35:33 +08 2018
RSI for BNBUSDT : 47.84161851469368
NOTE:
UPDATE : Please use the RSI function from "github.com/markcheno/go-talib" It matches the TradingView.com result.
I've tried to compare the result to see if it matches the RSI values calculated by TradingView.com (TV) and what I found out is that BTCUSDT and BNBUSDT RSI values are pretty close to the values calculated by this code example. I noticed that in TV's RSI row, it has complete data dating back to more than 1 year.
Most of the mismatched values found in TV have data less than 5 months. Some said that the data needs to be smoothed out first, etc. So far,I'm pretty ok with the values given by this script.
References:
https://stockcharts.com/school/doku.php?id=chartschool:technicalindicators:relativestrengthindex_rsi
See also : Golang : A simple forex opportunities scanner
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+15.8k Golang : Loop each day of the current month example
+5k JavaScript/JQuery : Redirect page examples
+19.8k Golang : Check if os.Stdin input data is piped or from terminal
+9.9k Golang : Compare files modify date example
+6.4k Golang : Join lines with certain suffix symbol example
+12.8k Golang : Handle or parse date string with Z suffix(RFC3339) example
+9.1k Golang : Terminate-stay-resident or daemonize your program?
+11.7k Golang : Convert a rune to unicode style string \u
+18.2k Golang : Example for RSA package functions
+7.1k Golang : How to convert strange string to JSON with json.MarshalIndent
+8.9k Golang : Generate Codabar
+10.8k Golang : Fix - does not implement sort.Interface (missing Len method)