-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtrade.js
169 lines (154 loc) · 5.66 KB
/
trade.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
const config = require('./config');
const bm = require('./bitmex').BitMEX;
const db = require('./db').Db;
const orderlog = require('./orderlog').OrderLog;
const Trade = function Trade() {};
function determineOrderAttributes(price, atr, orderType, balance) {
// I wrote this portion when I was high, I'm not sure how it works, but it works
// const toXBT = balance / 100000000;
// const dollarValue = price * toXBT;
// const bet = Math.round(dollarValue * config.margin * config.betSize);
const balxbt = balance / 100000000;
const bal = balxbt * price;
const atrk = config.atrmultiplier * parseFloat(atr);
let stopLossPosition = 1;
let absLoss = -1;
let lossVal = 1;
const priceXBT = 1 / parseFloat(price);
let stopXBT = 1;
let diff = 1;
let allocation = 1;
if (orderType === 'LONG') {
stopLossPosition = Math.round(parseFloat(price) - atrk);
absLoss = absLoss * bal * config.maxLoss;
stopXBT = 1 / stopLossPosition;
diff = priceXBT - stopXBT;
lossVal = diff * stopLossPosition;
allocation = Math.round(absLoss / lossVal);
const maxAlloc = Math.round(config.margin * config.maxBetSize * bal);
allocation = allocation > maxAlloc ? maxAlloc : allocation;
} else {
stopLossPosition = Math.round(parseFloat(price) + atrk);
absLoss = absLoss * bal * config.maxLoss;
stopXBT = 1 / stopLossPosition;
diff = stopXBT - priceXBT;
lossVal = diff * stopLossPosition;
allocation = Math.round(absLoss / lossVal);
const maxAlloc = Math.round(config.margin * config.maxBetSize * bal);
allocation = allocation > maxAlloc ? maxAlloc : allocation;
}
return [stopLossPosition, allocation];
}
Trade.prototype.init = function init() {
bm.adjustMargin(config.margin);
orderlog.init();
};
Trade.prototype.openPosition = function openPosition(orderType, currentPrice, avgTrueRange) {
return new Promise((resolve, reject) => {
const side = orderType === 'LONG' ? 'Buy' : 'Sell';
bm
.getBalance()
.then((balance) => {
// console.log(`balance: ${balance}`);
const [stopLossPosition, orderSize] = determineOrderAttributes(
currentPrice,
avgTrueRange,
orderType,
balance,
);
// console.log(`stop loss: ${stopLossPosition} order size: ${orderSize}`);
bm
.marketOrder(side, orderSize)
.then((response) => {
// Set stop Loss on service provider
const { orderID } = response.body;
// console.log('response recieved');
// console.log(response.body);
this.setStopLoss(orderType, orderID, orderSize, stopLossPosition)
.then(() => {
// record the order id return from service provider
// console.log('stop loss ---');
db
.setOrderID(orderID)
.then(() => {
// record the positionsize
// console.log('order id recorded in db');
db
.setOrderSize(orderSize)
.then(() => {
// LOG the Open position
// console.log('order size recorded in db');
orderlog.open(Date.now(), 'OPEN', orderType, currentPrice, orderSize);
})
.then(resolve(orderID))
.catch(reply => reject(reply));
})
.catch(reply => reject(reply));
})
.catch(reply => reject(reply));
})
.catch(reply => reject(reply));
})
.catch(reply => reject(reply));
});
};
Trade.prototype.closePosition = function closePosition(orderID, close) {
return new Promise((resolve, reject) => {
bm
.closePosition(orderID)
.then(bm.deleteUOrder(orderID).catch(reply => reject(reply)))
.then(orderlog.close(Date.now(), 'CLOSE', '-', close))
.then(reply => resolve(reply))
.catch(reply => reject(reply));
});
};
// FIXME this function was changed
Trade.prototype.setStopLoss = function setStopLoss(side, orderID, orderQty, stopPrice) {
// stop Price should be a rounded integer for BitMEX
return new Promise((resolve, reject) => {
bm
.setUStopLoss(side, stopPrice, orderQty, orderID)
.then(() => {
// console.log('stop loss assigned, recording in db');
// record stop loss price in db
db
.setStopLoss(stopPrice)
.then(reply => resolve(reply))
.catch(reply => reject(reply));
})
.catch(reply => reject(reply));
});
};
// FIXME this function was changed
Trade.prototype.amendStoploss = function amendStoploss(newPrice) {
return new Promise((resolve, reject) => {
db
.getOrderID() // Get the order from DB
// eslint-disable-next-line
.then(orderID => {
// if an order id is available
if (orderID !== '' || orderID !== null || orderID !== undefined) {
db
.getOrderSize() // get the order size
.then((orderSize) => {
bm
.amendUStopLoss(orderID, orderSize, newPrice) // amend stop loss
.then(() => {
// record change in the db
db
.setStopLoss(newPrice)
.then(reply => resolve(reply))
.catch(reply => reject(reply));
})
.catch(reply => reject(reply));
})
.catch(reply => reject(reply));
} else {
// invalid id reply callback error function
reject();
}
})
.catch(reply => reject(reply));
});
};
exports.Trade = new Trade();