fix.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved.
  2. package gofix
  3. /*
  4. #cgo CFLAGS: -I./include
  5. #cgo LDFLAGS: -L/usr/lib
  6. #cgo linux LDFLAGS: -lboost_system -lquickfix -lufx_security -lcfix -pthread -lm
  7. #include <libcfix.h>
  8. #include <stdlib.h>
  9. extern void GoOnMdMessage(TickFull* tick, void* ctx);
  10. void OnMdMessage(TickFull* tick, void* ctx) {
  11. GoOnMdMessage(tick, ctx);
  12. }
  13. */
  14. import "C"
  15. import (
  16. "errors"
  17. "log"
  18. "unsafe"
  19. )
  20. const MAX_BID_ASK_COUNT = 25
  21. type FixTick struct {
  22. AskPrice [MAX_BID_ASK_COUNT]float64
  23. BidPrice [MAX_BID_ASK_COUNT]float64
  24. AskVolume [MAX_BID_ASK_COUNT]float64
  25. BidVolume [MAX_BID_ASK_COUNT]float64
  26. Symbol [8]byte
  27. Time int32
  28. Millisecond int32
  29. AskCount int32
  30. BidCount int32
  31. }
  32. func Symbol(symbol [8]byte) string {
  33. for i, v := range symbol {
  34. if v == 0 {
  35. return string(symbol[:i])
  36. }
  37. }
  38. return string(symbol[:])
  39. }
  40. type App struct {
  41. symbolMap map[string]string
  42. account, password string
  43. Ch chan *FixTick
  44. }
  45. func NewApp(symbolMap map[string]string, account, password string) *App {
  46. return &App{
  47. symbolMap: symbolMap,
  48. account: account,
  49. password: password,
  50. Ch: make(chan *FixTick, 1024),
  51. }
  52. }
  53. func (app *App) Run(fname string) error {
  54. cacc := C.CString(app.account)
  55. defer C.free(unsafe.Pointer(cacc))
  56. cpswd := C.CString(app.password)
  57. defer C.free(unsafe.Pointer(cpswd))
  58. capp := C.CreateApp(cacc, cpswd)
  59. if capp == nil {
  60. return errors.New(" C.CreateApp() error")
  61. }
  62. for key, val := range app.symbolMap {
  63. csym := C.CString(key)
  64. cexchange := C.CString(val)
  65. C.AddSymbol(capp, csym, cexchange)
  66. C.free(unsafe.Pointer(csym))
  67. C.free(unsafe.Pointer(cexchange))
  68. }
  69. fn := C.fnOnMdMessage(C.OnMdMessage)
  70. C.Reg_OnMdMessage(capp, fn, unsafe.Pointer(app))
  71. cfname := C.CString(fname)
  72. defer C.free(unsafe.Pointer(cfname))
  73. C.AppRun(capp, cfname)
  74. return nil
  75. }
  76. func (app *App) onMdMessage(tick *FixTick) {
  77. log.Println("App.onMdMessage", string(tick.Symbol[:]), tick.Time)
  78. select {
  79. case app.Ch <- tick:
  80. default:
  81. }
  82. }