File size: 1,820 Bytes
683d9cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const cheerio = require('cheerio');
const fs = require('fs');

const parseMT5Report = (filePath) => {
  const html = fs.readFileSync(filePath, 'utf8');
  const $ = cheerio.load(html);
  const deals = [];

  // 1. Find the header "Deals" to locate the correct section
  // The user specified: <th colspan="13" ...>Deals</th>
  const dealsHeader = $('th:contains("Deals")');
  
  if (dealsHeader.length > 0) {
    // Find the parent table of this header
    const table = dealsHeader.closest('table');
    
    // Iterate through rows (skipping the header rows)
    // We look for rows that contain numeric data or standard deal attributes
    table.find('tr').each((i, row) => {
      const cols = $(row).find('td');
      
      // Basic validation to ensure it's a data row (13 columns based on prompt)
      if (cols.length === 13) {
        const rowData = {
          time: $(cols[0]).text().trim(),
          deal: $(cols[1]).text().trim(),
          symbol: $(cols[2]).text().trim(),
          type: $(cols[3]).text().trim(),
          direction: $(cols[4]).text().trim(),
          volume: parseFloat($(cols[5]).text().trim()),
          price: parseFloat($(cols[6]).text().trim()),
          order: $(cols[7]).text().trim(),
          commission: parseFloat($(cols[8]).text().trim()),
          swap: parseFloat($(cols[9]).text().trim()),
          profit: parseFloat($(cols[10]).text().trim()),
          balance: parseFloat($(cols[11]).text().trim()),
          comment: $(cols[12]).text().trim(),
        };

        // Filter out header row repetitions if any exist by checking if 'balance' is a number
        if (!isNaN(rowData.balance)) {
          deals.push(rowData);
        }
      }
    });
  }
  return deals;
};

module.exports = { parseMT5Report };