-
Notifications
You must be signed in to change notification settings - Fork 2
/
crypto_loader.js
196 lines (174 loc) · 6.61 KB
/
crypto_loader.js
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
registerIndicator("fintechee_crypto_loader", "A plugin to load the specific cryptocurrency's streaming quotes(v1.0)", function (context) {
// Disclaimer: we are not affiliated with the data providers or the API providers.
var changeCallback = function (ctx) {
var symbolName = getExtraSymbolName(ctx)
if (typeof window.fintecheeCryptoLoader.cryptocurrenciesList[symbolName] != "undefined") {
var chartId = getChartHandleByContext(ctx)
var symbol = window.fintecheeCryptoLoader.cryptocurrenciesList[symbolName].symbolName.split("/")
var baseCurrency = symbol[0]
var termCurrency = symbol[1]
setTakeoverMode(chartId)
changeChartMenuItemName(ctx)
var timeFrame = getTimeFrame(ctx)
window.fintecheeCryptoLoader.ticksList.add(baseCurrency)
window.fintecheeCryptoLoader.ticksList.add(termCurrency)
var chart = window.fintecheeCryptoLoader.charts[chartId + ""]
chart.timeFrame = timeFrame
chart.baseCurrency = baseCurrency
chart.termCurrency = termCurrency
$.ajax({
type: "GET",
url: "https://api.coincap.io/v2/candles",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: {
exchange: "poloniex",
interval: timeFrame.toLowerCase(),
baseId: baseCurrency,
quoteId: termCurrency
},
success: function (res) {
if (typeof res.data != "undefined") {
var data = []
if (Array.isArray(res.data)) {
for (var i in res.data) {
var ohlc = res.data[i]
data.push({
time: Math.floor(ohlc.period / 1000),
volume: parseFloat(ohlc.volume),
open: parseFloat(ohlc.open),
high: parseFloat(ohlc.high),
low: parseFloat(ohlc.low),
close: parseFloat(ohlc.close)
})
}
takeoverLoad(chartId, data)
}
}
}
})
window.fintecheeCryptoLoader.setupSocket()
} else {
var chartId = getChartHandleByContext(ctx)
unsetTakeoverMode(chartId)
takeoverLoad(chartId, [])
delete window.fintecheeCryptoLoader.charts[chartId + ""]
}
}
var removeCallback = function (ctx) {
var chartId = getChartHandleByContext(ctx)
unsetTakeoverMode(chartId)
takeoverLoad(chartId, [])
delete window.fintecheeCryptoLoader.charts[chartId + ""]
}
var chartId = getChartHandleByContext(context)
if (typeof window.fintecheeCryptoLoader == "undefined") {
window.fintecheeCryptoLoader = {
cryptocurrenciesList: [],
charts: [],
ticksList: new Set(),
socket: null,
onTick: function (msg) {
var ticks = JSON.parse(msg.data)
for (var i in window.fintecheeCryptoLoader.charts) {
var chart = window.fintecheeCryptoLoader.charts[i]
var bUpdatable = false
for (var j in ticks) {
if (j == chart.baseCurrency) {
chart.baseCurrencyPrice = parseFloat(ticks[j])
bUpdatable = true
}
if (j == chart.termCurrency) {
chart.termCurrencyPrice = parseFloat(ticks[j])
bUpdatable = true
}
}
if (bUpdatable) {
if (chart.baseCurrencyPrice != null && chart.termCurrencyPrice != null && chart.baseCurrencyPrice > 0 && chart.termCurrencyPrice > 0) {
var tick = {
time: Math.floor(new Date().getTime() / 1000),
volume: 0,
price: chart.baseCurrencyPrice / chart.termCurrencyPrice
}
if (chart.timeFrame == "M1") {
takeoverUpdate(chart.chartId, 60, tick)
} else if (chart.timeFrame == "M5") {
takeoverUpdate(chart.chartId, 300, tick)
} else if (chart.timeFrame == "M15") {
takeoverUpdate(chart.chartId, 900, tick)
} else if (chart.timeFrame == "M30") {
takeoverUpdate(chart.chartId, 1800, tick)
} else if (chart.timeFrame == "H1") {
takeoverUpdate(chart.chartId, 3600, tick)
} else if (chart.timeFrame == "H4") {
takeoverUpdate(chart.chartId, 14400, tick)
} else if (chart.timeFrame == "D") {
takeoverUpdate(chart.chartId, 86400, tick)
} else {
takeoverUpdate(chart.chartId, 86400, tick)
}
}
}
}
},
setupSocket: function () {
if (window.fintecheeCryptoLoader.socket != null) {
if(window.fintecheeCryptoLoader.socket.readyState == WebSocket.OPEN) {
window.fintecheeCryptoLoader.socket.close()
}
window.fintecheeCryptoLoader.socket = null
}
var ticksList = Array.from(window.fintecheeCryptoLoader.ticksList)
if (ticksList.length > 0) {
window.fintecheeCryptoLoader.socket = new WebSocket("wss://ws.coincap.io/prices?assets=" + ticksList.join(","))
window.fintecheeCryptoLoader.socket.onclose = function (e) {}
window.fintecheeCryptoLoader.socket.onmessage = this.onTick
}
window.addEventListener("unload", function () {
if (window.fintecheeCryptoLoader.socket != null) {
if(window.fintecheeCryptoLoader.socket.readyState == WebSocket.OPEN) {
window.fintecheeCryptoLoader.socket.close()
}
window.fintecheeCryptoLoader.socket = null
}
})
}
}
window.fintecheeCryptoLoader.charts[chartId + ""] = {
chartId: chartId,
timeFrame: null,
baseCurrency: null,
termCurrency: null,
baseCurrencyPrice: null,
termCurrencyPrice: null
}
createTakeover(chartId, changeCallback, removeCallback)
var cryptocurrenciesList = [{
symbolName: "ethereum/bitcoin",
displayName: "ETH/BTC"
}]
for (var i in cryptocurrenciesList) {
window.fintecheeCryptoLoader.cryptocurrenciesList[cryptocurrenciesList[i].displayName] = cryptocurrenciesList[i]
}
addExtraSymbols(cryptocurrenciesList)
} else if (typeof window.fintecheeCryptoLoader.charts[chartId + ""] == "undefined") {
window.fintecheeCryptoLoader.charts[chartId + ""] = {
chartId: chartId,
baseCurrency: null,
termCurrency: null,
baseCurrencyPrice: null,
termCurrencyPrice: null
}
createTakeover(chartId, changeCallback, removeCallback)
}
},
[],
[{
name: DATA_NAME.TIME,
index: 0
}],
[{
name: "crypto",
visible: false
}],
WHERE_TO_RENDER.CHART_WINDOW)