Golang : Calculate pivot points for a cross
As a habit, before I get into a trade position, I will always double check the target cross' pivot points. At minimum, I would like to know the Support and Resistance Level 1. This would help me to evaluate whether the trade position that I'm about to take make sense or not. For instance, if a trade position is near to Support Level 1....it doesn't make sense to take a short position.
Below is a code example on how to calculate the pivot points and the pivot points data can be very useful in tweaking a trading bot.
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)
fmt.Println("Calculating pivot points for NZD-JPY")
GetPivots(oanda, "NZD_JPY") // make sure to use underscore instead of hypen!
fmt.Println("Calculating pivot points for NZD-USD")
GetPivots(oanda, "NZD_USD") // make sure to use underscore instead of hypen!
}
//--------------------------------------------------------------------------------------------------------
func GetPivots(oanda *goanda.OandaConnection, cross string) {
data := oanda.GetCandles(cross, "2", "D") // H1 = 1 hour, D = 1 Day
high := data.Candles[0].Mid.High
low := data.Candles[0].Mid.Low
close := data.Candles[0].Mid.Close
current := GetPrice(oanda, cross)
pivot := (high + low + close) / 3
rangeHL := high - low
resistance1 := 2*pivot - low
resistance2 := pivot + rangeHL
resistance3 := resistance1 + rangeHL
support1 := 2*pivot - high
support2 := pivot - rangeHL
support3 := support1 - rangeHL
fmt.Println(cross)
fmt.Println("Current Price : ", current)
fmt.Println("High : ", high)
fmt.Println("Low : ", low)
fmt.Println("Range : ", rangeHL)
fmt.Println("Resistance 1 : ", resistance1)
fmt.Println("Resistance 2 : ", resistance2)
fmt.Println("Resistance 3 : ", resistance3)
fmt.Println("Support 1 : ", support1)
fmt.Println("Support 2 : ", support2)
fmt.Println("Support 3 : ", support3)
}
func GetPrice(oanda *goanda.OandaConnection, cross string) float64 {
instrument := cross
priceResponse := oanda.GetInstrumentPrice(instrument)
return priceResponse.Prices[0].Bids[0].Price
}
Example output:
Calculating pivot points for NZD-JPY
NZD_JPY
Current Price : 75.804
High : 76.092
Low : 75.5
Range : 0.5919999999999987
Resistance 1 : 75.99066666666667
Resistance 2 : 76.33733333333333
Resistance 3 : 76.58266666666667
Support 1 : 75.39866666666667
Support 2 : 75.15333333333334
Support 3 : 74.80666666666667
Calculating pivot points for NZD-USD
NZD_USD
Current Price : 0.67877
High : 0.68016
Low : 0.67533
Range : 0.004830000000000001
Resistance 1 : 0.6796233333333335
Resistance 2 : 0.6823066666666667
Resistance 3 : 0.6844533333333335
Support 1 : 0.6747933333333335
Support 2 : 0.6726466666666667
Support 3 : 0.6699633333333335
Reference :
See also : Golang : Find correlation coefficient example
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
+14.1k Golang : How to shuffle elements in array or slice?
+29.5k Golang : Get and Set User-Agent examples
+12.3k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+8.9k Android Studio : Indicate progression with ProgressBar example
+7.5k Golang : Trim everything onward after a word
+7.4k Golang : Command line ticker to show work in progress
+35.8k Golang : How to split or chunking a file to smaller pieces?
+13.1k Golang : Increment string example
+5.5k Swift : Get substring with rangeOfString() function example
+7k Golang : Null and nil value
+10.9k Google Maps URL parameters configuration