123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- 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)
- }
- }
|