future_sina_his.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. // const.go
  2. package tick
  3. import "fmt"
  4. import "net/http"
  5. import "encoding/json"
  6. import "tickserver/server/market"
  7. import "strconv"
  8. import "io/ioutil"
  9. import "time"
  10. import "os"
  11. import "errors"
  12. const (
  13. RB0 = iota
  14. AG0
  15. AU0
  16. CU0
  17. AL0
  18. ZN0
  19. PB0
  20. RU0
  21. FU0
  22. WR0
  23. A0
  24. M0
  25. Y0
  26. J0
  27. C0
  28. L0
  29. P0
  30. V0
  31. RS0
  32. RM0
  33. FG0
  34. CF0
  35. WS0
  36. ER0
  37. ME0
  38. RO0
  39. TA0
  40. IH1505
  41. IC1505
  42. )
  43. var CtpHisNames []string
  44. var SinaFutureSymbols = map[string]int{
  45. "RB0": RB0,
  46. "AG0": AG0,
  47. "AU0": AU0,
  48. "CU0": CU0,
  49. "AL0": AL0,
  50. "ZN0": ZN0,
  51. "PB0": PB0,
  52. "RU0": RU0,
  53. "FU0": FU0,
  54. "WR0": WR0,
  55. "A0": A0,
  56. "M0": M0,
  57. "Y0": Y0,
  58. "J0": J0,
  59. "C0": C0,
  60. "L0": L0,
  61. "P0": P0,
  62. "V0": V0,
  63. "RS0": RS0,
  64. "RM0": RM0,
  65. "FG0": FG0,
  66. "CF0": CF0,
  67. "WS0": WS0,
  68. "ER0": ER0,
  69. "ME0": ME0,
  70. "RO0": RO0,
  71. "TA0": TA0,
  72. "IH1505": IH1505,
  73. "IC1505": IC1505,
  74. }
  75. var SinaFuturePeriods = []string{
  76. "5m",
  77. "60m",
  78. "1d",
  79. }
  80. func saveCtpHisNames() error {
  81. b, err := json.MarshalIndent(CtpHisNames, "", " ")
  82. if err != nil {
  83. return err
  84. }
  85. ioutil.WriteFile("CtpHisNames.json", b, os.ModePerm)
  86. return nil
  87. }
  88. func LoadCtpHisNames() error {
  89. f, err := os.Open("CtpHisNames.json")
  90. if err != nil {
  91. return err
  92. }
  93. defer f.Close()
  94. dec := json.NewDecoder(f)
  95. err = dec.Decode(&CtpHisNames)
  96. if err != nil {
  97. return err
  98. }
  99. return nil
  100. }
  101. /*func DownloadCtp(db *market.MyDB, dataDir, typ, symbolComplex string) error {
  102. symbol := strings.Split(symbolComplex, ":")[0]
  103. bGoods, _ := strconv.Atoi(strings.Split(symbolComplex, ":")[1])
  104. var period int
  105. dir := dataDir + "/" + typ
  106. insId := fmt.Sprintf("ctp_%s", symbol)
  107. for _, v := range SinaFuturePeriods {
  108. if v == "5m" {
  109. period = market.M5
  110. }
  111. if v == "60m" {
  112. period = market.H1
  113. }
  114. if v == "1d" {
  115. period = market.D1
  116. }
  117. candles, err := downloadCtpPeriod(symbol, bGoods, v)
  118. if err != nil {
  119. log.Println(symbol, bGoods, v, err)
  120. return err
  121. }
  122. log.Println(symbol, bGoods, v, len(candles))
  123. //for ci, cv := range candles {
  124. //log.Println(ci, cv)
  125. //}
  126. //err = market.SaveCandlesAppend(db, dir, insId, candles, period)
  127. //if err != nil {
  128. //log.Println("SaveCandlesAppend", symbol, period, err)
  129. ////break //return err
  130. //}
  131. if v == "5m" {
  132. //candlesm1 := candlem5ToCandlem1(candles)
  133. ticks := candleToTick(candles)
  134. //_, err = market.SaveTick(db, dir, ticks, insId, true)
  135. //if err != nil {
  136. //log.Println("SaveTick", symbol, err)
  137. //break //return err
  138. //}
  139. //err = market.ConvAndSaveCandles(db, dir, insId, ticks)
  140. //if err != nil {
  141. //log.Println("ConvAndSaveCandles", symbol, err)
  142. //break //return err
  143. //}
  144. //for ci, cv := range ticks {
  145. //log.Println(ci, cv)
  146. //}
  147. //_, err = market.SaveTickAppend(db, dir, ticks, insId)
  148. //if err != nil {
  149. //log.Println("SaveTickAppend", symbol, err)
  150. ////break //return err
  151. //}
  152. candlesm1 := candlem5ToCandlem1(candles)
  153. //for ci, cv := range candlesm1 {
  154. //log.Println(ci, cv)
  155. //}
  156. //err = market.SaveCandlesAppend(db, dir, insId, candlesm1, market.M1)
  157. //if err != nil {
  158. //log.Println("SaveCandlesAppend", symbol, err)
  159. ////break //return err
  160. //}
  161. }
  162. }
  163. return nil
  164. }*/
  165. /*func DownloadCtp(db *market.MyDB, dataDir, typ, symbolComplex string) error {
  166. symbol := strings.Split(symbolComplex, ":")[0]
  167. bGoods, _ := strconv.Atoi(strings.Split(symbolComplex, ":")[1])
  168. var period int
  169. dir := dataDir + "/" + typ
  170. insId := fmt.Sprintf("ctp_%s", symbol)
  171. for _, v := range SinaFuturePeriods {
  172. if v == "5m" {
  173. period = market.M5
  174. }
  175. if v == "60m" {
  176. period = market.H1
  177. }
  178. if v == "1d" {
  179. period = market.D1
  180. }
  181. candles, err := downloadCtpPeriod(symbol, bGoods, v)
  182. if err != nil {
  183. log.Println(symbol, bGoods, v, err)
  184. return err
  185. }
  186. log.Println(symbol, bGoods, v, len(candles))
  187. var key string
  188. candlesMap := make(map[string][]market.Candle)
  189. for _, candleV := range candles {
  190. t := time.Unix(candleV.Timestamp/1000, 0)
  191. if v == "5m" {
  192. key = fmt.Sprintf("%d%d%d", t.Year(), t.Month(), t.Day())
  193. }
  194. if v == "60m" {
  195. key = fmt.Sprintf("%d%d", t.Year(), t.Month())
  196. }
  197. if v == "1d" {
  198. key = fmt.Sprintf("%d", t.Year())
  199. }
  200. _, ok := candlesMap[key]
  201. if ok {
  202. candlesMap[key] = append(candlesMap[key], candleV)
  203. } else {
  204. var tmpCandles []market.Candle
  205. tmpCandles = append(tmpCandles, candleV)
  206. candlesMap[key] = tmpCandles
  207. }
  208. }
  209. for _, cs := range candlesMap {
  210. err = market.SaveCandlesAppend(db, dir, insId, cs, period)
  211. if err != nil {
  212. log.Println("SaveCandlesAppend", symbol, period, err)
  213. break //return err
  214. }
  215. if v == "5m" {
  216. //candlesm1 := candlem5ToCandlem1(candles)
  217. ticks := candleToTick(cs)
  218. //_, err = market.SaveTick(db, dir, ticks, insId, true)
  219. //if err != nil {
  220. //log.Println("SaveTick", symbol, err)
  221. //break //return err
  222. //}
  223. //err = market.ConvAndSaveCandles(db, dir, insId, ticks)
  224. //if err != nil {
  225. //log.Println("ConvAndSaveCandles", symbol, err)
  226. //break //return err
  227. //}
  228. _, err = market.SaveTickAppend(db, dir, ticks, insId)
  229. if err != nil {
  230. log.Println("SaveTickAppend", symbol, err)
  231. break //return err
  232. }
  233. candlesm1 := candlem5ToCandlem1(cs)
  234. err = market.SaveCandlesAppend(db, dir, insId, candlesm1, market.M1)
  235. if err != nil {
  236. log.Println("SaveCandlesAppend", symbol, err)
  237. break //return err
  238. }
  239. }
  240. }
  241. //for _, candle := range candles {
  242. //log.Println(candle)
  243. //}
  244. }
  245. return nil
  246. }*/
  247. /*func DownloadSinaFuture(db *market.MyDB, dataDir, typ, symbol string) error {
  248. var period int
  249. dir := dataDir + "/" + typ
  250. insId := fmt.Sprintf("sina_%s", symbol)
  251. for _, v := range SinaFuturePeriods {
  252. if v == "5m" {
  253. period = market.M5
  254. }
  255. if v == "60m" {
  256. period = market.H1
  257. }
  258. if v == "1d" {
  259. period = market.D1
  260. }
  261. var lastTime int64
  262. //row, err := db.GetLastTime(insId, period)
  263. //if err != nil {
  264. //log.Println("db.GetLastTime", err)
  265. ////return err
  266. //}
  267. //if row != nil {
  268. //lastTime = row.StartTime
  269. //}
  270. candles, err := downloadSinaFuturePeriod(symbol, v, lastTime)
  271. if err != nil {
  272. return err
  273. }
  274. var key string
  275. candlesMap := make(map[string][]market.Candle)
  276. for _, candleV := range candles {
  277. t := time.Unix(candleV.Timestamp/1000, 0)
  278. if v == "5m" {
  279. key = fmt.Sprintf("%d%d%d", t.Year(), t.Month(), t.Day())
  280. }
  281. if v == "60m" {
  282. key = fmt.Sprintf("%d%d", t.Year(), t.Month())
  283. }
  284. if v == "1d" {
  285. key = fmt.Sprintf("%d", t.Year())
  286. }
  287. _, ok := candlesMap[key]
  288. if ok {
  289. candlesMap[key] = append(candlesMap[key], candleV)
  290. } else {
  291. var tmpCandles []market.Candle
  292. tmpCandles = append(tmpCandles, candleV)
  293. candlesMap[key] = tmpCandles
  294. }
  295. }
  296. for _, cs := range candlesMap {
  297. //err = market.SaveCandlesAppend(db, dir, insId, cs, period)
  298. //if err != nil {
  299. //log.Println("SaveCandlesAppend", symbol, period, err)
  300. //break //return err
  301. //}
  302. if v == "5m" {
  303. //candlesm1 := candlem5ToCandlem1(candles)
  304. ticks := candleToTick(cs)
  305. //_, err = market.SaveTick(db, dir, ticks, insId, true)
  306. //if err != nil {
  307. //log.Println("SaveTick", symbol, err)
  308. //break //return err
  309. //}
  310. //err = market.ConvAndSaveCandles(db, dir, insId, ticks)
  311. //if err != nil {
  312. //log.Println("ConvAndSaveCandles", symbol, err)
  313. //break //return err
  314. //}
  315. //_, err = market.SaveTickAppend(db, dir, ticks, insId)
  316. //if err != nil {
  317. //log.Println("SaveTickAppend", symbol, err)
  318. //break //return err
  319. //}
  320. candlesm1 := candlem5ToCandlem1(cs)
  321. //err = market.SaveCandlesAppend(db, dir, insId, candlesm1, market.M1)
  322. //if err != nil {
  323. //log.Println("SaveCandlesAppend", symbol, err)
  324. //break //return err
  325. //}
  326. }
  327. }
  328. //for _, candle := range candles {
  329. //log.Println(candle)
  330. //}
  331. }
  332. return nil
  333. }*/
  334. func downloadCtpPeriod(symbol string, bGoods int, period string) ([]market.Candle, error) {
  335. var u string
  336. if period == "1d" {
  337. if bGoods == 1 {
  338. u = fmt.Sprintf("http://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesDailyKLine?symbol=%s", symbol)
  339. } else {
  340. u = fmt.Sprintf("http://stock2.finance.sina.com.cn/futures/api/json.php/CffexFuturesService.getCffexFuturesDailyKLine?symbol=%s", symbol)
  341. }
  342. } else {
  343. if bGoods == 1 {
  344. u = fmt.Sprintf("http://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesMiniKLine%s?symbol=%s", period, symbol)
  345. } else {
  346. u = fmt.Sprintf("http://stock2.finance.sina.com.cn/futures/api/json.php/CffexFuturesService.getCffexFuturesMiniKLine%s?symbol=%s", period, symbol)
  347. }
  348. }
  349. res, err := http.Get(u)
  350. if err != nil {
  351. return nil, err
  352. }
  353. defer res.Body.Close()
  354. b, err := ioutil.ReadAll(res.Body)
  355. if err != nil {
  356. return nil, err
  357. }
  358. var candles [][6]string
  359. err = json.Unmarshal(b, &candles)
  360. if err != nil {
  361. return nil, err
  362. }
  363. if len(candles) == 0 {
  364. return nil, errors.New("no data")
  365. }
  366. candleLen := len(candles)
  367. candlelist := make([]market.Candle, candleLen)
  368. for i, v := range candles {
  369. var candle market.Candle
  370. var layout string
  371. if len(v[0]) == 10 {
  372. layout = "2006-01-02"
  373. } else {
  374. layout = "2006-01-02 15:04:05"
  375. }
  376. candleTime, err := time.Parse(layout, v[0])
  377. if err != nil {
  378. return nil, err
  379. }
  380. candle.Timestamp = candleTime.Unix() * 1000
  381. //if !checkTime("DCE", candle.Timestamp) {
  382. //continue
  383. //}
  384. candle.Open, err = strconv.ParseFloat(v[1], 64)
  385. if err != nil {
  386. return nil, err
  387. }
  388. candle.High, err = strconv.ParseFloat(v[2], 64)
  389. if err != nil {
  390. return nil, err
  391. }
  392. candle.Low, err = strconv.ParseFloat(v[3], 64)
  393. if err != nil {
  394. return nil, err
  395. }
  396. candle.Close, err = strconv.ParseFloat(v[4], 64)
  397. if err != nil {
  398. return nil, err
  399. }
  400. candle.RealVolums, err = strconv.ParseFloat(v[5], 64)
  401. if err != nil {
  402. return nil, err
  403. }
  404. if period == "1d" {
  405. candlelist[i] = candle
  406. } else {
  407. candlelist[candleLen-i-1] = candle
  408. }
  409. }
  410. return candlelist, nil
  411. }
  412. func downloadSinaFuturePeriod(symbol, period string, lastTime int64) ([]market.Candle, error) {
  413. var u string
  414. if period == "1d" {
  415. u = fmt.Sprintf("http://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesDailyKLine?symbol=%s", symbol)
  416. } else {
  417. u = fmt.Sprintf("http://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesMiniKLine%s?symbol=%s", period, symbol)
  418. }
  419. res, err := http.Get(u)
  420. if err != nil {
  421. return nil, err
  422. }
  423. defer res.Body.Close()
  424. b, err := ioutil.ReadAll(res.Body)
  425. if err != nil {
  426. return nil, err
  427. }
  428. var candles [][6]string
  429. err = json.Unmarshal(b, &candles)
  430. if err != nil {
  431. return nil, err
  432. }
  433. candleLen := len(candles)
  434. candlelist := make([]market.Candle, candleLen)
  435. for i, v := range candles {
  436. var candle market.Candle
  437. var layout string
  438. if len(v[0]) == 10 {
  439. layout = "2006-01-02"
  440. } else {
  441. layout = "2006-01-02 15:04:05"
  442. }
  443. candleTime, err := time.Parse(layout, v[0])
  444. if err != nil {
  445. return nil, err
  446. }
  447. candle.Timestamp = candleTime.Unix() * 1000
  448. if candle.Timestamp < lastTime {
  449. continue
  450. }
  451. //if !checkTime("DCE", candle.Timestamp) {
  452. //continue
  453. //}
  454. candle.Open, err = strconv.ParseFloat(v[1], 64)
  455. if err != nil {
  456. return nil, err
  457. }
  458. candle.High, err = strconv.ParseFloat(v[2], 64)
  459. if err != nil {
  460. return nil, err
  461. }
  462. candle.Low, err = strconv.ParseFloat(v[3], 64)
  463. if err != nil {
  464. return nil, err
  465. }
  466. candle.Close, err = strconv.ParseFloat(v[4], 64)
  467. if err != nil {
  468. return nil, err
  469. }
  470. candle.RealVolums, err = strconv.ParseFloat(v[5], 64)
  471. if err != nil {
  472. return nil, err
  473. }
  474. if period == "1d" {
  475. candlelist[i] = candle
  476. } else {
  477. candlelist[candleLen-i-1] = candle
  478. }
  479. }
  480. return candlelist, nil
  481. }
  482. func candleToTick(candles []market.Candle) []market.Tick {
  483. var ticks []market.Tick
  484. for i := 0; i < len(candles); i++ {
  485. ticko := market.Tick{}
  486. ticko.Price = candles[i].Open
  487. ticko.Timestamp = candles[i].Timestamp
  488. tickh := market.Tick{}
  489. tickh.Price = candles[i].High
  490. tickh.Timestamp = candles[i].Timestamp
  491. tickl := market.Tick{}
  492. tickl.Price = candles[i].Low
  493. tickl.Timestamp = candles[i].Timestamp
  494. tickc := market.Tick{}
  495. tickc.Price = candles[i].Close
  496. tickc.Timestamp = candles[i].Timestamp
  497. ticks = append(ticks, ticko)
  498. if candles[i].Open >= candles[i].Close {
  499. ticks = append(ticks, tickh)
  500. ticks = append(ticks, tickl)
  501. } else {
  502. ticks = append(ticks, tickl)
  503. ticks = append(ticks, tickh)
  504. }
  505. ticks = append(ticks, tickc)
  506. //log.Println(tickc)
  507. }
  508. return ticks
  509. }
  510. func candlem5ToCandlem1(candles []market.Candle) []market.Candle {
  511. var ticks []market.Candle
  512. for i := 0; i < len(candles); i++ {
  513. ticko := market.Candle{}
  514. ticko.Open = candles[i].Open
  515. ticko.High = candles[i].Open
  516. ticko.Low = candles[i].Open
  517. ticko.Close = candles[i].Open
  518. ticko.RealVolums = candles[i].RealVolums / 5
  519. ticko.Timestamp = candles[i].Timestamp
  520. tickh := market.Candle{}
  521. tickh.Open = candles[i].High
  522. tickh.High = candles[i].High
  523. tickh.Low = candles[i].High
  524. tickh.Close = candles[i].High
  525. tickh.RealVolums = candles[i].RealVolums / 5
  526. tickl := market.Candle{}
  527. tickl.Open = candles[i].Low
  528. tickl.High = candles[i].Low
  529. tickl.Low = candles[i].Low
  530. tickl.Close = candles[i].Low
  531. tickl.RealVolums = candles[i].RealVolums / 5
  532. tickc := market.Candle{}
  533. tickc.Open = candles[i].Close
  534. tickc.High = candles[i].Close
  535. tickc.Low = candles[i].Close
  536. tickc.Close = candles[i].Close
  537. tickc.RealVolums = candles[i].RealVolums / 5
  538. tickc.Timestamp = candles[i].Timestamp + 180000
  539. tickcc := tickc
  540. tickcc.Timestamp += 60000
  541. ticks = append(ticks, ticko)
  542. if candles[i].Open >= candles[i].Close {
  543. tickh.Timestamp = candles[i].Timestamp + 60000
  544. tickl.Timestamp = candles[i].Timestamp + 120000
  545. ticks = append(ticks, tickh)
  546. ticks = append(ticks, tickl)
  547. } else {
  548. tickl.Timestamp = candles[i].Timestamp + 60000
  549. tickh.Timestamp = candles[i].Timestamp + 120000
  550. ticks = append(ticks, tickl)
  551. ticks = append(ticks, tickh)
  552. }
  553. ticks = append(ticks, tickc)
  554. ticks = append(ticks, tickcc)
  555. //log.Println(tickc)
  556. }
  557. return ticks
  558. }