main.go 40 KB

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