Golang : Calculate a pip value and distance to target profit example
Below is an example on how to calculate a pip value from a given trade amount and exchange rate of EURGBP. Just for fun, it will also calculate the number of pips that it needs to gain in order to hit a profit target.
Here you go!
package main
import (
"fmt"
"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)
currencyPair := "EUR_GBP"
exchangeRate := GetPrice(oanda, currencyPair)
// 5 decimal places except for yen which is 3 decimal places
tradeAmountinEUR := 350000.00000
numberOfGBPperPip := tradeAmountinEUR * 0.0001
PipValue := numberOfGBPperPip / exchangeRate
fmt.Println(currencyPair, " exchange rate is : ", exchangeRate)
fmt.Println("Therefore a trade amount of : ", tradeAmountinEUR, "EUR ")
fmt.Printf("Will give a PIP value of : [%0.2f] EUR per pip.\n", PipValue)
// If we prefer to exit a position by hitting a target in EUR value instead of number of pips made
// then let's
// calculate how many pips we need in order to hit profit target of 200.00 EUR
targetProfit := 200.00 // EUR
pipsToGain := targetProfit / PipValue
fmt.Println("In order to hit a profit of ", targetProfit, "EUR, this trade needs....")
fmt.Printf("to gain : [%0.2f] pips \n", pipsToGain)
// verify
// PIP to price
pipToPrice := pipsToGain * PipValue
fmt.Println("Yup! Profit is :", pipToPrice, "EUR")
// Exercise
// Calculate how much is 1 pip worth in your home currency.
// Let say your account home currency is USD, calculate the 1 pip
// equivalent to USD.
// Should be very simple! Good luck!
}
//--------------------------------------------------------------------------------------------------------
func GetPrice(oanda *goanda.OandaConnection, cross string) float64 {
instrument := cross
priceResponse := oanda.GetInstrumentPrice(instrument)
return priceResponse.Prices[0].Bids[0].Price
}
Example output:
EUR_GBP exchange rate is : 0.85146
Therefore a trade amount of : 350000 EUR
Will give a PIP value of : [41.11] EUR per pip.
In order to hit a profit of 200 EUR, this trade needs....
to gain : [4.87] pips
Yup! Profit is : 200 EUR
References :
PIP - Price Interest Point
https://www.oanda.com/forex-trading/learn/getting-started/pips
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
+9.6k Golang : Get current, epoch time and display by year, month and day
+11.1k Golang : Byte format example
+33.4k Golang : All update packages with go get command
+36.1k Golang : Convert date or time stamp from string to time.Time type
+21.5k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+21.9k Golang : Repeat a character by multiple of x factor
+11.1k Golang : How to flush a channel before the end of program?
+36.1k Golang : Convert(cast) int64 to string
+21.5k Golang : How to reverse slice or array elements order
+8.6k Golang : HTTP Routing with Goji example
+12.7k Golang : Get terminal width and height example