-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbotHandler.js
More file actions
181 lines (150 loc) · 6.53 KB
/
botHandler.js
File metadata and controls
181 lines (150 loc) · 6.53 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
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
175
176
177
178
179
180
181
const _ = require('lodash');
const { updateAllDeals, getLastDealsInTimePeriod, updateBot, getAllActiveDeals } = require('./services/apiService');
const { subMinutes, parseISO } = require('date-fns');
const ACCOUNT_ID_TOM = process.env.THREE_COMMAS_ACCOUNT_ID;
const ACCOUNT_ID_CHRIS = process.env.THREE_COMMAS_ACCOUNT_ID_CHRIS;
const SHOULD_RUN_BOTS = true;
const paramsToEnableSuperBot = {
timePeriod: 15,
amountOfStartedAndClosedDeals: 2,
baseOrder: 60,
safetyOrder: 20,
maxAmountOfSuperBots: 1,
}
const paramsToDisableSuperBot = {
timePeriod: 60,
baseOrder: 20,
safetyOrder: 10,
}
module.exports.handleBots = async () => {
console.log("STARTING update of all bots");
if (!SHOULD_RUN_BOTS) {
return {
statusCode: 200,
body: JSON.stringify({ message: 'NOT UPDATING BOTS', success: true }),
};
}
const filters = [];
filters.push((deal) => deal.completed_safety_orders_count >= 3);
filters.push({trailing_enabled: false});
filters.push((deal) => !deal.bot_name.includes("(SKIP)"));
const deals = await updateAllDeals(ACCOUNT_ID_TOM, filters, {
trailing_enabled: true,
take_profit: 1.9,
trailing_deviation: 0.4
});
console.log("FINISHED update of all bots");
return {
statusCode: 200,
body: JSON.stringify({ message: 'Updated all bots, see logs', success: true, deals }),
};
};
module.exports.toggleSuperBots = async () => {
console.log("STARTING review of bots to enable a superbot");
const superBotDeals = await getAllActiveDeals(ACCOUNT_ID_TOM, [(deal) => {
return deal.bot_name.includes('SUPERBOT')
}]);
if (!SHOULD_RUN_BOTS) {
return {
statusCode: 200,
body: JSON.stringify({ message: 'NOT UPDATING BOTS', success: true }),
};
}
// if we still want to enable super bots
if (superBotDeals.length < paramsToEnableSuperBot.maxAmountOfSuperBots) {
const lastDeals = await getLastDealsInTimePeriod(ACCOUNT_ID_TOM, subMinutes(new Date(), paramsToEnableSuperBot.timePeriod));
const groups = _.map(_.groupBy(lastDeals, 'pair'), (item,index) => {
return {
count: item.length,
botId: item[0].bot_id,
name: item[0].bot_name,
pairs: [item[0].pair],
take_profit: item[0].take_profit,
martingale_step_coefficient: item[0].martingale_step_coefficient,
martingale_volume_coefficient: item[0].martingale_volume_coefficient,
max_safety_orders: item[0].max_safety_orders,
active_safety_orders_count: item[0].active_safety_orders_count,
safety_order_step_percentage: item[0].safety_order_step_percentage,
take_profit_type: item[0].take_profit_type,
start_order_type: 'market',
strategy_list: [{"strategy":"nonstop"}],
}
});
const superBotToEnable = _.first(groups, pair => pair.count >= paramsToEnableSuperBot.amountOfStartedAndClosedDeals);
if (superBotToEnable) {
console.log(`Upgrading bot to SUPERBOT: ${superBotToEnable.name}(${superBotToEnable.botId}) count: ${superBotToEnable.count}`);
if (! superBotToEnable.name.includes("SUPERBOT")) {
const params = {
base_order_volume: paramsToEnableSuperBot.baseOrder,
safety_order_volume: paramsToEnableSuperBot.safetyOrder,
...superBotToEnable,
name: superBotToEnable.name + ` - SUPERBOT`
};
delete params.count;
const succes = await updateBot(superBotToEnable.botId, params);
}
}
return {
statusCode: 200,
body: JSON.stringify({ message: 'Updated all bots, see logs', success: true, superBotDeals, lastDeals, groups }),
};
}
const botToDisable = _.first(superBotDeals, deal => parseISO(deal.created_at) < subMinutes(new Date(), paramsToDisableSuperBot.timePeriod));
if (botToDisable) {
console.log(`Reducing bot ${deal.bot_name} to normal params`);
const params = {
base_order_volume: paramsToDisableSuperBot.baseOrder,
safety_order_volume: paramsToDisableSuperBot.safetyOrder,
name: botToDisable.bot_name.replace(/ - SUPERBOT/, ''),
pairs: [botToDisable.pair],
take_profit: botToDisable.take_profit,
martingale_step_coefficient: botToDisable.martingale_step_coefficient,
martingale_volume_coefficient: botToDisable.martingale_volume_coefficient,
max_safety_orders: botToDisable.max_safety_orders,
active_safety_orders_count: botToDisable.active_safety_orders_count,
safety_order_step_percentage: botToDisable.safety_order_step_percentage,
take_profit_type: botToDisable.take_profit_type,
start_order_type: 'limit',
strategy_list: [{"strategy":"nonstop"}],
};
await updateBot(botToDisable.bot_id, params);
}
console.log("FINISHED review of bots to enable a superbot");
return {
statusCode: 200,
body: JSON.stringify({ message: 'Updated all bots, see logs', success: true, superBotDeals, botToDisable }),
};
};
module.exports.handleChrisBots = async () => {
console.log("STARTING update of all Chris bots");
const filters = [];
filters.push((deal) => deal.completed_safety_orders_count >= 3);
filters.push({trailing_enabled: false});
filters.push((deal) => !deal.bot_name.includes("(SKIP)"));
const deals = await updateAllDeals(ACCOUNT_ID_CHRIS, filters, {
trailing_enabled: true,
take_profit: 2,
trailing_deviation: 0.5
});
console.log("FINISHED update of all Chris bots");
return {
statusCode: 200,
body: JSON.stringify({ message: 'Updated all bots, see logs', success: true, deals }),
};
};
module.exports.updateAllDeals = async () => {
const filters = [];
filters.push((deal) => deal.completed_safety_orders_count >= 1);
filters.push({trailing_enabled: true});
filters.push((deal) => !deal.bot_name.includes("(SKIP)"));
const deals = await updateAllDeals(ACCOUNT_ID_TOM, filters, {
trailing_enabled: false,
take_profit: 1.5,
trailing_deviation: 0.5,
profit_currency: 'quote_currency'
});
return {
statusCode: 200,
body: JSON.stringify({ message: 'Updated all bots, see logs', success: true, deals }),
};
};