-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtradingVolume.gs
190 lines (169 loc) · 5.11 KB
/
tradingVolume.gs
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
// Configuration
// Function to fetch KuCoin volume
function fetchKuCoinVolume() {
try {
const response = UrlFetchApp.fetch(
'https://api.kucoin.com/api/v1/market/stats?symbol=ROUTE-USDT',
{ muteHttpExceptions: true }
);
const data = JSON.parse(response.getContentText());
return parseFloat(data.data.volValue) || 0;
} catch (error) {
Logger.log('Error fetching KuCoin volume: ' + error);
return 0;
}
}
// Function to fetch MEXC volume
function fetchMEXCVolume() {
try {
const response = UrlFetchApp.fetch(
'https://api.mexc.com/api/v3/ticker/24hr?symbol=ROUTEUSDT',
{ muteHttpExceptions: true }
);
const data = JSON.parse(response.getContentText());
return parseFloat(data.quoteVolume) || 0;
} catch (error) {
Logger.log('Error fetching MEXC volume: ' + error);
return 0;
}
}
// Function to fetch AscendEX volume
function fetchAscendEXVolume() {
try {
const response = UrlFetchApp.fetch(
'https://ascendex.com/api/pro/v1/spot/ticker?symbol=ROUTE/USDT',
{ muteHttpExceptions: true }
);
const data = JSON.parse(response.getContentText());
const price = parseFloat(data.data.close);
const volume = parseFloat(data.data.volume);
return price * volume || 0;
} catch (error) {
Logger.log('Error fetching AscendEX volume: ' + error);
return 0;
}
}
// Function to fetch Gate.io volume
function fetchGateVolume() {
try {
const response = UrlFetchApp.fetch(
'https://api.gateio.ws/api/v4/spot/tickers?currency_pair=ROUTE_USDT',
{ muteHttpExceptions: true }
);
const data = JSON.parse(response.getContentText());
return parseFloat(data[0].quote_volume) || 0;
} catch (error) {
Logger.log('Error fetching Gate.io volume: ' + error);
return 0;
}
}
// Function to fetch Bitget volume
function fetchBitgetVolume() {
try {
const response = UrlFetchApp.fetch(
'https://api.bitget.com/api/v2/spot/market/tickers?symbol=ROUTEUSDT',
{ muteHttpExceptions: true }
);
const data = JSON.parse(response.getContentText());
return parseFloat(data.data[0].usdtVolume) || 0;
} catch (error) {
Logger.log('Error fetching Bitget volume: ' + error);
return 0;
}
}
// Function to fetch HTX volume
function fetchHTXVolume() {
try {
const response = UrlFetchApp.fetch(
'https://api.huobi.pro/market/detail?symbol=routeusdt',
{ muteHttpExceptions: true }
);
const data = JSON.parse(response.getContentText());
return parseFloat(data.tick.vol) || 0;
} catch (error) {
Logger.log('Error fetching HTX volume: ' + error);
return 0;
}
}
// Function to get volume for a specific exchange
function getExchangeVolume(exchange) {
switch(exchange) {
case 'KuCoin':
return fetchKuCoinVolume();
case 'MEXC':
return fetchMEXCVolume();
case 'ASCENDEX':
return fetchAscendEXVolume();
case 'GATE':
return fetchGateVolume();
case 'BITGET':
return fetchBitgetVolume();
case 'HTX':
return fetchHTXVolume();
default:
return 0;
}
}
// Main function to calculate and record total volume
function calculateTotalVolume() {
try {
const ss = SpreadsheetApp.getActive();
let sheet = ss.getSheetByName('Total Trading Volume');
// Create sheet if it doesn't exist
if (!sheet) {
sheet = ss.insertSheet('Total Trading Volume');
// Add headers
sheet.getRange('A1:H1').setValues([[
'Timestamp',
'Total Volume',
...CONFIG.CEX_LIST
]]);
// Format headers
sheet.getRange('A1:H1')
.setBackground('#D3D3D3')
.setFontWeight('bold')
.setHorizontalAlignment('center');
}
let totalVolume = 0;
const volumeByExchange = {};
// Collect volume data from each exchange
for (const cex of CONFIG.CEX_LIST) {
const volume = getExchangeVolume(cex);
volumeByExchange[cex] = volume;
totalVolume += volume;
}
// Prepare row data
const currentDate = new Date();
const rowData = [
currentDate,
totalVolume,
...CONFIG.CEX_LIST.map(cex => volumeByExchange[cex] || 0)
];
// Add new row of data
const nextRow = sheet.getLastRow() + 1;
sheet.getRange(nextRow, 1, 1, rowData.length).setValues([rowData]);
// Format the new row
sheet.getRange(nextRow, 2, 1, rowData.length - 1).setNumberFormat('$#,##0.00');
sheet.getRange(nextRow, 1).setNumberFormat('yyyy-mm-dd hh:mm:ss');
// Auto-size columns
sheet.autoResizeColumns(1, rowData.length);
Logger.log('Volume data updated successfully');
} catch(error) {
Logger.log('Error calculating total volume: ' + error);
}
}
// Create trigger to run every hour
function createHourlyVolumeTrigger() {
// Delete any existing triggers
const triggers = ScriptApp.getProjectTriggers();
triggers.forEach(trigger => {
if(trigger.getHandlerFunction() === 'calculateTotalVolume') {
ScriptApp.deleteTrigger(trigger);
}
});
// Create new hourly trigger
ScriptApp.newTrigger('calculateTotalVolume')
.timeBased()
.everyHours(1)
.create();
}