main.go 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  1. // main.go
  2. package main
  3. import (
  4. "compress/gzip"
  5. "encoding/json"
  6. "errors"
  7. "flag"
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. "log"
  12. "net/http"
  13. "os"
  14. "runtime"
  15. "strconv"
  16. "strings"
  17. "sync"
  18. "time"
  19. "tickserver/client"
  20. "tickserver/framework/base"
  21. "tickserver/markinfo"
  22. "tickserver/server/market"
  23. )
  24. type Hadax struct {
  25. Status string `json:"status"`
  26. Ch string `json:"ch"`
  27. Ts int64 `json:"ts"`
  28. Data []Kdata `json:"data"`
  29. }
  30. type Kdata struct {
  31. Id int64 `json:"id"`
  32. Open float64 `json:"open"`
  33. Close float64 `json:"close"`
  34. Low float64 `json:"low"`
  35. High float64 `json:"high"`
  36. Amount float64 `json:"amount"`
  37. Vol float64 `json:"vol"`
  38. Count int64 `json:"count"`
  39. }
  40. type RealTick struct {
  41. tg base.TickGo
  42. //tgCh <-chan base.TickGo
  43. now int64
  44. }
  45. type Options struct {
  46. Start int `json:"start"`
  47. End int `json:"end"`
  48. Total int `json:"total_size"`
  49. }
  50. type KCandle struct {
  51. X []int `json:"x"`
  52. Y [][5]float32 `json:"y"`
  53. }
  54. type KVolume struct {
  55. X []int `json:"x"`
  56. Y []int `json:"y"`
  57. }
  58. type KData struct {
  59. C string `json:"c"`
  60. P string `json:"p"`
  61. Action string `json:"action"`
  62. KOptions Options `json:"options"`
  63. Candles KCandle `json:"main"`
  64. Volumes KVolume `json:"volumes|||__ignore__"`
  65. }
  66. type HistoryData struct {
  67. C string `json:"c"`
  68. P string `json:"p"`
  69. Action string `json:"action"`
  70. End int64 `json:"end"`
  71. MainData [][6]string `json:"main"`
  72. Easyforex [][6]string `json:"easyforex"`
  73. Oanda [][6]string `json:"oanda"`
  74. }
  75. type SymbolData struct {
  76. Unit float64
  77. PriceIncrement float64
  78. Symbol int
  79. Name string
  80. }
  81. //type byDlInfo []market.DlInfo
  82. //func (a byDlInfo) Len() int { return len(a) }
  83. //func (a byDlInfo) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  84. //func (a byDlInfo) Less(i, j int) bool { return a[i].Reply.St < a[j].Reply.St }
  85. var saddr1 = flag.String("s1", "127.0.0.1:19528", "tick server address 1") //115.231.103.7
  86. var saddr2 = flag.String("s2", "127.0.0.1:19528", "tick server address 2") //127.0.0.1
  87. var saddr3 = flag.String("s3", "127.0.0.1:19529", "tick server address 3") //115.236.75.194
  88. var saddr4 = flag.String("s4", "127.0.0.1:9090", "tick server address 4") //19528 9090
  89. var clientSub *client.ClientSimple
  90. var clientDown *client.ClientSimple
  91. var tkMap = make(map[string]base.TickGo)
  92. var tkMutex sync.Mutex
  93. type Conf struct {
  94. Saddr1 string //
  95. Saddr2 string //
  96. Saddr3 string //
  97. Saddr4 string
  98. }
  99. type SpecialTick struct {
  100. Timestamp int64 `json:"ts"`
  101. Open float64 `json:"open"`
  102. High float64 `json:"high"`
  103. Low float64 `json:"low"`
  104. Last float64 `json:"last"`
  105. Volume float64 `json:"volume"`
  106. }
  107. type StringSpecialTick struct {
  108. Timestamp string `json:"ts"`
  109. Open string `json:"open"`
  110. High string `json:"high"`
  111. Low string `json:"low"`
  112. Last string `json:"last"`
  113. Volume string `json:"volume"`
  114. }
  115. func readConf() (*Conf, error) {
  116. f, err := os.Open("webproxy.json")
  117. if err != nil {
  118. return nil, err
  119. }
  120. defer f.Close()
  121. dec := json.NewDecoder(f)
  122. conf := &Conf{}
  123. err = dec.Decode(conf)
  124. if err != nil {
  125. return nil, err
  126. }
  127. return conf, nil
  128. }
  129. func getStringSTK(stk SpecialTick, insId string) StringSpecialTick {
  130. sstk := StringSpecialTick{
  131. Timestamp: fmt.Sprintf("%d", stk.Timestamp),
  132. }
  133. switch insId {
  134. case "bty_BCCBTC":
  135. sstk.Open = fmt.Sprintf("%.4f", stk.Open)
  136. sstk.Last = fmt.Sprintf("%.4f", stk.Last)
  137. sstk.High = fmt.Sprintf("%.4f", stk.High)
  138. sstk.Low = fmt.Sprintf("%.4f", stk.Low)
  139. sstk.Volume = fmt.Sprintf("%.4f", stk.Volume)
  140. case "bty_ETHBTC":
  141. sstk.Open = fmt.Sprintf("%.5f", stk.Open)
  142. sstk.Last = fmt.Sprintf("%.5f", stk.Last)
  143. sstk.High = fmt.Sprintf("%.5f", stk.High)
  144. sstk.Low = fmt.Sprintf("%.5f", stk.Low)
  145. sstk.Volume = fmt.Sprintf("%.3f", stk.Volume)
  146. case "bty_ETCBTC":
  147. sstk.Open = fmt.Sprintf("%.6f", stk.Open)
  148. sstk.Last = fmt.Sprintf("%.6f", stk.Last)
  149. sstk.High = fmt.Sprintf("%.6f", stk.High)
  150. sstk.Low = fmt.Sprintf("%.6f", stk.Low)
  151. sstk.Volume = fmt.Sprintf("%.2f", stk.Volume)
  152. case "bty_ZECBTC":
  153. sstk.Open = fmt.Sprintf("%.5f", stk.Open)
  154. sstk.Last = fmt.Sprintf("%.5f", stk.Last)
  155. sstk.High = fmt.Sprintf("%.5f", stk.High)
  156. sstk.Low = fmt.Sprintf("%.5f", stk.Low)
  157. sstk.Volume = fmt.Sprintf("%.3f", stk.Volume)
  158. case "bty_LTCBTC":
  159. sstk.Open = fmt.Sprintf("%.6f", stk.Open)
  160. sstk.Last = fmt.Sprintf("%.6f", stk.Last)
  161. sstk.High = fmt.Sprintf("%.6f", stk.High)
  162. sstk.Low = fmt.Sprintf("%.6f", stk.Low)
  163. sstk.Volume = fmt.Sprintf("%.2f", stk.Volume)
  164. case "bty_BTCUSDT":
  165. sstk.Open = fmt.Sprintf("%.2f", stk.Open)
  166. sstk.Last = fmt.Sprintf("%.2f", stk.Last)
  167. sstk.High = fmt.Sprintf("%.2f", stk.High)
  168. sstk.Low = fmt.Sprintf("%.2f", stk.Low)
  169. sstk.Volume = fmt.Sprintf("%.4f", stk.Volume)
  170. case "bty_BCCUSDT":
  171. sstk.Open = fmt.Sprintf("%.2f", stk.Open)
  172. sstk.Last = fmt.Sprintf("%.2f", stk.Last)
  173. sstk.High = fmt.Sprintf("%.2f", stk.High)
  174. sstk.Low = fmt.Sprintf("%.2f", stk.Low)
  175. sstk.Volume = fmt.Sprintf("%.4f", stk.Volume)
  176. case "bty_ETHUSDT":
  177. sstk.Open = fmt.Sprintf("%.2f", stk.Open)
  178. sstk.Last = fmt.Sprintf("%.2f", stk.Last)
  179. sstk.High = fmt.Sprintf("%.2f", stk.High)
  180. sstk.Low = fmt.Sprintf("%.2f", stk.Low)
  181. sstk.Volume = fmt.Sprintf("%.3f", stk.Volume)
  182. case "bty_ETCUSDT":
  183. sstk.Open = fmt.Sprintf("%.2f", stk.Open)
  184. sstk.Last = fmt.Sprintf("%.2f", stk.Last)
  185. sstk.High = fmt.Sprintf("%.2f", stk.High)
  186. sstk.Low = fmt.Sprintf("%.2f", stk.Low)
  187. sstk.Volume = fmt.Sprintf("%.2f", stk.Volume)
  188. case "bty_ZECUSDT":
  189. sstk.Open = fmt.Sprintf("%.2f", stk.Open)
  190. sstk.Last = fmt.Sprintf("%.2f", stk.Last)
  191. sstk.High = fmt.Sprintf("%.2f", stk.High)
  192. sstk.Low = fmt.Sprintf("%.2f", stk.Low)
  193. sstk.Volume = fmt.Sprintf("%.3f", stk.Volume)
  194. case "bty_LTCUSDT":
  195. sstk.Open = fmt.Sprintf("%.2f", stk.Open)
  196. sstk.Last = fmt.Sprintf("%.2f", stk.Last)
  197. sstk.High = fmt.Sprintf("%.2f", stk.High)
  198. sstk.Low = fmt.Sprintf("%.2f", stk.Low)
  199. sstk.Volume = fmt.Sprintf("%.2f", stk.Volume)
  200. case "bty_YCCUSDT":
  201. sstk.Open = fmt.Sprintf("%.6f", stk.Open)
  202. sstk.Last = fmt.Sprintf("%.6f", stk.Last)
  203. sstk.High = fmt.Sprintf("%.6f", stk.High)
  204. sstk.Low = fmt.Sprintf("%.6f", stk.Low)
  205. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  206. case "bty_BTYUSDT":
  207. sstk.Open = fmt.Sprintf("%.4f", stk.Open)
  208. sstk.Last = fmt.Sprintf("%.4f", stk.Last)
  209. sstk.High = fmt.Sprintf("%.4f", stk.High)
  210. sstk.Low = fmt.Sprintf("%.4f", stk.Low)
  211. sstk.Volume = fmt.Sprintf("%.1f", stk.Volume)
  212. case "bty_BTCSUSDT":
  213. sstk.Open = fmt.Sprintf("%.2f", stk.Open)
  214. sstk.Last = fmt.Sprintf("%.2f", stk.Last)
  215. sstk.High = fmt.Sprintf("%.2f", stk.High)
  216. sstk.Low = fmt.Sprintf("%.2f", stk.Low)
  217. sstk.Volume = fmt.Sprintf("%.2f", stk.Volume)
  218. case "bty_SCUSDT":
  219. sstk.Open = fmt.Sprintf("%.6f", stk.Open)
  220. sstk.Last = fmt.Sprintf("%.6f", stk.Last)
  221. sstk.High = fmt.Sprintf("%.6f", stk.High)
  222. sstk.Low = fmt.Sprintf("%.6f", stk.Low)
  223. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  224. case "bty_BTSUSDT":
  225. sstk.Open = fmt.Sprintf("%.4f", stk.Open)
  226. sstk.Last = fmt.Sprintf("%.4f", stk.Last)
  227. sstk.High = fmt.Sprintf("%.4f", stk.High)
  228. sstk.Low = fmt.Sprintf("%.4f", stk.Low)
  229. sstk.Volume = fmt.Sprintf("%.2f", stk.Volume)
  230. case "bty_DCRUSDT":
  231. sstk.Open = fmt.Sprintf("%.2f", stk.Open)
  232. sstk.Last = fmt.Sprintf("%.2f", stk.Last)
  233. sstk.High = fmt.Sprintf("%.2f", stk.High)
  234. sstk.Low = fmt.Sprintf("%.2f", stk.Low)
  235. sstk.Volume = fmt.Sprintf("%.2f", stk.Volume)
  236. case "bty_BNTUSDT":
  237. sstk.Open = fmt.Sprintf("%.4f", stk.Open)
  238. sstk.Last = fmt.Sprintf("%.4f", stk.Last)
  239. sstk.High = fmt.Sprintf("%.4f", stk.High)
  240. sstk.Low = fmt.Sprintf("%.4f", stk.Low)
  241. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  242. case "bty_BNTBTC":
  243. sstk.Open = fmt.Sprintf("%.8f", stk.Open)
  244. sstk.Last = fmt.Sprintf("%.8f", stk.Last)
  245. sstk.High = fmt.Sprintf("%.8f", stk.High)
  246. sstk.Low = fmt.Sprintf("%.8f", stk.Low)
  247. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  248. case "bty_SCTCUSDT":
  249. sstk.Open = fmt.Sprintf("%.6f", stk.Open)
  250. sstk.Last = fmt.Sprintf("%.6f", stk.Last)
  251. sstk.High = fmt.Sprintf("%.6f", stk.High)
  252. sstk.Low = fmt.Sprintf("%.6f", stk.Low)
  253. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  254. case "bty_SCTCBTC":
  255. sstk.Open = fmt.Sprintf("%.8f", stk.Open)
  256. sstk.Last = fmt.Sprintf("%.8f", stk.Last)
  257. sstk.High = fmt.Sprintf("%.8f", stk.High)
  258. sstk.Low = fmt.Sprintf("%.8f", stk.Low)
  259. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  260. case "bty_YCCBTC":
  261. sstk.Open = fmt.Sprintf("%.8f", stk.Open)
  262. sstk.Last = fmt.Sprintf("%.8f", stk.Last)
  263. sstk.High = fmt.Sprintf("%.8f", stk.High)
  264. sstk.Low = fmt.Sprintf("%.8f", stk.Low)
  265. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  266. case "bty_YCCETH":
  267. sstk.Open = fmt.Sprintf("%.8f", stk.Open)
  268. sstk.Last = fmt.Sprintf("%.8f", stk.Last)
  269. sstk.High = fmt.Sprintf("%.8f", stk.High)
  270. sstk.Low = fmt.Sprintf("%.8f", stk.Low)
  271. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  272. case "bty_JBUSDT":
  273. sstk.Open = fmt.Sprintf("%.4f", stk.Open)
  274. sstk.Last = fmt.Sprintf("%.4f", stk.Last)
  275. sstk.High = fmt.Sprintf("%.4f", stk.High)
  276. sstk.Low = fmt.Sprintf("%.4f", stk.Low)
  277. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  278. case "bty_JBBTC":
  279. sstk.Open = fmt.Sprintf("%.8f", stk.Open)
  280. sstk.Last = fmt.Sprintf("%.8f", stk.Last)
  281. sstk.High = fmt.Sprintf("%.8f", stk.High)
  282. sstk.Low = fmt.Sprintf("%.8f", stk.Low)
  283. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  284. case "bty_JBETH":
  285. sstk.Open = fmt.Sprintf("%.6f", stk.Open)
  286. sstk.Last = fmt.Sprintf("%.6f", stk.Last)
  287. sstk.High = fmt.Sprintf("%.6f", stk.High)
  288. sstk.Low = fmt.Sprintf("%.6f", stk.Low)
  289. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  290. case "bty_OPTCUSDT":
  291. sstk.Open = fmt.Sprintf("%.4f", stk.Open)
  292. sstk.Last = fmt.Sprintf("%.4f", stk.Last)
  293. sstk.High = fmt.Sprintf("%.4f", stk.High)
  294. sstk.Low = fmt.Sprintf("%.4f", stk.Low)
  295. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  296. case "bty_OPTCBTC":
  297. sstk.Open = fmt.Sprintf("%.8f", stk.Open)
  298. sstk.Last = fmt.Sprintf("%.8f", stk.Last)
  299. sstk.High = fmt.Sprintf("%.8f", stk.High)
  300. sstk.Low = fmt.Sprintf("%.8f", stk.Low)
  301. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  302. case "bty_OPTCETH":
  303. sstk.Open = fmt.Sprintf("%.8f", stk.Open)
  304. sstk.Last = fmt.Sprintf("%.8f", stk.Last)
  305. sstk.High = fmt.Sprintf("%.8f", stk.High)
  306. sstk.Low = fmt.Sprintf("%.8f", stk.Low)
  307. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  308. case "bty_BTCETH":
  309. sstk.Open = fmt.Sprintf("%.4f", stk.Open)
  310. sstk.Last = fmt.Sprintf("%.4f", stk.Last)
  311. sstk.High = fmt.Sprintf("%.4f", stk.High)
  312. sstk.Low = fmt.Sprintf("%.4f", stk.Low)
  313. sstk.Volume = fmt.Sprintf("%.4f", stk.Volume)
  314. case "bty_BCCETH":
  315. sstk.Open = fmt.Sprintf("%.4f", stk.Open)
  316. sstk.Last = fmt.Sprintf("%.4f", stk.Last)
  317. sstk.High = fmt.Sprintf("%.4f", stk.High)
  318. sstk.Low = fmt.Sprintf("%.4f", stk.Low)
  319. sstk.Volume = fmt.Sprintf("%.4f", stk.Volume)
  320. case "bty_ZECETH":
  321. sstk.Open = fmt.Sprintf("%.3f", stk.Open)
  322. sstk.Last = fmt.Sprintf("%.3f", stk.Last)
  323. sstk.High = fmt.Sprintf("%.3f", stk.High)
  324. sstk.Low = fmt.Sprintf("%.3f", stk.Low)
  325. sstk.Volume = fmt.Sprintf("%.5f", stk.Volume)
  326. case "bty_ETCETH":
  327. sstk.Open = fmt.Sprintf("%.2f", stk.Open)
  328. sstk.Last = fmt.Sprintf("%.2f", stk.Last)
  329. sstk.High = fmt.Sprintf("%.2f", stk.High)
  330. sstk.Low = fmt.Sprintf("%.2f", stk.Low)
  331. sstk.Volume = fmt.Sprintf("%.6f", stk.Volume)
  332. case "bty_LTCETH":
  333. sstk.Open = fmt.Sprintf("%.3f", stk.Open)
  334. sstk.Last = fmt.Sprintf("%.3f", stk.Last)
  335. sstk.High = fmt.Sprintf("%.3f", stk.High)
  336. sstk.Low = fmt.Sprintf("%.3f", stk.Low)
  337. sstk.Volume = fmt.Sprintf("%.5f", stk.Volume)
  338. case "bty_STILT":
  339. sstk.Open = fmt.Sprintf("%.2f", stk.Open)
  340. sstk.Last = fmt.Sprintf("%.2f", stk.Last)
  341. sstk.High = fmt.Sprintf("%.2f", stk.High)
  342. sstk.Low = fmt.Sprintf("%.2f", stk.Low)
  343. sstk.Volume = fmt.Sprintf("%.2f", stk.Volume)
  344. case "bty_ITVBUSDT":
  345. sstk.Open = fmt.Sprintf("%.4f", stk.Open)
  346. sstk.Last = fmt.Sprintf("%.4f", stk.Last)
  347. sstk.High = fmt.Sprintf("%.4f", stk.High)
  348. sstk.Low = fmt.Sprintf("%.4f", stk.Low)
  349. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  350. case "bty_ITVBBTC":
  351. sstk.Open = fmt.Sprintf("%.8f", stk.Open)
  352. sstk.Last = fmt.Sprintf("%.8f", stk.Last)
  353. sstk.High = fmt.Sprintf("%.8f", stk.High)
  354. sstk.Low = fmt.Sprintf("%.8f", stk.Low)
  355. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  356. case "bty_ITVBETH":
  357. sstk.Open = fmt.Sprintf("%.8f", stk.Open)
  358. sstk.Last = fmt.Sprintf("%.8f", stk.Last)
  359. sstk.High = fmt.Sprintf("%.8f", stk.High)
  360. sstk.Low = fmt.Sprintf("%.8f", stk.Low)
  361. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  362. case "bty_BTYETH":
  363. sstk.Open = fmt.Sprintf("%.8f", stk.Open)
  364. sstk.Last = fmt.Sprintf("%.8f", stk.Last)
  365. sstk.High = fmt.Sprintf("%.8f", stk.High)
  366. sstk.Low = fmt.Sprintf("%.8f", stk.Low)
  367. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  368. case "bty_FHUSDT":
  369. sstk.Open = fmt.Sprintf("%.5f", stk.Open)
  370. sstk.Last = fmt.Sprintf("%.5f", stk.Last)
  371. sstk.High = fmt.Sprintf("%.5f", stk.High)
  372. sstk.Low = fmt.Sprintf("%.5f", stk.Low)
  373. sstk.Volume = fmt.Sprintf("%.1f", stk.Volume)
  374. case "bty_CWVUSDT":
  375. sstk.Open = fmt.Sprintf("%.6f", stk.Open)
  376. sstk.Last = fmt.Sprintf("%.6f", stk.Last)
  377. sstk.High = fmt.Sprintf("%.6f", stk.High)
  378. sstk.Low = fmt.Sprintf("%.6f", stk.Low)
  379. sstk.Volume = fmt.Sprintf("%.0f", stk.Volume)
  380. case "bty_FHETH":
  381. sstk.Open = fmt.Sprintf("%.7f", stk.Open)
  382. sstk.Last = fmt.Sprintf("%.7f", stk.Last)
  383. sstk.High = fmt.Sprintf("%.7f", stk.High)
  384. sstk.Low = fmt.Sprintf("%.7f", stk.Low)
  385. sstk.Volume = fmt.Sprintf("%.1f", stk.Volume)
  386. case "bty_TMCETH":
  387. sstk.Open = fmt.Sprintf("%.7f", stk.Open)
  388. sstk.Last = fmt.Sprintf("%.7f", stk.Last)
  389. sstk.High = fmt.Sprintf("%.7f", stk.High)
  390. sstk.Low = fmt.Sprintf("%.7f", stk.Low)
  391. sstk.Volume = fmt.Sprintf("%.1f", stk.Volume)
  392. case "bty_FANSUSDT":
  393. sstk.Open = fmt.Sprintf("%.5f", stk.Open)
  394. sstk.Last = fmt.Sprintf("%.5f", stk.Last)
  395. sstk.High = fmt.Sprintf("%.5f", stk.High)
  396. sstk.Low = fmt.Sprintf("%.5f", stk.Low)
  397. sstk.Volume = fmt.Sprintf("%.1f", stk.Volume)
  398. case "bty_FANSBTC":
  399. sstk.Open = fmt.Sprintf("%.7f", stk.Open)
  400. sstk.Last = fmt.Sprintf("%.7f", stk.Last)
  401. sstk.High = fmt.Sprintf("%.7f", stk.High)
  402. sstk.Low = fmt.Sprintf("%.7f", stk.Low)
  403. sstk.Volume = fmt.Sprintf("%.1f", stk.Volume)
  404. case "bty_FANSETH":
  405. sstk.Open = fmt.Sprintf("%.7f", stk.Open)
  406. sstk.Last = fmt.Sprintf("%.7f", stk.Last)
  407. sstk.High = fmt.Sprintf("%.7f", stk.High)
  408. sstk.Low = fmt.Sprintf("%.7f", stk.Low)
  409. sstk.Volume = fmt.Sprintf("%.1f", stk.Volume)
  410. case "bty_WTBWTC":
  411. sstk.Open = fmt.Sprintf("%.7f", stk.Open)
  412. sstk.Last = fmt.Sprintf("%.7f", stk.Last)
  413. sstk.High = fmt.Sprintf("%.7f", stk.High)
  414. sstk.Low = fmt.Sprintf("%.7f", stk.Low)
  415. sstk.Volume = fmt.Sprintf("%.1f", stk.Volume)
  416. case "bty_CNSUSDT":
  417. sstk.Open = fmt.Sprintf("%.5f", stk.Open)
  418. sstk.Last = fmt.Sprintf("%.5f", stk.Last)
  419. sstk.High = fmt.Sprintf("%.5f", stk.High)
  420. sstk.Low = fmt.Sprintf("%.5f", stk.Low)
  421. sstk.Volume = fmt.Sprintf("%.1f", stk.Volume)
  422. default:
  423. }
  424. return sstk
  425. }
  426. func dayHandler(w http.ResponseWriter, r *http.Request) {
  427. //cb := r.FormValue("callback")
  428. typ := r.FormValue("type")
  429. symbol := r.FormValue("symbol")
  430. //log.Println(cb, typ, symbol)
  431. insId := typ + "_" + symbol
  432. candles, err := clientDown.GetLastCandles(insId, market.D1, 1)
  433. if err != nil {
  434. http.Error(w, "server get data failed", http.StatusInternalServerError)
  435. return
  436. }
  437. if len(candles) == 0 {
  438. _, candles, _ = clientDown.GetHistory(insId, market.D1, -1, -1)
  439. }
  440. if len(candles) == 0 {
  441. http.Error(w, "server get data failed", http.StatusInternalServerError)
  442. return
  443. }
  444. stk := SpecialTick{Timestamp: candles[0].Timestamp / 1000, Open: candles[0].Open, High: candles[0].High,
  445. Low: candles[0].Low, Last: candles[0].Close, Volume: candles[0].RealVolums}
  446. sstk := getStringSTK(stk, insId)
  447. b, err := json.Marshal(&sstk)
  448. if err != nil {
  449. http.Error(w, "json encoding error", http.StatusInternalServerError)
  450. return
  451. }
  452. w.Header().Set("Access-Control-Allow-Origin", "*")
  453. w.Header().Add("Access-Control-Allow-Headers", "Content-Type")
  454. w.Header().Set("content-type", "application/json")
  455. io.WriteString(w, string(b))
  456. }
  457. func h24Handler(w http.ResponseWriter, r *http.Request) {
  458. //cb := r.FormValue("callback")
  459. typ := r.FormValue("type")
  460. symbol := r.FormValue("symbol")
  461. //log.Println(cb, typ, symbol)
  462. var candles []client.Candle
  463. insId := typ + "_" + symbol
  464. bufcandles, err := clientDown.GetLastCandles(insId, market.H1, 24)
  465. /*if insId == "bty_BTCUSDT" {
  466. for i, _ := range bufcandles {
  467. if bufcandles[i].Open == 7000 && (bufcandles[i].Timestamp >= 1528941600000 && bufcandles[i].Timestamp <= 1528945200000) {
  468. bufcandles[i].High = 6468.7001953125
  469. bufcandles[i].Open = 6269.2998046875
  470. }
  471. }
  472. }*/
  473. /*for i, _ := range bufcandles {
  474. if insId == "bty_ETHUSDT" {
  475. if bufcandles[i].Timestamp >= 1524031200000 && bufcandles[i].Timestamp <= 1524031200000 {
  476. bufcandles[i].High = bufcandles[i-1].High
  477. bufcandles[i].Open = bufcandles[i-1].Open
  478. bufcandles[i].Close = bufcandles[i-1].Close
  479. bufcandles[i].Low = bufcandles[i-1].Low
  480. }
  481. }
  482. if insId == "bty_BTCUSDT" {
  483. if bufcandles[i].Timestamp >= 1524034800000 && bufcandles[i].Timestamp <= 1524034800000 {
  484. bufcandles[i].High = bufcandles[i-1].High
  485. bufcandles[i].Open = bufcandles[i-1].Open
  486. bufcandles[i].Close = bufcandles[i-1].Close
  487. bufcandles[i].Low = bufcandles[i-1].Low
  488. }
  489. }
  490. if insId == "bty_BCCUSDT" {
  491. if bufcandles[i].Timestamp >= 1524031200000 && bufcandles[i].Timestamp <= 1524034800000 {
  492. bufcandles[i].High = bufcandles[i-1].High
  493. bufcandles[i].Open = bufcandles[i-1].Open
  494. bufcandles[i].Close = bufcandles[i-1].Close
  495. bufcandles[i].Low = bufcandles[i-1].Low
  496. }
  497. }
  498. if insId == "bty_ETCUSDT" {
  499. if bufcandles[i].Timestamp >= 1524031200000 && bufcandles[i].Timestamp <= 1524034800000 {
  500. bufcandles[i].High = bufcandles[i-1].High
  501. bufcandles[i].Open = bufcandles[i-1].Open
  502. bufcandles[i].Close = bufcandles[i-1].Close
  503. bufcandles[i].Low = bufcandles[i-1].Low
  504. }
  505. }
  506. if insId == "bty_ZECUSDT" {
  507. if bufcandles[i].Timestamp >= 1524031200000 && bufcandles[i].Timestamp <= 1524034800000 {
  508. bufcandles[i].High = bufcandles[i-1].High
  509. bufcandles[i].Open = bufcandles[i-1].Open
  510. bufcandles[i].Close = bufcandles[i-1].Close
  511. bufcandles[i].Low = bufcandles[i-1].Low
  512. }
  513. }
  514. if insId == "bty_LTCUSDT" {
  515. if bufcandles[i].Timestamp >= 1524031200000 && bufcandles[i].Timestamp <= 1524034800000 {
  516. bufcandles[i].High = bufcandles[i-1].High
  517. bufcandles[i].Open = bufcandles[i-1].Open
  518. bufcandles[i].Close = bufcandles[i-1].Close
  519. bufcandles[i].Low = bufcandles[i-1].Low
  520. }
  521. }
  522. if insId == "bty_DCRUSDT" {
  523. if bufcandles[i].Timestamp >= 1524031200000 && bufcandles[i].Timestamp <= 1524034800000 {
  524. bufcandles[i].High = bufcandles[i-1].High
  525. bufcandles[i].Open = bufcandles[i-1].Open
  526. bufcandles[i].Close = bufcandles[i-1].Close
  527. bufcandles[i].Low = bufcandles[i-1].Low
  528. }
  529. }
  530. }*/
  531. if err != nil {
  532. http.Error(w, "server get data failed", http.StatusInternalServerError)
  533. return
  534. } else {
  535. //fmt.Println("h24 1", len(bufcandles), bufcandles[0], bufcandles[len(bufcandles)-1])
  536. if len(bufcandles) < 24 {
  537. _, filecandles, _ := clientDown.GetHistory(insId, market.H1, -24, -1)
  538. //fmt.Println("h24 2", len(filecandles), filecandles[0], filecandles[len(filecandles)-1])
  539. var candlesDesc []client.Candle
  540. var bufCandleTime int64
  541. if len(bufcandles) > 0 {
  542. bufCandleTime = bufcandles[0].Timestamp
  543. }
  544. for i := 0; i < len(filecandles); i++ {
  545. if filecandles[i].Timestamp < bufCandleTime {
  546. candlesDesc = append(candlesDesc, filecandles[i])
  547. }
  548. if len(candlesDesc) >= (24 - len(bufcandles)) {
  549. break
  550. }
  551. }
  552. for i := len(candlesDesc) - 1; i >= 0; i-- {
  553. candles = append(candles, candlesDesc[i])
  554. }
  555. //fmt.Println("h24 3", len(candles), candles[0], candles[len(candles)-1])
  556. candles = append(candles, bufcandles[:]...)
  557. //fmt.Println("h24 4", len(candles), candles[0], candles[len(candles)-1])
  558. } else {
  559. candles = bufcandles[:]
  560. }
  561. var stk SpecialTick
  562. if len(candles) > 0 {
  563. candlesOk := make([]client.Candle, len(candles))
  564. begin, end := getTimeRange()
  565. begin *= 1000
  566. end *= 1000
  567. pos := 0
  568. for i := 0; i < len(candles); i++ {
  569. if candles[i].Timestamp >= begin && candles[i].Timestamp <= end {
  570. candlesOk[pos] = candles[i]
  571. pos++
  572. }
  573. }
  574. if pos == 0 {
  575. candlesHadax := getHadax(strings.ToLower(symbol), "60min", 24)
  576. if len(candlesHadax) > 0 {
  577. candles = candlesHadax[:]
  578. } else {
  579. leng := len(candles)
  580. candles = candles[leng-1 : leng]
  581. candles[0].Open = candles[0].Close
  582. candles[0].High = candles[0].Close
  583. candles[0].Low = candles[0].Close
  584. }
  585. } else {
  586. candles = candlesOk[:pos]
  587. }
  588. stk.Low = candles[0].Low
  589. stk.Open = candles[0].Open
  590. stk.Timestamp = candles[0].Timestamp / 1000
  591. for i := 0; i < len(candles); i++ {
  592. //fmt.Println("h24 5", i, candles[i])
  593. stk.Volume += candles[i].RealVolums
  594. if candles[i].High > stk.High {
  595. stk.High = candles[i].High
  596. }
  597. if candles[i].Low < stk.Low {
  598. stk.Low = candles[i].Low
  599. }
  600. stk.Last = candles[i].Close
  601. }
  602. }
  603. sstk := getStringSTK(stk, insId)
  604. b, err := json.Marshal(&sstk)
  605. if err != nil {
  606. http.Error(w, "json encoding error", http.StatusInternalServerError)
  607. return
  608. }
  609. w.Header().Set("Access-Control-Allow-Origin", "*")
  610. w.Header().Add("Access-Control-Allow-Headers", "Content-Type")
  611. w.Header().Set("content-type", "application/json")
  612. io.WriteString(w, string(b))
  613. }
  614. }
  615. func subTick(insId string, symbolId int16) error {
  616. ins := clientSub.GetIns(insId)
  617. if ins == nil {
  618. errinfo := fmt.Sprintf("instrument %s not supported.", insId)
  619. return errors.New(errinfo)
  620. }
  621. clientSub.SubMarket(insId)
  622. ins.OnMarket().Attach(func(v interface{}) error {
  623. m, ok := v.(*client.Market)
  624. if !ok {
  625. //log.Println("v.(*Market) is NOT ok", insId)
  626. err := errors.New("v.(*Market) is NOT ok")
  627. return err
  628. }
  629. if m.InsId != insId {
  630. //log.Println("m.InsId != insId", m.InsId, insId)
  631. err := errors.New("v.(*Market) insid is NOT ok")
  632. return err
  633. }
  634. //log.Println("@@@:Market:", m)
  635. //now := time.Now()
  636. //timestampNow := now.Unix()*1000 + int64(now.Nanosecond())/1000000
  637. //log.Println("query", timestampNow, m.Timestamp, timestampNow-m.Timestamp)
  638. var tg base.TickGo
  639. tg.Time = int32(m.Timestamp / 1000)
  640. tg.Ms = int16(m.Timestamp % 1000)
  641. tg.Symbol = symbolId
  642. tg.Ask = float32(m.LastPrice)
  643. tg.Bid = float32(m.LastPrice)
  644. tg.Askv = float32(m.Volume)
  645. tg.Bidv = float32(m.Volume)
  646. if tg.Ask != 0. && tg.Bid != 0. {
  647. tkMutex.Lock()
  648. tkMap[insId] = tg
  649. tkMutex.Unlock()
  650. } else {
  651. //log.Println("value of ask or bid is 0", tg)
  652. }
  653. //log.Println(tg)
  654. return nil
  655. })
  656. return nil
  657. }
  658. func getOutput(cb string, ticks []base.TickGo) (output string) {
  659. b, err := json.Marshal(ticks)
  660. if err != nil {
  661. //log.Println(err)
  662. }
  663. output = fmt.Sprintf("if (%s) %s(%s)\n", cb, cb, string(b))
  664. return output
  665. }
  666. func connectServer() (err error) {
  667. clientSub, err = client.NewClientSimple(*saddr1, *saddr2, *saddr3, *saddr4, "./tmp")
  668. if err != nil {
  669. //log.Println("new client", err)
  670. return err
  671. }
  672. //insMap := clientSub.GetInsMap()
  673. //for insId, ins := range insMap {
  674. //log.Println(insId, ins)
  675. //}
  676. clientDown, err = client.NewClientSimple(*saddr1, *saddr2, *saddr3, *saddr4, "./tmp")
  677. if err != nil {
  678. //log.Println("new client", err)
  679. return err
  680. }
  681. return nil
  682. }
  683. func tickHandler(w http.ResponseWriter, r *http.Request) {
  684. //log.Println(r)
  685. //_, ok := w.(http.Hijacker)
  686. //if !ok {
  687. //http.Error(w, "webserver doesn't support hijacking", http.StatusInternalServerError)
  688. //return
  689. //} else {
  690. //}
  691. cb := r.FormValue("callback")
  692. typ := r.FormValue("type")
  693. symbol := r.FormValue("symbol")
  694. //log.Println(cb, typ, symbol)
  695. symbols := make([]string, 0)
  696. if symbol == "" {
  697. insMap := clientSub.GetInsMap()
  698. for _, v := range insMap {
  699. insTyp := strings.Split(v.Id, "_")[0]
  700. insSymbol := strings.Split(v.Id, "_")[1]
  701. if insTyp == typ {
  702. if typ == "lmax" {
  703. bookId, err := strconv.Atoi(insSymbol)
  704. if err != nil {
  705. continue
  706. }
  707. symbolId, err := markinfo.BookIdToSymbolId(bookId)
  708. if err != nil {
  709. continue
  710. }
  711. insSymbol, err = markinfo.SymbolName(symbolId)
  712. if err != nil {
  713. continue
  714. }
  715. }
  716. symbols = append(symbols, insSymbol)
  717. }
  718. }
  719. } else {
  720. symbols = strings.Split(symbol, ",")
  721. }
  722. ticks := make([]base.TickGo, 0)
  723. for _, v := range symbols {
  724. symbolId, err := markinfo.SymbolId(v)
  725. if err != nil {
  726. if typ == "lmax" && v == "CRUDE" {
  727. symbolId = markinfo.OILUSD
  728. } // else {
  729. //http.Error(w, "webserver doesn't support symbol", http.StatusBadRequest)
  730. //return
  731. //}
  732. }
  733. myInsId := typ + "_" + v
  734. if typ == "lmax" {
  735. lmaxId, err := markinfo.SymbolIdToBookId(symbolId)
  736. if err != nil {
  737. if typ == "lmax" && (v == "CRUDE" || v == "OILUSD") {
  738. lmaxId = 100800
  739. } else {
  740. http.Error(w, "webserver doesn't support symbol", http.StatusBadRequest)
  741. return
  742. }
  743. }
  744. slmaxId := fmt.Sprintf("%d", lmaxId)
  745. myInsId = typ + "_" + slmaxId
  746. }
  747. tkMutex.Lock()
  748. tick, ok := tkMap[myInsId]
  749. tkMutex.Unlock()
  750. if ok {
  751. /*if typ == "lmax" {
  752. tick.Time = int32(time.Now().Unix())
  753. tick.Ms = 0
  754. }*/
  755. ticks = append(ticks, tick)
  756. } else {
  757. /*if typ == "lmax" {
  758. tmpticks, err := clientSub.GetTickHistory(myInsId, -1, -1)
  759. if len(tmpticks) > 0 {
  760. tick.Time = int32(time.Now().Unix()) //int32(tmpticks[0].Timestamp / 1000)
  761. tick.Ms = 0 //int16(tmpticks[0].Timestamp % 1000)
  762. tick.Symbol = int16(symbolId)
  763. tick.Ask = float32(tmpticks[0].Price)
  764. tick.Bid = float32(tmpticks[0].Price)
  765. tick.Askv = int32(tmpticks[0].Volume)
  766. tick.Bidv = int32(tmpticks[0].Volume)
  767. ticks = append(ticks, tick)
  768. tkMutex.Lock()
  769. tkMap[myInsId] = tick
  770. tkMutex.Unlock()
  771. }
  772. log.Println("fuck", err, len(tmpticks))
  773. }*/
  774. err := subTick(myInsId, int16(symbolId))
  775. if err != nil {
  776. //log.Println(err)
  777. }
  778. }
  779. }
  780. output := getOutput(cb, ticks)
  781. io.WriteString(w, output)
  782. }
  783. func getDownOutput(cb string, hisData *HistoryData) (output string) {
  784. b, err := json.Marshal(hisData)
  785. if err != nil {
  786. //log.Println(err)
  787. }
  788. if len(cb) > 0 {
  789. output = fmt.Sprintf("if (%s) %s(%s)\n", cb, cb, string(b))
  790. } else {
  791. output = string(b) + "\n"
  792. }
  793. return output
  794. }
  795. func candle2Str(candle market.Candle) [6]string {
  796. var candleStrings [6]string
  797. candleStrings[0] = fmt.Sprintf("%d", candle.Timestamp/1000)
  798. candleStrings[1] = fmt.Sprintf("%f", candle.Open)
  799. candleStrings[2] = fmt.Sprintf("%f", candle.High)
  800. candleStrings[3] = fmt.Sprintf("%f", candle.Low)
  801. candleStrings[4] = fmt.Sprintf("%f", candle.Close)
  802. candleStrings[5] = fmt.Sprintf("%d", 0)
  803. return candleStrings
  804. }
  805. func downHandler(w http.ResponseWriter, r *http.Request) {
  806. symbol := r.FormValue("c")
  807. period := r.FormValue("p")
  808. action := r.FormValue("action")
  809. count := r.FormValue("count")
  810. typ := r.FormValue("type")
  811. //out := r.FormValue("out")
  812. cb := r.FormValue("callback")
  813. var ts, te string
  814. if action == "new" {
  815. te = r.FormValue("te")
  816. }
  817. if action == "down" {
  818. ts = r.FormValue("ts")
  819. }
  820. //log.Println(symbol, period, action, count, typ, out, te, ts)
  821. symbolId, err := markinfo.SymbolId(symbol)
  822. if err != nil {
  823. if symbol == "CRUDE" {
  824. symbolId = markinfo.OILUSD
  825. } else {
  826. http.Error(w, "webserver doesn't support symbol", http.StatusBadRequest)
  827. return
  828. }
  829. }
  830. periodId, ok := market.PeriodIdMap[period]
  831. if !ok {
  832. http.Error(w, "webserver doesn't support period", http.StatusBadRequest)
  833. return
  834. }
  835. iCount, err := strconv.Atoi(count)
  836. if action != "new" && err != nil {
  837. http.Error(w, "webserver doesn't support count", http.StatusBadRequest)
  838. return
  839. }
  840. types := strings.Split(typ, "|")
  841. for i, v := range types {
  842. if v == "main" {
  843. types[i] = types[0]
  844. types[0] = v
  845. break
  846. }
  847. }
  848. typeCount := len(types)
  849. done := make(chan bool, typeCount)
  850. endCh := make(chan int64, typeCount-1)
  851. var hisData HistoryData
  852. hisData.C = symbol
  853. hisData.P = period
  854. hisData.Action = action
  855. var mainTimestamps []int64
  856. var easyforexCandles, oandaCandles []client.Candle
  857. easyforexTimestampMap := make(map[int64]int)
  858. oandaTimestampMap := make(map[int64]int)
  859. //log.Println("test", types)
  860. for i := 0; i < typeCount; i++ {
  861. v := types[i]
  862. //for i, v := range types {
  863. var prefix string
  864. switch v {
  865. case "main":
  866. prefix = market.LmaxPrefix
  867. if symbol == "BTCCNY" || symbol == "BTCUSD" || symbol == "BTCFUSD" {
  868. prefix = market.BtcPrefix
  869. } else if symbol == "BTCETH" {
  870. prefix = market.PoloPrefix
  871. } else if symbol == "ETHCNY" || symbol == "BTYCNY" || symbol == "ETCCNY" {
  872. prefix = market.BtyPrefix
  873. }
  874. case "easyforex":
  875. prefix = market.EasyForexPrefix
  876. if symbol == "CRUDE" {
  877. symbol = "OILUSD"
  878. }
  879. case "oanda":
  880. prefix = market.OandaPrefix
  881. if symbol == "CRUDE" {
  882. symbol = "OILUSD"
  883. }
  884. }
  885. myInsId := prefix + symbol
  886. if prefix == market.LmaxPrefix {
  887. lmaxId, err := markinfo.SymbolIdToBookId(symbolId)
  888. if err != nil {
  889. if symbolId == markinfo.OILUSD {
  890. lmaxId = 100800
  891. } else {
  892. http.Error(w, "webserver doesn't support symbol", http.StatusBadRequest)
  893. return
  894. }
  895. }
  896. slmaxId := fmt.Sprintf("%d", lmaxId)
  897. myInsId = market.LmaxPrefix + slmaxId
  898. }
  899. go func() {
  900. var end, iTE, iTS int64
  901. var bufferedCandlesDesc []client.Candle
  902. var downN int
  903. var downTS int64
  904. switch action {
  905. case "init":
  906. bufferedCandlesAsc, _ := clientDown.GetLastCandles(myInsId, periodId, 0x7fffffff)
  907. for cindex := len(bufferedCandlesAsc) - 1; cindex >= 0; cindex-- {
  908. bufferedCandlesDesc = append(bufferedCandlesDesc, bufferedCandlesAsc[cindex])
  909. if len(bufferedCandlesDesc) >= iCount {
  910. break
  911. }
  912. }
  913. downN = -(iCount - len(bufferedCandlesDesc))
  914. downTS = -1
  915. case "new":
  916. iCount = 1000
  917. iTE, _ = strconv.ParseInt(te, 10, 64)
  918. iTE *= 1000
  919. bufferedCandlesAsc, _ := clientDown.GetLastCandles(myInsId, periodId, 0x7fffffff)
  920. //log.Println("haha.....", myInsId, bufferedCandlesAsc)
  921. for cindex := len(bufferedCandlesAsc) - 1; cindex >= 0; cindex-- {
  922. if bufferedCandlesAsc[cindex].Timestamp >= iTE {
  923. bufferedCandlesDesc = append(bufferedCandlesDesc, bufferedCandlesAsc[cindex])
  924. if len(bufferedCandlesDesc) >= iCount {
  925. break
  926. }
  927. }
  928. }
  929. if len(bufferedCandlesDesc) > 0 {
  930. downN = -(iCount - len(bufferedCandlesDesc))
  931. downTS = -1
  932. } else {
  933. downN = iCount
  934. downTS = iTE
  935. }
  936. case "down":
  937. iTS, _ = strconv.ParseInt(ts, 10, 64)
  938. iTS *= 1000
  939. tmpBufferedCandles, _ := clientDown.GetLastCandles(myInsId, periodId, 1)
  940. if len(tmpBufferedCandles) > 0 && tmpBufferedCandles[0].Timestamp <= iTS {
  941. bufferedCandlesAsc, _ := clientDown.GetLastCandles(myInsId, periodId, 0x7fffffff)
  942. for cindex := len(bufferedCandlesAsc) - 1; cindex >= 0; cindex-- {
  943. if bufferedCandlesAsc[cindex].Timestamp <= iTS {
  944. bufferedCandlesDesc = append(bufferedCandlesDesc, bufferedCandlesAsc[cindex])
  945. if len(bufferedCandlesDesc) >= iCount {
  946. break
  947. }
  948. }
  949. }
  950. }
  951. if len(bufferedCandlesDesc) > 0 {
  952. downN = -(iCount - len(bufferedCandlesDesc))
  953. downTS = -1
  954. } else {
  955. downN = -iCount
  956. downTS = iTS
  957. }
  958. }
  959. //log.Println(myInsId, len(bufferedCandlesDesc))
  960. //for _, candle := range bufferedCandlesDesc {
  961. //log.Println(candle)
  962. //}
  963. //log.Println("test", myInsId, periodId, downN, downTS, false)
  964. _, filecandles, _ := clientDown.GetHistory(myInsId, periodId, downN, downTS)
  965. //log.Println("test", myInsId, len(fileticks), len(filecandles))
  966. if v != "main" {
  967. end = <-endCh
  968. }
  969. bEnd := false
  970. var timestampLast int64
  971. timestampLast = 0x7fffffffffffffff
  972. for findex := 1; findex >= 0; findex-- {
  973. var candles []client.Candle
  974. if findex == 1 {
  975. //candles = append(candles, bufferedCandlesDesc...)
  976. for iBuffered := len(bufferedCandlesDesc) - 1; iBuffered >= 0; iBuffered-- {
  977. candles = append(candles, bufferedCandlesDesc[iBuffered])
  978. }
  979. if v == "oanda" {
  980. //for _, vtmp := range candles {
  981. //log.Println("test1", vtmp)
  982. //}
  983. }
  984. //log.Println("buf", v, len(candles))
  985. } else {
  986. if downN < 0 {
  987. for iFile := len(filecandles) - 1; iFile >= 0; iFile-- {
  988. candles = append(candles, filecandles[iFile])
  989. }
  990. } else {
  991. candles = filecandles[:]
  992. }
  993. if v == "oanda" {
  994. //for _, vtmp := range candles {
  995. //log.Println("test2", vtmp)
  996. //}
  997. }
  998. //log.Println("file", v, len(candles), fnames[findex].Fname)
  999. }
  1000. //log.Println("aaaa", v, candles)
  1001. for cindex := len(candles) - 1; cindex >= 0; cindex-- {
  1002. if v == "main" && periodId == market.D1 {
  1003. tmpTime := time.Unix(candles[cindex].Timestamp/1000, 0)
  1004. if tmpTime.Hour() != 0 || tmpTime.Minute() != 0 || tmpTime.Second() != 0 {
  1005. //log.Println("data time not standard", v, tmpTime)
  1006. //candles[cindex].Timestamp -= (int64(tmpTime.Hour())*3600 + int64(tmpTime.Minute())*60 + int64(tmpTime.Second())) * 1000
  1007. }
  1008. }
  1009. if candles[cindex].Timestamp >= timestampLast {
  1010. continue
  1011. }
  1012. timestampLast = candles[cindex].Timestamp
  1013. if action == "down" && candles[cindex].Timestamp > iTS {
  1014. continue
  1015. }
  1016. if action == "new" && candles[cindex].Timestamp < iTE {
  1017. continue
  1018. }
  1019. if v == "main" {
  1020. var candleStrings [6]string
  1021. candleStrings[0] = fmt.Sprintf("%d", candles[cindex].Timestamp/1000)
  1022. candleStrings[1] = fmt.Sprintf("%f", candles[cindex].Open)
  1023. candleStrings[2] = fmt.Sprintf("%f", candles[cindex].High)
  1024. candleStrings[3] = fmt.Sprintf("%f", candles[cindex].Low)
  1025. candleStrings[4] = fmt.Sprintf("%f", candles[cindex].Close)
  1026. candleStrings[5] = fmt.Sprintf("%d", 0)
  1027. hisData.MainData = append(hisData.MainData, candleStrings)
  1028. end = candles[cindex].Timestamp
  1029. mainTimestamps = append(mainTimestamps, end)
  1030. //tmpTime := time.Unix(end/1000, 0)
  1031. //if tmpTime.Hour() != 0 || tmpTime.Minute() != 0 || tmpTime.Second() != 0 {
  1032. //log.Println("data error", v, tmpTime)
  1033. //}
  1034. if len(hisData.MainData) >= iCount {
  1035. bEnd = true
  1036. //log.Println("test", v, len(hisData.MainData))
  1037. break
  1038. }
  1039. } else {
  1040. if candles[cindex].Timestamp < end {
  1041. bEnd = true
  1042. if v == "oanda" {
  1043. //log.Println("test", v, end, candles[cindex].Timestamp, len(hisData.Oanda))
  1044. }
  1045. if v == "easyforex" {
  1046. //log.Println("test", v, end, candles[cindex].Timestamp, len(hisData.Easyforex))
  1047. }
  1048. break
  1049. }
  1050. if v == "easyforex" {
  1051. //hisData.Easyforex = append(hisData.Easyforex, candleStrings)
  1052. //easyforexMap[candles[cindex].Timestamp] = candles[cindex]
  1053. easyforexCandles = append(easyforexCandles, candles[cindex])
  1054. easyforexTimestampMap[candles[cindex].Timestamp] = len(easyforexCandles) - 1
  1055. //tmpTime := time.Unix(candles[cindex].Timestamp/1000, 0)
  1056. //if tmpTime.Hour() != 0 || tmpTime.Minute() != 0 || tmpTime.Second() != 0 {
  1057. //log.Println("data error", v, tmpTime)
  1058. //}
  1059. }
  1060. if v == "oanda" {
  1061. //hisData.Oanda = append(hisData.Oanda, candleStrings)
  1062. //oandaMap[candles[cindex].Timestamp] = candles[cindex]
  1063. oandaCandles = append(oandaCandles, candles[cindex])
  1064. oandaTimestampMap[candles[cindex].Timestamp] = len(oandaCandles) - 1
  1065. //log.Println("hhhhhhh", oandaCandles)
  1066. }
  1067. }
  1068. }
  1069. if bEnd {
  1070. //log.Println("test", v)
  1071. break
  1072. }
  1073. }
  1074. if v == "main" {
  1075. for endCount := 1; endCount < typeCount; endCount++ {
  1076. endCh <- end
  1077. }
  1078. //log.Println("test", v, end)
  1079. }
  1080. if v == "easyforex" {
  1081. index := -1 //var index int
  1082. for _, iMain := range mainTimestamps {
  1083. var candleStrings [6]string
  1084. vEasyforex, ok := easyforexTimestampMap[iMain]
  1085. if ok {
  1086. candleStrings[1] = fmt.Sprintf("%f", easyforexCandles[vEasyforex].Open)
  1087. candleStrings[2] = fmt.Sprintf("%f", easyforexCandles[vEasyforex].High)
  1088. candleStrings[3] = fmt.Sprintf("%f", easyforexCandles[vEasyforex].Low)
  1089. candleStrings[4] = fmt.Sprintf("%f", easyforexCandles[vEasyforex].Close)
  1090. index = vEasyforex
  1091. } else {
  1092. //tmpTime := time.Unix(iMain/1000, 0)
  1093. //log.Println("data mismatch", tmpTime)
  1094. /*if index < (len(easyforexCandles) - 1) {
  1095. candleStrings[1] = fmt.Sprintf("%f", easyforexCandles[index+1].Open)
  1096. candleStrings[2] = fmt.Sprintf("%f", easyforexCandles[index+1].High)
  1097. candleStrings[3] = fmt.Sprintf("%f", easyforexCandles[index+1].Low)
  1098. candleStrings[4] = fmt.Sprintf("%f", easyforexCandles[index+1].Close)
  1099. } else {
  1100. candleStrings[1] = fmt.Sprintf("%f", 0.0)
  1101. candleStrings[2] = fmt.Sprintf("%f", 0.0)
  1102. candleStrings[3] = fmt.Sprintf("%f", 0.0)
  1103. candleStrings[4] = fmt.Sprintf("%f", 0.0)
  1104. }*/
  1105. if index != -1 {
  1106. candleStrings[1] = fmt.Sprintf("%f", easyforexCandles[index].Open)
  1107. candleStrings[2] = fmt.Sprintf("%f", easyforexCandles[index].High)
  1108. candleStrings[3] = fmt.Sprintf("%f", easyforexCandles[index].Low)
  1109. candleStrings[4] = fmt.Sprintf("%f", easyforexCandles[index].Close)
  1110. } else {
  1111. candleStrings[1] = fmt.Sprintf("%f", 0.0)
  1112. candleStrings[2] = fmt.Sprintf("%f", 0.0)
  1113. candleStrings[3] = fmt.Sprintf("%f", 0.0)
  1114. candleStrings[4] = fmt.Sprintf("%f", 0.0)
  1115. }
  1116. }
  1117. candleStrings[0] = fmt.Sprintf("%d", iMain/1000)
  1118. candleStrings[5] = fmt.Sprintf("%d", 0)
  1119. hisData.Easyforex = append(hisData.Easyforex, candleStrings)
  1120. }
  1121. }
  1122. if v == "oanda" {
  1123. index := -1 //var index int
  1124. //for _, vtmp := range oandaCandles {
  1125. //log.Println("test3", vtmp)
  1126. //}
  1127. //log.Println("test3", len(oandaTimestampMap), len(oandaCandles))
  1128. for _, iMain := range mainTimestamps {
  1129. var candleStrings [6]string
  1130. vOanda, ok := oandaTimestampMap[iMain]
  1131. if ok {
  1132. //log.Println(vOanda, iMain, oandaCandles[vOanda].Timestamp)
  1133. candleStrings[1] = fmt.Sprintf("%f", oandaCandles[vOanda].Open)
  1134. candleStrings[2] = fmt.Sprintf("%f", oandaCandles[vOanda].High)
  1135. candleStrings[3] = fmt.Sprintf("%f", oandaCandles[vOanda].Low)
  1136. candleStrings[4] = fmt.Sprintf("%f", oandaCandles[vOanda].Close)
  1137. index = vOanda
  1138. } else {
  1139. /*if index < (len(oandaCandles) - 1) {
  1140. candleStrings[1] = fmt.Sprintf("%f", oandaCandles[index+1].Open)
  1141. candleStrings[2] = fmt.Sprintf("%f", oandaCandles[index+1].High)
  1142. candleStrings[3] = fmt.Sprintf("%f", oandaCandles[index+1].Low)
  1143. candleStrings[4] = fmt.Sprintf("%f", oandaCandles[index+1].Close)
  1144. } else {
  1145. candleStrings[1] = fmt.Sprintf("%f", 0.0)
  1146. candleStrings[2] = fmt.Sprintf("%f", 0.0)
  1147. candleStrings[3] = fmt.Sprintf("%f", 0.0)
  1148. candleStrings[4] = fmt.Sprintf("%f", 0.0)
  1149. }*/
  1150. if index != -1 {
  1151. candleStrings[1] = fmt.Sprintf("%f", oandaCandles[index].Open)
  1152. candleStrings[2] = fmt.Sprintf("%f", oandaCandles[index].High)
  1153. candleStrings[3] = fmt.Sprintf("%f", oandaCandles[index].Low)
  1154. candleStrings[4] = fmt.Sprintf("%f", oandaCandles[index].Close)
  1155. } else {
  1156. candleStrings[1] = fmt.Sprintf("%f", 0.0)
  1157. candleStrings[2] = fmt.Sprintf("%f", 0.0)
  1158. candleStrings[3] = fmt.Sprintf("%f", 0.0)
  1159. candleStrings[4] = fmt.Sprintf("%f", 0.0)
  1160. }
  1161. }
  1162. candleStrings[0] = fmt.Sprintf("%d", iMain/1000)
  1163. candleStrings[5] = fmt.Sprintf("%d", 0)
  1164. hisData.Oanda = append(hisData.Oanda, candleStrings)
  1165. }
  1166. }
  1167. done <- true
  1168. }()
  1169. }
  1170. for i := 0; i < typeCount; i++ {
  1171. <-done
  1172. }
  1173. output := getDownOutput(cb, &hisData)
  1174. io.WriteString(w, output)
  1175. }
  1176. func symbolsHandler(w http.ResponseWriter, r *http.Request) {
  1177. cb := r.FormValue("callback")
  1178. //log.Println(cb)
  1179. var symbols []SymbolData
  1180. /*for i := markinfo.EURUSD; i < markinfo.CurrencyCount; i++ {
  1181. var symbol SymbolData
  1182. symbol.Symbol = i
  1183. symbol.Name, _ = markinfo.SymbolName(i)
  1184. symbol.PriceIncrement, _ = markinfo.SymbolUint(symbol.Name)
  1185. symbol.Unit = int64(1 / symbol.PriceIncrement)
  1186. if (symbol.Unit % 10) == 9 {
  1187. symbol.Unit++
  1188. }
  1189. symbols = append(symbols, symbol)
  1190. }*/
  1191. symbol1 := SymbolData{Unit: 500, PriceIncrement: 0.001, Symbol: 20, Name: "XAGUSD"}
  1192. symbols = append(symbols, symbol1)
  1193. symbol2 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 38, Name: "EURMXN"}
  1194. symbols = append(symbols, symbol2)
  1195. symbol3 := SymbolData{Unit: 100, PriceIncrement: 0.01, Symbol: 22, Name: "OILUSD"}
  1196. symbols = append(symbols, symbol3)
  1197. symbol4 := SymbolData{Unit: 10000, PriceIncrement: 0.0001, Symbol: 26, Name: "EURCZK"}
  1198. symbols = append(symbols, symbol4)
  1199. symbol5 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 39, Name: "GBPMXN"}
  1200. symbols = append(symbols, symbol5)
  1201. symbol6 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 62, Name: "NZDCAD"}
  1202. symbols = append(symbols, symbol6)
  1203. symbol7 := SymbolData{Unit: 10, PriceIncrement: 0.01, Symbol: 21, Name: "XAUUSD"}
  1204. symbols = append(symbols, symbol7)
  1205. symbol8 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 56, Name: "EURZAR"}
  1206. symbols = append(symbols, symbol8)
  1207. symbol9 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 52, Name: "GBPSGD"}
  1208. symbols = append(symbols, symbol9)
  1209. symbol10 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 25, Name: "GBPCAD"}
  1210. symbols = append(symbols, symbol10)
  1211. symbol11 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 58, Name: "USDZAR"}
  1212. symbols = append(symbols, symbol11)
  1213. symbol12 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 50, Name: "USDSEK"}
  1214. symbols = append(symbols, symbol12)
  1215. symbol13 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 23, Name: "USDTRY"}
  1216. symbols = append(symbols, symbol13)
  1217. symbol14 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 6, Name: "NZDUSD"}
  1218. symbols = append(symbols, symbol14)
  1219. symbol15 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 18, Name: "USDMXN"}
  1220. symbols = append(symbols, symbol15)
  1221. symbol16 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 42, Name: "USDNOK"}
  1222. symbols = append(symbols, symbol16)
  1223. symbol17 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 32, Name: "EURHKD"}
  1224. symbols = append(symbols, symbol17)
  1225. symbol18 := SymbolData{Unit: 10000, PriceIncrement: 0.001, Symbol: 35, Name: "EURHUF"}
  1226. symbols = append(symbols, symbol18)
  1227. symbol19 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 55, Name: "GBPTRY"}
  1228. symbols = append(symbols, symbol19)
  1229. symbol20 := SymbolData{Unit: 10000, PriceIncrement: 0.0001, Symbol: 28, Name: "USDCZK"}
  1230. symbols = append(symbols, symbol20)
  1231. symbol21 := SymbolData{Unit: 10000, PriceIncrement: 0.001, Symbol: 37, Name: "USDHUF"}
  1232. symbols = append(symbols, symbol21)
  1233. symbol22 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 4, Name: "AUDUSD"}
  1234. symbols = append(symbols, symbol22)
  1235. symbol23 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 5, Name: "USDCAD"}
  1236. symbols = append(symbols, symbol23)
  1237. symbol24 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 60, Name: "AUDCHF"}
  1238. symbols = append(symbols, symbol24)
  1239. symbol25 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 29, Name: "EURDKK"}
  1240. symbols = append(symbols, symbol25)
  1241. symbol26 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 47, Name: "USDPLN"}
  1242. symbols = append(symbols, symbol26)
  1243. symbol27 := SymbolData{Unit: 10000, PriceIncrement: 0.0001, Symbol: 27, Name: "GBPCZK"}
  1244. symbols = append(symbols, symbol27)
  1245. symbol28 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 41, Name: "GBPNOK"}
  1246. symbols = append(symbols, symbol28)
  1247. symbol29 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 1, Name: "GBPUSD"}
  1248. symbols = append(symbols, symbol29)
  1249. symbol30 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 3, Name: "USDCHF"}
  1250. symbols = append(symbols, symbol30)
  1251. symbol31 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 51, Name: "EURSGD"}
  1252. symbols = append(symbols, symbol31)
  1253. symbol32 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 24, Name: "GBPAUD"}
  1254. symbols = append(symbols, symbol32)
  1255. symbol33 := SymbolData{Unit: 10000, PriceIncrement: 0.001, Symbol: 14, Name: "CHFJPY"}
  1256. symbols = append(symbols, symbol33)
  1257. symbol34 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 30, Name: "GBPDKK"}
  1258. symbols = append(symbols, symbol34)
  1259. symbol35 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 57, Name: "GBPZAR"}
  1260. symbols = append(symbols, symbol35)
  1261. symbol36 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 63, Name: "NZDCHF"}
  1262. symbols = append(symbols, symbol36)
  1263. symbol37 := SymbolData{Unit: 1, PriceIncrement: 0.1, Symbol: 68, Name: "UK100"}
  1264. symbols = append(symbols, symbol37)
  1265. symbol38 := SymbolData{Unit: 25, PriceIncrement: 0.1, Symbol: 70, Name: "SPX"}
  1266. symbols = append(symbols, symbol38)
  1267. symbol39 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 0, Name: "EURUSD"}
  1268. symbols = append(symbols, symbol39)
  1269. symbol40 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 12, Name: "GBPCHF"}
  1270. symbols = append(symbols, symbol40)
  1271. symbol41 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 46, Name: "GBPPLN"}
  1272. symbols = append(symbols, symbol41)
  1273. symbol42 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 7, Name: "EURGBP"}
  1274. symbols = append(symbols, symbol42)
  1275. symbol43 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 17, Name: "AUDCAD"}
  1276. symbols = append(symbols, symbol43)
  1277. symbol44 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 44, Name: "GBPNZD"}
  1278. symbols = append(symbols, symbol44)
  1279. symbol45 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 64, Name: "NZDSGD"}
  1280. symbols = append(symbols, symbol45)
  1281. symbol46 := SymbolData{Unit: 10000, PriceIncrement: 0.001, Symbol: 16, Name: "AUDJPY"}
  1282. symbols = append(symbols, symbol46)
  1283. symbol47 := SymbolData{Unit: 1, PriceIncrement: 0.1, Symbol: 74, Name: "STOXX50E"}
  1284. symbols = append(symbols, symbol47)
  1285. symbol48 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 53, Name: "USDSGD"}
  1286. symbols = append(symbols, symbol48)
  1287. symbol49 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 9, Name: "EURCHF"}
  1288. symbols = append(symbols, symbol49)
  1289. symbol50 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 40, Name: "EURNOK"}
  1290. symbols = append(symbols, symbol50)
  1291. symbol51 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 43, Name: "EURNZD"}
  1292. symbols = append(symbols, symbol51)
  1293. symbol52 := SymbolData{Unit: 10000, PriceIncrement: 0.001, Symbol: 59, Name: "NZDJPY"}
  1294. symbols = append(symbols, symbol52)
  1295. symbol53 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 34, Name: "USDHKD"}
  1296. symbols = append(symbols, symbol53)
  1297. symbol54 := SymbolData{Unit: 10000, PriceIncrement: 0.001, Symbol: 2, Name: "USDJPY"}
  1298. symbols = append(symbols, symbol54)
  1299. symbol55 := SymbolData{Unit: 10000, PriceIncrement: 0.001, Symbol: 15, Name: "CADJPY"}
  1300. symbols = append(symbols, symbol55)
  1301. symbol56 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 45, Name: "EURPLN"}
  1302. symbols = append(symbols, symbol56)
  1303. symbol57 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 33, Name: "GBPHKD"}
  1304. symbols = append(symbols, symbol57)
  1305. symbol58 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 48, Name: "EURSEK"}
  1306. symbols = append(symbols, symbol58)
  1307. symbol59 := SymbolData{Unit: 1, PriceIncrement: 0.1, Symbol: 73, Name: "FCHI"}
  1308. symbols = append(symbols, symbol59)
  1309. symbol60 := SymbolData{Unit: 10, PriceIncrement: 0.1, Symbol: 71, Name: "NDX"}
  1310. symbols = append(symbols, symbol60)
  1311. symbol61 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 10, Name: "EURAUD"}
  1312. symbols = append(symbols, symbol61)
  1313. symbol62 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 19, Name: "AUDNZD"}
  1314. symbols = append(symbols, symbol62)
  1315. symbol63 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 54, Name: "EURTRY"}
  1316. symbols = append(symbols, symbol63)
  1317. symbol64 := SymbolData{Unit: 10000, PriceIncrement: 0.001, Symbol: 36, Name: "GBPHUF"}
  1318. symbols = append(symbols, symbol64)
  1319. symbol65 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 49, Name: "GBPSEK"}
  1320. symbols = append(symbols, symbol65)
  1321. symbol66 := SymbolData{Unit: 2.5, PriceIncrement: 0.1, Symbol: 72, Name: "GDAXI"}
  1322. symbols = append(symbols, symbol66)
  1323. symbol67 := SymbolData{Unit: 10000, PriceIncrement: 0.001, Symbol: 8, Name: "EURJPY"}
  1324. symbols = append(symbols, symbol67)
  1325. symbol68 := SymbolData{Unit: 10000, PriceIncrement: 0.001, Symbol: 13, Name: "GBPJPY"}
  1326. symbols = append(symbols, symbol68)
  1327. symbol69 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 11, Name: "EURCAD"}
  1328. symbols = append(symbols, symbol69)
  1329. symbol70 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 61, Name: "CADCHF"}
  1330. symbols = append(symbols, symbol70)
  1331. symbol71 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 31, Name: "USDDKK"}
  1332. symbols = append(symbols, symbol71)
  1333. symbol72 := SymbolData{Unit: 1, PriceIncrement: 1, Symbol: 69, Name: "WS30"}
  1334. symbols = append(symbols, symbol72)
  1335. symbol73 := SymbolData{Unit: 10000, PriceIncrement: 1e-05, Symbol: 65, Name: "LTCUSD"}
  1336. symbols = append(symbols, symbol73)
  1337. symbol74 := SymbolData{Unit: 10000, PriceIncrement: 0.001, Symbol: 66, Name: "BTCUSD"}
  1338. symbols = append(symbols, symbol74)
  1339. b, _ := json.Marshal(symbols)
  1340. output := fmt.Sprintf("if (%s) %s(%s)\n", cb, cb, string(b))
  1341. io.WriteString(w, output)
  1342. }
  1343. /*
  1344. func testHandler(w http.ResponseWriter, r *http.Request) {
  1345. insId := r.FormValue("insid")
  1346. period := r.FormValue("period")
  1347. st := r.FormValue("st")
  1348. et := r.FormValue("et")
  1349. log.Println(insId, period, st, et)
  1350. periodId, ok := market.PeriodIdMap[period]
  1351. if !ok {
  1352. http.Error(w, "webserver doesn't support period", http.StatusBadRequest)
  1353. return
  1354. }
  1355. st64, _ := strconv.ParseInt(st, 10, 64)
  1356. et64, _ := strconv.ParseInt(et, 10, 64)
  1357. fnames, err := clientDown.GetHisEx(insId, periodId, st64, et64, false)
  1358. if err != nil {
  1359. http.Error(w, "webserver can't get data", http.StatusBadRequest)
  1360. return
  1361. }
  1362. sort.Sort(byDlInfo(fnames))
  1363. var output string
  1364. for i, v := range fnames {
  1365. output += fmt.Sprintf("%d %s %s %d %d %d", i, v.Fname, v.Reply.Url, v.Reply.St, v.Reply.Et, v.Reply.N)
  1366. }
  1367. io.WriteString(w, output)
  1368. }
  1369. */
  1370. type gzipResponseWriter struct {
  1371. io.Writer
  1372. http.ResponseWriter
  1373. }
  1374. func (w gzipResponseWriter) Write(b []byte) (int, error) {
  1375. return w.Writer.Write(b)
  1376. }
  1377. func (w gzipResponseWriter) Flush() {
  1378. w.Writer.(*gzip.Writer).Flush()
  1379. w.ResponseWriter.(http.Flusher).Flush()
  1380. }
  1381. func makeGzipHandler(fn http.HandlerFunc) http.HandlerFunc {
  1382. return func(w http.ResponseWriter, r *http.Request) {
  1383. if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
  1384. fn(w, r)
  1385. return
  1386. }
  1387. w.Header().Set("Content-Encoding", "gzip")
  1388. w.Header().Set("Content-Type", "text/javascript")
  1389. gz := gzip.NewWriter(w)
  1390. defer gz.Close()
  1391. fn(gzipResponseWriter{Writer: gz, ResponseWriter: w}, r)
  1392. }
  1393. }
  1394. func getHadax(sym, per string, size int) []client.Candle {
  1395. url := fmt.Sprintf("https://api.hadax.com/market/history/kline?period=%s&size=%d&symbol=%s", per, size, sym)
  1396. response, err := http.Get(url)
  1397. if err != nil {
  1398. log.Println(err)
  1399. return nil
  1400. }
  1401. body, err := ioutil.ReadAll(response.Body)
  1402. if err != nil {
  1403. log.Println(err)
  1404. return nil
  1405. }
  1406. var data Hadax
  1407. err = json.Unmarshal(body, &data)
  1408. if err != nil {
  1409. log.Println(err)
  1410. return nil
  1411. }
  1412. response.Body.Close()
  1413. //log.Println(data)
  1414. datalen := len(data.Data)
  1415. if datalen > 0 {
  1416. candles := make([]client.Candle, datalen)
  1417. for i := datalen - 1; i >= 0; i-- {
  1418. candles[(datalen-1)-i].Timestamp = data.Data[i].Id * 1000
  1419. if per == "1day" {
  1420. candles[(datalen-1)-i].Timestamp += 8 * 60 * 60 * 1000
  1421. }
  1422. candles[(datalen-1)-i].Open = data.Data[i].Open
  1423. candles[(datalen-1)-i].High = data.Data[i].High
  1424. candles[(datalen-1)-i].Low = data.Data[i].Low
  1425. candles[(datalen-1)-i].Close = data.Data[i].Close
  1426. candles[(datalen-1)-i].RealVolums = data.Data[i].Amount
  1427. candles[(datalen-1)-i].TickVolums = float64(data.Data[i].Count)
  1428. }
  1429. return candles
  1430. }
  1431. return nil
  1432. }
  1433. func getTimeRange() (begin int64, end int64) {
  1434. now := time.Now().Unix()
  1435. end = now - now%3600
  1436. begin = end - 24*3600
  1437. return
  1438. }
  1439. func main() {
  1440. runtime.GOMAXPROCS(runtime.NumCPU())
  1441. conf, err := readConf()
  1442. if err != nil {
  1443. flag.Parse()
  1444. } else {
  1445. *saddr1 = conf.Saddr1
  1446. *saddr2 = conf.Saddr2
  1447. *saddr3 = conf.Saddr3
  1448. *saddr4 = conf.Saddr4
  1449. }
  1450. err = connectServer()
  1451. if err != nil {
  1452. log.Fatal("connect server", err)
  1453. }
  1454. //go updateTick()
  1455. s := &http.Server{
  1456. Addr: ":6062",
  1457. ReadTimeout: 10 * time.Second,
  1458. WriteTimeout: 10 * time.Second,
  1459. MaxHeaderBytes: 1 << 20,
  1460. }
  1461. //http.HandleFunc("/tickdata", tickHandler)
  1462. //http.HandleFunc("/api.php", makeGzipHandler(downHandler))
  1463. //http.HandleFunc("/symbols", symbolsHandler)
  1464. http.HandleFunc("/daydata", dayHandler)
  1465. http.HandleFunc("/h24data", h24Handler)
  1466. //http.HandleFunc("/test", testHandler)
  1467. log.Fatal(s.ListenAndServe())
  1468. }