forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
210 lines (169 loc) · 4.6 KB
/
logger.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
var util = require('./util.js');
var _ = require('lodash');
var log = require('./log.js');
var moment = require('moment');
var Logger = function(config) {
this.config = util.getConfig();
this.verbose = config.verbose;
this.fee = 1 - config.fee / 100;
this.reportInCurrency = config.reportInCurrency;
if(this.reportInCurrency)
this.reportIn = config.currency;
else
this.reportIn = config.asset;
// virtual balance
this.start = {
asset: config.simulationBalance.asset,
currency: config.simulationBalance.currency,
balance: false
}
this.current = {
asset: this.start.asset,
currency: this.start.currency,
balance: false
}
this.trades = 0;
this.tracks = 0;
_.bindAll(this);
if(config.enabled)
log.info('Profit reporter active on simulated balance');
}
// log advice
Logger.prototype.inform = function(what, price, meta) {
if(!this.verbose && what !== 'SELL' && !this.config.backtest)
return;
if(!this.verbose && what === 'HOLD' && this.config.backtest)
return;
log.info('ADVICE is to', what, meta);
}
Logger.prototype.extractFee = function(amount) {
amount *= 100000000;
amount *= this.fee;
amount = Math.floor(amount);
amount /= 100000000;
return amount;
}
// after every succesfull trend ride we end up with more BTC than we started with,
// this function calculates Gekko's profit in %.
Logger.prototype.trackProfits = function(what, price, meta) {
this.tracks++;
// first time calculate the virtual account balance
if(!this.start.balance) {
if(this.reportInCurrency)
this.start.balance = this.start.currency + price * this.start.asset;
else
this.start.balance = this.start.asset + this.start.currency / price;
}
// virtually trade all USD to BTC at the current MtGox price
if(what === 'BUY') {
this.current.asset += this.extractFee(this.current.currency / price);
this.current.currency = 0;
this.trades++;
}
// virtually trade all BTC to USD at the current MtGox price
if(what === 'SELL') {
this.current.currency += this.extractFee(this.current.asset * price);
this.current.asset = 0;
this.trades++;
}
if(this.reportInCurrency)
this.current.balance = this.current.currency + price * this.current.asset;
else
this.current.balance = this.current.asset + this.current.currency / price;
this.profit = this.current.balance - this.start.balance;
this.relativeProfit = this.current.balance / this.start.balance * 100 - 100;
if(this.tracks === 1)
return;
if(!this.verbose && what === 'SELL' && !this.config.backtest)
this.report();
else if(this.verbose && !this.config.backtest)
this.report();
}
Logger.prototype.finish = function(data) {
var round = function(amount) {
return amount.toFixed(6);
}
console.log();
console.log();
log.info('\tWARNING: BACKTESTING FEATURE NEEDS PROPER TESTING')
log.info('\tWARNING: ACT ON THESE NUMBERS AT YOUR OWN RISK!')
console.log();
console.log();
var start = moment.unix(data.startTime);
var end = moment.unix(data.endTime);
var timespan = end.diff(start, 'days');
log.info(
'(PROFIT REPORT)',
'start time:\t\t\t',
start.format('YYYY-MM-DD HH:mm:ss')
);
log.info(
'(PROFIT REPORT)',
'end time:\t\t\t',
end.format('YYYY-MM-DD HH:mm:ss')
);
log.info(
'(PROFIT REPORT)',
'timespan:\t\t\t',
timespan,
'days'
);
console.log();
log.info(
'(PROFIT REPORT)',
'start price:\t\t\t',
data.start
);
log.info(
'(PROFIT REPORT)',
'end price:\t\t\t',
data.end
);
log.info(
'(PROFIT REPORT)',
'Buy and Hold profit:\t\t',
round((data.end - data.start) / data.start * 100) + '%'
);
console.log();
log.info(
'(PROFIT REPORT)',
'amount of trades:\t\t',
this.trades
);
this.report(timespan);
}
Logger.prototype.report = function(timespan) {
var round = function(amount) {
return amount.toFixed(6);
}
log.info(
'(PROFIT REPORT)',
'original simulated balance:\t',
round(this.start.balance),
this.reportIn
);
log.info(
'(PROFIT REPORT)',
'current simulated balance:\t',
round(this.current.balance),
this.reportIn
);
log.info(
'(PROFIT REPORT)',
'simulated profit:\t\t',
round(this.profit),
this.reportIn,
'(' + round(this.relativeProfit) + '%)'
);
if(timespan) {
var timespanPerYear = 356 / timespan;
log.info(
'(PROFIT REPORT)',
'simulated yearly profit:\t',
round(this.profit * timespanPerYear),
this.reportIn,
'(' + round(this.relativeProfit * timespanPerYear) + '%)'
);
}
}
module.exports = Logger;