MarketDataClient.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package main
  2. import (
  3. "tickserver/api/lmaxapi"
  4. "tickserver/api/lmaxapi/main/common"
  5. "tickserver/api/lmaxapi/request"
  6. "tickserver/api/lmaxapi/response"
  7. "log"
  8. "time"
  9. )
  10. func CreateSession() (*lmaxapi.Session, error) {
  11. lmaxapi.SetBaseUrl("https://trade.lmaxtrader.com")
  12. // user, password := "wave1970", "Tg417397"
  13. user, password := "wave3366", "Tg417395"
  14. req := request.NewLoginRequest(user, password, request.ProductType.CFD_LIVE)
  15. session, err := lmaxapi.Login(req)
  16. if err == nil {
  17. log.Println("My accountId is:", session.GetAccountDetails().AccountId)
  18. }
  19. return session, err
  20. }
  21. func CreateSession2() (*lmaxapi.Session, error) {
  22. lmaxapi.SetBaseUrl("http://87.117.229.236:8088")
  23. req := request.NewLoginRequest("wave3366", "Tg417395", request.ProductType.CFD_LIVE)
  24. session, err := lmaxapi.Login(req)
  25. if err == nil {
  26. log.Println("My accountId is:", session.GetAccountDetails().AccountId)
  27. }
  28. return session, err
  29. }
  30. func CreateSession3() (*lmaxapi.Session, error) {
  31. lmaxapi.SetBaseUrl("http://199.188.105.71:8088")
  32. req := request.NewLoginRequest("wave3366", "Tg417395", request.ProductType.CFD_DEMO)
  33. session, err := lmaxapi.Login(req)
  34. if err == nil {
  35. log.Println("My accountId is:", session.GetAccountDetails().AccountId)
  36. }
  37. return session, err
  38. }
  39. func CreateSession6() (*lmaxapi.Session, error) {
  40. lmaxapi.SetBaseUrl("https://210.209.115.182:8088")
  41. req := request.NewLoginRequest("wave3366", "Tg417395", request.ProductType.CFD_DEMO)
  42. session, err := lmaxapi.Login(req)
  43. if err == nil {
  44. log.Println("My accountId is:", session.GetAccountDetails().AccountId)
  45. }
  46. return session, err
  47. }
  48. func CreateSession7() (*lmaxapi.Session, error) {
  49. lmaxapi.SetBaseUrl("https://122.10.93.170:8088")
  50. req := request.NewLoginRequest("wave3366", "Tg417395", request.ProductType.CFD_DEMO)
  51. session, err := lmaxapi.Login(req)
  52. if err == nil {
  53. log.Println("My accountId is:", session.GetAccountDetails().AccountId)
  54. }
  55. return session, err
  56. }
  57. func CreateSession4() (*lmaxapi.Session, error) {
  58. lmaxapi.SetBaseUrl("https://210.209.115.182:8087")
  59. req := request.NewLoginRequest("niniwzw", "P2assword123", request.ProductType.CFD_DEMO)
  60. session, err := lmaxapi.Login(req)
  61. if err == nil {
  62. log.Println("My accountId is:", session.GetAccountDetails().AccountId)
  63. }
  64. return session, err
  65. }
  66. func CreateSession5() (*lmaxapi.Session, error) {
  67. lmaxapi.SetBaseUrl("http://localhost:8088")
  68. req := request.NewLoginRequest("niniwzw", "Password123", request.ProductType.CFD_DEMO)
  69. session, err := lmaxapi.Login(req)
  70. if err == nil {
  71. log.Println("My accountId is:", session.GetAccountDetails().AccountId)
  72. }
  73. return session, err
  74. }
  75. func main() {
  76. log.Println("MarketDataClient")
  77. common.EnableLog("proto.log")
  78. session, err := CreateSession()
  79. if err != nil {
  80. log.Println("Login Failed:", err)
  81. return
  82. }
  83. iMap := make(map[int64]*response.Instrument)
  84. session.RegisterStreamFailureEvent(common.DefaultStreamFailureCB)
  85. var prevPrice float64
  86. session.RegisterOrderBookEvent(func(s *lmaxapi.Session, event *response.OrderBookEvent) {
  87. id := event.InstrumentId
  88. if len(event.BidPrices) > 0 && len(event.AskPrices) > 0 && (iMap[id].Name == "EUR/USD" || iMap[id].Name == "GBP/USD") {
  89. if event.BidPrices[0].Price == prevPrice {
  90. return
  91. }
  92. // prevPrice = event.BidPrices[0].Price
  93. // log.Println(iMap[id].Name, event.BidPrices[0])
  94. }
  95. log.Printf("%+v \n\n", event)
  96. })
  97. session.RegisterHeartbeatEvent(func(s *lmaxapi.Session, id int64, token string) {
  98. //log.Println("heartbeat accountId:", id)
  99. })
  100. // log.Println("begin subscribe")
  101. subs := lmaxapi.NewMultiSubscribe()
  102. session.LoadAllInstruments(func(value *response.Instrument) {
  103. iMap[value.Id] = value
  104. subs.Add(request.NewOrderBookSubscriptionRequest(value.Id))
  105. })
  106. session.Subscribe(subs, func(err error) {
  107. if err != nil {
  108. log.Println(err)
  109. }
  110. })
  111. session.Wait()
  112. // log.Println("end subscribe")
  113. go func() {
  114. time.Sleep(10 * time.Second)
  115. sub := request.NewOrderBookSubscriptionRequest(4001)
  116. session.Unsubscribe(sub, func(err error) {
  117. if err != nil {
  118. log.Println(err)
  119. }
  120. })
  121. }()
  122. common.Heartbeat(session)
  123. session.Start()
  124. }