-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrebalance.js
100 lines (83 loc) · 3.17 KB
/
rebalance.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
// This is a Director that accepts new candlesticks
// and has a strategy of rebalancing between the two pairs
// with a fixed percentage.
// Rebalance at this interval of number of candlesticks
var MAKER_FEE = 0.0025 // 0.25 percent
var TAKER_FEE = 0.0015 // 0.15 percent
var MIN_AMT = 0.001
var Rebalance = function(frequency_minutes, _inBalance, _outBalance) {
this.inBalance = _inBalance
this.outBalance = _outBalance
this.total = 0
this.originalTotal = 0
this.originalPrice = 0
this.originalInBalance = _inBalance
this.originalOutBalance = _outBalance
var defaultCallback = function(price, amount) {
console.log(`Rebalance DEFAULT buy/sell amt ${amount} @ price ${price}`);
};
this.buyCallback = defaultCallback;
this.sellCallback = defaultCallback;
this.onBuy = function(callback) {
this.buyCallback = callback;
};
this.onSell = function(callback) {
this.sellCallback = callback;
};
var phase = 0;
this.setOutBalance = function(newOutBalance) {
console.log("Setting outBalance: " + newOutBalance);
this.outBalance = Number(newOutBalance)
}
this.setInBalance = function(newInBalance) {
console.log("Setting inBalance: " + newInBalance);
this.inBalance = Number(newInBalance)
}
this.newCandlestick = (candlestick) => {
var price = candlestick.getWeightedAvg();
console.log("Phase " + phase);
console.log("Price " + price);
console.log("In Value " + this.inBalance * price);
console.log("Out Value " + this.outBalance);
this.total = (this.inBalance*price) + this.outBalance;
console.log("Total Value " + this.total);
console.log("IN Balance % Change " + (this.inBalance / this.originalInBalance));
console.log("OUT Balance % Change " + (this.outBalance / this.originalOutBalance));
console.log("Rebalance Profit (out) " + (this.total - this.originalTotal));
console.log("Holding Profit " + (this.originalInBalance*(price - this.originalPrice)));
if (phase % frequency_minutes != 0) {
phase += 1;
return;
}
phase += 1;
var diff = (this.inBalance*price) - this.outBalance;
var half = diff / 2.0;
var inAmt = Math.abs(half / price);
if (inAmt < MIN_AMT) {
console.log(`Amount ${inAmt} smaller than Poloniex minimum ${MIN_AMT}`)
return;
}
if (diff > 0) {
// We have too much IN, sell half the difference to get it OUT
this.sellCallback(price, inAmt)
this.inBalance -= inAmt;
this.outBalance += half;
console.log("Sold inAmt " + inAmt);
} else {
// We have too much OUT, buy half the difference to get it IN
this.buyCallback(price, inAmt)
this.inBalance += inAmt;
this.outBalance += half; // half is negative, so this is the right sign
console.log("Bought inAmt " + inAmt);
}
if (this.originalTotal == 0) {
console.log("FIRST REBALANCE");
this.originalTotal = this.total;
this.originalInBalance = this.inBalance;
this.originalOutBalance = this.outBalance;
console.log(`Original Total ${this.originalTotal}`);
}
if (this.originalPrice == 0) { this.originalPrice = price; }
};
};
module.exports = Rebalance;