Documentation
¶
Overview ¶
OandaGo package is a wrapper API for Oanda-V20 RESTful API. Currently this wrapper API only covers two endpoints:
Get request for Instrument - candles endpoint which returns historical OHLC Bid/Ask.
- Parameters requires instrument symbol, token, and granularity (i.e., 'S5' for 5 second candles)
Get JSON Stream for Pricing - stream endpoint which returns live Bid/Ask.
- Parameters requires list of instruments, token, and id
Don't forget to check Oanda's Best Practices before querying any of their endpoints.
Additionally this package can be used with either Micrsoft SQL or PostgreSQL for inserting data and querying a local database instead of querying Oanda's API each time one needs historical data. You can also insert data from the live stream into a database.
Note: `main_psql.go` is for using PostgreSQL and `main_mssql.go` is for using Microsoft SQL as your database.
Example (Candles) ¶
package main
import (
"log"
"github.com/davidhintelmann/Oanda-Go/restful"
)
// must include ID and Token into
// res.json file, one can get these at
// https://fxtrade.oanda.com/your_account/fxtrade/register/gate?utm_source=oandaapi&utm_medium=link&utm_campaign=devportaldocs_demo
const account_path string = "./res.json"
func main() {
// set log flags for date and script file with line number for where the error occurred
log.SetFlags(log.Ldate | log.Lshortfile)
// Get ID and Token for Oanda Account
idToken, err := restful.GetIdToken(account_path, false)
_, token := idToken.Account.ID, idToken.Account.Token
if err != nil {
log.Fatalf("error during GetIdToken(): %v", err)
}
// GetCandlesBA function sends a GET request to Oanda's API
// set the display parameter to true to output OHLC data to the console
_, err = restful.GetCandlesBA("USD_CAD", "S5", token, true)
// candles := _.Candles
if err != nil {
log.Fatalf("error during GetCandlesBA(): %v", err)
}
}
Example (Mssql) ¶
package main
import (
"context"
"database/sql"
"log"
"github.com/davidhintelmann/Oanda-Go/restful"
)
// must include ID and Token into
// res.json file, one can get these at
// https://fxtrade.oanda.com/your_account/fxtrade/register/gate?utm_source=oandaapi&utm_medium=link&utm_campaign=devportaldocs_demo
const account_json_path string = "./res.json"
// server, database, driver configuration
var server, database, driver = "lpc:localhost", "Oanda-Stream", "mssql"
// trusted connection, and encryption configuraiton
var trusted_connection, encrypt = true, true
// db is global variable to pass between functions
var conn *sql.DB
// Use background context globally to pass between functions
var ctx_mssql = context.Background()
func main() {
// set log flags for date and script file with line number for where the error occurred
log.SetFlags(log.Ldate | log.Lshortfile)
// Get ID and Token for Oanda Account
idToken, err := restful.GetIdToken(account_json_path, false)
id, token := idToken.Account.ID, idToken.Account.Token
if err != nil {
log.Fatalf("error during GetIdToken(): %v", err)
}
// GetCandlesBA function sends a GET request to Oanda's API
// set the display parameter to true to output OHLC data to the console
_, err = restful.GetCandlesBA("USD_CAD", "S5", token, true)
if err != nil {
log.Fatalf("error during GetCandlesBA(): %v", err)
}
// connect to local instance of Microsoft SQL
conn, err := restful.ConnectMSSQL(ctx_mssql, conn, driver, server, database, trusted_connection, encrypt)
if err != nil {
log.Fatalf("error during ConnectMSSQL(): %v", err)
}
defer conn.Close()
// getSteamPSQL will get Oanda price stream and insert into local
// instance of Microsoft SQL
instrumentList := "USD_CAD,USD_JPY,USD_CHF,USD_HKD,USD_SGD,GBP_USD,NZD_USD,EUR_CAD,EUR_USD,EUR_GBP,EUR_AUD,EUR_JPY,AUD_CAD,AUD_USD,AUD_NZD,AUD_JPY,AUD_HKD,CAD_HKD,CAD_CHF,CAD_JPY,CAD_SGD"
restful.GetStreamMSSQL(ctx_mssql, conn, instrumentList, token, id, false)
}
Example (Psql) ¶
package main
import (
"context"
"log"
"github.com/davidhintelmann/Oanda-Go/connect"
"github.com/davidhintelmann/Oanda-Go/restful"
)
// must include ID and Token into
// res.json file, one can get these at
// https://fxtrade.oanda.com/your_account/fxtrade/register/gate?utm_source=oandaapi&utm_medium=link&utm_campaign=devportaldocs_demo
const account_path string = "./res.json"
// user, password, database name for postgresql instance, and ssl mode
const user, dbname, sslmode = "david", "GinTest", "disable"
// be careful not to expose your password to the public
// modify password_edit.go file found in connect directory
// rename that file password.go and this file only has one function.
// This functions name needs to be edited by removing the underscore
// at the end of 'ImportPassword_' function.
// modify the return statement for the password of your PostgreSQL
// user that is accessing your local instance
var password = connect.ImportPassword_()
// use background context globally to pass between functions
var ctx_psql = context.Background()
func main() {
// set log flags for date and script file with line number for where the error occurred
log.SetFlags(log.Ldate | log.Lshortfile)
// Get ID and Token for Oanda Account
idToken, err := restful.GetIdToken(account_path, false)
id, token := idToken.Account.ID, idToken.Account.Token
if err != nil {
log.Fatalf("error during GetIdToken(): %v", err)
}
// GetCandlesBA function sends a GET request to Oanda's API
// set the display parameter to true to output OHLC data to the console
_, err = restful.GetCandlesBA("USD_CAD", "S5", token, true)
// candles := _.Candles
if err != nil {
log.Fatalf("error during GetCandlesBA(): %v", err)
}
// connect to local instance of PostgreSQL
conn, err := connect.ConnectPSQL(ctx_psql, user, password, dbname, sslmode)
if err != nil {
log.Fatalf("error during ConnectPSQL(): %v", err)
}
defer conn.Close()
err = conn.Ping(ctx_psql)
if err != nil {
log.Fatalf("error pinning the local instance of PostgreSQL: %v\n", err)
}
// getSteamPSQL will get Oanda price stream and insert into local
// instance of Postgresql
instrumentList := "USD_CAD,USD_JPY,USD_CHF,USD_HKD,USD_SGD,GBP_USD,NZD_USD,EUR_CAD,EUR_USD,EUR_GBP,EUR_AUD,EUR_JPY,AUD_CAD,AUD_USD,AUD_NZD,AUD_JPY,AUD_HKD,CAD_HKD,CAD_CHF,CAD_JPY,CAD_SGD"
connect.GetStreamPSQL(ctx_psql, conn, password, instrumentList, token, id, false)
}