项目作者: sklinkert

项目描述 :
Unofficial IG Markets API client for Go/Golang
高级语言: Go
项目地址: git://github.com/sklinkert/igmarkets.git
创建时间: 2018-03-19T22:44:27Z
项目社区:https://github.com/sklinkert/igmarkets

开源协议:MIT License

下载


igmarkets - Unofficial IG Markets Trading API for Golang

This is an unofficial API for IG Markets Trading REST API. The StreamingAPI is not part of this project.

Disclaimer: This library is not associated with IG Markets Limited or any of its affiliates or subsidiaries. If you use this library, you should contact them to make sure they are okay with how you intend to use it. Use this lib at your own risk.

Reference: https://labs.ig.com/rest-trading-api-reference

Currently supported endpoints

Lightstreamer

  • Create session, add subscription(control), bind session

Account

  • GET /accounts
  • GET /accounts/preferences

Session

  • POST /session (version 2 + 3)

Markets

  • GET /markets/{epic}
  • GET /markets?searchTerm=…

Client sentiment

  • GET /clientsentiment/{marketID}

Positions

  • POST /positions/otc
  • PUT /positions/otc/{dealId}
  • GET /positions
  • DELETE /positions
  • GET /confirms/{dealReference}

Workingorders

  • GET /workingorders
  • POST /workingorders/otc
  • DELETE /workingorders/otc/{dealId}

Prices

  • GET /prices/{epic}/{resolution}/{startDate}/{endDate}

Watchlists

  • POST /watchlists/ (Create watchlist)
  • GET /watchlists/{watchlistid}
  • DELETE /watchlists/{watchlistid} (Delete watchlist)

  • GET /watchlists (Get all watchlists)

  • PUT /watchlists/{watchlistid} (Add epic)
  • DELETE /watchlists/{watchlistid}/{epic} (Delete epic)

History

  • GET /history/activity
  • GET /history/transactions

Example

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/sklinkert/igmarkets"
  6. "time"
  7. )
  8. var ig *igmarkets.IGMarkets
  9. func main() {
  10. var ctx = context.Background()
  11. ig = igmarkets.New(igmarkets.DemoAPIURL, "APIKEY", "ACCOUNTID", "USERNAME/IDENTIFIER", "PASSWORD")
  12. if err := ig.Login(ctx); err != nil {
  13. fmt.Println("Unable to login into IG account", err)
  14. }
  15. // Get current open ask, open bid, close ask, close bid, high ask, high bid, low ask, and low bid
  16. prices, _ := ig.GetPrice(ctx, "CS.D.EURUSD.CFD.IP")
  17. fmt.Println(prices)
  18. // Place a new order
  19. order := igmarkets.OTCOrderRequest{
  20. Epic: "CS.D.EURUSD.CFD.IP",
  21. OrderType: "MARKET",
  22. CurrencyCode: "USD",
  23. Direction: "BUY",
  24. Size: 1.0,
  25. Expiry: "-",
  26. StopDistance: "10", // Pips
  27. LimitDistance: "5", // Pips
  28. GuaranteedStop: true,
  29. ForceOpen: true,
  30. }
  31. dealRef, err := ig.PlaceOTCOrder(ctx, order)
  32. if err != nil {
  33. fmt.Println("Unable to place order:", err)
  34. return
  35. }
  36. fmt.Println("New order placed with dealRef", dealRef)
  37. // Check order status
  38. confirmation, err := ig.GetDealConfirmation(ctx, dealRef.DealReference)
  39. if err != nil {
  40. fmt.Println("Cannot get deal confirmation for:", dealRef, err)
  41. return
  42. }
  43. fmt.Println("Order dealRef", dealRef)
  44. fmt.Println("DealStatus", confirmation.DealStatus) // "ACCEPTED"
  45. fmt.Println("Profit", confirmation.Profit, confirmation.ProfitCurrency)
  46. fmt.Println("Status", confirmation.Status) // "OPEN"
  47. fmt.Println("Reason", confirmation.Reason)
  48. fmt.Println("Level", confirmation.Level) // Buy price
  49. // List transactions
  50. transactionResponse, err := ig.GetTransactions(ctx, "ALL", time.Now().AddDate(0, 0, -30).UTC()) // last 30 days
  51. if err != nil {
  52. fmt.Println("Unable to get transactions: ", err)
  53. }
  54. for _, transaction := range transactionResponse.Transactions {
  55. fmt.Println("Found new transaction")
  56. fmt.Println("Epic:", transaction.InstrumentName)
  57. fmt.Println("Type:", transaction.TransactionType)
  58. fmt.Println("OpenDate:", transaction.OpenDateUtc)
  59. fmt.Println("CloseDate:", transaction.DateUTC)
  60. fmt.Println("OpenLevel:", transaction.OpenLevel)
  61. fmt.Println("CloseLevel:", transaction.CloseLevel)
  62. fmt.Println("Profit/Loss:", transaction.ProfitAndLoss)
  63. }
  64. // Example of getting client sentiment
  65. sentiment, _ := ig.GetClientSentiment(ctx, "F-US") //Ford
  66. fmt.Println("Sentiment example:", sentiment)
  67. }

