-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbybitProfit.js
114 lines (92 loc) · 3.47 KB
/
bybitProfit.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
const config = require('./config.json');
const ccxt = require('ccxt');
const dayjs = require('dayjs');
const winston = require('winston');
const { program } = require('commander');
const ora = require('ora');
const chalk = require('chalk');
program.version('0.0.1');
program
.option('-s, --symbol <type>', 'symbol', 'ETH')
.option('-d, --daysAgo <type>', 'time period to lookback', 7)
program.parse(process.argv);
const options = program.opts();
console.log(options.daysAgo)
console.log(options.symbol)
// Create connection to Bybit using APIs in config.json
const bybit = new ccxt.bybit({
'apiKey': config.apiKey,
'secret': config.secret
})
// Calculate Percentage
const pnlPerc = function (pnl, wallet) {
let perc = (pnl/wallet)*100
return(perc)
}
// Calculate Sum of an Array of values
const sumArr = function (arr) {
sum = arr.reduce((a, b) => {
return a + b;
});
return sum
}
// Calculate Average of an Array of values
const avgArr = function (arr) {
var total = 0;
for(var i = 0; i < arr.length; i++) {
total += arr[i];
}
var avg = total / arr.length;
return avg
}
// Get PNL data from Bybit
// Function takes 2 variables:
// afterDate : Do not include any PNL data from before this date
// symbol : Currency/Coin you want PNL data from, Bybit accepts only these: BTC ETH EOS XRP USDT
//
// Function will print out every entry for Realized PNL to console and return the Average and Cumalative Totals of PNL and Profit Percentage
const getPNL = async function (daysAgo, symbol) {
let pnl = await bybit.v2PrivateGetWalletFundRecords({'coin':symbol, 'limit': 50})
let data = pnl.result.data
let cumPNL = []
let cumPer = []
let dateOffset = (24*60*60*1000) * daysAgo
let myDate = new Date()
let daysAgoVal = myDate.setTime(myDate.getTime()- dateOffset)
data.forEach(i => {
let date = i.exec_time
let p = i.amount
let w = i.wallet_balance
let per = pnlPerc(p, w).toFixed(2)
if(dayjs(i.exec_time).isAfter(dayjs(daysAgoVal))) {
if(i.type == 'RealisedPNL'){
if(isNaN(parseFloat(p))) {
console.error('NaN found')
} else {
console.log(`${date} - ${symbol} - Daily PNL: ${p} - Wallet Balance: ${w} - Profit %: ${per}`);
// console.log(`${date} - ${symbol} - Daily PNL: ${'**********'} - Wallet Balance: ${'**********'} - Profit %: ${per}`);
cumPNL.push(p*1)
cumPer.push(per*1)
}
} else {
// console.log('Not PNL') // Debug
}
} else {
// console.log(`Days ago: ${dayjs(daysAgoVal)}`)
}
});
let totalPNL = sumArr(cumPNL).toFixed(10)*1
let totalPerc = sumArr(cumPer)
let avgPNL = avgArr(cumPNL)
let avgPer = avgArr(cumPer)
return({'pnl':totalPNL, 'perc': totalPerc, 'coin':symbol, 'avgPNL':avgPNL, 'avgPer':avgPer})
}
// Examples of using function in a Promise
getPNL(options.daysAgo, options.symbol).then(function(btc){
console.log(`Total PNL in ${btc.coin}: ${btc.pnl}`)
console.log(`Cumulative Profit Percentage: ${btc.perc}%\n Average Daily Percentage: ${btc.avgPer.toFixed(2)}%`)
})
// getPNL('2021-01-14', 'ETH').then(function(eth){
// console.log(`Total PNL in ${eth.coin}: ${eth.pnl}`)
// console.log(`Cumulative Profit Percentage: ${eth.perc}%\n Average Daily Percentage: ${eth.avgPer.toFixed(2)}%`)
// });