PositionTrackingClient.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package main
  2. import (
  3. "lmaxapi"
  4. "log"
  5. "tickserver/api/lmaxapi/request"
  6. "tickserver/api/lmaxapi/response"
  7. "time"
  8. "os"
  9. )
  10. func main() {
  11. logw, e := os.Create("positionTracking.log")
  12. if e != nil {
  13. log.Println(e)
  14. return
  15. }
  16. lmaxapi.EnableLog(logw)
  17. defer logw.Close()
  18. lmaxapi.SetBaseUrl("https://trade.lmaxtrader.com")
  19. session := lmaxapi.NewSession("", "", nil)
  20. //跟踪所有的订单,订单更新都会在这里执行,包括订单的价格的改动
  21. session.GetOrderBook().SetOrderBookStatusEvent(func (status string, event *response.OrderBookStatusEvent) {
  22. log.Println(status, event)
  23. })
  24. session.GetOrderBook().SetOrderEvent(func (status string, order *response.OrderEvent) {
  25. //log.Println(status, order)
  26. })
  27. session.GetOrderBook().SetRejectedEvent(func (status string, rejected *response.InstructionRejectedEvent) {
  28. log.Println(status, rejected)
  29. })
  30. session.GetOrderBook().SetPositionEvent(func (status string, position *response.PositionEvent) {
  31. //lmaxapi.PrintStruct(position)
  32. })
  33. session.GetOrderBook().SetAccountEvent(func (status string, account *response.AccountStateEvent) {
  34. log.Println(status, account)
  35. })
  36. session.GetOrderBook().SetOrderBookEvent(func (event *response.OrderBookEvent) {
  37. var prevPrice float64
  38. if len(event.BidPrices) > 0 && len(event.AskPrices) > 0 && event.InstrumentId == 4001 {
  39. if (event.BidPrices[0].Price == prevPrice) {
  40. return
  41. }
  42. prevPrice = event.BidPrices[0].Price
  43. //log.Println(4001, event.BidPrices[0])
  44. }
  45. })
  46. //事件主循环
  47. go func() {
  48. for {
  49. err := session.Login("wave3366", "Tg417395", request.ProductType.CFD_LIVE)
  50. //err := session.Login("niniwzw", "Password123", request.ProductType.CFD_DEMO)
  51. if err != nil {
  52. log.Println(err)
  53. time.Sleep(1 * time.Second)
  54. continue
  55. }
  56. log.Println("login success.")
  57. //这些事件要重新添加与注册
  58. lmaxapi.PositionTrackingInit(session)
  59. err = lmaxapi.PositionTrackingSub(session)
  60. if err != nil {
  61. log.Println(err)
  62. time.Sleep(1 * time.Second)
  63. continue
  64. }
  65. session.HeartbeatTimeout(4 * time.Second)
  66. session.Start()
  67. }
  68. }()
  69. for !session.Isrunning() {
  70. time.Sleep(1 * time.Second)
  71. }
  72. //下了一个market 订单,并跟踪这个订单
  73. sendMarketOrderAndClose(session)
  74. time.Sleep(time.Second)
  75. //下一个limit订单,然后撤销这个挂单
  76. sendLimitOrderAndCancal(session)
  77. time.Sleep(time.Second)
  78. //下一个limit订单成交,然后平仓
  79. sendLimitOrderAndClose(session)
  80. select{}
  81. }
  82. func sendMarketOrderAndClose(session *lmaxapi.Session) {
  83. o := request.NewMarketOrderRequest(100437, "123456789012345678900", request.TimeInForce.IMMEDIATE_OR_CANCEL, 1, 0, 0)
  84. id, err := session.PlaceMarketOrder(o, nil)
  85. if err != nil {
  86. log.Println(err)
  87. }
  88. //跟踪某个订单全过程,包括价格改动,订单修改等
  89. session.GetOrderBook().AddEvent(id, func (status string, order *response.OrderEvent) {
  90. log.Println(status, order.Margin, order.Profit)
  91. })
  92. time.Sleep(2 * time.Second)
  93. //对这个订单进行平仓的操作
  94. id, err = session.PlaceClosingOrder(request.NewClosingOrderRequest(100437, id, "", -1), nil)
  95. log.Println(id, err)
  96. if err != nil {
  97. log.Println(err)
  98. }
  99. }
  100. func sendLimitOrderAndCancal(session *lmaxapi.Session) {
  101. offset, err := session.GetOrderBook().GetOffsetByPoint(4001, 5)
  102. if err != nil {
  103. log.Println(err)
  104. }
  105. id, err := session.PlaceLimitOrder(request.NewLimitOrderRequest(4001, "",
  106. request.TimeInForce.GOOD_FOR_DAY, 1.36, -0.1, offset, offset), nil)
  107. log.Println(id, err)
  108. if err != nil {
  109. log.Println(err)
  110. }
  111. session.GetOrderBook().AddEvent(id, func (status string, order *response.OrderEvent) {
  112. if status != "TickUpdate" {
  113. log.Println("sendLimitOrderAndCancal", status, order)
  114. }
  115. })
  116. session.GetOrderBook().AddRejectEvent(id, func (status string, rejected *response.InstructionRejectedEvent) {
  117. log.Println("sendLimitOrderAndCancal:", status, rejected)
  118. })
  119. /*
  120. time.Sleep(2 * time.Second)
  121. id, err = session.PlaceLimitOrder(request.NewCancelOrderRequest(100437, id, ""), nil)
  122. log.Println(id, err)
  123. if err != nil {
  124. log.Println(err)
  125. }
  126. */
  127. }
  128. func sendLimitOrderAndClose(session *lmaxapi.Session) {
  129. offset, err := session.GetOrderBook().GetOffsetByPoint(100437, 100)
  130. if err != nil {
  131. log.Println(err)
  132. }
  133. id, err := session.PlaceLimitOrder(request.NewLimitOrderRequest(100437, "",
  134. request.TimeInForce.GOOD_FOR_DAY, 1.32, -1, offset, offset), nil)
  135. log.Println(id, err)
  136. if err != nil {
  137. log.Println(err)
  138. }
  139. session.GetOrderBook().AddEvent(id, func (status string, order *response.OrderEvent) {
  140. if status != "TickUpdate" {
  141. log.Println("AddEvent", status, order.Margin, order.Profit)
  142. }
  143. })
  144. time.Sleep(2 * time.Second)
  145. id, err = session.PlaceClosingOrder(request.NewClosingOrderRequest(100437, id, "", 1), nil)
  146. log.Println(id, err)
  147. if err != nil {
  148. log.Println(err)
  149. }
  150. }