forked from symdesign/mintos-trading-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrade.js
More file actions
executable file
·362 lines (260 loc) · 14.2 KB
/
trade.js
File metadata and controls
executable file
·362 lines (260 loc) · 14.2 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
const settings = require('./settings');
const strategy = settings.strategy ? require( settings.strategy ) : require('./sample-strategy');
const init = require('./init');
const addToLog = require('./log-trades');
const refreshMarketplace = require('./refresh-marketplace');
(async () => {
//
// INIT
//
const i = await init();
const page = i.page;
const browser = i.browser;
//
// TRADE
//
let accountBalance = await page.evaluate( async () => {
let balance = document.getElementById('sel-header-balance').innerText.replace( /\(\s€\s((\d+\s)?\d+\.\d{2})\s\)/, '$1').replace(' ','');
return parseFloat( balance );
});
console.log(`Account Balance: € ${accountBalance.toFixed(2)}`);
// Backwards compatibility
if (strategy.invest.cashQuota) {
console.log(`Cash Quota: € ${strategy.invest.cashQuota.toFixed(2)}`);
accountBalance = accountBalance - strategy.invest.cashQuota;
}
// Backwards compatibility
let runtimeMax = strategy.invest.totalMax ? strategy.invest.totalMax : strategy.invest.runtimeMax;
if ( accountBalance < strategy.invest.min ) {
console.log('Balance is too low.\n')
await page.waitFor(500) // 1000ms = 1s
try {
await refreshMarketplace( page );
} catch(e) {
console.error(e)
}
await page.waitFor(500) // 1000ms = 1s
await browser.close();
} else {
//
// SECONDARY MARKET
//
await page.waitFor(10000);
let hasBasketItemsAlready = await page.evaluate( () => parseInt( document.querySelector('#basket-total').innerText ) > 0 )
let isBasketOpen = await page.evaluate( () => document.querySelector('#investor-basket-wrapper.active') ? true : false );
if ( hasBasketItemsAlready ) {
if ( !isBasketOpen ) {
let investorBasket = await page.$('#investor-basket-wrapper');
await investorBasket.click();
}
let hasPurchases = await page.evaluate(() => document.querySelector('#investor-purchases .basket-empty') ? false : true );
if ( hasPurchases ) {
let emtyInvestmentsCartButton = await page.$('#empty-investments-cart-button');
await emtyInvestmentsCartButton.click();
await page.waitFor(500);
}
let hasSales = await page.evaluate(() => document.querySelector('#investor-sales .basket-empty') ? false : true );
if ( hasSales ) {
let emptySalesCartButton = await page.$('#empty-sales-cart-button');
await emptySalesCartButton.click();
await page.waitFor(500);
}
}
var currentBalance = accountBalance;
var investTotal = 0.0;
let loans = [];
let iteration = 0;
let maxIterations = strategy.filters.length;
for await ( let link of strategy.filters ) {
iteration++;
let isLastIteration = maxIterations > iteration ? false : true;
if ( currentBalance < strategy.invest.min || investTotal >= runtimeMax ) break;
// Set filter and invest
console.log('');
console.log('Opening link...');
console.log(`${link}`);
console.log('');
await page.goto( link );
await page.waitFor(2000);
try {
await page.waitForSelector('#filter-results-wrapper.appear');
} catch(e) {
let pleaseWait = await page.evaluate( () => document.querySelector('#filter-results-wrapper').innerText == 'Please wait' ? true : false );
if ( pleaseWait ) console.log('Please wait...')
if ( pleaseWait ) await page.waitForSelector('#filter-results-wrapper.appear', {
timeout: 90000 // Let's wait a little longer than default 30 sec.
});
}
if ( runtimeMax && investTotal > runtimeMax ) break;
// Has results?
let hasResults = await page.evaluate( () => document.querySelector('table#secondary-market-table') ? true : false );
console.log(`${hasResults ? '' : 'No results :('} \n`)
if ( !hasResults && !isLastIteration ) continue;
// Buy
let loanEntries = await page.$$('.m-loan-entry');
let n = 0; // nth-child of table
for await (let loanEntry of loanEntries) {
n++;
let loan = await page.evaluate( loanEntry => {
let termRegex = /((\d{1,2}\s[a-z]\.\s?){1,2})\n\((\d)\)/;
let discountRegex = /\(([-|+]?\d*\.\d{1,2}%)\)/;
let trimEuroRegex = /€\s(\d{1,3}\.\d{2})/;
let trimPercentRegex = /([-|+]?\d*\.\d{1,2})%/;
return {
id: loanEntry.querySelector('.m-loan-id').innerText,
type: loanEntry.querySelector('.m-loan-type').innerText,
originator: loanEntry.querySelector('.m-loan-originator').innerText,
bapr: parseFloat( loanEntry.querySelector('[data-m-label="Borrower APR"]').innerText.replace(trimPercentRegex,'$1') ),
interest: parseFloat( loanEntry.querySelector('[data-m-label="Interest Rate"]').innerText.replace(trimPercentRegex,'$1') ),
term: loanEntry.querySelector('[data-m-label="Term"]').innerText.replace(termRegex, '$1'),
repayments: loanEntry.querySelector('[data-m-label="Term"]').innerText.replace(termRegex, '$3'),
status: loanEntry.querySelector('[data-m-label="Status"]').innerText,
ytm: parseFloat( loanEntry.querySelector('[data-m-label="YTM"]').innerText.replace(trimPercentRegex,'$1') ),
myInvestment: loanEntry.querySelector('.my-investment') ? parseFloat( loanEntry.querySelector('.my-investment').dataset.tooltip.replace( /Your investment:\s€\s((\d+\s)?\d+\.\d{2})/, '$1' ) ) : 0.00,
value: parseFloat( loanEntry.querySelector('[data-m-label="Available for Investment"]').innerText.replace(trimEuroRegex,'$1') ),
price: parseFloat( loanEntry.querySelector('[data-m-label="Price / Discount"]').innerText.replace(trimEuroRegex,'$1') ),
discount_rel: parseFloat( loanEntry.querySelector('.m-loan-premium-change').innerText.replace(discountRegex, '$1').replace(trimPercentRegex,'$1') ),
}
}, loanEntry )
console.log('\nLoan:');
console.log(loan);
console.log('');
let isAlreadyInBasket = await page.evaluate( loanEntry => loanEntry.querySelector('.already-in-basket') ? true : false, loanEntry );
console.log(`Current Balance? ${currentBalance > strategy.invest.min ? '✓ ok' : '✗'} ` );
if ( strategy.invest.min && currentBalance < strategy.invest.min ) break;
console.log(`Runtime Maximum? ${investTotal < runtimeMax ? '✓ ok' : '✗'} ` );
if ( runtimeMax && investTotal > runtimeMax ) break;
console.log(`Investment amount? ${loan.price > strategy.invest.min || loan.myInvestment < strategy.invest.max ? '✓ ok' : '✗'}`)
if ( loan.price <= strategy.invest.min
|| loan.myInvestment >= strategy.invest.max
|| isAlreadyInBasket
) continue;
let netMax = strategy.invest.max - loan.myInvestment;
let investAmount = loan.price > netMax ? ( netMax > currentBalance ? currentBalance : netMax ) : loan.price > currentBalance ? currentBalance : loan.price;
investAmount = parseFloat(investAmount);
investAmount = investAmount.toFixed(2);
if ( investAmount <= 0 ) continue;
try {
let addToCartWrapper = await page.$(`.m-loan-entry:nth-child(${n}) .add-to-cart-wrapper`);
await addToCartWrapper.click();
await page.waitFor(500);
let inputAmountValue = await page.$(`.m-loan-entry:nth-child(${n}) .amount-value`);
await inputAmountValue.type(`${investAmount}`);
await page.waitFor(500);
let inputSubmit = await page.$(`.m-loan-entry:nth-child(${n}) .trigger-submit`);
await inputSubmit.click();
await page.waitFor(500);
let amountError = await page.evaluate( () => document.querySelector('#black-error-box') ? true : false );
if ( amountError ) {
investAmount = investAmount - 0.1;
await inputAmountValue.click({ clickCount: 3 })
await inputAmountValue.type(`${investAmount}`); // bugfix: '-0.02' as amounts are sometimes not rounded perfectly by mintos which means the invested amount will exceed the loan size (even if it does not)
await page.waitFor(500);
await inputSubmit.click();
await page.waitFor(500);
}
currentBalance = currentBalance - investAmount;
investTotal = parseFloat(investTotal);
investAmount = parseFloat(investAmount);
investTotal = investTotal + investAmount;
investTotal = investTotal.toFixed(2);
loan.myInvestment = loan.myInvestment + investAmount;
loan.value = investAmount / ( 1 + loan.discount_rel / 100 ); // EUR
loan.price = investAmount; // EUR
loan.discount_abs = (loan.price - loan.value).toFixed(4); // EUR
loan.discount_rel = loan.discount_rel; // %
loans.push( loan );
console.log(`\n€ ${investAmount} of loan added to cart.`)
console.log('Current investment total: ' + investTotal)
} catch(error) {
console.log(`An error occured. Loan not added to cart.`)
continue;
}
console.log('');
}
}
if (strategy.invest.cashQuota) {
// Adding back what we have removed in the beginning
currentBalance = currentBalance + strategy.invest.cashQuota;
}
console.log('');
console.log( `Number of investments: ${loans.length}` )
console.log( `New Balance: € ${currentBalance.toFixed(2)}\n` )
if ( investTotal == 0 ) {
await page.waitFor(500) // 1000ms = 1s
try {
await refreshMarketplace( page );
} catch(e) {
console.error(e)
}
await browser.close();
return;
}
// Trigger Review and Approve
let basketWrapper = await page.$('#investor-basket-wrapper');
let isBasketWrapperActive = await page.evaluate( () => document.querySelector('#investor-basket-wrapper.active') ? true : false );
if ( !isBasketWrapperActive ) await basketWrapper.click();
await page.waitFor(500);
const reviewAndApprovePurchaseButton = await page.$('#investor-purchases .btn-primary')
// await page.evaluate( () => document.querySelector('#investor-basket-wrapper').classList.add('active'));
await reviewAndApprovePurchaseButton.click();
await page.waitForNavigation();
await page.waitFor(500);
let currentURL = await page.url();
// Approve Investment
const investmentConfirmButton = await page.$('#investment-confirm-button');
await investmentConfirmButton.click();
await page.waitForNavigation();
// Check if navigation failed which indicates that there was an error
let newURL = await page.url();
if ( newURL == currentURL ) {
let loanEntries = await page.$$('.m-loan-entry');
let n = 0; // nth-child of table
for await (let loanEntry of loanEntries) {
n++;
let hasError = await page.evaluate( loanEntry => loanEntry.querySelector('.error-message') ? true : false, loanEntry );
if ( hasError ) {
try {
let removeInvestmentButton = await page.$(`.m-loan-entry:nth-child(${n}) .remove-investment`);
await removeInvestmentButton.click();
await page.waitFor(200);
} catch(e) {
console.log(e)
}
}
}
await waitFor(1000000);
const newInvestmentConfirmButton = await page.$('#investment-confirm-button');
await newInvestmentConfirmButton.click();
await page.waitForNavigation();
}
//
// LOG TRADE
//
console.log('Adding new trades to log file...')
const trades = [];
for await ( let loan of loans ) {
let today = new Date();
let dd = String(today.getDate()).padStart(2, '0');
let mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
let yyyy = today.getFullYear();
trades.push({
date: `${dd}.${mm}.${yyyy}`,
id: loan.id,
value: loan.value,
price: loan.price,
profit_abs: loan.discount_abs * -1,
profit_rel: loan.discount_rel * -1,
});
}
console.log('All trades added.')
await addToLog( trades );
await page.waitFor(4000);
try {
await refreshMarketplace( page );
} catch(e) {
console.error(e)
}
await browser.close();
}
})();