-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathalarm.js
More file actions
142 lines (103 loc) · 3.52 KB
/
alarm.js
File metadata and controls
142 lines (103 loc) · 3.52 KB
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
'use strict';
const config = require('./config'),
_ = require('underscore'),
Markets = require('./Services/Markets/Markets'),
BuyService = require('./Services/BuyService'),
SellService = require('./Services/SellService'),
AccountsModel = require('./App/Models/accountModel'),
BalanceModel = require('./App/Models/balanceModel'),
ResourceModel = require('./App/Models/resourceModel'),
MarketModel = require('./App/Models/marketModel'),
AlarmModel = require('./App/Models/alarmModel'),
MarketLogModel = require('./App/Models/marketLogModel'),
StrategyModel = require('./App/Models/strategyModel'),
PriceModel = require('./App/Models/priceModel'),
BotUtils = require("./App/Utils/BotUtils"),
Logger = require('./App/Utils/Logger'),
AlarmChecker = require('./Services/AlarmChecker');
async function init() {
let accounts = await AccountsModel.scope(['active']).findAll({
include: [{
model: BalanceModel,
as: 'balances',
where: {
status: 1
},
include: [{
model: ResourceModel,
where: {
status: 1
},
include: [
{
model: AlarmModel,
as: "alarms",
where: {
status: 1
}
}
],
as: 'resources'
},
{
model: MarketModel,
as: 'market'
}
]
}]
});
//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) {
for (let account of accounts) {
for (let balance of account.balances) {
for (let resource of balance.resources) {
for (let alarm of resource.alarms) {
let alarmChecker = new AlarmChecker(resource,balance, alarm, prices);
let checkResult = alarmChecker.check()
if(checkResult.result && alarmChecker.shouldSendAlarm())
{
console.log(new Date(),"Should Send Message ",true)
var messageCount = alarm.message_count + 1;
Logger.alarm(checkResult,account,alarm,resource)
alarm.update({
last_message_time: new Date().getTime(),
message_count: messageCount
});
}else
{
console.log(new Date(),"Should Send Message ",false)
}
}
}
}
}
}
async function run() {
let run_start = +new Date();
let [accounts, prices] = await init();
router(accounts, prices).then(() => {
let run_completed = +new Date();
let execution_time = run_completed - run_start;
setTimeout(() => {
Logger.info('\n');
try {
run();
} catch (error) {
console.error(error)
}
}, Math.abs(10000 - execution_time));
});
}
run();