1
jhzh
2025-04-14 a5601fbcdc19292b32423bea88e67fc9ab4422a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
localStorage.removeItem("tradingview.chartproperties");
$(function () {
    let light = {
        'volumePaneSize': "small",//large, medium, small, tiny
        "paneProperties.background": "#ffffff",
        "paneProperties.vertGridProperties.color": "#ffffff",
        "paneProperties.horzGridProperties.color": "#ffffff",
        "scalesProperties.backgroundColor": "#ffffff",
        "scalesProperties.lineColor": "#dcdee0",
        "scalesProperties.textColor": "#333",
        "scalesProperties.fontSize": 9,
        "mainSeriesProperties.style": 1,
        "paneProperties.legendProperties.showSeriesOHLC": true
    }
 
    let dark = {
        'volumePaneSize': "small",//large, medium, small, tiny
        "paneProperties.background": "#2b2b37",
        "paneProperties.vertGridProperties.color": "#49495F",
        "paneProperties.horzGridProperties.color": "#49495F",
        "scalesProperties.backgroundColor": "#2b2b37",
        "scalesProperties.textColor": "#fff",
        "scalesProperties.lineColor": "#49495F",
        "scalesProperties.fontSize": 100,
        "mainSeriesProperties.style": 1,
        "paneProperties.legendProperties.showSeriesOHLC": true
    }
    let tvStyle = {
        light,
        dark
    }
    class Datafeed {
        constructor(vm) {
            this.self = vm;
        }
        onReady(callback) {
            setTimeout(()=>{
                callback({
                    supports_search: false,
                    supports_group_request: false,
                    supported_resolutions: this.self.resolutions,
                    supports_marks: true,
                    supports_timescale_marks: true,
                    supports_time: true
                })
            },30)
        }
        resolveSymbol(symbolName, onSymbolResolvedCallback, onResolveErrorCallback) {
            setTimeout(()=>{
                let data = this.defaultSymbol()
                if (this.self.resolveSymbol) {
                    this.self.resolveSymbol((res) => {
                        onSymbolResolvedCallback(Object.assign(data, res))
                    })
                } else {
                    onSymbolResolvedCallback(data)
                }
            },60)
        }
        getBars() {
            this.self.getBars(...arguments)
        }
 
        subscribeBars() {
            this.self.subscribeBars(...arguments)
        }
        unsubscribeBars() {
 
        }
        defaultSymbol() {
            return {
                'timezone': 'Asia/Shanghai',
                'minmov': 1,
                'minmov2': 0,
                'fractional': true,
                //设置周期
                'session': '24x7',
                'has_intraday': true,
                'has_no_volume': false,
                //设置是否支持周月线
                "has_daily": true,
                //设置是否支持周月线
                "has_weekly_and_monthly": true,
                //设置精度  100表示保留两位小数   1000三位   10000四位
                'pricescale': 10000
 
            };
        }
    }
 
 
    class Page {
        constructor() {
            this.datafeed = undefined;
            this.page = 1;
            this.onRealtimeCallback = undefined;
            this.TView = undefined;
            this.interval = this.getQuery('interval');
            this.symbolName = this.getQuery('symbol');
            this.theme = this.getQuery('theme') || 'dark';
            this.lang = this.getQuery('lang') || 'zh-CN';
            this.resolutions = this.getQuery('resolutions') || ["5", "15", "30", "60","240", "1D", "1W", "1M"];
            this.isLoad = false;
            this.url = this.getQuery('getLinkUrl');
            this.TVID = "tradingview_10798345";
            this.Ws = undefined;
            this.msg = '';
            this.contract = this.getQuery('contract');
            this.init();
            this.studies = []; //配置项
            this.tvBars = [];
        }
        // 获取路劲上的参数
        getQuery(name) {
            let str = window.location.search.replace('?','')
            let data = Qs.parse(str)||{}
            return data[name]
        }
        // 初始化
        init() {
            this.linkSocket()
            // 数据模型
            this.datafeed = new Datafeed(this)
            // 初始化图表
            this.TView = new TradingView.widget({
                debug: true,
                fullscreen: false,
                autosize: true,
                interval: this.interval,
                timezone: "Asia/Shanghai",
                theme: "Dark", // 自定义主题
                height:500,
                // style: "1",
                library_path: "./chart_main/",
                datafeed: this.datafeed,
                // datafeed: {},
                locale: this.chartLang(),
                toolbar_bg: this.theme == "light" ? "#fff" : "#2b2b37",
                // toolbar_bg: "#2b2b37",
                enable_publishing: false,
                withdateranges: false,
                hide_side_toolbar: false,
                allow_symbol_change: true,
                show_popup_button: true,
                hideideas: true,
                studies_overrides: {},
                container_id: "tradingview_10798345",
                enabled_features:[
                    'dont_show_boolean_study_arguments',
                ],
                disabled_features: [
                    "header_symbol_search",
                    "header_compare",
                    "control_bar",
                    "main_series_scale_menu",
                    "volume_force_overlay",
                    "header_resolutions",
                    "legend_context_menu",
                    "symbol_search_hot_key",
                    "symbol_info",
                    "pane_context_menu",
                    "header_widget_dom_node",
                    'timeframes_toolbar',
                    'header_indicators',
                    'widget_logo',
                    'header_chart_type'
 
                ],
                overrides: tvStyle[this.theme],
                custom_css_url: this.theme == "light" ? "light-chart.css" : "chart.css",
                // custom_css_url: "chart.css",
            });
            this.TView.onChartReady(() => {
                this.createStudy()
                this.TView.chart().crossHairMoved(({time, price}) => {
                    const resolutionTime = this.getResolutionTime();
                    const fTime = Math.floor(time/resolutionTime)*resolutionTime;
                    this.showKlineQuoter(fTime);
                })
            });
        }
        upsertTvBars(isFirstCall, bars) {
            if (isFirstCall) {
                this.tvBars = [];
            }
            bars.forEach((bar) => {
                if (this.tvBars.length === 0 || !this.tvBars.find(o => o.time === bar.time)) {
                    this.tvBars.push(bar);
                }
            })
            this.tvBars.sort((a,b) => {
                if (a.time < b.time) return -1;
                if( a.time > b.time ) return 1;
                return 0;
            });
        }
 
        showKlineQuoter(time) {
            if (this.tvBars.length === 0) return;
 
            const {from, to} = this.TView.chart().getVisibleRange();
            const bar = this.tvBars.find(o => o.time  === time*1000);
            if(!bar) return;
            const barTime = bar.time/1000 + 60*60*8;
            const fromSize = barTime - from;
            const toSize = to - barTime;
            const tvQuoter = $($('iframe').contents()[0]).find('#tv-quoter');
            tvQuoter.css({
                visibility: 'visible',
            })
            if (fromSize > toSize) {
                tvQuoter.css({
                    left: '10px',
                    right: 'auto'
                })
            } else {
                tvQuoter.css({
                    right: '10px',
                    left: 'auto'
                })
                tvQuoter.removeClass('left')
            }
 
            tvQuoter.find('[data-name="date"]').text(this.timestampToTime(bar.time));
            tvQuoter.find('[data-name="open"]').text(bar.open);
            tvQuoter.find('[data-name="high"]').text(bar.high);
            tvQuoter.find('[data-name="low"]').text(bar.low);
            tvQuoter.find('[data-name="close"]').text(bar.close);
            tvQuoter.find('[data-name="volume"]').text((+bar.volume).toFixed());
        }
        timestampToTime(timestamp) {
            const date = new Date(timestamp);
            const yyyy = `${date.getFullYear()}`;
            const yy = `${date.getFullYear()}`.substr(2);
            const MM = `0${date.getMonth() + 1}`.slice(-2);
            const dd = `0${date.getDate()}`.slice(-2);
            const HH = `0${date.getHours()}`.slice(-2);
            const mm = `0${date.getMinutes()}`.slice(-2);
 
 
            const resolution = this.TView.chart().resolution();
            let dateStr = ''
            if (resolution === 'D' || resolution === 'W' || resolution === 'M') {
                dateStr =`${yyyy}-${MM}-${dd}`;
            } else {
                dateStr =`${yy}-${MM}-${dd} ${HH}:${mm}`;
            }
            return dateStr
        }
        getResolutionTime() {
            const resolution = this.TView.chart().resolution();
            switch (resolution) {
                case '1':
                    return 60;
                case '5':
                    return 5 * 60;
                case '10':
                    return 10 * 60;
                case '15':
                    return 15 * 60;
                case '30':
                    return 30 * 60;
                case '60':
                    return 60 * 60;
                case '120':
                    return 120 * 60;
                case 'D':
                    return 24 * 60 * 60;
                case 'W':
                    return 7 * 24 * 60 * 60;
                case 'M':
                    return 4 * 7 * 24 * 60 * 60;
            }
            return 1;
        }
 
        createStudy() {
            let thats = this.TView;
            let id = thats.chart().createStudy('Moving Average', false, true, [5], null, {
              'Plot.color': 'rgb(150, 95, 196)'
            });
            this.studies.push(id);
            id = thats.chart().createStudy('Moving Average', false, true, [10], null, {
              'Plot.color': 'rgb(116,149,187)'
            });
            this.studies.push(id);
            id = thats.chart().createStudy('Moving Average', false, true, [20], null, {
              "plot.color": "rgb(118,32,99)"
            });
            this.studies.push(id);
          }
        // 连接socket
        linkSocket() {
            // 连接socket
            this.Ws = new Ws(this.getQuery('ws'))
            this.Ws.on('message', (evt) => {
                if (evt.cmd == 'ping') {
                    this.Ws.send({ cmd: 'pong' })
                }
                if (evt.type == 'ping') {
                    this.Ws.send({ cmd: 'pong' })
                }
                //  追加数据
                // console.log(evt,evt.sub ,this.msg)
                if (evt.sub == this.msg) {
                    this.onRealtimeCallback(this.getMap(evt.data))
                }
            })
        }
        // 图表语言映射
        chartLang() {
            switch (this.lang) {
                case "zh-CN":
                    return 'zh';
                case "zh-TW":
                    return 'zh_TW';
                case "tr":
                    return 'tr';
                case "jp":
                    return 'ja';
                case "kor":
                    return 'ko';
                case "de":
                    return 'de_DE';
                case "fra":
                    return 'fr';
                case "it":
                    return 'it';
                case "pt":
                    return 'pt';
                case "spa":
                    return 'es';
                default:
                    return 'en';
            }
        }
        getMap(data) {
            return {
                time: data.id * 1000,
                close: data.close * 1,
                open: data.open * 1,
                high: data.high * 1,
                low: data.low * 1,
                volume: data.vol * 1,
            };
        }
        resolveSymbol(call) {
            // 名称
            call({
                'name': this.symbolName.toLocaleUpperCase(),
                'description': this.symbolName.toLocaleUpperCase(),
                'ticker': this.symbolName.toLocaleUpperCase(),
                'supported_resolutions': this.resolutions
            })
        }
        // 获取数据
        getBars(symbolInfo, resolution, rangeStartDate, rangeEndDate, onDataCallback, onErrorCallback, isFirstCall) {
 
            let page = this.page > 3 ? 3 : this.page;
            let data = {
                symbol: symbolInfo.name,
                period: this.resolution(resolution),
                form: rangeStartDate,
                to: rangeEndDate,
                size: page * 200,
                zip: 2
            }
            this.page++
            this.isLoad = true
            this.unSub();
            $.get(this.url, data).then(res => {
                let arr = this.unzip(res.data.data).map((item) => {
                    return this.getMap(item);
                });
                this.upsertTvBars(isFirstCall, arr);
                onDataCallback(arr);
                this.msg = this.createMsg()
                this.sub()
 
            }).catch(err => {
                onDataCallback([]);
            })
        }
        // 解压
        unzip(b64Data) {
            let u8 = atob(b64Data)
            let jiya = pako.inflate(u8)
            let str = this.Uint8ArrayToString(jiya)
            return JSON.parse(str);
        }
        Uint8ArrayToString(fileData){
            var dataString = "";
            for (var i = 0; i < fileData.length; i++) {
              dataString += String.fromCharCode(fileData[i]);
            }
 
            return dataString
 
          }
        // 获取传给后台的精度
        resolution(resolution) {
            let T = "";
            if (isNaN(resolution * 1)) {
                T = resolution
                    .replace("D", "day")
                    .replace("W", "week")
                    .replace("M", "mon");
            } else {
                if (resolution > 60) {
                    T = Math.floor(resolution / 60) + "hour";
                } else {
                    T = resolution + "min";
                }
            }
            return T;
        }
        // 获取推送回调
        subscribeBars(symbolInfo,
            resolution,
            onRealtimeCallback,
            subscriberUID,
            onResetCacheNeededCallback) {
            this.onRealtimeCallback = onRealtimeCallback;
            if (!this.symbolName) {
                setTimeout(() => {
                    onResetCacheNeededCallback();
                }, 100);
            }
        }
        getSymbol(name) {
            return name.split("/").join("").toLowerCase();
        }
        // 生成订阅数据
        createMsg() {
           // console.log(this.contract)
           console.log(`swapKline_${this.symbolName}_${this.resolution(this.interval)}`)
            if (this.contract) {
                return `swapKline_${this.symbolName}_${this.resolution(this.interval)}`
            } else {
 
                return `Kline_${this.getSymbol(this.symbolName)}_${this.resolution(this.interval)}`
            }
        }
        // 订阅消息
        sub() {
            console.log('msg1111',this.msg)
            this.Ws.send({
                cmd: "sub",
                msg: this.msg
            })
        }
        // 取消订阅
        unSub() {
            if (!this.msg) return;
            this.Ws.send({
                cmd: "unsub",
                msg: this.msg
            })
        }
    }
    new Page()
})