-
Notifications
You must be signed in to change notification settings - Fork 5
/
Code.gs
164 lines (146 loc) · 6.29 KB
/
Code.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
//
// Developed by Tooraj Enayati
// Copying and distribution is promitted as long as credits are given
// Donation are greatly appreciated
// BTC: 3QSSMwKuoS4wRJQCNofnqBVitpheBDPx8w
// ETH: 0x5d883ef2ddac91034186b732cd1126cdb5d2c0f4
// LTC: MBsbj8q38seA3Pk6tZk1WY7DkFRf2Yf6x1
// Twitter: @tooraj_enayati
// Telegram: ToorajEnayati
// Email: [email protected]
// Discord: tooraj#7318
//
//--------------------
// How to Use instructions:
//
// This script will add a menu item to your Google Sheet to get BitMEX account history.
// You can select the menu option to download your history as many time as you want.
//--------------------
// How to Setup Instructions:
//
// Create sheet called "Settings"
// Add a row for each bot with the following headings - currently rows 1-2 and columns 1-5 are all ignored and only columns 6 & 7 are used.
//
// Configuration
// Name<tab>Description<tab>Exchange<tab>Currency<tab>Download Limit<tab>API Key<tab>API Secret
// BOT1<tab>Short Bot<tab>BitMEX<tab>XBT<tab>100<tab><your key><tab><your secret>
// BOT2<tab>Long Bot<tab>BitMEX<tab>XBT<tab>100<tab><your key><tab><your secret>
//
// Use the Tool > Script Editor for you Google Sheet to add all of this to the editor, the save it.
// Save it all, the close and reopen you Google Sheet. You will be prompted to give the scrip run permissions.
// Once the permissions are granted, you should see see a "Get BitMEX History" menu option - USE IT :)
//--------------------
// Future enhancements:
// 1) Call the BitMEXGetHistory() for each bot/row
// 2) Use the "Download Limit" column for getting the history for more than 100
// 3) Use the "Currency" column for getting the history for other currencies
// 4) Use the "Exchange" column for getting the history from other exchanges
//
function onOpen() {
createMenu();
}
function createMenu() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Get BitMEX History')
.addItem('Download History', 'GetHistory')
.addItem('About', 'about')
.addToUi();
}
function GetHistory() {
// Read the settings for each bot and call BitMEXGetHistory() for each one
//
// sheetConf: The name of the configuration sheet to read from
//
var ss = SpreadsheetApp.getActive();
var notBlank=true, i=3;
var sheetConf = "Settings";
// For each bot listed in settings; get the API keys from the sheet
while (notBlank){
var botName = ss.getSheetByName(sheetConf).getRange(i,1).getValue();
var limit = ss.getSheetByName(sheetConf).getRange(i,5).getValue();
var sLimit = Utilities.formatString('%d', limit);
var key = ss.getSheetByName(sheetConf).getRange(i,6).getValue();
var secret = ss.getSheetByName(sheetConf).getRange(i,7).getValue();
var destName = "PASTE" + String(i-2);
if (botName!==""){
BitMEXGetHistory(sLimit,key,secret,destName);
i++;
}else{ notBlank = false;}
}
}
//
// Reads the wallet history
// apiKey: the cell coordinate for reading the API key
// apiSecret: the cell coordinate for reading the API secret
// destSheet: The name of the configuration sheet to read from and write to
//
function BitMEXGetHistory(downLimit,apiKey,apiSecret,destSheet){
// Constrcut the URL https://www.bitmex.com/api/v1/user/walletHistory?currency=XBt&count=100
var webSite = "https://www.bitmex.com";
var path = "/api/v1/user/walletHistory?currency=XBt&count=" + downLimit;
url = webSite + path;
// Construct the signature
var nonce = Number(new Date().getTime()).toFixed(0);
var string = 'GET'+path+nonce;
var sKey = Utilities.computeHmacSha256Signature(string, apiSecret);
sKey = sKey.map(function(e) {
var v = (e < 0 ? e + 256 : e).toString(16);
return v.length == 1 ? "0" + v : v;
}).join("");
// Construct the header details
var params = {
'method': 'GET',
'headers': {
'api-signature': sKey,
'api-key': apiKey,
'api-nonce': nonce
},
'muteHttpExceptions': true
};
// Send the request to the BitMEX API and receive the user data.
var response = UrlFetchApp.fetch(url, params);
var dataAll = JSON.parse(response.getContentText());
var dataSet = dataAll;
//Logger.log(dataSet);
var rows = [], data;
var tempDate;
// write the data in rows
for (i = 0; i < dataSet.length; i++) {
data = dataSet[i];
if (data.transactTime !== null){
// tempDate = new Date(data.transactTime.replace(/^(\d{1,2})[-.](\d{1,2})[-.](\d{4})/g,"$3/$2/$1"));
tempDate = data.transactTime.replace("T", " ");
tempDate = tempDate.replace("Z", "");
Logger.log(data.transactTime + " > " + tempDate);
}else{
tempDate = "null"
}
rows.push([tempDate,data.transactType,data.amount,data.fee,data.address,data.transactStatus,data.walletBalance]);
}
var ss = SpreadsheetApp.getActive();
var currentSheet = ss.getSheetByName(destSheet);
currentSheet.clearContents();
var header = [];
header.push(["transactTime","transactType","amount","fee","address","transactStatus","walletBalance"]);
var cell = ss.getSheetByName(destSheet).getRange(1,1,header.length, 7);
cell.setValues(header);
var cell = ss.getSheetByName(destSheet).getRange(2,1,rows.length, 7);
cell.setValues(rows);
}
function about() {
// Display a modeless dialog box with custom HtmlService content.
var htmlOutput = HtmlService
.createHtmlOutput('<font face="verdana"><p><b>Developed by Tooraj Enayati</b></p>' +
'<p>Copying and distribution is promitted as long as credits are given</p>' +
'<p><b>Donation are greatly appreciated</b></p>' +
'<p>BTC: 3QSSMwKuoS4wRJQCNofnqBVitpheBDPx8w<br>' +
'ETH: 0x5d883ef2ddac91034186b732cd1126cdb5d2c0f4<br>' +
'LTC: MBsbj8q38seA3Pk6tZk1WY7DkFRf2Yf6x1</p>' +
'<p><b>Twitter</b>: @tooraj_enayati<br>' +
'<b>Telegram</b>: ToorajEnayati<br>' +
'<b>Email</b>: [email protected]<br>' +
'<b>Discord</b>: tooraj#7318</p></font>')
.setWidth(450)
.setHeight(350);
SpreadsheetApp.getUi().showModelessDialog(htmlOutput, 'BitMEX History Downloader');
}