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
+34.9k Golang : Strip slashes from string example
+27.1k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+8.8k Golang : Simple histogram example
+26.5k Golang : Find files by extension
+13.3k Golang : Set image canvas or background to transparent
+22.7k Golang : Randomly pick an item from a slice/array example
+9.9k Golang : How to tokenize source code with text/scanner package?
+4.8k Golang : Display packages names during compilation
+7.6k Golang : Regular Expression find string example
+11.9k Golang : flag provided but not defined error
+10.8k Golang : Fix - does not implement sort.Interface (missing Len method)
+10.5k Golang : Command line file upload program to server example