package lmaxapi import ( "tickserver/api/lmaxapi/request" "log" "testing" "time" ) func TestNewRegister(t *testing.T) { req := &request.RegisterRequest{} req.UserName = "niniwzw" req.Password = "password123" req.AccountType = request.AccountTypeStandard req.ProductType = request.ProductType.CFD_DEMO req.Currency = request.CurrencyUSD req.Email = "niniwzw@qq.com" req.Phone = "12345678" req.Title = "Mr." req.FirstName = "king" req.Surname = "wang" SetBaseUrl("http://localhost:8086") err := Register(req) if err == nil { t.Error(err) return } } func login(t *testing.T) *Session { SetBaseUrl("http://localhost:8086") enableLog("d:/lmaxapi.log") req := request.NewLoginRequest("niniwzw", "password123", request.ProductType.CFD_DEMO) session, err := Login(req) if err != nil { t.Error(err) return nil } if session.accountDetails.Username != "niniwzw" { t.Error("account detail error.") } session.RegisterStreamFailureEvent(func(session *Session, err error) { session.Stop() }) return session } func TestNewLogin(t *testing.T) { login(t) } func TestNewSubscribe(t *testing.T) { session := login(t) subReq1 := request.NewHeartbeatSubscriptionRequest() err := session.Subscribe(subReq1, nil) if err != nil { t.Error(err) return } } func BenchmarkOrderMarket(b *testing.B) { SetBaseUrl("http://localhost:8086") enableLog("d:/lmaxapi.log") req := request.NewLoginRequest("niniwzw", "password123", request.ProductType.CFD_DEMO) session, err := Login(req) if err != nil { b.Error(err) return } if session.accountDetails.Username != "niniwzw" { b.Error("account detail error.") return } session.RegisterStreamFailureEvent(func(session *Session, err error) { session.Stop() }) go session.Start() b.ResetTimer() for i := 0; i < b.N; i++ { reqid := getId() + "_10000000" req := request.NewMarketOrderRequest(4001, reqid, request.TimeInForce.IMMEDIATE_OR_CANCEL, 1, 0, 0) id, err := session.PlaceMarketOrder(req, nil) if err != nil { b.Errorf("MarkerOrder Error:", id, err) return } } } func TestNewOrderMarket(t *testing.T) { session := login(t) instructionId := make(chan string, 1) subOrderEvent(session, t, instructionId, "Market") go session.Start() reqid := getId() + "_10000000" req := request.NewMarketOrderRequest(4002, reqid, request.TimeInForce.IMMEDIATE_OR_CANCEL, 1, 0, 0) id, err := session.PlaceMarketOrder(req, nil) if err != nil { t.Errorf("MarkerOrder Error:", err) return } if id != reqid { t.Errorf("MarketOrder instructionId Error:", id) return } getMyId(instructionId, reqid) //修改订单 err = amendOrder(session, t, reqid, instructionId) if err != nil { t.Error("MarkerOrder, amendOrder", err) return } //平仓订单 closedId := getId() creq := request.NewClosingOrderRequest(4001, reqid, closedId, -1) id, err = session.PlaceClosingOrder(creq, nil) if err != nil { t.Errorf("MarkerOrder ClosingOrder Error:", err.Error()) } getMyId(instructionId, closedId) } //批量发送订单。保证按照顺序进入交易所,特殊权限用户使用 func TestNewOrderActions(t *testing.T) { session := login(t) go session.Start() req := NewOrderActionsRequest() reqid := getId() order1 := request.NewMarketOrderRequest(4001, reqid, request.TimeInForce.IMMEDIATE_OR_CANCEL, 1, 0, 0) req.Add(order1) reqid = getId() order2 := request.NewMarketOrderRequest(4001, reqid, request.TimeInForce.IMMEDIATE_OR_CANCEL, 1, 0, 0) req.Add(order2) reqid = getId() creq := request.NewCancelOrderRequest(4001, reqid, "cancel") req.Add(creq) reqid = getId() areq := request.NewAmendStopsOrderRequest(4001, reqid, "amend", 50.0, 50.0) req.Add(areq) reqid = getId() closedreq := request.NewClosingOrderRequest(4001, reqid, "closed", 0.0) req.Add(closedreq) id, err := session.OrderActions(req, nil) log.Println("id = ", id) if err != nil { t.Errorf("send action: ", err.Error()) } } func TestNewOrderLimit(t *testing.T) { session := login(t) instructionId := make(chan string, 1) subOrderEvent(session, t, instructionId, "Limit") go session.Start() reqId := getId() req := request.NewLimitOrderRequest(4001, reqId, request.TimeInForce.GOOD_FOR_DAY, 1.34, 1, 0, 0) id, err := session.PlaceLimitOrder(req, nil) if err != nil { t.Errorf("TestLimitOrder Error:", err) return } if id != reqId { t.Errorf("TestLimitOrder instructionId Error:", id) return } getMyId(instructionId, reqId) //修改订单 err = amendOrder(session, t, reqId, instructionId) if err != nil { t.Error("TestLimitOrder, amendOrder", err) return } //撤销订单 cancelId := getId() creq := request.NewCancelOrderRequest(4001, reqId, cancelId) _, err = session.CancelOrder(creq, nil) if err != nil { t.Errorf("TestLimitOrder CancelOrder Error:", err.Error()) return } getMyId(instructionId, cancelId) } func TestNewOrderCancel(t *testing.T) { session := login(t) instructionId := make(chan string, 1) subOrderEvent(session, t, instructionId, "Limit") go session.Start() reqId := getId() req := request.NewLimitOrderRequest(2001, reqId, request.TimeInForce.GOOD_FOR_DAY, 1.34, 1, 0, 0) id, err := session.PlaceLimitOrder(req, nil) if err != nil { t.Errorf("TestNewOrderCancel Error:", err) return } if id != reqId { t.Errorf("TestNewOrderCancel instructionId Error:", id) return } getMyId(instructionId, reqId) //撤销订单 cancelId := getId() creq := request.NewCancelOrderRequest(2001, reqId, cancelId) _, err = session.CancelOrder(creq, nil) if err != nil { t.Errorf("TestNewOrderCancel CancelOrder Error:", err.Error()) return } getMyId(instructionId, cancelId) } func TestNewOrderPair(t *testing.T) { session := login(t) instructionId := make(chan string, 1) subOrderEvent(session, t, instructionId, "Limit") go session.Start() done := make(chan int) go func() { reqId := getId() req := request.NewLimitOrderRequest(2001, reqId, request.TimeInForce.GOOD_FOR_DAY, 1.34, 1, 0, 0) id, err := session.PlaceLimitOrder(req, nil) if err != nil { t.Errorf("TestNewOrderCancel Error:", err) return } if id != reqId { t.Errorf("TestNewOrderCancel instructionId Error:", id) return } getMyId(instructionId, reqId) done <- 1 }() reqId := getId() req := request.NewLimitOrderRequest(2001, reqId, request.TimeInForce.GOOD_FOR_DAY, 1.34, -1, 0, 0) id, err := session.PlaceLimitOrder(req, nil) if err != nil { t.Errorf("TestNewOrderCancel Error:", err) return } if id != reqId { t.Errorf("TestNewOrderCancel instructionId Error:", id) return } getMyId(instructionId, reqId) <-done } func TestNewHeartBeat(t *testing.T) { w, err := enableLog("d:\\lmaxapi.proto") if err != nil { t.Error(err) return } defer w.Close() session := login(t) subReq1 := request.NewHeartbeatSubscriptionRequest() err = session.Subscribe(subReq1, nil) if err != nil { t.Errorf("Heartbeat sub Error %s", err.Error()) } c := make(chan int, 1) session.RegisterHeartbeatEvent(func(s *Session, id int64, tk string) { log.Println("Receive heartbeat", id, tk) if tk != "hello" { t.Errorf("Heartbeat token Error %s", tk) } session.Stop() c <- 1 }) session.HeartbeatTimeout(time.Second * 1) go session.Start() wait(c, 10*time.Second, t, "Error Heartbeat") } func TestNewSubscribeNotLogin(t *testing.T) { session := NewSession("http://localhost:8086", "", nil) subReq1 := request.NewHeartbeatSubscriptionRequest() err := session.Subscribe(subReq1, nil) if err == nil { t.Error("TestNewSubscribeNotLogin error") return } } func TestNewSearchInstrument2(t *testing.T) { req := request.NewSearchInstrumentRequest("", 0) session := login(t) insList, hasMore, err := session.SearchInstruments(req, nil) if err != nil { t.Errorf("Search Error", err) } log.Printf("Search insList len Error %d\n", len(insList)) if hasMore != false { t.Errorf("Search hasMore Error") } }