Golang : A simple forex opportunities scanner
Below is a simple program that will scan for trading opportunities. Basically what this program does is to calculate a given cross(instrument) 1 hour RSI value and display the result on screen.
What this program does is to help me in focusing on crosses that have good trading potential. Kinda like those instruments found in a cockpit, this is just one of a "trading instruments" that I use to sniff for opportunities.
Disclaimer : It is not meant for a fully automated trading bot but rather for my own viewing.
Use it at your own risk!!
Here you go!
package main
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/awoldes/goanda"
"github.com/markcheno/go-talib"
)
func main() {
// put Oanda.com API key and account ID here
oandaAccountID := ""
oandaAPIKey := ""
// set the NewConnection 3rd parameter to [false] to use DEMO account.
// [true] for LIVE account
fmt.Println("Starting Oanda Trading bot....")
UsingLIVEAccount := false // set false to use https://api-fxpractice.oanda.com
oanda := goanda.NewConnection(oandaAccountID, oandaAPIKey, UsingLIVEAccount)
// list of crosses from
// https://www.socketloop.com/tutorials/golang-get-a-list-of-crosses-instruments-available-to-trade-from-oanda-account
temp := "USD_DKK,EUR_AUD,CHF_JPY,EUR_SGD,USD_JPY,EUR_TRY,USD_CZK,GBP_AUD,USD_PLN,USD_SGD,EUR_SEK,USD_HKD,EUR_NZD,SGD_JPY,AUD_CAD,GBP_CHF,USD_THB,TRY_JPY,CHF_HKD,AUD_USD,EUR_DKK,EUR_USD,AUD_NZD,SGD_HKD,EUR_HUF,USD_CNH,EUR_HKD,EUR_JPY,NZD_USD,GBP_PLN,GBP_JPY,USD_TRY,EUR_CAD,USD_SEK,GBP_SGD,EUR_GBP,GBP_HKD,USD_ZAR,AUD_CHF,USD_CHF,USD_MXN,GBP_USD,EUR_CHF,EUR_NOK,AUD_SGD,CAD_CHF,SGD_CHF,CAD_HKD,USD_INR,NZD_CAD,GBP_ZAR,NZD_SGD,ZAR_JPY,CAD_JPY,GBP_CAD,USD_SAR,NZD_CHF,NZD_HKD,GBP_NZD,AUD_HKD,EUR_CZK,CHF_ZAR,USD_HUF,NZD_JPY,HKD_JPY,CAD_SGD,USD_NOK,USD_CAD,AUD_JPY,EUR_PLN,EUR_ZAR"
// first, clean/remove the comma
cleaned := strings.Replace(temp, ",", " ", -1)
// crosses to monitor
crossesToMonitor := strings.Fields(cleaned)
fmt.Println("Crosses to monitor : ")
fmt.Println(crossesToMonitor)
// We want to scan for crosses with RSI above 60 and below 40
// every 15 seconds
go ScanCrossByRSI(oanda, crossesToMonitor, 15)
select {} // this will cause the program to run forever until termination
}
//--------------------------------------------------------------------------------------------------------
func ScanCrossByRSI(oanda *goanda.OandaConnection, crosses []string, intervalSecond int) {
counter := time.Tick(time.Duration(intervalSecond) * time.Second) // poll every 15 seconds
for range counter {
for k, v := range crosses {
rsi := GetOneHourRSI(oanda, v)
rsiStr := strconv.FormatFloat(rsi, 'f', 6, 64) // convert float64 to string
if rsi >= 60 {
if rsi >= 70 {
fmt.Println(k, v, "RSI : "+rsiStr+" >= 70 [***SHORT***]")
} else {
fmt.Println(k, v, "RSI : "+rsiStr+" >= 60 [MAYBE SHORT]")
}
} else if rsi <= 40 {
if rsi <= 30 {
fmt.Println(k, v, "RSI : "+rsiStr+" <= 30 [***LONG***]")
} else {
fmt.Println(k, v, "RSI : "+rsiStr+" <= 40 [MAYBE LONG]")
}
} else {
fmt.Println(k, v, "RSI : "+rsiStr)
}
}
}
}
func GetOneHourRSI(oanda *goanda.OandaConnection, cross string) float64 {
length := 14
// See http://developer.oanda.com/rest-live-v20/instrument-ep/#collapse_2_example_curl_1
// and for the candlestick granularity ... see
// http://developer.oanda.com/rest-live-v20/instrument-df/#CandlestickGranularity
// Get 100 candles with 1 hour granularity
data := oanda.GetCandles(cross, "100", "H1") // H1 = 1 hour
closeSlice := make([]float64, len(data.Candles))
// calculate RSI
for i := 0; i < len(data.Candles); i++ {
closeSlice[i] = data.Candles[i].Mid.Close
}
rsiSlice := talib.Rsi(closeSlice, length)
rsi := rsiSlice[len(rsiSlice)-1] // match tradingview.com RSI
return rsi
}
Sample output:
0 USD_DKK RSI : 51.451647
1 EUR_AUD RSI : 56.934427
2 CHF_JPY RSI : 50.384710
3 EUR_SGD RSI : 56.429897
4 USD_JPY RSI : 52.345976
5 EUR_TRY RSI : 47.017935
6 USD_CZK RSI : 48.813204
7 GBP_AUD RSI : 54.111088
8 USD_PLN RSI : 51.368513
9 USD_SGD RSI : 58.852058
10 EUR_SEK RSI : 60.429787 >= 60 [MAYBE SHORT]
11 USD_HKD RSI : 55.950313
12 EUR_NZD RSI : 73.207817 >= 70 [***SHORT***]
13 SGD_JPY RSI : 45.009616
Reference:
https://www.socketloop.com/blogs/don-t-trade-without-guiding-instruments-and-bot
See also : Golang : Get a list of crosses(instruments) available to trade from Oanda account
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
+7k Golang : alternative to os.Exit() function
+17.1k Golang : delete and modify XML file content
+10.8k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+11.1k Golang : Fuzzy string search or approximate string matching example
+5.2k Unix/Linux : How to find out the hard disk size?
+7.3k Gogland : Where to put source code files in package directory for rookie
+22.9k Find and replace a character in a string in Go
+11k Golang : Byte format example
+15.8k Golang : How to extract links from web page ?
+10.1k Golang : Allow Cross-Origin Resource Sharing request
+12.2k Golang : Transform comma separated string to slice example