More examples can be found here.

LightStreamer API Subscription Example

  1. var ctx = context.Background()
  2. for {
  3. tickChan := make(chan igmarkets.LightStreamerTick)
  4. err := igHandle.OpenLightStreamerSubscription(ctx, []string{"CS.D.BITCOIN.CFD.IP"}, tickChan)
  5. if err != nil {
  6. log.WithError(err).Error("OpenLightStreamerSubscription() failed")
  7. }
  8. for tick := range tickChan {
  9. log.Infof("tick: %+v", tick)
  10. }
  11. log.Infof("Server closed stream, restarting...")
  12. }

Output:

  1. INFO[0003] tick: {Epic:CS.D.BITCOIN.CFD.IP Time:2020-11-22 14:14:15 +0000 UTC Bid:18230.35 Ask:18266.35}
  2. INFO[0003] tick: {Epic:CS.D.BITCOIN.CFD.IP Time:2020-11-22 14:14:15 +0000 UTC Bid:18230.45 Ask:18266.45}
  3. INFO[0003] tick: {Epic:CS.D.BITCOIN.CFD.IP Time:2020-11-22 14:14:16 +0000 UTC Bid:18231.14 Ask:18267.14}
  4. INFO[0003] tick: {Epic:CS.D.BITCOIN.CFD.IP Time:2020-11-22 14:14:16 +0000 UTC Bid:18231.04 Ask:18267.04}
  5. INFO[0004] tick: {Epic:CS.D.BITCOIN.CFD.IP Time:2020-11-22 14:14:16 +0000 UTC Bid:18231.53 Ask:18267.53}
  6. INFO[0004] tick: {Epic:CS.D.BITCOIN.CFD.IP Time:2020-11-22 14:14:16 +0000 UTC Bid:18231.35 Ask:18267.35}
  7. INFO[0004] tick: {Epic:CS.D.BITCOIN.CFD.IP Time:2020-11-22 14:14:17 +0000 UTC Bid:18230.64 Ask:18266.64}
  8. INFO[0004] tick: {Epic:CS.D.BITCOIN.CFD.IP Time:2020-11-22 14:14:17 +0000 UTC Bid:18231.08 Ask:18267.08}
  9. INFO[0005] tick: {Epic:CS.D.BITCOIN.CFD.IP Time:2020-11-22 14:14:17 +0000 UTC Bid:18231.36 Ask:18267.36}
  10. INFO[0005] tick: {Epic:CS.D.BITCOIN.CFD.IP Time:2020-11-22 14:14:17 +0000 UTC Bid:18230.93 Ask:18266.93}

TODOs

  • Write basic tests

Feel free to send PRs.