123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- package lmaxapi
- import (
- "fmt"
- "tickserver/api/lmaxapi/request"
- "tickserver/api/lmaxapi/response"
- "log"
- "testing"
- "time"
- )
- func getMyId(instructionId chan string, id string) string {
- for {
- instId := <-instructionId
- if instId == id {
- return <-instructionId
- }
- }
- }
- func subOrderEvent(session *Session, t *testing.T, instructionId chan string, name string) {
- req := request.NewOrderSubscriptionRequest()
- err := session.Subscribe(req, nil)
- if err != nil {
- t.Errorf("sub OrderEvent Error:", err.Error())
- }
- err = session.Subscribe(request.NewOrderBookSubscriptionRequest(4001), nil)
- if err != nil {
- t.Errorf("sub OrderBookEvent Error:", err.Error())
- }
- session.RegisterOrderEvent(func(s *Session, event *response.OrderEvent) {
- fmt.Println("OrderEvent send beg", event.InstructionId)
- instructionId <- event.InstructionId
- instructionId <- event.OrderId
- fmt.Println("OrderEvent send end", event.InstructionId)
- })
- req2 := request.NewExecutionSubscriptionRequest()
- err = session.Subscribe(req2, nil)
- if err != nil {
- t.Errorf("Execution Error:", err.Error())
- }
- session.RegisterExecutionEvent(func(s *Session, event *response.ExecutionEvent) {
- fmt.Println("Receive ExecutionEvent", event.InstructionId)
- })
- session.RegisterStreamFailureEvent(func(s *Session, err error) {
- t.Errorf(err.Error())
- s.Stop()
- })
- session.RegisterInstructionRejectedEvent(func(s *Session, event *response.InstructionRejectedEvent) {
- if name != "Rejected" {
- t.Error("Error Rejected InstructionId", event.Reason)
- }
- log.Println("Error Rejected InstructionId", event)
- panic("Rejected")
- instructionId <- event.InstructionId
- instructionId <- "not_order"
- })
- fmt.Println("event setting ok.")
- }
- func TestOrderMarket(t *testing.T) {
- session := createSession()
- c := make(chan int, 1)
- instructionId := make(chan string, 1)
- subOrderEvent(session, t, instructionId, "Market")
- go session.Start()
- 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 {
- t.Errorf("MarkerOrder Error:", err)
- }
- if id != reqid {
- t.Errorf("MarketOrder instructionId Error:", id)
- }
- 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)
- c <- 1
- wait(c, 10*time.Second, t, "MarketOrder")
- }
- func TestOrderLimit(t *testing.T) {
- session := createSession()
- c := make(chan int, 1)
- 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)
- c <- 1
- wait(c, 10*time.Second, t, "TestLimitOrder")
- }
- func TestOrderStop(t *testing.T) {
- session := createSession()
- c := make(chan int, 1)
- instructionId := make(chan string, 1)
- subOrderEvent(session, t, instructionId, "Limit")
- go session.Start()
- reqId := getId()
- req := request.NewStopOrderRequest(4001, reqId, request.TimeInForce.GOOD_FOR_DAY, 1.38, 1, 0, 0)
- id, err := session.PlaceStopOrder(req, nil)
- if err != nil {
- t.Errorf("TestOrderStop Error:", err)
- return
- }
- if id != reqId {
- t.Errorf("TestOrderStop instructionId Error:", id)
- return
- }
- getMyId(instructionId, reqId)
- //修改订单
- fmt.Println("TestOrderStop place order success.")
- err = amendOrder(session, t, reqId, instructionId)
- if err != nil {
- t.Error("TestOrderStop, amendOrder", err)
- return
- }
- fmt.Println("TestOrderStop amend order success.")
- //撤销订单
- cancelId := getId()
- creq := request.NewCancelOrderRequest(4001, reqId, cancelId)
- _, err = session.CancelOrder(creq, nil)
- if err != nil {
- t.Errorf("TestOrderStop CancelOrder Error:", err.Error())
- return
- }
- getMyId(instructionId, cancelId)
- c <- 1
- wait(c, 10*time.Second, t, "TestOrderStop")
- }
- func amendOrder(session *Session, t *testing.T, refId string, instructionId chan string) error {
- amend := getId()
- creq := request.NewAmendStopsOrderRequest(4001, refId, amend, 0.005, 0.005)
- _, err := session.PlaceAmendStopsOrder(creq, nil)
- if err != nil {
- return err
- }
- getMyId(instructionId, amend)
- return nil
- }
- func TestOrderRejectEvent(t *testing.T) {
- session := createSession()
- c := make(chan int, 1)
- instructionId := make(chan string, 1)
- subOrderEvent(session, t, instructionId, "Rejected")
- go session.Start()
- reqId := getId()
- req := request.NewLimitOrderRequest(4001, reqId, request.TimeInForce.GOOD_FOR_DAY, 100, 1, 0, 0)
- _, err := session.PlaceLimitOrder(req, nil)
- if err != nil {
- t.Errorf("TestOrderRejectEvent Error:", err.Error())
- return
- }
- getMyId(instructionId, reqId)
- c <- 1
- wait(c, 10*time.Second, t, "TestOrderRejectEvent")
- }
|