-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscripts.js
158 lines (129 loc) · 5.9 KB
/
scripts.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
window.BreakEvenCalc = new (function () {
//constructor
var Calc = function () {
var self = this;
this.elements = {
buySide: document.querySelector('.buy-side'),
sellSide: document.querySelector('.sell-side'),
breakEven: document.querySelector('.break-even'),
totalInvested: document.querySelector('.total-invested')
}
var addButtons = document.querySelectorAll('button.add-row');
Array.prototype.forEach.call(addButtons, function(btn) {
btn.addEventListener('click', self.addRow.bind(self));
});
document.querySelector('#fileInput').addEventListener('change', function (event) {
self.resetForm();
self.loadCSVData(event.target.files[0]);
});
document.querySelector('.load-csv').addEventListener('click', function () {
document.querySelector('#fileInput').click();
});
document.body.addEventListener('input', this.onInputChange.bind(this));
}
Calc.prototype.resetForm = function () {
this.elements.buySide.querySelector('ul').innerHTML = '';
this.elements.sellSide.querySelector('ul').innerHTML = '';
}
Calc.prototype.addRow = function (ev) {
var list = ev.target.parentElement.querySelector('ul');
list.appendChild(this.getNewRow());
}
Calc.prototype.getNewRow = function () {
var newRow = document.createElement('li');
newRow.innerHTML = '<label class="six columns"><span>Amount: </span><input type="number" class="amount" /></label>' +
'<label class="six columns"><span>Price: </span><input type="number" class="price" /></label>';
return newRow;
}
Calc.prototype.onInputChange = function (ev) {
this.calculateBreakEven();
}
Calc.prototype.calculateBreakEven = function () {
var coinsBought = this.getAmountSum(this.elements.buySide);
var coinsSold = this.getAmountSum(this.elements.sellSide) || 0;
var btcSpent = this.getBTCSpent(this.elements.buySide);
var btcGained = this.getBTCSpent(this.elements.sellSide) || 0;
var aveBuyPrice = btcSpent / coinsBought;
var aveSellPrice = (btcGained / coinsSold) || 0;
var breakEven = (btcSpent - btcGained) / (coinsBought - coinsSold);
this.populateBreakEven(breakEven.toFixed(8));
this.populateTotalSpent((btcSpent - btcGained).toFixed(8));
}
Calc.prototype.getAmountSum = function (side) {
var amountInputs = side.querySelectorAll('input.amount');
var sum = 0;
for (var i = 0; i < amountInputs.length; i++) {
if (!isNaN(amountInputs[i].value)) {
sum += parseFloat(amountInputs[i].value);
}
}
return sum;
}
Calc.prototype.getBTCSpent = function (side) {
var amountInputs = side.querySelectorAll('input.amount');
var priceInputs = side.querySelectorAll('input.price');
var btcTotal = 0;
for (var i = 0; i < amountInputs.length; i++) {
if (!isNaN(amountInputs[i].value) && !isNaN(priceInputs[i].value)) {
var amountSpent = parseFloat(amountInputs[i].value) * parseFloat(priceInputs[i].value);
amountSpent = amountSpent + (amountSpent * 0.0025); //subtract commission
btcTotal += amountSpent;
}
}
return btcTotal;
}
Calc.prototype.populateBreakEven = function (value) {
this.elements.breakEven.innerHTML = value;
}
Calc.prototype.populateTotalSpent = function (value) {
this.elements.totalInvested.innerHTML = value;
}
Calc.prototype.loadCSVData = function (file, encoding) {
var self = this;
Papa.parse(file, {
header: true,
complete: function (results, file) {
if (results.errors[0] && results.errors[0].code === "UndetectableDelimiter") {
if (encoding === 'UTF8') {
return; //quit trying, already tried UTF8 encoding and it still didn't work
}
//try alternate encoding as I seem to get different file encodings from bittrex at different times
self.loadCSVData(file, 'UTF8');
} else {
self.addCSVData(results.data);
}
},
encoding: encoding || 'Unicode'
});
}
Calc.prototype.addCSVData = function (data) {
var coinsBoughtProp = this.getObjectProperty('Units Filled', data[0]) || this.getObjectProperty('Quantity', data[0]) || this.getObjectProperty('Amount', data[0]);
var costProp = this.getObjectProperty('Actual Rate', data[0]) || this.getObjectProperty('Limit', data[0]) || this.getObjectProperty('Price', data[0]);
var commissionProp = this.getObjectProperty('Commission', data[0]) || this.getObjectProperty('Fee', data[0]);
for (var i = 0; i < data.length; i++) {
if (data[i].Type) {
var isBuy = data[i].Type.toUpperCase().indexOf('BUY') > -1;
this.addCSVRow(data[i][coinsBoughtProp], data[i][costProp], isBuy);
}
}
//caclulate result
this.calculateBreakEven();
}
Calc.prototype.getObjectProperty = function (propStr, obj) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
if (prop.indexOf(propStr) > -1) {
return prop;
}
}
}
}
Calc.prototype.addCSVRow = function (coins, cost, isBuySide) {
var listEl = isBuySide ? this.elements.buySide.querySelector('ul') : this.elements.sellSide.querySelector('ul');
var newRow = this.getNewRow();
newRow.querySelector('input.amount').value = coins;
newRow.querySelector('input.price').value = Math.abs(cost);
listEl.appendChild(newRow);
}
return Calc;
}());