package main import ( "lmaxapi" "log" "tickserver/api/lmaxapi/request" "tickserver/api/lmaxapi/response" "time" "os" ) func main() { logw, e := os.Create("positionTracking.log") if e != nil { log.Println(e) return } lmaxapi.EnableLog(logw) defer logw.Close() lmaxapi.SetBaseUrl("https://trade.lmaxtrader.com") session := lmaxapi.NewSession("", "", nil) //跟踪所有的订单,订单更新都会在这里执行,包括订单的价格的改动 session.GetOrderBook().SetOrderBookStatusEvent(func (status string, event *response.OrderBookStatusEvent) { log.Println(status, event) }) session.GetOrderBook().SetOrderEvent(func (status string, order *response.OrderEvent) { //log.Println(status, order) }) session.GetOrderBook().SetRejectedEvent(func (status string, rejected *response.InstructionRejectedEvent) { log.Println(status, rejected) }) session.GetOrderBook().SetPositionEvent(func (status string, position *response.PositionEvent) { //lmaxapi.PrintStruct(position) }) session.GetOrderBook().SetAccountEvent(func (status string, account *response.AccountStateEvent) { log.Println(status, account) }) session.GetOrderBook().SetOrderBookEvent(func (event *response.OrderBookEvent) { var prevPrice float64 if len(event.BidPrices) > 0 && len(event.AskPrices) > 0 && event.InstrumentId == 4001 { if (event.BidPrices[0].Price == prevPrice) { return } prevPrice = event.BidPrices[0].Price //log.Println(4001, event.BidPrices[0]) } }) //事件主循环 go func() { for { err := session.Login("wave3366", "Tg417395", request.ProductType.CFD_LIVE) //err := session.Login("niniwzw", "Password123", request.ProductType.CFD_DEMO) if err != nil { log.Println(err) time.Sleep(1 * time.Second) continue } log.Println("login success.") //这些事件要重新添加与注册 lmaxapi.PositionTrackingInit(session) err = lmaxapi.PositionTrackingSub(session) if err != nil { log.Println(err) time.Sleep(1 * time.Second) continue } session.HeartbeatTimeout(4 * time.Second) session.Start() } }() for !session.Isrunning() { time.Sleep(1 * time.Second) } //下了一个market 订单,并跟踪这个订单 sendMarketOrderAndClose(session) time.Sleep(time.Second) //下一个limit订单,然后撤销这个挂单 sendLimitOrderAndCancal(session) time.Sleep(time.Second) //下一个limit订单成交,然后平仓 sendLimitOrderAndClose(session) select{} } func sendMarketOrderAndClose(session *lmaxapi.Session) { o := request.NewMarketOrderRequest(100437, "123456789012345678900", request.TimeInForce.IMMEDIATE_OR_CANCEL, 1, 0, 0) id, err := session.PlaceMarketOrder(o, nil) if err != nil { log.Println(err) } //跟踪某个订单全过程,包括价格改动,订单修改等 session.GetOrderBook().AddEvent(id, func (status string, order *response.OrderEvent) { log.Println(status, order.Margin, order.Profit) }) time.Sleep(2 * time.Second) //对这个订单进行平仓的操作 id, err = session.PlaceClosingOrder(request.NewClosingOrderRequest(100437, id, "", -1), nil) log.Println(id, err) if err != nil { log.Println(err) } } func sendLimitOrderAndCancal(session *lmaxapi.Session) { offset, err := session.GetOrderBook().GetOffsetByPoint(4001, 5) if err != nil { log.Println(err) } id, err := session.PlaceLimitOrder(request.NewLimitOrderRequest(4001, "", request.TimeInForce.GOOD_FOR_DAY, 1.36, -0.1, offset, offset), nil) log.Println(id, err) if err != nil { log.Println(err) } session.GetOrderBook().AddEvent(id, func (status string, order *response.OrderEvent) { if status != "TickUpdate" { log.Println("sendLimitOrderAndCancal", status, order) } }) session.GetOrderBook().AddRejectEvent(id, func (status string, rejected *response.InstructionRejectedEvent) { log.Println("sendLimitOrderAndCancal:", status, rejected) }) /* time.Sleep(2 * time.Second) id, err = session.PlaceLimitOrder(request.NewCancelOrderRequest(100437, id, ""), nil) log.Println(id, err) if err != nil { log.Println(err) } */ } func sendLimitOrderAndClose(session *lmaxapi.Session) { offset, err := session.GetOrderBook().GetOffsetByPoint(100437, 100) if err != nil { log.Println(err) } id, err := session.PlaceLimitOrder(request.NewLimitOrderRequest(100437, "", request.TimeInForce.GOOD_FOR_DAY, 1.32, -1, offset, offset), nil) log.Println(id, err) if err != nil { log.Println(err) } session.GetOrderBook().AddEvent(id, func (status string, order *response.OrderEvent) { if status != "TickUpdate" { log.Println("AddEvent", status, order.Margin, order.Profit) } }) time.Sleep(2 * time.Second) id, err = session.PlaceClosingOrder(request.NewClosingOrderRequest(100437, id, "", 1), nil) log.Println(id, err) if err != nil { log.Println(err) } }