fix.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. package fix
  2. /*
  3. #cgo CFLAGS: -I./include
  4. #cgo LDFLAGS: -L./lib -lcfix
  5. #cgo linux LDFLAGS: -L/usr/lib -lquickfix -lsignalsync -lfix -lstdc++ -lboost_system -lboost_chrono -lboost_thread
  6. #include <cfunc.h>
  7. #include <stdlib.h>
  8. extern void message_callback_go(void* foo, void* trade, const void* msg, void* sid);
  9. void * message_callback (void *pfunc, AppTradeClient* trade, const Message* msg, SessionID* sid) {
  10. message_callback_go(pfunc, trade, msg, sid);
  11. return 0;
  12. }
  13. void * get_callback() {
  14. return message_callback;
  15. }
  16. */
  17. import "C"
  18. import "fmt"
  19. import "unsafe"
  20. import "log"
  21. import "time"
  22. /*
  23. func PrintHello(strs []string) {
  24. ss := make([]*C.char, len(strs))
  25. for i := 0; i < len(strs); i++ {
  26. ss[i] = C.CString(strs[i])
  27. }
  28. C.PrintHello((**C.char)(unsafe.Pointer(&ss[0])), C.int(len(strs)))
  29. for i := 0; i < len(strs); i++ {
  30. C.free(unsafe.Pointer(ss[i]))
  31. }
  32. }
  33. */
  34. type SessionSettings struct {
  35. obj *C.struct_SessionSettings
  36. }
  37. type IApplication interface {
  38. /// Notification of a session begin created
  39. onCreate(*AppTradeClient, *SessionID)
  40. /// Notification of a session successfully logging on
  41. onLogon(*AppTradeClient, *SessionID)
  42. /// Notification of a session logging off or disconnecting
  43. onLogout(*AppTradeClient, *SessionID)
  44. /// Notification of admin message being sent to target
  45. toAdmin(*AppTradeClient, *Message, *SessionID)
  46. /// Notification of app message being sent to target
  47. toApp(*AppTradeClient, *Message, *SessionID)
  48. /// Notification of admin message being received from target
  49. fromAdmin(*AppTradeClient, *Message, *SessionID)
  50. /// Notification of app message being received from target
  51. fromApp(*AppTradeClient, *Message, *SessionID)
  52. }
  53. type AppTradeClient struct {
  54. obj *C.struct_AppTradeClient
  55. app IApplication
  56. iapp *Application
  57. setting *SessionSettings
  58. onCreate func(unsafe.Pointer, unsafe.Pointer)
  59. /// Notification of a session successfully logging on
  60. onLogon func(unsafe.Pointer, unsafe.Pointer)
  61. /// Notification of a session logging off or disconnecting
  62. onLogout func(unsafe.Pointer, unsafe.Pointer)
  63. /// Notification of admin message being sent to target
  64. toAdmin func(unsafe.Pointer, unsafe.Pointer, unsafe.Pointer)
  65. /// Notification of app message being sent to target
  66. toApp func(unsafe.Pointer, unsafe.Pointer, unsafe.Pointer)
  67. /// Notification of admin message being received from target
  68. fromAdmin func(unsafe.Pointer, unsafe.Pointer, unsafe.Pointer)
  69. /// Notification of app message being received from target
  70. fromApp func(unsafe.Pointer, unsafe.Pointer, unsafe.Pointer)
  71. }
  72. type AppBase struct {
  73. }
  74. /// Notification of a session begin created
  75. func (app *AppBase) onCreate(tc *AppTradeClient, sid *SessionID) {
  76. log.Println("onCreate go")
  77. }
  78. /// Notification of a session successfully logging on
  79. func (app *AppBase) onLogon(tc *AppTradeClient, sid *SessionID) {
  80. log.Println("onLogon go")
  81. }
  82. /// Notification of a session logging off or disconnecting
  83. func (app *AppBase) onLogout(tc *AppTradeClient, sid *SessionID) {
  84. log.Println("onLogout go")
  85. }
  86. /// Notification of admin message being sent to target
  87. func (app *AppBase) toAdmin(tc *AppTradeClient, msg *Message, sid *SessionID) {
  88. log.Println("toAdmin go", msg.ToStr())
  89. }
  90. /// Notification of app message being sent to target
  91. func (app *AppBase) toApp(tc *AppTradeClient, msg *Message, sid *SessionID) {
  92. log.Println("toApp go", msg.ToStr())
  93. }
  94. /// Notification of admin message being received from target
  95. func (app *AppBase) fromAdmin(tc *AppTradeClient, msg *Message, sid *SessionID) {
  96. log.Println("fromAdmin go", msg.ToStr())
  97. }
  98. /// Notification of app message being received from target
  99. func (app *AppBase) fromApp(tc *AppTradeClient, msg *Message, sid *SessionID) {
  100. log.Println("fromApp go", msg.ToStr())
  101. }
  102. type Message struct {
  103. obj *C.struct_Message
  104. }
  105. type SessionID struct {
  106. obj *C.struct_SessionID
  107. }
  108. type Dictionary struct {
  109. obj *C.struct_Dictionary
  110. }
  111. type Application struct {
  112. obj *C.IApplication
  113. }
  114. const MAX_BID_ASK_COUNT = 25
  115. type TickFull struct {
  116. AskPrice [MAX_BID_ASK_COUNT]float64
  117. BidPrice [MAX_BID_ASK_COUNT]float64
  118. AskVolume [MAX_BID_ASK_COUNT]float64
  119. BidVolume [MAX_BID_ASK_COUNT]float64
  120. Symbol [8]byte
  121. Time int32
  122. Millisecond int32
  123. AskCount int32
  124. BidCount int32
  125. }
  126. type StockInfo struct {
  127. Symbol [15]byte
  128. Position int32
  129. SecurityExchange [10]byte
  130. }
  131. type orderInfo struct {
  132. OrderID [32]byte //Unique identifier for Order as assigned by exchange
  133. SecondaryExecID [32]byte
  134. ClOrdID [32]byte //Client side order identifier
  135. OrigClOrdID [32]byte
  136. OrdStatusReqID [32]byte
  137. ExecID [32]byte //Execution ID for this fill.
  138. Account [16]byte //Account ID of the current logged in LMAX member
  139. SecurityID [8]byte
  140. AvgPx float64 //Calculated average price of all fills on this order.
  141. CumQty float64 //Contains the cumulated traded quantity for the order through its life.
  142. LastPx float64 //Price of this fill
  143. LastQty float64 //
  144. OrderQty float64 //Number of contracts submitted by the client
  145. LeavesQty float64 //成交手数
  146. Price float64 //Price per contract
  147. Text unsafe.Pointer
  148. OrdRejReason int
  149. TransactTime int //Time of execution/order creation
  150. Millisecond int
  151. SecurityIDSource [2]byte
  152. TimeInForce byte
  153. OrdType byte
  154. OrdStatus byte
  155. Side byte //1=BUY ,2=SELL
  156. ExecType byte
  157. }
  158. func NewSessionSettings(file string) (*SessionSettings, error) {
  159. cstr := C.CString(file)
  160. defer C.free(unsafe.Pointer(cstr))
  161. c := C.NewSessionSettings(cstr)
  162. if c == nil {
  163. return nil, fmt.Errorf("create new NewSessionSettings object error.")
  164. }
  165. return &SessionSettings{obj: c}, nil
  166. }
  167. func (c *SessionSettings) Free() {
  168. if c.obj != nil {
  169. C.FreeSessionSettings(c.obj)
  170. c.obj = nil
  171. }
  172. }
  173. func (ss *SessionSettings) Get(sid *SessionID) *Dictionary {
  174. dict := &Dictionary{}
  175. dict.obj = C.SessionSettingsGet(ss.obj, sid.obj)
  176. return dict
  177. }
  178. func (dict *Dictionary) Has(find string) bool {
  179. cstr := C.CString(find)
  180. defer C.free(unsafe.Pointer(cstr))
  181. ret := C.DictionaryHas(dict.obj, cstr)
  182. if int(ret) == 1 {
  183. return true
  184. }
  185. return false
  186. }
  187. func (dict *Dictionary) GetString(find string) string {
  188. cstr := C.CString(find)
  189. defer C.free(unsafe.Pointer(cstr))
  190. ret := C.DictionaryGetString(dict.obj, cstr)
  191. return C.GoString(ret)
  192. }
  193. func (dict *Dictionary) Free() {
  194. if dict.obj != nil {
  195. C.FreeDictionary(dict.obj)
  196. dict.obj = nil
  197. }
  198. }
  199. func WarpSessionID(sid unsafe.Pointer) *SessionID {
  200. sessionId := &SessionID{}
  201. sessionId.obj = C.WarpSessionID(sid)
  202. return sessionId
  203. }
  204. func WarpMessage(msg unsafe.Pointer) *Message {
  205. message := &Message{}
  206. message.obj = C.WarpMessage(msg)
  207. return message
  208. }
  209. func (msg *Message) SetField(field int, value string) {
  210. cstr := C.CString(value)
  211. defer C.free(unsafe.Pointer(cstr))
  212. C.MessageSetField(msg.obj, C.int(field), cstr)
  213. }
  214. func (msg *Message) Send() int {
  215. return int(C.MessageSend(msg.obj))
  216. }
  217. func (msg *Message) ToStr() string {
  218. cstr := C.MessageToStr(msg.obj)
  219. if cstr == nil {
  220. return ""
  221. }
  222. defer C.Free(unsafe.Pointer(cstr))
  223. return C.GoString(cstr)
  224. }
  225. func NewAppTradeClient(app IApplication, setting *SessionSettings) (*AppTradeClient, error) {
  226. tradeclient := &AppTradeClient{}
  227. tradeclient.app = app
  228. tradeclient.onCreate = func(trade unsafe.Pointer, sid unsafe.Pointer) {
  229. tradeclient.app.onCreate(tradeclient, WarpSessionID(sid))
  230. }
  231. tradeclient.onLogon = func(trade unsafe.Pointer, sid unsafe.Pointer) {
  232. tradeclient.app.onLogon(tradeclient, WarpSessionID(sid))
  233. }
  234. tradeclient.onLogout = func(trade unsafe.Pointer, sid unsafe.Pointer) {
  235. tradeclient.app.onLogout(tradeclient, WarpSessionID(sid))
  236. }
  237. tradeclient.toAdmin = func(trade unsafe.Pointer, msg unsafe.Pointer, sid unsafe.Pointer) {
  238. tradeclient.app.toAdmin(tradeclient, WarpMessage(msg), WarpSessionID(sid))
  239. }
  240. tradeclient.fromAdmin = func(trade unsafe.Pointer, msg unsafe.Pointer, sid unsafe.Pointer) {
  241. tradeclient.app.fromAdmin(tradeclient, WarpMessage(msg), WarpSessionID(sid))
  242. }
  243. tradeclient.toApp = func(trade unsafe.Pointer, msg unsafe.Pointer, sid unsafe.Pointer) {
  244. tradeclient.app.toApp(tradeclient, WarpMessage(msg), WarpSessionID(sid))
  245. }
  246. tradeclient.fromApp = func(trade unsafe.Pointer, msg unsafe.Pointer, sid unsafe.Pointer) {
  247. tradeclient.app.fromApp(tradeclient, WarpMessage(msg), WarpSessionID(sid))
  248. }
  249. iapp := NewApplication(tradeclient)
  250. tradeclient.iapp = iapp
  251. tradeclient.setting = setting
  252. t := time.Now()
  253. tradeclient.obj = C.NewAppTradeClient(iapp.obj, setting.obj)
  254. log.Print(time.Now().Sub(t))
  255. if tradeclient.obj == nil {
  256. return nil, fmt.Errorf("NewAppTradeClient error.")
  257. }
  258. return tradeclient, nil
  259. }
  260. func NewApplication(tc *AppTradeClient) *Application {
  261. app := &Application{}
  262. app.obj = C.NewCBIApplication(C.get_callback(), unsafe.Pointer(&tc.onCreate), unsafe.Pointer(&tc.onLogon),
  263. unsafe.Pointer(&tc.onLogout), unsafe.Pointer(&tc.toAdmin),
  264. unsafe.Pointer(&tc.toApp), unsafe.Pointer(&tc.fromAdmin), unsafe.Pointer(&tc.fromApp))
  265. return app
  266. }
  267. func (app *Application) Free() {
  268. if app.obj != nil {
  269. C.FreeIApplication(app.obj)
  270. app.obj = nil
  271. }
  272. }
  273. func (tradeclient *AppTradeClient) Free() {
  274. tradeclient.setting.Free()
  275. tradeclient.iapp.Free()
  276. if tradeclient.obj != nil {
  277. C.FreeAppTradeClient(tradeclient.obj)
  278. tradeclient.obj = nil
  279. }
  280. }
  281. func (tradeclient *AppTradeClient) Start() {
  282. C.AppTradeClientStart(tradeclient.obj)
  283. }
  284. func (tradeclient *AppTradeClient) Stop() {
  285. C.AppTradeClientStop(tradeclient.obj)
  286. }
  287. func (tradeclient *AppTradeClient) Run() int {
  288. ret := C.AppTradeClientRun(tradeclient.obj)
  289. return int(ret)
  290. }
  291. func (tradeclient *AppTradeClient) SessionID() *SessionID {
  292. sid := C.AppTradeClientSessionID(tradeclient.obj)
  293. return WarpSessionID(unsafe.Pointer(sid))
  294. }
  295. func (tradeclient *AppTradeClient) SenderCompID() string {
  296. cstr := C.AppTradeClientSenderCompID(tradeclient.obj)
  297. return C.GoString(cstr)
  298. }
  299. func (tradeclient *AppTradeClient) TargetCompID() string {
  300. cstr := C.AppTradeClientTargetCompID(tradeclient.obj)
  301. return C.GoString(cstr)
  302. }
  303. func (tradeclient *AppTradeClient) ClOrdID() string {
  304. cstr := C.AppTradeClientClOrdID(tradeclient.obj)
  305. return C.GoString(cstr)
  306. }
  307. func GetMarketData(msg *Message) (*TickFull, bool) {
  308. tick := &TickFull{}
  309. ret := C.GetMarketData(msg.obj, (*C.TickFull)(unsafe.Pointer(tick)))
  310. if int(ret) == 1 {
  311. return tick, true
  312. }
  313. return tick, false
  314. }
  315. func (tradeclient *AppTradeClient) OrigClOrdID(msg *Message) string {
  316. cstr := C.AppTradeClientOrigClOrdID(tradeclient.obj, msg.obj)
  317. return C.GoString(cstr)
  318. }
  319. func (tradeclient *AppTradeClient) SendOrder(bookId string, clordId string, handInst byte, side byte, orderType byte,
  320. qty float64, price float64, account string, securitytype string, securityexchange string, currency string) bool {
  321. cbookId := C.CString(bookId)
  322. defer C.free(unsafe.Pointer(cbookId))
  323. cclordId := C.CString(clordId)
  324. defer C.free(unsafe.Pointer(cclordId))
  325. caccount := C.CString(account)
  326. defer C.free(unsafe.Pointer(caccount))
  327. csecuritytype := C.CString(securitytype)
  328. defer C.free(unsafe.Pointer(csecuritytype))
  329. csecurityexchange := C.CString(securityexchange)
  330. defer C.free(unsafe.Pointer(csecurityexchange))
  331. ccurrency := C.CString(currency)
  332. defer C.free(unsafe.Pointer(ccurrency))
  333. ret := C.AppTradeClientSendOrder(tradeclient.obj, cbookId, cclordId, C.char(side), C.char(orderType), C.double(qty),
  334. C.double(price), caccount)
  335. if int(ret) == 1 {
  336. return true
  337. }
  338. return false
  339. }
  340. func (tradeclient *AppTradeClient) CancleOrder(bookId string, origClordid string, clordId string, side byte, qty float64, account string, securitytype string, orderid string) bool {
  341. cbookId := C.CString(bookId)
  342. defer C.free(unsafe.Pointer(cbookId))
  343. cclordId := C.CString(clordId)
  344. defer C.free(unsafe.Pointer(cclordId))
  345. corigClordid := C.CString(origClordid)
  346. defer C.free(unsafe.Pointer(corigClordid))
  347. caccount := C.CString(account)
  348. defer C.free(unsafe.Pointer(caccount))
  349. csecuritytype := C.CString(securitytype)
  350. defer C.free(unsafe.Pointer(csecuritytype))
  351. corderid := C.CString(orderid)
  352. defer C.free(unsafe.Pointer(corderid))
  353. ret := C.AppTradeClientCancelOrder(tradeclient.obj, cbookId, corigClordid, cclordId, C.char(side), caccount, corderid)
  354. if int(ret) == 1 {
  355. return true
  356. }
  357. return false
  358. }
  359. /*
  360. func (tradeclient *AppTradeClient) StatusOrder(bookId string, clordId string, side byte, account string, securitytype string, orderid string) bool {
  361. cbookId := C.CString(bookId)
  362. defer C.free(unsafe.Pointer(cbookId))
  363. cclordId := C.CString(clordId)
  364. defer C.free(unsafe.Pointer(cclordId))
  365. caccount := C.CString(account)
  366. defer C.free(unsafe.Pointer(caccount))
  367. csecuritytype := C.CString(securitytype)
  368. defer C.free(unsafe.Pointer(csecuritytype))
  369. corderid := C.CString(orderid)
  370. defer C.free(unsafe.Pointer(corderid))
  371. ret := C.AppTradeClientOrderStatus(tradeclient.obj, cbookId, cclordId, C.char(side), caccount, csecuritytype, corderid)
  372. if int(ret) == 1 {
  373. return true
  374. }
  375. return false
  376. }
  377. */
  378. func (tradeclient *AppTradeClient) CollectiveOrder(clordId string, handlInst byte, ordertype byte, sidestr string, symbolstr string, orderQtyStr string,
  379. priceStr string, securityExchangeStr string, currentStr string, account string, securityTypeStr string, accountStr string, entrustNum int) bool {
  380. cclordId := C.CString(clordId)
  381. defer C.free(unsafe.Pointer(cclordId))
  382. csidestr := C.CString(sidestr)
  383. defer C.free(unsafe.Pointer(csidestr))
  384. csymbolstr := C.CString(symbolstr)
  385. defer C.free(unsafe.Pointer(csymbolstr))
  386. corderQtyStr := C.CString(orderQtyStr)
  387. defer C.free(unsafe.Pointer(corderQtyStr))
  388. cpriceStr := C.CString(priceStr)
  389. defer C.free(unsafe.Pointer(cpriceStr))
  390. csecurityExchangeStr := C.CString(securityExchangeStr)
  391. defer C.free(unsafe.Pointer(csecurityExchangeStr))
  392. ccurrentStr := C.CString(currentStr)
  393. defer C.free(unsafe.Pointer(ccurrentStr))
  394. caccount := C.CString(account)
  395. defer C.free(unsafe.Pointer(caccount))
  396. csecurityTypeStr := C.CString(securityTypeStr)
  397. defer C.free(unsafe.Pointer(csecurityTypeStr))
  398. caccountStr := C.CString(accountStr)
  399. defer C.free(unsafe.Pointer(caccountStr))
  400. ret := C.AppTradeClientCollectiveOrder(tradeclient.obj, cclordId, C.char(handlInst), C.char(ordertype), csidestr, csymbolstr, corderQtyStr, cpriceStr, csecurityExchangeStr,
  401. ccurrentStr, caccount, csecurityTypeStr, caccountStr, C.int(entrustNum))
  402. if int(ret) == 1 {
  403. return true
  404. }
  405. return false
  406. }
  407. func (tradeclient *AppTradeClient) CollectiveOrderCancel(clordId string, entrustNum int, entrustNoStr string, securityExchangeStr string,
  408. currencyStr string, account string) bool {
  409. cclordId := C.CString(clordId)
  410. defer C.free(unsafe.Pointer(cclordId))
  411. centrustNoStr := C.CString(entrustNoStr)
  412. defer C.free(unsafe.Pointer(centrustNoStr))
  413. csecurityExchangeStr := C.CString(securityExchangeStr)
  414. defer C.free(unsafe.Pointer(csecurityExchangeStr))
  415. ccurrencyStr := C.CString(currencyStr)
  416. defer C.free(unsafe.Pointer(ccurrencyStr))
  417. caccount := C.CString(account)
  418. defer C.free(unsafe.Pointer(caccount))
  419. ret := C.AppTradeClientCollectiveOrderCancel(tradeclient.obj, cclordId, C.int(entrustNum), centrustNoStr, csecurityExchangeStr, ccurrencyStr, caccount)
  420. if int(ret) == 1 {
  421. return true
  422. }
  423. return false
  424. }
  425. func (tradeclient *AppTradeClient) CollectiveOrderStatus(entrustNum int, entrustNoStr string, account string) bool {
  426. centrustNoStr := C.CString(entrustNoStr)
  427. defer C.free(unsafe.Pointer(centrustNoStr))
  428. caccount := C.CString(account)
  429. defer C.free(unsafe.Pointer(caccount))
  430. ret := C.AppTradeClientCollectiveOrderStatus(tradeclient.obj, C.int(entrustNum), centrustNoStr, caccount)
  431. if int(ret) == 1 {
  432. return true
  433. }
  434. return false
  435. }
  436. func (tradeclient *AppTradeClient) BatchOrder(clordId string, handlInst byte, ordertype byte, symbolstr []string, sidestr []string, orderQtyStr []string,
  437. priceStr []string, securityExchangeStr []string, currentStr []string, securityTypeStr []string, account string, num int) bool {
  438. cclordId := C.CString(clordId)
  439. defer C.free(unsafe.Pointer(cclordId))
  440. psymbolstr := make([]*C.char, len(symbolstr))
  441. for i := range symbolstr {
  442. psymbolstr[i] = C.CString(symbolstr[i])
  443. defer C.free(unsafe.Pointer(psymbolstr[i]))
  444. }
  445. psidestr := make([]*C.char, len(sidestr))
  446. for i := range sidestr {
  447. psidestr[i] = C.CString(sidestr[i])
  448. defer C.free(unsafe.Pointer(psidestr[i]))
  449. }
  450. pcorderQtyStr := make([]*C.char, len(orderQtyStr))
  451. for i := range orderQtyStr {
  452. pcorderQtyStr[i] = C.CString(orderQtyStr[i])
  453. defer C.free(unsafe.Pointer(pcorderQtyStr[i]))
  454. }
  455. ppriceStr := make([]*C.char, len(priceStr))
  456. for i := range priceStr {
  457. ppriceStr[i] = C.CString(priceStr[i])
  458. defer C.free(unsafe.Pointer(ppriceStr[i]))
  459. }
  460. pcsecurityExchangeStr := make([]*C.char, len(securityExchangeStr))
  461. for i := range securityExchangeStr {
  462. pcsecurityExchangeStr[i] = C.CString(securityExchangeStr[i])
  463. defer C.free(unsafe.Pointer(pcsecurityExchangeStr[i]))
  464. }
  465. pcurrentStr := make([]*C.char, len(currentStr))
  466. for i := range currentStr {
  467. pcurrentStr[i] = C.CString(currentStr[i])
  468. defer C.free(unsafe.Pointer(pcurrentStr[i]))
  469. }
  470. psecurityTypeStr := make([]*C.char, len(securityTypeStr))
  471. for i := range securityTypeStr {
  472. psecurityTypeStr[i] = C.CString(securityTypeStr[i])
  473. defer C.free(unsafe.Pointer(psecurityTypeStr[i]))
  474. }
  475. caccount := C.CString(account)
  476. defer C.free(unsafe.Pointer(caccount))
  477. ret := C.AppTradeClientBatchOrder(tradeclient.obj, cclordId, C.char(handlInst), C.char(ordertype), (**C.char)(unsafe.Pointer(&psymbolstr[0])), (**C.char)(unsafe.Pointer(&psidestr[0])), (**C.char)(unsafe.Pointer(&pcorderQtyStr[0])),
  478. (**C.char)(unsafe.Pointer(&ppriceStr[0])), (**C.char)(unsafe.Pointer(&pcsecurityExchangeStr[0])), (**C.char)(unsafe.Pointer(&pcurrentStr[0])), (**C.char)(unsafe.Pointer(&psecurityTypeStr[0])), caccount, C.int(num))
  479. if int(ret) == 1 {
  480. return true
  481. }
  482. return false
  483. }
  484. func (tradeclient *AppTradeClient) RequestForPositions(posReqID string, posReqType int, account string, accountType int) bool {
  485. cposReqID := C.CString(posReqID)
  486. defer C.free(unsafe.Pointer(cposReqID))
  487. caccount := C.CString(account)
  488. defer C.free(unsafe.Pointer(caccount))
  489. ret := C.AppTradeClientRequestForPositions(tradeclient.obj, cposReqID, C.int(posReqType), caccount, C.int(accountType))
  490. if int(ret) == 1 {
  491. return true
  492. }
  493. return false
  494. }
  495. func (tradeclient *AppTradeClient) TradeCaptureReportRequest(reqId string, reqType int, subType byte) bool {
  496. creqId := C.CString(reqId)
  497. defer C.free(unsafe.Pointer(creqId))
  498. ret := C.AppTradeClientTradeCaptureReportRequest(tradeclient.obj, creqId, C.int(reqType), C.char(subType))
  499. if int(ret) == 1 {
  500. return true
  501. }
  502. return false
  503. }
  504. func (tradeclient *AppTradeClient) MarketData(bookId string, subscriptionRequestType byte) bool {
  505. cbookId := C.CString(bookId)
  506. defer C.free(unsafe.Pointer(cbookId))
  507. //csecurityExchange := C.CString(securityExchange)
  508. //defer C.free(unsafe.Pointer(csecurityExchange))
  509. ret := C.AppTradeClientMarketData(tradeclient.obj, cbookId, C.char(subscriptionRequestType))
  510. if int(ret) == 1 {
  511. return true
  512. }
  513. return false
  514. }
  515. func GetMsgType(msg *Message) (string, bool) {
  516. var TypeValue [32]byte
  517. ret := C.GetMsgType(msg.obj, (*C.char)(unsafe.Pointer(&TypeValue[0])))
  518. if int(ret) == 1 {
  519. return cstring(TypeValue[:]), true
  520. }
  521. return "", false
  522. }
  523. func GetAccountStockInfo(msg *Message) (*StockInfo, int, bool) {
  524. stockinfo := &StockInfo{}
  525. nLastRptRequested := 0
  526. ret := C.GetAccountStockInfo(msg.obj, (*C.StockInfo)(unsafe.Pointer(stockinfo)), (*C.int)(unsafe.Pointer(&nLastRptRequested)))
  527. if int(ret) == 1 {
  528. return stockinfo, nLastRptRequested, true
  529. }
  530. return nil, 0, false
  531. }
  532. func GetOrderStatus(msg *Message) (*OrderStatus, bool) {
  533. info := &orderInfo{}
  534. ret := C.GetOrderInfo(msg.obj, (*C.OrderInfo)(unsafe.Pointer(info)))
  535. if int(ret) == 1 {
  536. return formatOrderInfo(info), true
  537. }
  538. return nil, false
  539. }
  540. func formatOrderInfo(info *orderInfo) *OrderStatus {
  541. status := &OrderStatus{}
  542. status.OrderID = cstring(info.OrderID[:])
  543. status.SecondaryExecID = cstring(info.SecondaryExecID[:])
  544. status.ClOrdID = cstring(info.ClOrdID[:])
  545. status.OrigClOrdID = cstring(info.OrigClOrdID[:])
  546. status.OrdStatusReqID = cstring(info.OrdStatusReqID[:])
  547. status.ExecID = cstring(info.ExecID[:])
  548. status.Account = cstring(info.Account[:])
  549. status.SecurityID = cstring(info.SecurityID[:])
  550. status.SecurityIDSource = cstring(info.SecurityIDSource[:])
  551. status.Text = C.GoString((*C.char)(info.Text))
  552. C.Free(info.Text)
  553. //复制其他的字段
  554. status.ExecType = info.ExecType
  555. status.OrdStatus = info.OrdStatus
  556. status.OrdRejReason = info.OrdRejReason
  557. status.Side = info.Side
  558. status.OrdType = info.OrdType
  559. status.Price = info.Price
  560. //status.TimeInForce = info.TimeInForce
  561. status.LastQty = info.LastQty
  562. status.LastPx = info.LastPx
  563. status.LeavesQty = info.LeavesQty
  564. status.CumQty = info.CumQty
  565. status.AvgPx = info.AvgPx
  566. return status
  567. }
  568. func cstring(s []byte) string {
  569. for i := range s {
  570. if s[i] == 0 {
  571. return string(s[0:i])
  572. }
  573. }
  574. return string(s)
  575. }