main.go 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. // main.go
  2. package main
  3. import (
  4. "compress/gzip"
  5. "encoding/json"
  6. "errors"
  7. "flag"
  8. "fmt"
  9. "io"
  10. "log"
  11. "net/http"
  12. _ "net/http/pprof"
  13. "os"
  14. "runtime"
  15. "strconv"
  16. "strings"
  17. "time"
  18. "tickserver/client"
  19. "tickserver/markinfo"
  20. "tickserver/server/market"
  21. )
  22. const (
  23. K_STYLE = iota
  24. T_STYLE
  25. )
  26. type Options struct {
  27. Start int `json:"start"`
  28. End int `json:"end"`
  29. Total int `json:"total_size"`
  30. }
  31. type KCandle struct {
  32. X []int `json:"x"`
  33. Y [][5]float32 `json:"y"`
  34. }
  35. type KVolume struct {
  36. X []int `json:"x"`
  37. Y []float32 `json:"y"`
  38. }
  39. type KData struct {
  40. C string `json:"c"`
  41. P string `json:"p"`
  42. Action string `json:"action"`
  43. End int `json:"end"`
  44. //KOptions Options `json:"options"`
  45. Candles KCandle `json:"main"`
  46. Volumes KVolume `json:"volumes|||__ignore__"`
  47. }
  48. type TData struct {
  49. End int `json:"end"`
  50. Candles []client.Candle `json:"data"`
  51. }
  52. var saddr1 = flag.String("s1", "127.0.0.1:19528", "tick server address 1") //115.231.103.7
  53. var saddr2 = flag.String("s2", "127.0.0.1:19528", "tick server address 2") //127.0.0.1
  54. var saddr3 = flag.String("s3", "127.0.0.1:19529", "tick server address 3") //115.236.75.194
  55. var saddr4 = flag.String("s4", "127.0.0.1:9090", "tick server address 4") //19528 9090
  56. var clientDown *client.ClientSimple
  57. //var clientDownTmp *client.ClientSimple
  58. type Conf struct {
  59. Saddr1 string //
  60. Saddr2 string //
  61. Saddr3 string //
  62. Saddr4 string
  63. }
  64. func readConf() (*Conf, error) {
  65. f, err := os.Open("webproxys.json")
  66. if err != nil {
  67. return nil, err
  68. }
  69. defer f.Close()
  70. dec := json.NewDecoder(f)
  71. conf := &Conf{}
  72. err = dec.Decode(conf)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return conf, nil
  77. }
  78. func connectServer() (err error) {
  79. clientDown, err = client.NewClientSimple(*saddr1, *saddr2, *saddr3, *saddr4, "./tmp")
  80. if err != nil {
  81. //log.Println("new client", err)
  82. return err
  83. }
  84. //clientDownTmp, err = client.NewClientSimple("114.215.207.24:19528", "114.215.207.24:19528", "114.215.207.24:19529", "114.215.207.24:9090", "./tmp")
  85. //if err != nil {
  86. //log.Println("new client", err)
  87. //return err
  88. //}
  89. return nil
  90. }
  91. func changeCandle(candle, candle2, candle3 client.Candle) client.Candle {
  92. newCandle := candle
  93. if newCandle.High >= newCandle.Low*1.05 || newCandle.High >= candle2.Low*1.05 || newCandle.High >= candle3.Low*1.05 {
  94. //log.Println(newCandle)
  95. var low float64
  96. if newCandle.High >= newCandle.Low*1.05 {
  97. low = newCandle.Low
  98. }
  99. if newCandle.High >= candle2.Low*1.05 {
  100. low = candle2.Low
  101. }
  102. if newCandle.High >= candle3.Low*1.05 {
  103. low = candle3.Low
  104. }
  105. if newCandle.Open > newCandle.Close {
  106. newCandle.Open = low * 1.003
  107. newCandle.Close = low * 1.002
  108. } else {
  109. newCandle.Open = low * 1.002
  110. newCandle.Close = low * 1.003
  111. }
  112. newCandle.High = low * 1.005
  113. if newCandle.Low != low {
  114. newCandle.Low = low * 1.001
  115. }
  116. }
  117. return newCandle
  118. }
  119. func getData(symbol, period, action, count, out, cb, te, ts string, style int) (string, error) {
  120. iCount, err := strconv.Atoi(count)
  121. if action != "new" && err != nil {
  122. return "", errors.New("webserver doesn't support count")
  123. }
  124. if action == "new" {
  125. iCount = 1000
  126. }
  127. periodId, ok := market.PeriodIdMap[period]
  128. if !ok {
  129. return "", errors.New("webserver doesn't support period")
  130. }
  131. periodIdOld := periodId
  132. if periodId == market.M15 || periodId == market.M30 {
  133. periodId = market.M5
  134. if periodIdOld == market.M15 {
  135. iCount *= 3
  136. }
  137. if periodIdOld == market.M30 {
  138. iCount *= 6
  139. }
  140. }
  141. if periodId == market.H2 || periodId == market.H4 {
  142. periodId = market.H1
  143. if periodIdOld == market.H2 {
  144. iCount *= 2
  145. }
  146. if periodIdOld == market.H4 {
  147. iCount *= 4
  148. }
  149. }
  150. if periodId == market.W1 || periodId == market.MN1 {
  151. periodId = market.D1
  152. if periodIdOld == market.W1 {
  153. iCount *= 7
  154. }
  155. if periodIdOld == market.MN1 {
  156. iCount *= 31
  157. }
  158. }
  159. iCount++
  160. //startTime := time.Now().UnixNano()
  161. //log.Println("step1", symbol, period, action, count, out, cb, te, ts)
  162. symbolU := strings.ToUpper(symbol)
  163. if len(symbolU) == 3 {
  164. symbolU += "CNY"
  165. }
  166. _, err = markinfo.SymbolId(symbolU)
  167. if err != nil {
  168. return "", err
  169. }
  170. var prefix string
  171. prefix = market.BtyPrefix
  172. myInsId := prefix + symbolU
  173. var iTE, iTS int64
  174. var bufferedCandles []client.Candle
  175. bufferedCandles, _ = clientDown.GetLastCandles(myInsId, periodId, 0x7fffffff)
  176. /*if myInsId == "bty_BTCUSDT" && periodId == market.D1 {
  177. for i, _ := range bufferedCandles {
  178. if bufferedCandles[i].Timestamp == 1528934400000 {
  179. bufferedCandles[i].High = 6625.7998046875
  180. }
  181. }
  182. }*/
  183. /*bNormal := false
  184. for i, _ := range bufferedCandles {
  185. if myInsId == "bty_ETHUSDT" {
  186. if periodId == market.M1 {
  187. if bufferedCandles[i].Timestamp >= 1524033780000 && bufferedCandles[i].Timestamp <= 1524035400000 {
  188. bufferedCandles[i].High = bufferedCandles[i-1].High
  189. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  190. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  191. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  192. }
  193. if !bNormal && bufferedCandles[i].Timestamp > 1524035400000 {
  194. bNormal = true
  195. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  196. if bufferedCandles[i].High < bufferedCandles[i].Open {
  197. bufferedCandles[i].High = bufferedCandles[i].Open
  198. }
  199. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  200. bufferedCandles[i].Low = bufferedCandles[i].Open
  201. }
  202. }
  203. }
  204. if periodId == market.M5 {
  205. if bufferedCandles[i].Timestamp >= 1524033600000 && bufferedCandles[i].Timestamp <= 1524035100000 {
  206. bufferedCandles[i].High = bufferedCandles[i-1].High
  207. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  208. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  209. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  210. }
  211. if !bNormal && bufferedCandles[i].Timestamp > 1524035100000 {
  212. bNormal = true
  213. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  214. if bufferedCandles[i].High < bufferedCandles[i].Open {
  215. bufferedCandles[i].High = bufferedCandles[i].Open
  216. }
  217. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  218. bufferedCandles[i].Low = bufferedCandles[i].Open
  219. }
  220. }
  221. }
  222. if periodId == market.H1 {
  223. if bufferedCandles[i].Timestamp >= 1524031200000 && bufferedCandles[i].Timestamp <= 1524031200000 {
  224. bufferedCandles[i].High = bufferedCandles[i-1].High
  225. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  226. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  227. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  228. }
  229. if !bNormal && bufferedCandles[i].Timestamp > 1524031200000 {
  230. bNormal = true
  231. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  232. if bufferedCandles[i].High < bufferedCandles[i].Open {
  233. bufferedCandles[i].High = bufferedCandles[i].Open
  234. }
  235. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  236. bufferedCandles[i].Low = bufferedCandles[i].Open
  237. }
  238. }
  239. }
  240. if periodId == market.D1 {
  241. if bufferedCandles[i].Timestamp >= 1524009600000 && bufferedCandles[i].Timestamp <= 1524009600000 {
  242. if bufferedCandles[i].Open > bufferedCandles[i].Close {
  243. bufferedCandles[i].Low = bufferedCandles[i-1].Close
  244. } else {
  245. bufferedCandles[i].Low = bufferedCandles[i-1].Open
  246. }
  247. }
  248. }
  249. }
  250. if myInsId == "bty_BTCUSDT" {
  251. if periodId == market.M1 {
  252. if bufferedCandles[i].Timestamp >= 1524035160000 && bufferedCandles[i].Timestamp <= 1524035520000 {
  253. bufferedCandles[i].High = bufferedCandles[i-1].High
  254. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  255. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  256. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  257. }
  258. if !bNormal && bufferedCandles[i].Timestamp > 1524035520000 {
  259. bNormal = true
  260. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  261. if bufferedCandles[i].High < bufferedCandles[i].Open {
  262. bufferedCandles[i].High = bufferedCandles[i].Open
  263. }
  264. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  265. bufferedCandles[i].Low = bufferedCandles[i].Open
  266. }
  267. }
  268. }
  269. if periodId == market.M5 {
  270. if bufferedCandles[i].Timestamp >= 1524035100000 && bufferedCandles[i].Timestamp <= 1524035400000 {
  271. bufferedCandles[i].High = bufferedCandles[i-1].High
  272. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  273. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  274. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  275. }
  276. if !bNormal && bufferedCandles[i].Timestamp > 1524035400000 {
  277. bNormal = true
  278. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  279. if bufferedCandles[i].High < bufferedCandles[i].Open {
  280. bufferedCandles[i].High = bufferedCandles[i].Open
  281. }
  282. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  283. bufferedCandles[i].Low = bufferedCandles[i].Open
  284. }
  285. }
  286. }
  287. if periodId == market.H1 {
  288. if bufferedCandles[i].Timestamp >= 1524034800000 && bufferedCandles[i].Timestamp <= 1524034800000 {
  289. bufferedCandles[i].High = bufferedCandles[i-1].High
  290. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  291. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  292. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  293. }
  294. if !bNormal && bufferedCandles[i].Timestamp > 1524034800000 {
  295. bNormal = true
  296. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  297. if bufferedCandles[i].High < bufferedCandles[i].Open {
  298. bufferedCandles[i].High = bufferedCandles[i].Open
  299. }
  300. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  301. bufferedCandles[i].Low = bufferedCandles[i].Open
  302. }
  303. }
  304. }
  305. if periodId == market.D1 {
  306. if bufferedCandles[i].Timestamp >= 1524009600000 && bufferedCandles[i].Timestamp <= 1524009600000 {
  307. if bufferedCandles[i].Open > bufferedCandles[i].Close {
  308. bufferedCandles[i].Low = bufferedCandles[i-1].Close
  309. } else {
  310. bufferedCandles[i].Low = bufferedCandles[i-1].Open
  311. }
  312. }
  313. }
  314. }
  315. if myInsId == "bty_BCCUSDT" {
  316. if periodId == market.M1 {
  317. if bufferedCandles[i].Timestamp >= 1524033780000 && bufferedCandles[i].Timestamp <= 1524038580000 {
  318. bufferedCandles[i].High = bufferedCandles[i-1].High
  319. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  320. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  321. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  322. }
  323. if !bNormal && bufferedCandles[i].Timestamp > 1524038580000 {
  324. bNormal = true
  325. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  326. if bufferedCandles[i].High < bufferedCandles[i].Open {
  327. bufferedCandles[i].High = bufferedCandles[i].Open
  328. }
  329. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  330. bufferedCandles[i].Low = bufferedCandles[i].Open
  331. }
  332. }
  333. }
  334. if periodId == market.M5 {
  335. if bufferedCandles[i].Timestamp >= 1524033600000 && bufferedCandles[i].Timestamp <= 1524038100000 {
  336. bufferedCandles[i].High = bufferedCandles[i-1].High
  337. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  338. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  339. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  340. }
  341. if !bNormal && bufferedCandles[i].Timestamp > 1524038100000 {
  342. bNormal = true
  343. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  344. if bufferedCandles[i].High < bufferedCandles[i].Open {
  345. bufferedCandles[i].High = bufferedCandles[i].Open
  346. }
  347. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  348. bufferedCandles[i].Low = bufferedCandles[i].Open
  349. }
  350. }
  351. }
  352. if periodId == market.H1 {
  353. if bufferedCandles[i].Timestamp >= 1524031200000 && bufferedCandles[i].Timestamp <= 1524034800000 {
  354. bufferedCandles[i].High = bufferedCandles[i-1].High
  355. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  356. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  357. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  358. }
  359. if !bNormal && bufferedCandles[i].Timestamp > 1524034800000 {
  360. bNormal = true
  361. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  362. if bufferedCandles[i].High < bufferedCandles[i].Open {
  363. bufferedCandles[i].High = bufferedCandles[i].Open
  364. }
  365. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  366. bufferedCandles[i].Low = bufferedCandles[i].Open
  367. }
  368. }
  369. }
  370. if periodId == market.D1 {
  371. if bufferedCandles[i].Timestamp >= 1524009600000 && bufferedCandles[i].Timestamp <= 1524009600000 {
  372. if bufferedCandles[i].Open > bufferedCandles[i].Close {
  373. bufferedCandles[i].Low = bufferedCandles[i-1].Close
  374. } else {
  375. bufferedCandles[i].Low = bufferedCandles[i-1].Open
  376. }
  377. }
  378. }
  379. }
  380. if myInsId == "bty_ETCUSDT" {
  381. if periodId == market.M1 {
  382. if bufferedCandles[i].Timestamp >= 1524033780000 && bufferedCandles[i].Timestamp <= 1524038520000 {
  383. bufferedCandles[i].High = bufferedCandles[i-1].High
  384. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  385. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  386. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  387. }
  388. if !bNormal && bufferedCandles[i].Timestamp > 1524038520000 {
  389. bNormal = true
  390. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  391. if bufferedCandles[i].High < bufferedCandles[i].Open {
  392. bufferedCandles[i].High = bufferedCandles[i].Open
  393. }
  394. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  395. bufferedCandles[i].Low = bufferedCandles[i].Open
  396. }
  397. }
  398. }
  399. if periodId == market.M5 {
  400. if bufferedCandles[i].Timestamp >= 1524033600000 && bufferedCandles[i].Timestamp <= 1524037800000 {
  401. bufferedCandles[i].High = bufferedCandles[i-1].High
  402. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  403. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  404. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  405. }
  406. if !bNormal && bufferedCandles[i].Timestamp > 1524037800000 {
  407. bNormal = true
  408. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  409. if bufferedCandles[i].High < bufferedCandles[i].Open {
  410. bufferedCandles[i].High = bufferedCandles[i].Open
  411. }
  412. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  413. bufferedCandles[i].Low = bufferedCandles[i].Open
  414. }
  415. }
  416. }
  417. if periodId == market.H1 {
  418. if bufferedCandles[i].Timestamp >= 1524031200000 && bufferedCandles[i].Timestamp <= 1524034800000 {
  419. bufferedCandles[i].High = bufferedCandles[i-1].High
  420. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  421. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  422. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  423. }
  424. if !bNormal && bufferedCandles[i].Timestamp > 1524034800000 {
  425. bNormal = true
  426. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  427. if bufferedCandles[i].High < bufferedCandles[i].Open {
  428. bufferedCandles[i].High = bufferedCandles[i].Open
  429. }
  430. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  431. bufferedCandles[i].Low = bufferedCandles[i].Open
  432. }
  433. }
  434. }
  435. if periodId == market.D1 {
  436. if bufferedCandles[i].Timestamp >= 1524009600000 && bufferedCandles[i].Timestamp <= 1524009600000 {
  437. if bufferedCandles[i].Open > bufferedCandles[i].Close {
  438. bufferedCandles[i].Low = bufferedCandles[i-1].Close
  439. } else {
  440. bufferedCandles[i].Low = bufferedCandles[i-1].Open
  441. }
  442. }
  443. }
  444. }
  445. if myInsId == "bty_ZECUSDT" {
  446. if periodId == market.M1 {
  447. if bufferedCandles[i].Timestamp >= 1524033780000 && bufferedCandles[i].Timestamp <= 1524038820000 {
  448. bufferedCandles[i].High = bufferedCandles[i-1].High
  449. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  450. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  451. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  452. }
  453. if !bNormal && bufferedCandles[i].Timestamp > 1524038820000 {
  454. bNormal = true
  455. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  456. if bufferedCandles[i].High < bufferedCandles[i].Open {
  457. bufferedCandles[i].High = bufferedCandles[i].Open
  458. }
  459. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  460. bufferedCandles[i].Low = bufferedCandles[i].Open
  461. }
  462. }
  463. }
  464. if periodId == market.M5 {
  465. if bufferedCandles[i].Timestamp >= 1524033600000 && bufferedCandles[i].Timestamp <= 1524038400000 {
  466. bufferedCandles[i].High = bufferedCandles[i-1].High
  467. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  468. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  469. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  470. }
  471. if !bNormal && bufferedCandles[i].Timestamp > 1524038400000 {
  472. bNormal = true
  473. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  474. if bufferedCandles[i].High < bufferedCandles[i].Open {
  475. bufferedCandles[i].High = bufferedCandles[i].Open
  476. }
  477. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  478. bufferedCandles[i].Low = bufferedCandles[i].Open
  479. }
  480. }
  481. }
  482. if periodId == market.H1 {
  483. if bufferedCandles[i].Timestamp >= 1524031200000 && bufferedCandles[i].Timestamp <= 1524034800000 {
  484. bufferedCandles[i].High = bufferedCandles[i-1].High
  485. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  486. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  487. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  488. }
  489. if !bNormal && bufferedCandles[i].Timestamp > 1524034800000 {
  490. bNormal = true
  491. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  492. if bufferedCandles[i].High < bufferedCandles[i].Open {
  493. bufferedCandles[i].High = bufferedCandles[i].Open
  494. }
  495. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  496. bufferedCandles[i].Low = bufferedCandles[i].Open
  497. }
  498. }
  499. }
  500. if periodId == market.D1 {
  501. if bufferedCandles[i].Timestamp >= 1524009600000 && bufferedCandles[i].Timestamp <= 1524009600000 {
  502. if bufferedCandles[i].Open > bufferedCandles[i].Close {
  503. bufferedCandles[i].Low = bufferedCandles[i-1].Close
  504. } else {
  505. bufferedCandles[i].Low = bufferedCandles[i-1].Open
  506. }
  507. }
  508. }
  509. }
  510. if myInsId == "bty_LTCUSDT" {
  511. if periodId == market.M1 {
  512. if bufferedCandles[i].Timestamp >= 1524033780000 && bufferedCandles[i].Timestamp <= 1524038760000 {
  513. bufferedCandles[i].High = bufferedCandles[i-1].High
  514. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  515. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  516. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  517. }
  518. if !bNormal && bufferedCandles[i].Timestamp > 1524038760000 {
  519. bNormal = true
  520. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  521. if bufferedCandles[i].High < bufferedCandles[i].Open {
  522. bufferedCandles[i].High = bufferedCandles[i].Open
  523. }
  524. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  525. bufferedCandles[i].Low = bufferedCandles[i].Open
  526. }
  527. }
  528. }
  529. if periodId == market.M5 {
  530. if bufferedCandles[i].Timestamp >= 1524033600000 && bufferedCandles[i].Timestamp <= 1524038400000 {
  531. bufferedCandles[i].High = bufferedCandles[i-1].High
  532. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  533. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  534. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  535. }
  536. if !bNormal && bufferedCandles[i].Timestamp > 1524038400000 {
  537. bNormal = true
  538. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  539. if bufferedCandles[i].High < bufferedCandles[i].Open {
  540. bufferedCandles[i].High = bufferedCandles[i].Open
  541. }
  542. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  543. bufferedCandles[i].Low = bufferedCandles[i].Open
  544. }
  545. }
  546. }
  547. if periodId == market.H1 {
  548. if bufferedCandles[i].Timestamp >= 1524031200000 && bufferedCandles[i].Timestamp <= 1524034800000 {
  549. bufferedCandles[i].High = bufferedCandles[i-1].High
  550. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  551. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  552. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  553. }
  554. if !bNormal && bufferedCandles[i].Timestamp > 1524034800000 {
  555. bNormal = true
  556. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  557. if bufferedCandles[i].High < bufferedCandles[i].Open {
  558. bufferedCandles[i].High = bufferedCandles[i].Open
  559. }
  560. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  561. bufferedCandles[i].Low = bufferedCandles[i].Open
  562. }
  563. }
  564. }
  565. if periodId == market.D1 {
  566. if bufferedCandles[i].Timestamp >= 1524009600000 && bufferedCandles[i].Timestamp <= 1524009600000 {
  567. if bufferedCandles[i].Open > bufferedCandles[i].Close {
  568. bufferedCandles[i].Low = bufferedCandles[i-1].Close
  569. } else {
  570. bufferedCandles[i].Low = bufferedCandles[i-1].Open
  571. }
  572. }
  573. }
  574. }
  575. if myInsId == "bty_DCRUSDT" {
  576. if periodId == market.M1 {
  577. if bufferedCandles[i].Timestamp >= 1524033840000 && bufferedCandles[i].Timestamp <= 1524038520000 {
  578. bufferedCandles[i].High = bufferedCandles[i-1].High
  579. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  580. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  581. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  582. }
  583. if !bNormal && bufferedCandles[i].Timestamp > 1524038520000 {
  584. bNormal = true
  585. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  586. if bufferedCandles[i].High < bufferedCandles[i].Open {
  587. bufferedCandles[i].High = bufferedCandles[i].Open
  588. }
  589. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  590. bufferedCandles[i].Low = bufferedCandles[i].Open
  591. }
  592. }
  593. }
  594. if periodId == market.M5 {
  595. if bufferedCandles[i].Timestamp >= 1524033900000 && bufferedCandles[i].Timestamp <= 1524038100000 {
  596. bufferedCandles[i].High = bufferedCandles[i-1].High
  597. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  598. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  599. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  600. }
  601. if !bNormal && bufferedCandles[i].Timestamp > 1524038100000 {
  602. bNormal = true
  603. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  604. if bufferedCandles[i].High < bufferedCandles[i].Open {
  605. bufferedCandles[i].High = bufferedCandles[i].Open
  606. }
  607. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  608. bufferedCandles[i].Low = bufferedCandles[i].Open
  609. }
  610. }
  611. }
  612. if periodId == market.H1 {
  613. if bufferedCandles[i].Timestamp >= 1524031200000 && bufferedCandles[i].Timestamp <= 1524034800000 {
  614. bufferedCandles[i].High = bufferedCandles[i-1].High
  615. bufferedCandles[i].Open = bufferedCandles[i-1].Open
  616. bufferedCandles[i].Close = bufferedCandles[i-1].Close
  617. bufferedCandles[i].Low = bufferedCandles[i-1].Low
  618. }
  619. if !bNormal && bufferedCandles[i].Timestamp > 1524034800000 {
  620. bNormal = true
  621. bufferedCandles[i].Open = bufferedCandles[i-1].Close
  622. if bufferedCandles[i].High < bufferedCandles[i].Open {
  623. bufferedCandles[i].High = bufferedCandles[i].Open
  624. }
  625. if bufferedCandles[i].Low > bufferedCandles[i].Open {
  626. bufferedCandles[i].Low = bufferedCandles[i].Open
  627. }
  628. }
  629. }
  630. if periodId == market.D1 {
  631. if bufferedCandles[i].Timestamp >= 1524009600000 && bufferedCandles[i].Timestamp <= 1524009600000 {
  632. if bufferedCandles[i].Open > bufferedCandles[i].Close {
  633. bufferedCandles[i].Low = bufferedCandles[i-1].Close
  634. } else {
  635. bufferedCandles[i].Low = bufferedCandles[i-1].Open
  636. }
  637. }
  638. }
  639. }
  640. }*/
  641. bufferedCandles = truncateBuffer(bufferedCandles, periodId, myInsId)
  642. var downN int
  643. var downTS int64
  644. switch action {
  645. case "init":
  646. if len(bufferedCandles) >= iCount {
  647. bufferedCandles = bufferedCandles[len(bufferedCandles)-iCount:]
  648. downN = 0
  649. downTS = 0
  650. } else {
  651. downN = -(iCount - len(bufferedCandles))
  652. downTS = -1
  653. }
  654. case "new":
  655. iTE, _ = strconv.ParseInt(te, 10, 64)
  656. iTE *= 1000
  657. beginTimeBuffered := int64(0x7fffffff)
  658. if len(bufferedCandles) > 0 {
  659. beginTimeBuffered = bufferedCandles[0].Timestamp
  660. }
  661. for cindex := 0; cindex < len(bufferedCandles); cindex++ {
  662. if bufferedCandles[cindex].Timestamp >= iTE {
  663. bufferedCandles = bufferedCandles[cindex:]
  664. break
  665. }
  666. }
  667. if beginTimeBuffered <= iTE {
  668. downN = 0
  669. downTS = 0
  670. if len(bufferedCandles) >= iCount {
  671. bufferedCandles = bufferedCandles[len(bufferedCandles)-iCount:]
  672. }
  673. } else {
  674. if len(bufferedCandles) >= iCount {
  675. bufferedCandles = bufferedCandles[len(bufferedCandles)-iCount:]
  676. downN = 0
  677. downTS = 0
  678. } else {
  679. if len(bufferedCandles) == 0 {
  680. downN = iCount
  681. downTS = iTE
  682. } else {
  683. downN = -(iCount - len(bufferedCandles))
  684. downTS = -1
  685. }
  686. }
  687. }
  688. case "down":
  689. iTS, _ = strconv.ParseInt(ts, 10, 64)
  690. iTS *= 1000
  691. for cindex := 0; cindex < len(bufferedCandles); cindex++ {
  692. if bufferedCandles[cindex].Timestamp > iTS {
  693. bufferedCandles = bufferedCandles[:cindex]
  694. break
  695. }
  696. }
  697. if len(bufferedCandles) >= iCount {
  698. bufferedCandles = bufferedCandles[len(bufferedCandles)-iCount:]
  699. downN = 0
  700. downTS = 0
  701. } else {
  702. if len(bufferedCandles) == 0 {
  703. downN = -iCount
  704. downTS = iTS
  705. } else {
  706. downN = -(iCount - len(bufferedCandles))
  707. downTS = -1
  708. }
  709. }
  710. }
  711. //log.Println("step2", len(bufferedCandles), downN, downTS)
  712. var filecandles []client.Candle
  713. _, filecandles, _ = clientDown.GetHistory(myInsId, periodId, downN, downTS)
  714. //log.Println("step3", len(filecandles))
  715. candles := make([]client.Candle, len(bufferedCandles)+len(filecandles))
  716. for findex := 0; findex <= 1; findex++ {
  717. if findex == 1 {
  718. for iBuf := 0; iBuf < len(bufferedCandles); iBuf++ {
  719. //bufferedCandles[iBuf] = changeCandle(bufferedCandles[iBuf], strings.ToUpper(period), strings.ToUpper(symbol))
  720. candles[len(filecandles)+iBuf] = bufferedCandles[iBuf]
  721. }
  722. } else {
  723. if downN < 0 {
  724. for iFile := len(filecandles) - 1; iFile >= 0; iFile-- {
  725. //filecandles[iFile] = changeCandle(filecandles[iFile], strings.ToUpper(period), strings.ToUpper(symbol))
  726. candles[len(filecandles)-iFile-1] = filecandles[iFile]
  727. }
  728. } else {
  729. for iFile := 0; iFile < len(filecandles); iFile++ {
  730. //filecandles[iFile] = changeCandle(filecandles[iFile], strings.ToUpper(period), strings.ToUpper(symbol))
  731. candles[iFile] = filecandles[iFile]
  732. }
  733. }
  734. }
  735. }
  736. var tData TData
  737. var kData KData
  738. if K_STYLE == style {
  739. kData.C = symbol
  740. kData.P = period
  741. kData.Action = action
  742. }
  743. if len(candles) >= iCount {
  744. if T_STYLE == style {
  745. tData.End = 0
  746. }
  747. if K_STYLE == style {
  748. kData.End = 0
  749. }
  750. } else {
  751. if T_STYLE == style {
  752. tData.End = 1
  753. }
  754. if K_STYLE == style {
  755. kData.End = 1
  756. }
  757. }
  758. if periodIdOld == market.M15 || periodIdOld == market.M30 ||
  759. periodIdOld == market.H2 || periodIdOld == market.H4 ||
  760. periodIdOld == market.W1 || periodIdOld == market.MN1 {
  761. candles = makeCandle(candles, periodIdOld, myInsId)
  762. }
  763. var candleLast client.Candle
  764. for cindex := 0; cindex < len(candles); cindex++ {
  765. if candles[cindex].Timestamp < candleLast.Timestamp {
  766. continue
  767. }
  768. bduplicated := false
  769. if candles[cindex].Timestamp == candleLast.Timestamp {
  770. bduplicated = true
  771. }
  772. if action == "down" && candles[cindex].Timestamp > iTS {
  773. break
  774. }
  775. if action == "new" && candles[cindex].Timestamp < iTE {
  776. continue
  777. }
  778. if candles[cindex].Open == 0 ||
  779. candles[cindex].High == 0 ||
  780. candles[cindex].Low == 0 ||
  781. candles[cindex].Close == 0 {
  782. candles[cindex] = candleLast
  783. }
  784. if bduplicated {
  785. if candleLast.RealVolums < candles[cindex].RealVolums {
  786. if T_STYLE == style {
  787. tData.Candles[len(tData.Candles)-1] = candles[cindex]
  788. }
  789. if K_STYLE == style {
  790. kData.Candles.X[len(kData.Candles.X)-1] = int(candles[cindex].Timestamp / 1000)
  791. kData.Candles.Y[len(kData.Candles.Y)-1] = [5]float32{float32(candles[cindex].Open), float32(candles[cindex].High), float32(candles[cindex].Low), float32(candles[cindex].Close), float32(candles[cindex].RealVolums)}
  792. kData.Volumes.X[len(kData.Volumes.X)-1] = int(candles[cindex].Timestamp / 1000)
  793. kData.Volumes.Y[len(kData.Volumes.Y)-1] = float32(candles[cindex].RealVolums)
  794. }
  795. candleLast = candles[cindex]
  796. }
  797. } else {
  798. if T_STYLE == style {
  799. tData.Candles = append(tData.Candles, candles[cindex])
  800. }
  801. if K_STYLE == style {
  802. kData.Candles.X = append(kData.Candles.X, int(candles[cindex].Timestamp/1000))
  803. kData.Candles.Y = append(kData.Candles.Y, [5]float32{float32(candles[cindex].Open), float32(candles[cindex].High), float32(candles[cindex].Low), float32(candles[cindex].Close), float32(candles[cindex].RealVolums)})
  804. kData.Volumes.X = append(kData.Volumes.X, int(candles[cindex].Timestamp/1000))
  805. kData.Volumes.Y = append(kData.Volumes.Y, float32(candles[cindex].RealVolums))
  806. }
  807. candleLast = candles[cindex]
  808. }
  809. }
  810. var dataLen int
  811. if T_STYLE == style {
  812. dataLen = len(tData.Candles)
  813. }
  814. if K_STYLE == style {
  815. dataLen = len(kData.Candles.X)
  816. }
  817. if dataLen >= iCount {
  818. if T_STYLE == style {
  819. tData.Candles = tData.Candles[dataLen-(iCount-1):]
  820. }
  821. if K_STYLE == style {
  822. kData.Candles.X = kData.Candles.X[dataLen-(iCount-1):]
  823. kData.Candles.Y = kData.Candles.Y[dataLen-(iCount-1):]
  824. kData.Volumes.X = kData.Volumes.X[dataLen-(iCount-1):]
  825. kData.Volumes.Y = kData.Volumes.Y[dataLen-(iCount-1):]
  826. }
  827. }
  828. var b []byte
  829. if K_STYLE == style {
  830. b, err = json.Marshal(&kData)
  831. }
  832. if T_STYLE == style {
  833. b, err = json.Marshal(&tData)
  834. }
  835. if err != nil {
  836. return "", errors.New("marshal error")
  837. }
  838. var output string
  839. if out == "jsonp" {
  840. output = fmt.Sprintf("if (%s) %s(%s)\n", cb, cb, string(b))
  841. } else {
  842. output = string(b)
  843. }
  844. return output, nil
  845. }
  846. func kdataHandler(w http.ResponseWriter, r *http.Request) {
  847. symbol := r.FormValue("c")
  848. period := r.FormValue("p")
  849. action := r.FormValue("action")
  850. count := r.FormValue("count")
  851. out := r.FormValue("out")
  852. cb := r.FormValue("callback")
  853. var ts, te string
  854. if action == "new" {
  855. te = r.FormValue("te")
  856. }
  857. if action == "down" {
  858. ts = r.FormValue("ts")
  859. }
  860. output, err := getData(symbol, period, action, count, out, cb, te, ts, K_STYLE)
  861. if err != nil {
  862. http.Error(w, err.Error(), http.StatusBadRequest)
  863. return
  864. }
  865. if out != "jsonp" {
  866. w.Header().Set("Access-Control-Allow-Origin", "*")
  867. w.Header().Add("Access-Control-Allow-Headers", "Content-Type")
  868. w.Header().Set("content-type", "application/json")
  869. }
  870. io.WriteString(w, output)
  871. }
  872. func tdataHandler(w http.ResponseWriter, r *http.Request) {
  873. symbol := r.FormValue("c")
  874. period := r.FormValue("p")
  875. action := r.FormValue("action")
  876. count := r.FormValue("count")
  877. out := r.FormValue("out")
  878. cb := r.FormValue("callback")
  879. var ts, te string
  880. if action == "new" {
  881. te = r.FormValue("te")
  882. }
  883. if action == "down" {
  884. ts = r.FormValue("ts")
  885. }
  886. output, err := getData(symbol, period, action, count, out, cb, te, ts, T_STYLE)
  887. if err != nil {
  888. http.Error(w, err.Error(), http.StatusBadRequest)
  889. return
  890. }
  891. if out != "jsonp" {
  892. w.Header().Set("Access-Control-Allow-Origin", "*")
  893. w.Header().Add("Access-Control-Allow-Headers", "Content-Type")
  894. w.Header().Set("content-type", "application/json")
  895. }
  896. io.WriteString(w, output)
  897. }
  898. func makeCandle(candles []client.Candle, periodIdOld int, insId string) []client.Candle {
  899. var candlestmp []market.Candle
  900. for _, v := range candles {
  901. candlestmp = append(candlestmp, market.Candle{
  902. Timestamp: v.Timestamp,
  903. Open: v.Open,
  904. High: v.High,
  905. Low: v.Low,
  906. Close: v.Close,
  907. RealVolums: v.RealVolums,
  908. TickVolums: v.TickVolums,
  909. })
  910. }
  911. r := market.NewCandleBuf(candlestmp)
  912. candlestmpnew, _ := market.ConvPeriod(r, insId, periodIdOld)
  913. candles = candles[0:0]
  914. for _, v := range candlestmpnew {
  915. candles = append(candles, client.Candle{
  916. Timestamp: v.Timestamp,
  917. Open: v.Open,
  918. High: v.High,
  919. Low: v.Low,
  920. Close: v.Close,
  921. RealVolums: v.RealVolums,
  922. TickVolums: v.TickVolums,
  923. })
  924. }
  925. return candles
  926. }
  927. func truncateBuffer(bufferedCandles []client.Candle, periodId int, insId string) []client.Candle {
  928. var filecandlelatesttime int64
  929. tmpDownN := -2
  930. var tmpfilecandles []client.Candle
  931. _, tmpfilecandles, _ = clientDown.GetHistory(insId, periodId, tmpDownN, -1)
  932. if len(tmpfilecandles) > 1 {
  933. filecandlelatesttime = tmpfilecandles[1].Timestamp
  934. /*if insId != "bty_BTYUSDT" && len(bufferedCandles) > 0 && bufferedCandles[0].Timestamp > tmpfilecandles[0].Timestamp {
  935. bufferedCandlesTmp := bufferedCandles[:]
  936. bufferedCandles = make([]client.Candle, len(bufferedCandlesTmp))
  937. for timestampAdded := tmpfilecandles[0].Timestamp + int64(periodId*1000); timestampAdded < bufferedCandlesTmp[0].Timestamp; timestampAdded += int64(periodId * 1000) {
  938. candleAdded := bufferedCandlesTmp[0]
  939. candleAdded.Timestamp = timestampAdded
  940. candleAdded.TickVolums = 0
  941. candleAdded.RealVolums = 0
  942. bufferedCandles = append(bufferedCandles, candleAdded)
  943. }
  944. bufferedCandles = append(bufferedCandles, bufferedCandlesTmp...)
  945. }*/
  946. var iPos int
  947. for iPos = 0; iPos < len(bufferedCandles); iPos++ {
  948. if bufferedCandles[iPos].Timestamp > filecandlelatesttime {
  949. break
  950. }
  951. }
  952. bufferedCandles = bufferedCandles[iPos:]
  953. }
  954. return bufferedCandles
  955. }
  956. func patchingByVolume(candles []client.Candle, periodId int, bAsc bool, bBuffered bool) []client.Candle {
  957. bNeeded := false
  958. for i := 0; i < len(candles); i++ {
  959. if candles[i].TickVolums == 0 {
  960. bNeeded = true
  961. break
  962. }
  963. }
  964. if bNeeded {
  965. var st, et int64
  966. var posHuobi int
  967. //var posOkCoin int
  968. if bAsc {
  969. st = candles[0].Timestamp
  970. et = candles[len(candles)-1].Timestamp
  971. } else {
  972. st = candles[len(candles)-1].Timestamp
  973. et = candles[0].Timestamp
  974. }
  975. var candlesHuobi []client.Candle
  976. if bBuffered {
  977. candlesHuobi, _ = clientDown.GetLastCandles("huobi_BTCCNY", periodId, 0x7fffffff)
  978. //candlesOkCoin, _ = clientDown.GetLastCandles("btc_BTCCNY", periodId, 0x7fffffff)
  979. } else {
  980. _, candlesHuobi, _ = clientDown.GetHistoryEx("huobi_BTCCNY", periodId, st, et)
  981. //_, candlesOkCoin, _ = clientDown.GetHistoryEx("btc_BTCCNY", periodId, st, et)
  982. }
  983. if bAsc {
  984. posHuobi = 0
  985. //posOkCoin = 0
  986. } else {
  987. posHuobi = len(candlesHuobi) - 1
  988. //posOkCoin = len(filecandlesOkCoin) - 1
  989. }
  990. candlesTmp := candles[:]
  991. candles = candles[0:0]
  992. for i := 0; i < len(candlesTmp); i++ {
  993. if candlesTmp[i].TickVolums == 0 {
  994. count := 0
  995. var candleHuobi client.Candle
  996. //var candleOkCoin client.Candle
  997. if bAsc {
  998. for j := posHuobi; j < len(candlesHuobi); j++ {
  999. if candlesHuobi[j].Timestamp == candlesTmp[i].Timestamp {
  1000. posHuobi = j
  1001. count++
  1002. candleHuobi = candlesHuobi[j]
  1003. break
  1004. }
  1005. if candlesHuobi[j].Timestamp > candlesTmp[i].Timestamp {
  1006. posHuobi = j
  1007. break
  1008. }
  1009. }
  1010. /*for k := posOkCoin; k < len(filecandlesOkCoin); k++ {
  1011. if filecandlesOkCoin[k].Timestamp == filecandlesTmp[i].Timestamp {
  1012. posOkCoin = k
  1013. count++
  1014. candleOkCoin = filecandlesOkCoin[k]
  1015. break
  1016. }
  1017. if filecandlesOkCoin[k].Timestamp > filecandlesTmp[i].Timestamp {
  1018. posOkCoin = k
  1019. break
  1020. }
  1021. }*/
  1022. } else {
  1023. for j := posHuobi; j >= 0; j-- {
  1024. if candlesHuobi[j].Timestamp == candlesTmp[i].Timestamp {
  1025. posHuobi = j
  1026. count++
  1027. candleHuobi = candlesHuobi[j]
  1028. break
  1029. }
  1030. if candlesHuobi[j].Timestamp < candlesTmp[i].Timestamp {
  1031. posHuobi = j
  1032. break
  1033. }
  1034. }
  1035. /*for k := posOkCoin; k >= 0; k-- {
  1036. if filecandlesOkCoin[k].Timestamp == filecandlesTmp[i].Timestamp {
  1037. posOkCoin = k
  1038. count++
  1039. candleOkCoin = filecandlesOkCoin[k]
  1040. break
  1041. }
  1042. if filecandlesOkCoin[k].Timestamp < filecandlesTmp[i].Timestamp {
  1043. posOkCoin = k
  1044. break
  1045. }
  1046. }*/
  1047. }
  1048. if count > 0 {
  1049. var candleComplex client.Candle
  1050. count = 0
  1051. if candleHuobi.Open > 0 && candleHuobi.High > 0 && candleHuobi.Low > 0 && candleHuobi.Close > 0 {
  1052. count++
  1053. candleComplex.Open += candleHuobi.Open
  1054. candleComplex.High += candleHuobi.High
  1055. candleComplex.Low += candleHuobi.Low
  1056. candleComplex.Close += candleHuobi.Close
  1057. }
  1058. /*if candleOkCoin.Open > 0 && candleOkCoin.High > 0 && candleOkCoin.Low > 0 && candleOkCoin.Close > 0 {
  1059. count++
  1060. candleComplex.Open += candleOkCoin.Open
  1061. candleComplex.High += candleOkCoin.High
  1062. candleComplex.Low += candleOkCoin.Low
  1063. candleComplex.Close += candleOkCoin.Close
  1064. }*/
  1065. //filecandles = append(filecandles, client.Candle{Timestamp: filecandlesTmp[i].Timestamp, Open: candleComplex.Open / float64(count),
  1066. //High: candleComplex.High / float64(count), Low: candleComplex.Low / float64(count),
  1067. //Close: candleComplex.Close / float64(count), RealVolums: (candleHuobi.TickVolums + candleOkCoin.TickVolums) * 150, TickVolums: (candleHuobi.TickVolums + candleOkCoin.TickVolums) / 2})
  1068. candleHuobi.RealVolums /= 1e6
  1069. candles = append(candles, candleHuobi)
  1070. //log.Println("1", candleHuobi)
  1071. } else {
  1072. candles = append(candles, candlesTmp[i])
  1073. //log.Println("2", candlesTmp[i])
  1074. }
  1075. } else {
  1076. candles = append(candles, candlesTmp[i])
  1077. //log.Println("3", candlesTmp[i])
  1078. }
  1079. }
  1080. }
  1081. return candles
  1082. }
  1083. func patchingByTime(candles []client.Candle, periodId int, bAsc bool) []client.Candle {
  1084. bNeedAdd := false
  1085. for i := 0; i < len(candles); i++ {
  1086. if bAsc {
  1087. if i > 0 && ((candles[i].Timestamp - candles[i-1].Timestamp) > int64(periodId*1000)) {
  1088. bNeedAdd = true
  1089. break
  1090. }
  1091. } else {
  1092. if i > 0 && ((candles[i-1].Timestamp - candles[i].Timestamp) > int64(periodId*1000)) {
  1093. bNeedAdd = true
  1094. break
  1095. }
  1096. }
  1097. }
  1098. if bNeedAdd {
  1099. candlesTmp := make([]client.Candle, len(candles))
  1100. for k, v := range candles {
  1101. candlesTmp[k] = v
  1102. }
  1103. candles = candles[0:0]
  1104. for i := 0; i < len(candlesTmp); i++ {
  1105. if bAsc {
  1106. if i > 0 && ((candlesTmp[i].Timestamp - candlesTmp[i-1].Timestamp) > int64(periodId*1000)) {
  1107. for timestampAdded := candlesTmp[i-1].Timestamp + int64(periodId*1000); timestampAdded < candlesTmp[i].Timestamp; timestampAdded += int64(periodId * 1000) {
  1108. candleAdded := candlesTmp[i-1]
  1109. candleAdded.Timestamp = timestampAdded
  1110. candleAdded.TickVolums = 0
  1111. candles = append(candles, candleAdded)
  1112. }
  1113. }
  1114. } else {
  1115. if i > 0 && ((candlesTmp[i-1].Timestamp - candlesTmp[i].Timestamp) > int64(periodId*1000)) {
  1116. for timestampAdded := candlesTmp[i-1].Timestamp - int64(periodId*1000); timestampAdded > candlesTmp[i].Timestamp; timestampAdded -= int64(periodId * 1000) {
  1117. candleAdded := candlesTmp[i-1]
  1118. candleAdded.Timestamp = timestampAdded
  1119. candleAdded.TickVolums = 0
  1120. candles = append(candles, candleAdded)
  1121. }
  1122. }
  1123. }
  1124. candles = append(candles, candlesTmp[i])
  1125. }
  1126. }
  1127. return candles
  1128. }
  1129. type gzipResponseWriter struct {
  1130. io.Writer
  1131. http.ResponseWriter
  1132. }
  1133. func (w gzipResponseWriter) Write(b []byte) (int, error) {
  1134. return w.Writer.Write(b)
  1135. }
  1136. func (w gzipResponseWriter) Flush() {
  1137. w.Writer.(*gzip.Writer).Flush()
  1138. w.ResponseWriter.(http.Flusher).Flush()
  1139. }
  1140. func makeGzipHandler(fn http.HandlerFunc) http.HandlerFunc {
  1141. return func(w http.ResponseWriter, r *http.Request) {
  1142. if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
  1143. fn(w, r)
  1144. return
  1145. }
  1146. w.Header().Set("Content-Encoding", "gzip")
  1147. w.Header().Set("Content-Type", "text/javascript")
  1148. gz := gzip.NewWriter(w)
  1149. defer gz.Close()
  1150. fn(gzipResponseWriter{Writer: gz, ResponseWriter: w}, r)
  1151. }
  1152. }
  1153. func main() {
  1154. runtime.GOMAXPROCS(runtime.NumCPU())
  1155. conf, err := readConf()
  1156. if err != nil {
  1157. flag.Parse()
  1158. } else {
  1159. *saddr1 = conf.Saddr1
  1160. *saddr2 = conf.Saddr2
  1161. *saddr3 = conf.Saddr3
  1162. *saddr4 = conf.Saddr4
  1163. }
  1164. err = connectServer()
  1165. if err != nil {
  1166. log.Fatal("connect server", err)
  1167. }
  1168. s := &http.Server{
  1169. //Addr: ":3062",
  1170. //Addr: ":4062",
  1171. Addr: ":7062",
  1172. //Addr: ":9062",
  1173. ReadTimeout: 10 * time.Second,
  1174. WriteTimeout: 10 * time.Second,
  1175. MaxHeaderBytes: 1 << 20,
  1176. }
  1177. http.HandleFunc("/kdata", makeGzipHandler(kdataHandler))
  1178. http.HandleFunc("/tdata", makeGzipHandler(tdataHandler))
  1179. //log.Fatal(s.ListenAndServeTLS("214341259320977.pem", "214341259320977.key"))
  1180. log.Fatal(s.ListenAndServe())
  1181. //log.Fatal(s.ListenAndServeTLS("licai20170620.pem", "licai20170620.key"))
  1182. }