Golang : Calculate US Dollar Index (DXY)
Below is a code example on how to calculate the US dollar index based on a basket of other related currencies pairs. The formula to calculate US dollar index is :
USDX = 50.14348112 × EUR/USD^(-0.576) × USD/JPY^(0.136) × GBP/USD^(-0.119) × USD/CAD^(0.091) × USD/SEK^(0.042) × USD/CHF^(0.036)
How you want to deploy the US dollar index in your trading strategy? You can read more about it here.
Here you go!
package main
import (
"fmt"
"math"
"github.com/awoldes/goanda"
)
func main() {
oandaAccountID := ""
oandaAPIKey := ""
// set the NewConnection 3rd parameter to [false] to use DEMO account.
// [true] for LIVE account
UsingLIVEAccount := false // set false to use https://api-fxpractice.oanda.com
oanda := goanda.NewConnection(oandaAccountID, oandaAPIKey, UsingLIVEAccount)
fmt.Println("Calculating USD index using this formula...")
fmt.Println("USDX = 50.14348112 × EUR/USD^(-0.576) × USD/JPY^(0.136) × GBP/USD^(-0.119) x USD/CAD^(0.091) × USD/SEK^(0.042) × USD/CHF^(0.036)")
EURUSD := GetPrice(oanda, "EUR_USD")
USDJPY := GetPrice(oanda, "USD_JPY")
GBPUSD := GetPrice(oanda, "GBP_USD")
USDCAD := GetPrice(oanda, "USD_CAD")
USDSEK := GetPrice(oanda, "USD_SEK")
USDCHF := GetPrice(oanda, "USD_CHF")
EURUSDPowerOf := math.Pow(EURUSD, -0.576)
USDJPYPowerOf := math.Pow(USDJPY, 0.136)
GBPUSDPowerOf := math.Pow(GBPUSD, -0.119)
USDCADPowerOf := math.Pow(USDCAD, 0.091)
USDSEKPowerOf := math.Pow(USDSEK, 0.042)
USDCHFPowerOf := math.Pow(USDCHF, 0.036)
USDX := 50.14348112 * EURUSDPowerOf * USDJPYPowerOf * GBPUSDPowerOf * USDCADPowerOf * USDSEKPowerOf * USDCHFPowerOf
fmt.Println("Current USD Index : ", USDX)
}
//--------------------------------------------------------------------------------------------------------
func GetPrice(oanda *goanda.OandaConnection, cross string) float64 {
instrument := cross
priceResponse := oanda.GetInstrumentPrice(instrument)
return priceResponse.Prices[0].Bids[0].Price
}
Sample output for 8-March-2019:
Calculating USD index using this formula...
USDX = 50.14348112 × EUR/USD^(-0.576) × USD/JPY^(0.136) × GBP/USD^(-0.119) x USD/CAD^(0.091) × USD/SEK^(0.042) × USD/CHF^(0.036)
Current USD Index : 97.54459899738309
Points to remember
If USD is the base currency (USD/XXX), then the USDX and the currency pair should move the same direction.
If USD is the quote currency (XXX/USD), then the USDX and the currency pair should move in opposite directions.
References:
https://www.babypips.com/learn/forex/using-the-usdx-for-forex
See also : Golang : Calculate pivot points for a cross
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
+31.9k Golang : Math pow(the power of x^y) example
+47.7k Golang : How to convert JSON string to map and slice
+7.7k Golang : Check from web if Go application is running or not
+10.8k Golang : Create S3 bucket with official aws-sdk-go package
+10.9k Golang : Calculate Relative Strength Index(RSI) example
+8k Swift : Convert (cast) Character to Integer?
+7.3k Golang : Gorrila set route name and get the current route name
+5.6k Cash Flow : 50 days to pay your credit card debt
+9.3k Golang : How to extract video or image files from html source code
+14.3k Golang : GUI with Qt and OpenCV to capture image from camera
+22.3k Generate checksum for a file in Go
+7.6k Swift : Convert (cast) String to Double