fix.go 1.9 KB

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