app.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package fix
  2. import "fmt"
  3. import "time"
  4. import "log"
  5. type MarketInfoApp struct {
  6. AppBase
  7. ch chan *TickFull
  8. }
  9. func NewMarketInfoApp(ch chan *TickFull) *MarketInfoApp {
  10. app := &MarketInfoApp{}
  11. app.ch = ch
  12. return app
  13. }
  14. var stocks map[string]*StockInfo
  15. func (app *MarketInfoApp) onLogon(tc *AppTradeClient, sid *SessionID) {
  16. tc.MarketData("AUD/CAD", '1')
  17. tc.MarketData("AUD/CHF", '1')
  18. tc.MarketData("AUD/JPY", '1')
  19. tc.MarketData("AUD/NZD", '1')
  20. tc.MarketData("AUD/USD", '1')
  21. tc.MarketData("CAD/JPY", '1')
  22. tc.MarketData("CHF/JPY", '1')
  23. tc.MarketData("EUR/AUD", '1')
  24. tc.MarketData("EUR/CAD", '1')
  25. tc.MarketData("EUR/CHF", '1')
  26. tc.MarketData("EUR/GBP", '1')
  27. tc.MarketData("EUR/JPY", '1')
  28. tc.MarketData("EUR/NZD", '1')
  29. tc.MarketData("EUR/USD", '1')
  30. tc.MarketData("GBP/AUD", '1')
  31. tc.MarketData("GBP/CAD", '1')
  32. tc.MarketData("GBP/CHF", '1')
  33. tc.MarketData("GBP/JPY", '1')
  34. tc.MarketData("GBP/USD", '1')
  35. tc.MarketData("NZD/JPY", '1')
  36. tc.MarketData("NZD/USD", '1')
  37. tc.MarketData("OIL/USD", '1')
  38. tc.MarketData("SGD/JPY", '1')
  39. tc.MarketData("USD/CAD", '1')
  40. tc.MarketData("USD/CHF", '1')
  41. tc.MarketData("USD/JPY", '1')
  42. tc.MarketData("USD/PLN", '1')
  43. tc.MarketData("XAG/USD", '1')
  44. tc.MarketData("XAG/EUR", '1')
  45. tc.MarketData("XAU/USD", '1')
  46. tc.MarketData("XAU/EUR", '1')
  47. tc.MarketData("ZAR/JPY", '1')
  48. /*for i := 1; i <= 17; i++ {
  49. tc.MarketData(fmt.Sprintf("%d", 4000+i), "1")
  50. }
  51. for i := 1; i <= 7; i += 2 {
  52. tc.MarketData(fmt.Sprintf("%d", 100088+i), "1")
  53. }
  54. for i := 1; i <= 57; i += 2 {
  55. tc.MarketData(fmt.Sprintf("%d", 100478+i), "1")
  56. }
  57. */
  58. fmt.Println("onLogon -------------------")
  59. /*
  60. stocks = make(map[string]*StockInfo)
  61. posReqID := tc.ClOrdID()
  62. fmt.Println("test:" , posReqID);
  63. posReqType := 0
  64. account := "20410767"
  65. accountType := 300
  66. tc.RequestForPositions(posReqID ,posReqType ,account ,accountType )
  67. */
  68. }
  69. func (app *MarketInfoApp) fromApp(tc *AppTradeClient, msg *Message, sid *SessionID) {
  70. typevalue, ok := GetMsgType(msg)
  71. if (typevalue == "UAP") && (ok == true) {
  72. /* stockinfo, nLastRptRequested, ok := GetAccountStockInfo(msg)
  73. fmt.Println(stockinfo, nLastRptRequested)
  74. if ok {
  75. stocks[cstring(stockinfo.Symbol[:])] = stockinfo
  76. if nLastRptRequested == 1 {
  77. tc.MarketData(cstring(stockinfo.Symbol[:]), cstring(stockinfo.SecurityExchange[:]))
  78. }
  79. }
  80. */
  81. } else if (typevalue == "UAN") && (ok == true) {
  82. //get value
  83. } else if (typevalue == "W") && (ok == true) {
  84. tick, ok := GetMarketData(msg)
  85. if ok {
  86. //非阻塞模式
  87. select {
  88. case app.ch <- tick:
  89. default:
  90. }
  91. }
  92. }
  93. }
  94. const (
  95. INIT = iota
  96. CREATE
  97. READY
  98. CLOSE
  99. )
  100. //请求的结构体
  101. type OrderRequest struct {
  102. BookID string
  103. ClOrdID string
  104. HandInst byte
  105. Side byte
  106. OrderType byte
  107. Qty float64
  108. Price float64
  109. Account string
  110. SecurityType string
  111. SecurityExchange string
  112. Currency string
  113. }
  114. type OrderStatus struct {
  115. OrderID string
  116. SecondaryExecID string
  117. ClOrdID string
  118. OrigClOrdID string
  119. OrdStatusReqID string
  120. ExecID string
  121. ExecType byte
  122. OrdStatus byte
  123. OrdRejReason int
  124. Account string
  125. SecurityID string
  126. SecurityIDSource string
  127. Side byte
  128. OrdType byte
  129. Price float64
  130. TimeInForce byte
  131. LastQty float64
  132. LastPx float64
  133. LeavesQty float64
  134. CumQty float64
  135. AvgPx float64
  136. Text string
  137. }
  138. type OrderInfo struct {
  139. req *OrderRequest
  140. status *OrderStatus
  141. callback func(status interface{})
  142. }
  143. type OrderApp struct {
  144. AppBase
  145. state int
  146. tc *AppTradeClient
  147. orders map[string]*OrderInfo
  148. }
  149. //订单相关的操作:先完成市价单的处理,然后完成limit单的处理。
  150. //不管市价的单 和 limit单,都是采用异步通知的模式
  151. func NewOrderApp() *OrderApp {
  152. app := &OrderApp{}
  153. app.state = INIT
  154. app.orders = make(map[string]*OrderInfo)
  155. return app
  156. }
  157. func (app *OrderApp) onCreate(tc *AppTradeClient, sid *SessionID) {
  158. fmt.Println("onCreate")
  159. app.state = CREATE
  160. }
  161. func (app *OrderApp) onLogon(tc *AppTradeClient, sid *SessionID) {
  162. fmt.Println("onLogon")
  163. app.tc = tc
  164. app.state = READY
  165. }
  166. func (app *OrderApp) onLogout(tc *AppTradeClient, sid *SessionID) {
  167. fmt.Println("onLogout")
  168. app.state = CLOSE
  169. }
  170. func (app *OrderApp) GetTC() *AppTradeClient {
  171. return app.tc
  172. }
  173. func (app *OrderApp) SendOrder(req *OrderRequest, cb func(interface{})) bool {
  174. //真正发送到服务器的操作
  175. if app.tc == nil || app.state != READY {
  176. return false
  177. }
  178. info := &OrderInfo{}
  179. 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)
  180. if ret {
  181. info.req = req
  182. info.callback = cb
  183. app.orders[req.ClOrdID] = info
  184. }
  185. return ret
  186. }
  187. func (app *OrderApp) TradeCaptureReportRequest(reqId string, reqType int, subType byte, cb func(interface{})) bool {
  188. //真正发送到服务器的操作
  189. if app.tc == nil || app.state != READY {
  190. return false
  191. }
  192. info := &OrderInfo{}
  193. ret := app.tc.TradeCaptureReportRequest(reqId, reqType, subType)
  194. if ret {
  195. info.req = nil
  196. info.callback = cb
  197. app.orders[reqId] = info
  198. }
  199. return ret
  200. }
  201. func (app *OrderApp) CancleOrder(clordId string, qty float64, cb func(interface{})) bool {
  202. if info, ok := app.orders[clordId]; ok {
  203. id := app.tc.ClOrdID()
  204. ret := app.tc.CancleOrder(info.req.BookID, info.req.ClOrdID, id, info.req.Side, qty, info.req.Account, info.req.SecurityType, clordId)
  205. if ret {
  206. req := *info.req
  207. req.ClOrdID = id
  208. info2 := &OrderInfo{}
  209. info2.req = &req
  210. info2.callback = cb
  211. app.orders[req.ClOrdID] = info2
  212. }
  213. return ret
  214. }
  215. return false
  216. }
  217. func (app *OrderApp) StatusOrder(clordId string, cb func(interface{})) bool {
  218. return true
  219. }
  220. func (app *OrderApp) fromApp(tc *AppTradeClient, msg *Message, sid *SessionID) {
  221. log.Println("fromApp go", msg.ToStr())
  222. status, ok := GetOrderStatus(msg)
  223. if ok {
  224. if info, ok := app.orders[status.ClOrdID]; ok {
  225. info.status = status
  226. info.callback(status)
  227. } else {
  228. fmt.Println("error clordid", status.ClOrdID, status)
  229. }
  230. }
  231. }
  232. func (app *OrderApp) WaitReady() {
  233. for {
  234. if app.state == READY {
  235. break
  236. }
  237. time.Sleep(time.Millisecond)
  238. }
  239. }