|
| 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; |
0 commit comments