OandaGo

package module
v0.0.0-...-0b3e7cc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 16, 2023 License: MIT Imports: 6 Imported by: 0

README

Query Oanda's REST-V20 API with Go

This repo contains go code for querying Oanda's API for Forex price info. One could also set up a demo account to trade

WARNING: This is for educational purposes only.

Taken from Oanda's REST V-20 API Introduction page:

The Oanda's REST V-20 API provides programmatic access to Oandas's next generation v20 trading engine. To use this API you must have a v20 trading account, which is available to all divisions except OANDA Global Markets.

What can I do with the OANDA REST-v20 API?

- Get real-time rates for all tradeable pairs 24 hours a day
- Access historical pricing information dating back to 2005
- Place, modify, close orders
- Manage your account settings
- Access your account/trading history

Installation

  1. Requires Go 1.20.3 or greater
  2. Register demo account from Oanda to obtain an API key
  3. Modify the res_edit.json file in this repo's root directory with ID and Token obtained in the second step
    • rename res_edit.json to res.json for go code to work correctly

Once the above is satisfied you can get the functions in this repo with:

go get github.com/davidhintelmann/Oanda-Go/restful

Then import in your main.go (or any go file) with

import "github.com/davidhintelmann/Oanda-Go/restful"

Documentation

Overview

OandaGo package is a wrapper API for Oanda-V20 RESTful API. Currently this wrapper API only covers two endpoints:

  1. 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)

  2. 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)

}

Directories

Path Synopsis
cmd module
connect module
oanda module
restful module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL