-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
186 lines (151 loc) · 5.04 KB
/
app.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
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
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var CronJob = require('cron').CronJob;
var axios = require('axios');
var chalk = require('chalk');
var index = require('./routes/index');
var R = require('ramda');
require('console.table');
var readline = require('readline');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
const remaining_ETH = 4.57655486;
const my_investment = require('./investment');
function generate_coin_data(data,my_investment)
{
let table = [];
var total = {
initial_buy:0,
profit:0,
precentage_change:0,
all:0
};
for (var key in my_investment)
{
if(!my_investment[key].initial_buying) continue;
const symbol = key+my_investment[key].currency;
const fiatSymbol = key+'USDT';
//filter it from all data
const coin = R.propEq('symbol', symbol);
const coin_data = R.filter(coin, data);
const coin_price =coin_data[0].price;
const coin_fiat = R.propEq('symbol',fiatSymbol);
const coin_fiat_data = R.filter(coin_fiat, data);
const coin_fiat_price =coin_fiat_data[0].price;
const cashout_price = coin_price * my_investment[key].balance * 0.99;
const profit = cashout_price - my_investment[key].initial_buying;
const precentage_change = ((cashout_price - my_investment[key].initial_buying) / my_investment[key].initial_buying) *100;
const table_row =
{
'Coin' : key,
'Initial Buy': my_investment[key].initial_buying,
'Profit': profit < 0 ? chalk.red("↓"+my_investment[key].currency+" "+Math.round(Math.abs(profit) * 1000000) / 1000000)
: chalk.green("↑"+my_investment[key].currency+" "+Math.round(Math.abs(profit) * 1000000) / 1000000),
'%': precentage_change < 0 ? chalk.red("↓% "+Math.round(Math.abs(precentage_change) * 100) / 100)
: chalk.green("↑% "+Math.round(Math.abs(precentage_change) * 100) / 100),
'$': coin_fiat_price,
'Total ETH': cashout_price
};
total.all += cashout_price;
total.initial_buy += my_investment[key].initial_buying;
total.precentage_change += precentage_change;
total.profit += profit
table.push(table_row);
}
table.push({'Coin' :'',
'Initial Buy':'-------------------',
'Profit':'-------------------',
'%':'-------------------',
'$':'-------------------',
'Total ETH': '-------------------'
});
const total_amount = {
'Coin' :'',
'Initial Buy':chalk.yellow(total.initial_buy),
'Profit':chalk.yellow(total.profit),
'%': '',
'$': '',
'Total ETH': chalk.yellow(total.all)
};
table.push(total_amount);
table.push({'Coin' :'',
'Initial Buy':' ',
'Profit':' ',
'%':' ',
'$':' ',
'Total ETH': '+'
});
table.push({'Coin' :'',
'Initial Buy':' ',
'Profit':' ',
'%':'Remaining ETH',
'$':' ',
'Total ETH': remaining_ETH
});
table.push({'Coin' :'',
'Initial Buy':'-------------------',
'Profit':'-------------------',
'%':'-------------------',
'$':'-------------------',
'Total ETH': '-------------------'
});
table.push({'Coin' :'',
'Initial Buy':' ',
'Profit':' ',
'%':'Total ETH',
'$':' ',
'Total ETH': remaining_ETH+total.all
});
return table;
}
var apicall_cronjob = new CronJob('*/3 * * * * *', function() {
axios.get('https://api.binance.com/api/v3/ticker/price')
.then(function (response) {
//clear the console
const blank = '\n'.repeat(process.stdout.rows);
console.log(blank);
readline.cursorTo(process.stdout, 0, 0);
readline.clearScreenDown(process.stdout);
//get binance result for all markets
let data=response.data;
table = generate_coin_data(data,my_investment);
console.table(table);
})
.catch(function (error) {
console.log(error);
});
}, function () {
console.log('Woops apicall cron stopped!');
},
true /* Start the job right now */
); //end of cron
module.exports = app;