Repository navigation
Expand file tree
/
Copy pathfunctions.js
More file actions
213 lines (186 loc) · 7.15 KB
/
Copy pathfunctions.js
File metadata and controls
213 lines (186 loc) · 7.15 KB
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
"use strict";
const log = require ('ololog').configure ({ locate: false })
let details
function calculateFee (exchangeId, markets, symbol, type, side, amount, price, takerOrMaker = 'taker', params = {}) {
let market = markets[exchangeId][symbol];
// //log.magenta(exchangeId, "calculateFee", "market",market)
// let pricePrecision = market.precision.price
let key = 'quote';
let rate = market[takerOrMaker];
let cost = parseFloat (costToPrecision (exchangeId,symbol, amount * rate));
if (side === 'sell') {
cost *= price;
} else {
key = 'base';
}
return {
'type': takerOrMaker,
'currency': market[key],
'rate': rate,
'cost': parseFloat (feeToPrecision (exchangeId,symbol, cost)),
};
}
function toFixed (x) { // avoid scientific notation for too large and too small numbers
if (Math.abs (x) < 1.0) {
const e = parseInt (x.toString ().split ('e-')[1])
if (e) {
x *= Math.pow (10, e-1)
x = '0.' + (new Array (e)).join ('0') + x.toString ().substring (2)
}
} else {
let e = parseInt (x.toString ().split ('+')[1])
if (e > 20) {
e -= 20
x /= Math.pow (10, e)
x += (new Array (e+1)).join ('0')
}
}
return x
}
//log to file
function logToFile(exchangeid, symbol, algo_namespace){
var timestamp = new Date().getTime();
var _symbol = symbol.replace('/','')
var fileCSV = './data/'+algo_namespace+'.'+exchangeid +'.'+ _symbol +'.'+ new Date().getFullYear() +'.'+ (new Date().getMonth() +1)+'.'+new Date().getDate()+'.log'; //opportunities enalbed
// try {
// fs.unlinkSync(fileCSV,(err) => {
// if (err) log.red( err.message);
// log.magenta('CSV created for writing the opportunity found during the arbitrage!!!');
// });
// } catch (error) {
// log("error during delete file");
// }
var obj = {
table: []
}
obj.table.push(details);
var json = JSON.stringify(obj);
var fs = require('fs');
if (!fs.existsSync(fileCSV)) {
log("log created")
details = {}
}
//log("lgged")
fs.appendFile(fileCSV, json, 'utf8', callback);
}
function addToLog (exchange, extra, tobeinitialazed) {
if (tobeinitialazed){
details = {}
}
details = exchange.extend (extra, details)
//logToFile(exchange.id, symbol, algo)
//log.yellow("details",details, "extra", extra)
}
function callback() {
//log.yellow("write")
}
/**calculate the correct profit for given price
* currentPrice 0.00000087
* currenctProfit 0.004
* return a new profit just only if the new price > currentPrice
*/
function fetchProfit(currentPrice, currentProfit, precision) {
//log("fetchProfit",currentPrice, currentProfit, precision );
var newProfit = currentProfit
var digitProfit = currentProfit.toString().slice(currentProfit.toString().indexOf(".")+1)
var numberOfDeciaml = digitProfit.length
var newPrice = (currentPrice *(1+newProfit))
newPrice = parseFloat (newPrice).toFixed (precision)
var cP = (newPrice/currentPrice-1)*100
while (newPrice<=currentPrice && cP<currentProfit) {
newProfit = ((newProfit * Math.pow(10, numberOfDeciaml))) +0.5
newProfit = newProfit / (Math.pow(10, numberOfDeciaml))
newPrice = (currentPrice * (1+newProfit))
newPrice = parseFloat (newPrice).toFixed (precision)
cP = (newPrice/currentPrice-1)*100
//log("cP", cP, "newProfit", newProfit, "newPrice", newPrice, "currentPrice", currentPrice)
}
return newProfit
}
//apply precision to price
function priceToPrecision (exchange, markets, symbol, price) {
var precision = 8
try {
if (typeof markets[exchange] !== 'undefined' && typeof markets[exchange][symbol] !== 'undefined' && typeof markets[exchange][symbol].precision.price !== 'undefined') {
precision = markets[exchange][symbol].precision.price
}
}catch (error) {
log.red(exchange,symbol, amount, "error", error.message)
}
return parseFloat (price).toFixed (precision)
}
//apply precision to cost
function costToPrecision (exchange,markets, symbol, cost) {
var precision = 8
try {
if (typeof markets[exchange] !== 'undefined' && typeof markets[exchange][symbol] !== 'undefined' && typeof markets[exchange][symbol].precision.price !== 'undefined') {
precision = markets[exchange][symbol].precision.price
}
}catch (error) {
log.red(exchange,symbol, amount, "error", error.message)
}
return parseFloat (cost).toFixed (precision)
}
//apply precision to fee ...see above ;)
function feeToPrecision (exchangeId, markets, symbol, fee) {
var precision = 8
try {
if (typeof markets[exchange] !== 'undefined' && typeof markets[exchange][symbol] !== 'undefined' && typeof markets[exchange][symbol].precision.price !== 'undefined') {
precision = markets[exchange][symbol].precision.price
}
}catch (error) {
log.red(exchange,symbol, amount, "error", error.message)
}
return parseFloat (fee).toFixed (precision)
}
const truncate_regExpCache = []
, truncate_to_string = (num, precision = 0) => {
num = toFixed (num)
if (precision > 0) {
const re = truncate_regExpCache[precision] || (truncate_regExpCache[precision] = new RegExp("([-]*\\d+\\.\\d{" + precision + "})(\\d)"))
const [,result] = num.toString ().match (re) || [null, num]
return result.toString ()
}
return parseInt (num).toString ()
}
, truncate = (num, precision = 0) => parseFloat (truncate_to_string (num, precision))
const precisionFromString = (string) => {
const split = string.replace (/0+$/g, '').split ('.')
return (split.length > 1) ? (split[1].length) : 0
}
//apply precision to amount
function amountToPrecision (exchange,markets, symbol, amount) {
try {
var precision = 8 //as default
if (typeof markets[exchange] !== 'undefined' && typeof markets[exchange][symbol] !== 'undefined' && typeof markets[exchange][symbol].precision.amount !== 'undefined') {
precision = markets[exchange][symbol].precision.amount
}
var result = truncate (amount, precision)
return result
}
catch (error ) {
log.red(exchange,symbol, amount, "error", error.message)
}
}
function amountToString (exchange,markets,symbol, amount) {
var precision = 8 //as default
if (typeof markets[exchange] !== 'undefined' && typeof markets[exchange][symbol] !== 'undefined' && typeof markets[exchange][symbol].precision.amount !== 'undefined') {
precision = markets[exchange][symbol].precision.amount
}
return truncate_to_string (amount, precision)
}
module.exports = {
logToFile,
addToLog,
amountToString,
amountToPrecision,
precisionFromString,
truncate_to_string,
truncate,
feeToPrecision,
costToPrecision,
priceToPrecision,
calculateFee,
toFixed,
fetchProfit,
}