func NewTestClient(c *check.C, selectAccount bool) *oanda.Client { envName := "FXPRACTICE_TOKEN" token := os.Getenv(envName) if token == "" { c.Skip(fmt.Sprintf("Environment variable %s is not defined", envName)) return nil } client, err := oanda.NewFxPracticeClient(token) c.Assert(err, check.IsNil) if !selectAccount { return client } envName = "FXPRACTICE_ACCOUNT" accountIdStr := os.Getenv(envName) if accountIdStr == "" { c.Skip(fmt.Sprintf("Environment variable %s is not defined", envName)) return nil } accountId, err := strconv.Atoi(accountIdStr) c.Assert(err, check.IsNil) client.SelectAccount(accountId) return client }
func main() { flag.Parse() if *token == "" { panic("An Oanda authorization token is required") } if *account == 0 { panic("An Oanda account is required") } client, err := oanda.NewFxPracticeClient(*token) if err != nil { panic(err) } client.SelectAccount(oanda.Id(*account)) // List available instruments instruments, err := client.Instruments(nil, nil) if err != nil { panic(err) } fmt.Println(instruments) // Buy one unit of EUR/USD with a trailing stop of 10 pips. tradeInfo, err := client.NewTrade(oanda.Buy, 1, "eur_usd", oanda.TrailingStop(10.0)) if err != nil { panic(err) } fmt.Println(tradeInfo) // Create and run a price server. priceServer, err := client.NewPriceServer("eur_usd") if err != nil { panic(err) } priceServer.ConnectAndHandle(func(instrument string, tick oanda.PriceTick) { fmt.Println("Received tick:", instrument, tick) priceServer.Stop() }) // Close the previously opened trade. tradeCloseInfo, err := client.CloseTrade(tradeInfo.TradeId) if err != nil { panic(err) } fmt.Println(tradeCloseInfo) }
func (s *OandaSuite) SetUpSuite(c *check.C) { envName := "FXPRACTICE_TOKEN" token := os.Getenv(envName) if token == "" { c.Skip(fmt.Sprintf("Environment variable %s is not defined", envName)) return } time.Sleep(2 * time.Second) client, err := oanda.NewFxPracticeClient(token) c.Assert(err, check.IsNil) c.Assert(client, check.NotNil) s.Client = client }