package fix import "fmt" import "time" import "log" type MarketInfoApp struct { AppBase ch chan *TickFull } func NewMarketInfoApp(ch chan *TickFull) *MarketInfoApp { app := &MarketInfoApp{} app.ch = ch return app } var stocks map[string]*StockInfo func (app *MarketInfoApp) onLogon(tc *AppTradeClient, sid *SessionID) { tc.MarketData("AUD/CAD", '1') tc.MarketData("AUD/CHF", '1') tc.MarketData("AUD/JPY", '1') tc.MarketData("AUD/NZD", '1') tc.MarketData("AUD/USD", '1') tc.MarketData("CAD/JPY", '1') tc.MarketData("CHF/JPY", '1') tc.MarketData("EUR/AUD", '1') tc.MarketData("EUR/CAD", '1') tc.MarketData("EUR/CHF", '1') tc.MarketData("EUR/GBP", '1') tc.MarketData("EUR/JPY", '1') tc.MarketData("EUR/NZD", '1') tc.MarketData("EUR/USD", '1') tc.MarketData("GBP/AUD", '1') tc.MarketData("GBP/CAD", '1') tc.MarketData("GBP/CHF", '1') tc.MarketData("GBP/JPY", '1') tc.MarketData("GBP/USD", '1') tc.MarketData("NZD/JPY", '1') tc.MarketData("NZD/USD", '1') tc.MarketData("OIL/USD", '1') tc.MarketData("SGD/JPY", '1') tc.MarketData("USD/CAD", '1') tc.MarketData("USD/CHF", '1') tc.MarketData("USD/JPY", '1') tc.MarketData("USD/PLN", '1') tc.MarketData("XAG/USD", '1') tc.MarketData("XAG/EUR", '1') tc.MarketData("XAU/USD", '1') tc.MarketData("XAU/EUR", '1') tc.MarketData("ZAR/JPY", '1') /*for i := 1; i <= 17; i++ { tc.MarketData(fmt.Sprintf("%d", 4000+i), "1") } for i := 1; i <= 7; i += 2 { tc.MarketData(fmt.Sprintf("%d", 100088+i), "1") } for i := 1; i <= 57; i += 2 { tc.MarketData(fmt.Sprintf("%d", 100478+i), "1") } */ fmt.Println("onLogon -------------------") /* stocks = make(map[string]*StockInfo) posReqID := tc.ClOrdID() fmt.Println("test:" , posReqID); posReqType := 0 account := "20410767" accountType := 300 tc.RequestForPositions(posReqID ,posReqType ,account ,accountType ) */ } func (app *MarketInfoApp) fromApp(tc *AppTradeClient, msg *Message, sid *SessionID) { typevalue, ok := GetMsgType(msg) if (typevalue == "UAP") && (ok == true) { /* stockinfo, nLastRptRequested, ok := GetAccountStockInfo(msg) fmt.Println(stockinfo, nLastRptRequested) if ok { stocks[cstring(stockinfo.Symbol[:])] = stockinfo if nLastRptRequested == 1 { tc.MarketData(cstring(stockinfo.Symbol[:]), cstring(stockinfo.SecurityExchange[:])) } } */ } else if (typevalue == "UAN") && (ok == true) { //get value } else if (typevalue == "W") && (ok == true) { tick, ok := GetMarketData(msg) if ok { //非阻塞模式 select { case app.ch <- tick: default: } } } } const ( INIT = iota CREATE READY CLOSE ) //请求的结构体 type OrderRequest struct { BookID string ClOrdID string HandInst byte Side byte OrderType byte Qty float64 Price float64 Account string SecurityType string SecurityExchange string Currency string } type OrderStatus struct { OrderID string SecondaryExecID string ClOrdID string OrigClOrdID string OrdStatusReqID string ExecID string ExecType byte OrdStatus byte OrdRejReason int Account string SecurityID string SecurityIDSource string Side byte OrdType byte Price float64 TimeInForce byte LastQty float64 LastPx float64 LeavesQty float64 CumQty float64 AvgPx float64 Text string } type OrderInfo struct { req *OrderRequest status *OrderStatus callback func(status interface{}) } type OrderApp struct { AppBase state int tc *AppTradeClient orders map[string]*OrderInfo } //订单相关的操作:先完成市价单的处理,然后完成limit单的处理。 //不管市价的单 和 limit单,都是采用异步通知的模式 func NewOrderApp() *OrderApp { app := &OrderApp{} app.state = INIT app.orders = make(map[string]*OrderInfo) return app } func (app *OrderApp) onCreate(tc *AppTradeClient, sid *SessionID) { fmt.Println("onCreate") app.state = CREATE } func (app *OrderApp) onLogon(tc *AppTradeClient, sid *SessionID) { fmt.Println("onLogon") app.tc = tc app.state = READY } func (app *OrderApp) onLogout(tc *AppTradeClient, sid *SessionID) { fmt.Println("onLogout") app.state = CLOSE } func (app *OrderApp) GetTC() *AppTradeClient { return app.tc } func (app *OrderApp) SendOrder(req *OrderRequest, cb func(interface{})) bool { //真正发送到服务器的操作 if app.tc == nil || app.state != READY { return false } info := &OrderInfo{} ret := app.tc.SendOrder(req.BookID, req.ClOrdID, req.HandInst, req.Side, req.OrderType, req.Qty, req.Price, req.Account, req.SecurityType, req.SecurityExchange, req.Currency) if ret { info.req = req info.callback = cb app.orders[req.ClOrdID] = info } return ret } func (app *OrderApp) TradeCaptureReportRequest(reqId string, reqType int, subType byte, cb func(interface{})) bool { //真正发送到服务器的操作 if app.tc == nil || app.state != READY { return false } info := &OrderInfo{} ret := app.tc.TradeCaptureReportRequest(reqId, reqType, subType) if ret { info.req = nil info.callback = cb app.orders[reqId] = info } return ret } func (app *OrderApp) CancleOrder(clordId string, qty float64, cb func(interface{})) bool { if info, ok := app.orders[clordId]; ok { id := app.tc.ClOrdID() ret := app.tc.CancleOrder(info.req.BookID, info.req.ClOrdID, id, info.req.Side, qty, info.req.Account, info.req.SecurityType, clordId) if ret { req := *info.req req.ClOrdID = id info2 := &OrderInfo{} info2.req = &req info2.callback = cb app.orders[req.ClOrdID] = info2 } return ret } return false } func (app *OrderApp) StatusOrder(clordId string, cb func(interface{})) bool { return true } func (app *OrderApp) fromApp(tc *AppTradeClient, msg *Message, sid *SessionID) { log.Println("fromApp go", msg.ToStr()) status, ok := GetOrderStatus(msg) if ok { if info, ok := app.orders[status.ClOrdID]; ok { info.status = status info.callback(status) } else { fmt.Println("error clordid", status.ClOrdID, status) } } } func (app *OrderApp) WaitReady() { for { if app.state == READY { break } time.Sleep(time.Millisecond) } }