-
Notifications
You must be signed in to change notification settings - Fork 1
/
getPrice.gs
193 lines (151 loc) · 4.78 KB
/
getPrice.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
191
192
193
function addBtcPrice(e) { // Btc
// https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getsheetbynamename
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("BTC");
if (sheet != null) {
// sheet.getRange('B1').setValue(elaLastPrice());
sheet.getCurrentCell().setValue(getCurrTime());
sheet.getCurrentCell().offset(0, 1).setValue(getLastPrice("btcusdt")); // 向右一个单元格
}
}
function addEthPrice(e) { // ETH
insertCellPrice("ETH", "ethusdt")
}
function addHtPrice(e) { // HT
insertCellPrice("HT", "htusdt")
}
function addNasPrice(e) { // 星云
insertCellPrice("NAS", "nasusdt")
}
function addEosPrice(e) { // Eos
insertCellPrice("EOS", "eosusdt")
}
function addElaPrice(e) { // Ela 亦来云
insertCellPrice("BTC", "elausdt")
}
function addCkbPrice(e) { // Ckb
insertCellPrice("ckb", "ckbusdt")
}
function addBandPrice(e) { // BAND
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("BAND");
if (sheet != null) {
sheet.getCurrentCell().setValue(getCurrTime());
sheet.getCurrentCell().offset(0, 1).setValue(getLastPriceByBinance("BANDUSDT"));
}
}
function addXRPPrice(e) { // XRP
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("XRP");
if (sheet != null) {
sheet.getCurrentCell().setValue(getCurrTime());
sheet.getCurrentCell().offset(0, 1).setValue(getLastPriceByBinance("XRPUSDT"));
}
}
function addAtomPrice(e) { // ATOM
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ATOM");
if (sheet != null) {
sheet.getCurrentCell().setValue(getCurrTime());
sheet.getCurrentCell().offset(0, 1).setValue(getLastPriceByBinance("ATOMUSDT"));
}
}
function addDotPrice(e) { // DOT
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DOT");
if (sheet != null) {
sheet.getCurrentCell().setValue(getCurrTime());
sheet.getCurrentCell().offset(0, 1).setValue(getLastPriceByBinance("DOTUSDT"));
}
}
function insertCellPrice(sheet_name, symbol) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet_name);
if (sheet != null) {
sheet.getCurrentCell().setValue(getCurrTime());
sheet.getCurrentCell().offset(0, 1).setValue(getLastPrice(symbol));
}
}
function getCurrTime() { // 当前时间
var _now = new Date();
var now = _now.format();
Logger.log("now: " + now);
return now;
}
function getLastPrice(symbol) { // 获取当前现货价格
/*
{
"status":"ok",
"ch":"market.btcusdt.detail",
"ts":1567396831582,
"tick":{
"amount":24130.29991795428,
"open":9093.53,
"close":8745.28,
"high":9187.7,
"id":210053472405,
"count":238168,
"low":8702.01,
"version":210053472405,
"vol":216036731.61609185
}
}
*/
var url = "https://api.huobi.pro/market/detail?symbol=" + symbol;
var options = {
'method': 'get',
'contentType': 'application/json',
muteHttpExceptions: true, // https://www.coder.work/article/1860288
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
var obj = JSON.parse(response.getContentText());
var tick = obj['tick'];
var close = tick['close'];
Logger.log(symbol + " close price: " + close);
return parseFloat(close);
}
function getLastPriceByBinance(symbol) { // 获取当前现货价格
/*
https://api.binance.com/api/v3/ticker/price?symbol=BANDUSDT
{
"symbol":"BANDUSDT",
"price": "0.85670"
}
*/
var url = "https://api.binance.com/api/v3/ticker/price?symbol=" + symbol;
var options = {
'method' : 'get',
'contentType': 'application/json',
muteHttpExceptions: true,
};
var resp = UrlFetchApp.fetch(url, options);
Logger.log(resp.getContentText());
var obj = JSON.parse(resp.getContentText());
var price = obj['price'];
Logger.log(symbol + " price: " + price);
return parseFloat(price);
}
Date.prototype.format = function(format) {
// https://segmentfault.com/q/1010000011772052
// format="yyyy-MM-dd hh:mm:ss";
if (!format) {
format = "yyyy-MM-dd hh:mm:ss";
}
var o = {
"M+": this.getMonth() + 1, // month
"d+": this.getDate(), // day
"H+": this.getHours(), // hour
"h+": this.getHours(), // hour
"m+": this.getMinutes(), // minute
"s+": this.getSeconds(), // second
"q+": Math.floor((this.getMonth() + 3) / 3), // quarter
"S": this.getMilliseconds()
};
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "")
.substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
o[k] :
("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
};