Skip to content
This repository was archived by the owner on Feb 16, 2020. It is now read-only.

Commit 5d36ee0

Browse files
crypto49eraskmike
crypto49er
authored andcommitted
Added blotter plugin
1 parent 19c630a commit 5d36ee0

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

plugins.js

+11
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,17 @@ var plugins = [
241241
version: '0.18.0'
242242
}]
243243
},
244+
{
245+
name: 'Blotter',
246+
description: 'Writes all buy/sell trades to a blotter CSV file',
247+
slug: 'blotter',
248+
async: false,
249+
modes: ['realtime'],
250+
dependencies: [{
251+
module: 'fs',
252+
version: '0.0.1-security'
253+
}]
254+
},
244255
];
245256

246257
module.exports = plugins;

plugins/blotter.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const fsw = require('fs');
2+
const _ = require('lodash');
3+
const log = require('../core/log.js');
4+
const util = require('../core/util.js');
5+
const config = util.getConfig();
6+
const blotterConfig = config.blotter;
7+
8+
var Blotter = function(done) {
9+
_.bindAll(this);
10+
11+
this.time;
12+
this.valueAtBuy = 0.0;
13+
this.filename = blotterConfig.filename;
14+
this.dateformat = blotterConfig.dateFormat;
15+
this.timezone = blotterConfig.timezone;
16+
this.headertxt = '';
17+
this.outtxt = '';
18+
19+
this.done = done;
20+
this.setup();
21+
};
22+
23+
Blotter.prototype.setup = function(done) {
24+
25+
this.headertxt = "Date,Price,Amount,Side,Fees,Value,P&L,Notes\n";
26+
27+
fsw.readFile(this.filename, (err, _) => {
28+
if (err) {
29+
log.warn('No file with the name', this.filename, 'found. Creating new blotter file');
30+
fsw.appendFileSync(this.filename, this.headertxt, encoding='utf8');
31+
}
32+
});
33+
34+
};
35+
36+
Blotter.prototype.processTradeCompleted = function(trade) {
37+
// if exchange doesn't send correct timezone, correct it
38+
if (trade.date.format('Z') == '+00:00') {
39+
var adjustTimezone = trade.date.utcOffset(this.timezone);
40+
this.time = adjustTimezone.format(this.dateformat);
41+
} else {
42+
this.time = trade.date.format(this.dateformat);
43+
}
44+
45+
if (trade.action === 'buy') {
46+
this.valueAtBuy = this.roundUp(trade.effectivePrice * trade.amount);
47+
//time, price, amount, side, fees, value at buy
48+
this.outtxt = this.time + "," + trade.effectivePrice.toFixed(2) + "," + trade.amount.toFixed(8) + "," + trade.action + "," + trade.feePercent + "," + this.valueAtBuy;
49+
}
50+
else if (trade.action === 'sell'){
51+
var sellValue = (this.roundUp(trade.effectivePrice * trade.amount));
52+
var pl = this.roundUp(this.valueAtBuy - this.roundUp(trade.effectivePrice * trade.amount));
53+
log.info('Buy Value', this.valueAtBuy, 'Sell Value', sellValue, 'P&L', pl);
54+
//time, price, amount, side, fees, value at sell, P&L
55+
this.outtxt = this.time + "," + trade.effectivePrice.toFixed(2) + "," + trade.amount.toFixed(8) + "," + trade.action + "," + trade.feePercent + "," + sellValue + "," + pl;
56+
this.valueAtBuy = 0.0;
57+
}
58+
59+
// If trade.price is 0 and trade amount is 0, note the error
60+
if (trade.price == 0 && trade.amount == 0 ) {
61+
// add extra comma for buy as it doesn't have P&L info
62+
if (trade.action === 'buy') {
63+
this.outtxt = this.outtxt + ",";
64+
}
65+
this.outtxt = this.outtxt + "," + "Trade probably went through but didn't receive correct price/amount info\n";
66+
} else {
67+
this.outtxt = this.outtxt + "\n";
68+
}
69+
70+
// If a trade date is from 1969 or 1970, there was an error with the trade
71+
if (trade.date.format('YY') == '69' || trade.date.format('YY') == '70') {
72+
log.error('Received 1969/1970 error, trade failed to execute, did not record in blotter');
73+
}
74+
else {
75+
fsw.appendFileSync(this.filename, this.outtxt, encoding='utf8');
76+
}
77+
this.outtxt = "";
78+
79+
}
80+
81+
Blotter.prototype.roundUp = function(value) {
82+
var cents = value * 100;
83+
var roundedCents = Math.round(cents);
84+
return roundedCents / 100;
85+
}
86+
87+
module.exports = Blotter;

sample-config.js

+7
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ config.pushover = {
112112
user: ''
113113
}
114114

115+
config.blotter = {
116+
enabled: false,
117+
filename: 'blotter.csv',
118+
dateFormat: 'l LT',
119+
timezone: -300, // -300 minutes for EST(-5:00), only used if exchange doesn't provide correct timezone
120+
}
121+
115122
// want Gekko to send a mail on buy or sell advice?
116123
config.mailer = {
117124
enabled: false, // Send Emails if true, false to turn off

0 commit comments

Comments
 (0)