order_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package lmaxapi
  2. import (
  3. "fmt"
  4. "tickserver/api/lmaxapi/request"
  5. "tickserver/api/lmaxapi/response"
  6. "log"
  7. "testing"
  8. "time"
  9. )
  10. func getMyId(instructionId chan string, id string) string {
  11. for {
  12. instId := <-instructionId
  13. if instId == id {
  14. return <-instructionId
  15. }
  16. }
  17. }
  18. func subOrderEvent(session *Session, t *testing.T, instructionId chan string, name string) {
  19. req := request.NewOrderSubscriptionRequest()
  20. err := session.Subscribe(req, nil)
  21. if err != nil {
  22. t.Errorf("sub OrderEvent Error:", err.Error())
  23. }
  24. err = session.Subscribe(request.NewOrderBookSubscriptionRequest(4001), nil)
  25. if err != nil {
  26. t.Errorf("sub OrderBookEvent Error:", err.Error())
  27. }
  28. session.RegisterOrderEvent(func(s *Session, event *response.OrderEvent) {
  29. fmt.Println("OrderEvent send beg", event.InstructionId)
  30. instructionId <- event.InstructionId
  31. instructionId <- event.OrderId
  32. fmt.Println("OrderEvent send end", event.InstructionId)
  33. })
  34. req2 := request.NewExecutionSubscriptionRequest()
  35. err = session.Subscribe(req2, nil)
  36. if err != nil {
  37. t.Errorf("Execution Error:", err.Error())
  38. }
  39. session.RegisterExecutionEvent(func(s *Session, event *response.ExecutionEvent) {
  40. fmt.Println("Receive ExecutionEvent", event.InstructionId)
  41. })
  42. session.RegisterStreamFailureEvent(func(s *Session, err error) {
  43. t.Errorf(err.Error())
  44. s.Stop()
  45. })
  46. session.RegisterInstructionRejectedEvent(func(s *Session, event *response.InstructionRejectedEvent) {
  47. if name != "Rejected" {
  48. t.Error("Error Rejected InstructionId", event.Reason)
  49. }
  50. log.Println("Error Rejected InstructionId", event)
  51. panic("Rejected")
  52. instructionId <- event.InstructionId
  53. instructionId <- "not_order"
  54. })
  55. fmt.Println("event setting ok.")
  56. }
  57. func TestOrderMarket(t *testing.T) {
  58. session := createSession()
  59. c := make(chan int, 1)
  60. instructionId := make(chan string, 1)
  61. subOrderEvent(session, t, instructionId, "Market")
  62. go session.Start()
  63. reqid := getId() + "_10000000"
  64. req := request.NewMarketOrderRequest(4001, reqid, request.TimeInForce.IMMEDIATE_OR_CANCEL, 1, 0, 0)
  65. id, err := session.PlaceMarketOrder(req, nil)
  66. if err != nil {
  67. t.Errorf("MarkerOrder Error:", err)
  68. }
  69. if id != reqid {
  70. t.Errorf("MarketOrder instructionId Error:", id)
  71. }
  72. getMyId(instructionId, reqid)
  73. //修改订单
  74. err = amendOrder(session, t, reqid, instructionId)
  75. if err != nil {
  76. t.Error("MarkerOrder, amendOrder", err)
  77. return
  78. }
  79. //平仓订单
  80. closedId := getId()
  81. creq := request.NewClosingOrderRequest(4001, reqid, closedId, -1)
  82. id, err = session.PlaceClosingOrder(creq, nil)
  83. if err != nil {
  84. t.Errorf("MarkerOrder ClosingOrder Error:", err.Error())
  85. }
  86. getMyId(instructionId, closedId)
  87. c <- 1
  88. wait(c, 10*time.Second, t, "MarketOrder")
  89. }
  90. func TestOrderLimit(t *testing.T) {
  91. session := createSession()
  92. c := make(chan int, 1)
  93. instructionId := make(chan string, 1)
  94. subOrderEvent(session, t, instructionId, "Limit")
  95. go session.Start()
  96. reqId := getId()
  97. req := request.NewLimitOrderRequest(4001, reqId, request.TimeInForce.GOOD_FOR_DAY, 1.34, 1, 0, 0)
  98. id, err := session.PlaceLimitOrder(req, nil)
  99. if err != nil {
  100. t.Errorf("TestLimitOrder Error:", err)
  101. return
  102. }
  103. if id != reqId {
  104. t.Errorf("TestLimitOrder instructionId Error:", id)
  105. return
  106. }
  107. getMyId(instructionId, reqId)
  108. //修改订单
  109. err = amendOrder(session, t, reqId, instructionId)
  110. if err != nil {
  111. t.Error("TestLimitOrder, amendOrder", err)
  112. return
  113. }
  114. //撤销订单
  115. cancelId := getId()
  116. creq := request.NewCancelOrderRequest(4001, reqId, cancelId)
  117. _, err = session.CancelOrder(creq, nil)
  118. if err != nil {
  119. t.Errorf("TestLimitOrder CancelOrder Error:", err.Error())
  120. return
  121. }
  122. getMyId(instructionId, cancelId)
  123. c <- 1
  124. wait(c, 10*time.Second, t, "TestLimitOrder")
  125. }
  126. func TestOrderStop(t *testing.T) {
  127. session := createSession()
  128. c := make(chan int, 1)
  129. instructionId := make(chan string, 1)
  130. subOrderEvent(session, t, instructionId, "Limit")
  131. go session.Start()
  132. reqId := getId()
  133. req := request.NewStopOrderRequest(4001, reqId, request.TimeInForce.GOOD_FOR_DAY, 1.38, 1, 0, 0)
  134. id, err := session.PlaceStopOrder(req, nil)
  135. if err != nil {
  136. t.Errorf("TestOrderStop Error:", err)
  137. return
  138. }
  139. if id != reqId {
  140. t.Errorf("TestOrderStop instructionId Error:", id)
  141. return
  142. }
  143. getMyId(instructionId, reqId)
  144. //修改订单
  145. fmt.Println("TestOrderStop place order success.")
  146. err = amendOrder(session, t, reqId, instructionId)
  147. if err != nil {
  148. t.Error("TestOrderStop, amendOrder", err)
  149. return
  150. }
  151. fmt.Println("TestOrderStop amend order success.")
  152. //撤销订单
  153. cancelId := getId()
  154. creq := request.NewCancelOrderRequest(4001, reqId, cancelId)
  155. _, err = session.CancelOrder(creq, nil)
  156. if err != nil {
  157. t.Errorf("TestOrderStop CancelOrder Error:", err.Error())
  158. return
  159. }
  160. getMyId(instructionId, cancelId)
  161. c <- 1
  162. wait(c, 10*time.Second, t, "TestOrderStop")
  163. }
  164. func amendOrder(session *Session, t *testing.T, refId string, instructionId chan string) error {
  165. amend := getId()
  166. creq := request.NewAmendStopsOrderRequest(4001, refId, amend, 0.005, 0.005)
  167. _, err := session.PlaceAmendStopsOrder(creq, nil)
  168. if err != nil {
  169. return err
  170. }
  171. getMyId(instructionId, amend)
  172. return nil
  173. }
  174. func TestOrderRejectEvent(t *testing.T) {
  175. session := createSession()
  176. c := make(chan int, 1)
  177. instructionId := make(chan string, 1)
  178. subOrderEvent(session, t, instructionId, "Rejected")
  179. go session.Start()
  180. reqId := getId()
  181. req := request.NewLimitOrderRequest(4001, reqId, request.TimeInForce.GOOD_FOR_DAY, 100, 1, 0, 0)
  182. _, err := session.PlaceLimitOrder(req, nil)
  183. if err != nil {
  184. t.Errorf("TestOrderRejectEvent Error:", err.Error())
  185. return
  186. }
  187. getMyId(instructionId, reqId)
  188. c <- 1
  189. wait(c, 10*time.Second, t, "TestOrderRejectEvent")
  190. }