main.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved.
  2. // 本程序用来把永华的期货数据导入到tickserver中
  3. package main
  4. import (
  5. "database/sql"
  6. "encoding/json"
  7. "fmt"
  8. "log"
  9. "os"
  10. "path"
  11. "sort"
  12. "strings"
  13. "tickserver/server/market"
  14. )
  15. var pair = []string{
  16. "EURUSD",
  17. "EURGBP",
  18. "GBPUSD",
  19. "USDJPY",
  20. "USDCHF",
  21. "AUDUSD",
  22. "USDCAD",
  23. "NZDUSD",
  24. "CHFJPY",
  25. "EURJPY",
  26. "EURCHF",
  27. "EURAUD",
  28. "EURCAD",
  29. "GBPCHF",
  30. "GBPJPY",
  31. "OILUSD",
  32. "CADJPY",
  33. "AUDJPY",
  34. "AUDCAD",
  35. "AUDNZD",
  36. "XAGUSD",
  37. "XAUUSD",
  38. }
  39. const (
  40. PTK = 0 //period index
  41. PM1 = 1
  42. PM5 = 2
  43. PH1 = 3
  44. PD1 = 4
  45. TK = 0 //period duration
  46. M1 = 1 * 60
  47. M5 = 5 * 60
  48. H1 = 60 * 60
  49. D1 = 24 * 3600
  50. )
  51. const (
  52. DATATYPE = "easyforex"
  53. BUFLEN = 1024
  54. )
  55. var PeriodMap = map[int]int{
  56. PTK: TK,
  57. PM1: M1,
  58. PM5: M5,
  59. PH1: H1,
  60. PD1: D1,
  61. }
  62. var PeriodNameMap = map[int]string{
  63. TK: "Tick",
  64. M1: "M1",
  65. M5: "M5",
  66. H1: "H1",
  67. D1: "D1",
  68. }
  69. type Tick struct { //tick结构
  70. time int64
  71. buyPercent float64
  72. }
  73. type Conf struct {
  74. DSN string // dsn = fmt.Sprintf("root:fzm@1001@/%s?charset=%s", dbName, "utf8")
  75. DataDir string
  76. }
  77. type byTime []market.Candle
  78. func (a byTime) Len() int { return len(a) }
  79. func (a byTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  80. func (a byTime) Less(i, j int) bool { return a[i].Timestamp < a[j].Timestamp }
  81. var conf *Conf
  82. func main() {
  83. // set log
  84. /*logF, err := os.Create("./hisconv.log.txt")
  85. if err != nil {
  86. log.Fatal(err)
  87. }
  88. defer logF.Close()
  89. log.SetOutput(logF)*/
  90. var err error
  91. conf, err = ReadConf()
  92. if err != nil {
  93. log.Fatal(err)
  94. }
  95. go getTick()
  96. select {}
  97. }
  98. func connectDB(dsn string) (*sql.DB, error) {
  99. db, err := sql.Open("mysql", dsn)
  100. if err != nil {
  101. return nil, err
  102. }
  103. return db, nil
  104. }
  105. func getInstrumentName(symbol string) string {
  106. return DATATYPE + "_" + symbol
  107. }
  108. func getTableName(symbol string, period int) string {
  109. tableName := symbol
  110. tableName += "_"
  111. tableName += PeriodNameMap[period]
  112. return strings.ToLower(tableName)
  113. }
  114. func getTickFromDB(symbol string, period int, tkCh chan<- Tick) error {
  115. db, err := connectDB(conf.DSN)
  116. if err != nil {
  117. log.Println("error: connect db", symbol, err)
  118. return err
  119. }
  120. defer db.Close()
  121. tableName := getTableName(symbol, period)
  122. var offset int
  123. for {
  124. queryString := fmt.Sprintf("SELECT time,buy_percent FROM %s order by time ASC limit %d,1000", tableName, offset)
  125. //log.Println(queryString)
  126. rows, err := db.Query(queryString)
  127. if err != nil {
  128. log.Println("error: query", symbol, err)
  129. return err
  130. }
  131. defer rows.Close()
  132. var count int
  133. for rows.Next() {
  134. var tk Tick
  135. if err := rows.Scan(&tk.time, &tk.buyPercent); err != nil {
  136. log.Println("error: scan", symbol, err)
  137. return err
  138. }
  139. tkCh <- tk
  140. count++
  141. }
  142. offset += count
  143. if count < 1000 {
  144. break
  145. }
  146. }
  147. log.Println("source tick of", symbol, "finished.")
  148. tk := Tick{-1, 0.0} //notify save goroutine there's no data anymore
  149. tkCh <- tk
  150. return nil
  151. }
  152. func processTick(symbol string, tkCh <-chan Tick) {
  153. var ticks []market.Tick
  154. insId := getInstrumentName(symbol)
  155. var last int64
  156. for {
  157. tk := <-tkCh
  158. if tk.time == -1 || ((tk.time/(3600*24) != last) && (last != 0)) {
  159. if len(ticks) > 0 {
  160. fname, err := market.SaveTickEx(conf.DataDir, ticks, insId, true)
  161. if err != nil {
  162. log.Println("error: savetickex", symbol, fname, err)
  163. }
  164. err = convAndSaveCandles(insId, ticks)
  165. if err != nil {
  166. log.Println("error: convandsavecandles", symbol, err)
  167. }
  168. ticks = nil
  169. }
  170. if tk.time == -1 {
  171. break
  172. }
  173. }
  174. var mk market.Tick
  175. mk.Timestamp = tk.time * 1000
  176. mk.Price = 100 - tk.buyPercent
  177. ticks = append(ticks, mk)
  178. last = tk.time / (3600 * 24)
  179. }
  180. }
  181. func getTick() (err error) {
  182. for _, symbol := range pair {
  183. tkCh := make(chan Tick, 1024)
  184. go getTickFromDB(symbol, TK, tkCh)
  185. go processTick(symbol, tkCh)
  186. }
  187. return nil
  188. }
  189. func convAndSaveCandles(insId string, ticks []market.Tick) error {
  190. //refer := path
  191. var candles []market.Candle
  192. pa := []int{market.M1, market.M5, market.H1, market.D1}
  193. for _, period := range pa {
  194. var err error
  195. if period == market.M1 {
  196. candles, err = convCandles0(ticks, insId, market.M1)
  197. } else {
  198. candles, err = convCandles1(candles, insId, period)
  199. }
  200. if err != nil {
  201. return err
  202. }
  203. if period == market.D1 {
  204. dir := path.Join(conf.DataDir, insId)
  205. os.MkdirAll(dir, 0777)
  206. var bname string
  207. bname = fmt.Sprintf("%s.gz", market.PeriodNameMap[period])
  208. fname := path.Join(dir, bname)
  209. candles, _ = combinEx(fname, candles)
  210. }
  211. _, err = market.SaveCandlesEx(conf.DataDir, insId, candles, period, true)
  212. if err != nil {
  213. return err
  214. }
  215. }
  216. return nil
  217. }
  218. func convCandles0(ticks []market.Tick, insId string, period int) ([]market.Candle, error) {
  219. r := market.NewTickBuf(ticks)
  220. return market.TickConvCandle(r, insId, period)
  221. }
  222. func convCandles1(candles []market.Candle, insId string, period int) ([]market.Candle, error) {
  223. r := market.NewCandleBuf(candles)
  224. return market.ConvPeriod(r, insId, period)
  225. }
  226. func ReadConf() (*Conf, error) {
  227. f, err := os.Open("efhisconv.json")
  228. if err != nil {
  229. return nil, err
  230. }
  231. defer f.Close()
  232. dec := json.NewDecoder(f)
  233. conf := &Conf{}
  234. err = dec.Decode(conf)
  235. if err != nil {
  236. return nil, err
  237. }
  238. return conf, nil
  239. }
  240. func combinEx(filename string, candles []market.Candle) ([]market.Candle, error) {
  241. buf, err := market.ReadCandleFile(filename)
  242. if err != nil {
  243. return candles, err
  244. }
  245. candles = append(buf, candles[:]...)
  246. sort.Sort(byTime(candles))
  247. return candles, nil
  248. }