-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimulateTrades.js
More file actions
24 lines (19 loc) · 838 Bytes
/
Copy pathsimulateTrades.js
File metadata and controls
24 lines (19 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const { fetchHistoricalData } = require('./marketData');
const moment = require('moment');
async function simulateTradingFrom(startDate) {
const historicalData = await fetchHistoricalData();
let initialPrice = 0;
let finalPrice = 0;
let profitLoss = 0;
// Filter data from start date
const filteredData = historicalData.filter(data => {
return moment(data.date).isSameOrAfter(moment(startDate));
});
if (filteredData.length > 0) {
initialPrice = filteredData[0].price; // Assuming price is in a 'price' property
finalPrice = filteredData[filteredData.length - 1].price;
profitLoss = ((finalPrice - initialPrice) / initialPrice) * 100; // Percentage profit or loss
}
return { initialPrice, finalPrice, profitLoss };
}
module.exports = { simulateTradingFrom };