-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
132 lines (111 loc) · 3.02 KB
/
app.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
// /* Dependencies */
const Sheet = require('./lib/sheet');
const Trader = require('./lib/trader');
const Boot = require('./lib/boot');
const Order = require('./lib/order');
const Logger = require('./lib/logger');
const Bitfinex = require('./lib/bitfinex');
const bitOptions = {
rest_key: process.env.BIT_REST_KEY,
rest_secret: process.env.BIT_REST_SECRET,
socket_key: process.env.BIT_WS_KEY,
socket_secret: process.env.BIT_WS_SECRET,
no_trade_time: process.env.NO_TRADE_TIME
};
/**
* Instanciate helpers
*/
let trader;
const boot = new Boot();
const sheet = new Sheet();
const logger = new Logger(trader, sheet);
const bitfinex = new Bitfinex(bitOptions, gotTrade, gotOrderUpdate);
/**
* Main datastructure recording state
*/
let data = {
positions: [],
activeSell: null
};
let makingOrder = false;
boot.init(bitfinex, function (accountData, feesData)
{
data = accountData;
trader = new Trader(feesData, placeOrder);
startLogging();
});
function gotTrade(trade)
{
if (trader) trader.gotTrade(trade, data);
}
function gotOrderUpdate(order)
{
// Ignore fee orders
if (order.status.indexOf('@') > -1) return;
bitfinex.getActivePositions(function ()
{
if (order.status.indexOf('CANCELED') > -1)
{
data.activeSell = null;
}
logger.orderUpdate(order);
});
}
function placeOrder(order)
{
if (makingOrder || !order) return;
makingOrder = true;
bitfinex.placeOrder(order, function (err, res)
{
makingOrder = false;
if (err || !res)
{
console.log('Could not perform trade or update');
console.log(err);
return;
}
const newOrder = new Order.FromRestA(res);
// Record to active order
if (newOrder.type === 'sell') data.activeSell = newOrder;
});
}
/**
* Log in drive what the trader is thinking
* for debugging purposes
*/
function startLogging()
{
function logStatus()
{
let logData = Object.assign(
{
timestamp: Date.now(),
average: trader.currentAverage
}, data, trader.activeData);
const positions = data.positions;
if (positions && positions.length)
{
logData.boughtAt = positions[0].price;
}
sheet.recordTraderData(logData).catch(console.log);
}
// Current operation status check every second
setInterval(function ()
{
if (trader.activeData.sellPrice) logStatus();
}, 1000);
// Main status check every minute
setInterval(function ()
{
const busySelling = trader.activeData.sellPrice;
const avgTrades = trader.average.tradesInAverage();
if (!busySelling && avgTrades > 3) logStatus();
}, 60000);
// Low activity status check every 5 minutes
setInterval(function ()
{
const busySelling = trader.activeData.sellPrice;
const avgTrades = trader.average.tradesInAverage();
if (!busySelling && avgTrades <= 3) logStatus();
}, 300000);
}