-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.js
176 lines (127 loc) · 4.76 KB
/
service.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
170
171
172
173
174
'use strict';
const config = require('./config'),
_ = require('underscore'),
Markets = require('./Services/Markets/Markets'),
TraderObject = require('./Trader'),
AccountsModel = require('./App/Models/accountModel'),
BalanceModel = require('./App/Models/balanceModel'),
ResourceModel = require('./App/Models/resourceModel'),
MarketModel = require('./App/Models/marketModel'),
StrategyModel = require('./App/Models/strategyModel'),
PriceModel = require('./App/Models/priceModel'),
Logger = require('./App/Utils/Logger');
let Trader = new TraderObject();
async function init() {
//Getting active accounts with balances and their resources and buy/sell strategies
let accounts = await AccountsModel.scope(['active']).findAll({
include: [{
model: BalanceModel,
as: 'balances',
where: {
status: 1
},
include: [{
model: ResourceModel,
where: {
status: 1
},
include: [{
model: StrategyModel,
as: 'buyStrategy',
foreignKey: 'buy_strategy_id'
},
{
model: StrategyModel,
as: 'sellStrategy',
foreignKey: 'sell_strategy_id'
}
],
as: 'resources'
},
{
model: MarketModel,
as: 'market'
}
]
}]
});
//TODO: divide prices by market_id
//Getting price history, order by timestamp
let prices = await PriceModel.findAll({
limit: 3000,
order: [
['created_at', 'DESC']
]
});
//Get Plain Objects into prices
prices = prices.map((price) => price.get({
plain: true
})).reverse();
return [accounts, prices];
}
async function router(accounts, prices) {
// We will check all active accounts
for (let account of accounts) {
Logger.bot(account,Trader);
//Balances associated with account
for (let balance of account.balances) {
// select correct market for balance
let market = Trader.selectMarket(balance);
market.transaction_fee = balance.market.transaction_fee;
// init market from balance market information
market.class.init(balance.hashed_username, balance.hashed_special_key, balance.hashed_secret_key);
// get last prices
//TODO: cache last prices for 10 second
//TODO: make sure about multiple currency
let lastPrices = await market.class.lastPrices(balance.symbol);
Logger.info('Last Ask Price: ' + lastPrices.ask);
Logger.info('Last Bid Price: ' + lastPrices.bid);
Logger.info('\n');
let balanceRelatedPrices = _.where(prices, {symbol: balance.symbol});
//Resources associated with balances
for (let resource of balance.resources) {
switch (resource.final_state) {
case 'buy':
await Trader.buy(account, market, balance.symbol, resource, balanceRelatedPrices, lastPrices.ask,false);
break;
case 'sell':
await Trader.sell(account, market, balance.symbol, resource, balanceRelatedPrices, lastPrices.bid,false);
break;
case 'close':
//do nothing
break;
default:
}
//process.exit(0);
Logger.info(' \n');
}
Logger.info(' \n');
}
}
}
function stop() {
}
async function run() {
//run start date for calculating execution time
let run_start = +new Date();
//Firstly we will get accounts balances, resources and prices data
let [accounts, prices] = await init();
//Then we'll pass all data to Router method. Router method redirects resources to relevant function.
//If routing completed run again
router(accounts, prices).then(() => {
//run stop date
let run_completed = +new Date();
//We will execute run again after 10 second including this one's execution time
let execution_time = run_completed - run_start;
Logger.info('This run took ' + execution_time + ' miliseconds, next one will start after ' + (10000 - execution_time) + ' miliseconds');
setTimeout(() => {
Logger.info('\n');
try {
run();
} catch (error) {
console.error(error)
}
}, Math.abs(10000 - execution_time));
});
}
run();