123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- 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")
- }
- }
|