-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximize-profit.js
More file actions
50 lines (45 loc) · 1.75 KB
/
Copy pathmaximize-profit.js
File metadata and controls
50 lines (45 loc) · 1.75 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
import pricesJson from './price.json' assert {type: "json"}
const prices = pricesJson.prices;
prices.sort((x,y) => parseFloat(x.Price) - parseFloat(y.Price));
let bestTrade = {
"BuyDate": new Date(),
"BuyPrice": 0.0,
"BuyIndex": prices.length - 1,
"SellDate": new Date(),
"SellPrice": 0.0,
"Profit": 0.0,
"Rate": 0.0
};
for(let i=prices.length-1; i>-1; i--)
for(let j=0;
j < i && j < bestTrade.BuyIndex; // profit must be smaller for any greater j
j++) {
const trade = {
"BuyDate": new Date(prices[j].Date),
"BuyPrice": prices[j].Price,
"BuyIndex": j,
"SellDate": new Date(prices[i].Date),
"SellPrice": prices[i].Price,
"Profit": prices[i].Price - prices[j].Price,
"Rate": +(100 * (prices[i].Price - prices[j].Price)/prices[j].Price).toFixed(0)
};
if(trade.BuyDate < trade.SellDate)
if(trade.Profit > bestTrade.Profit){
bestTrade = trade;
break; // profit must be smaller for any greater j
}
};
const currencyFormat = { style: "currency", "currency":"USD" };
const dateFormat = { dateStyle: "full", timeZone: 'America/New_York' };
const format = {
"BuyDate": new Intl.DateTimeFormat ("en-US", dateFormat).format (bestTrade.BuyDate),
"BuyPrice": new Intl.NumberFormat ("en-US", currencyFormat).format (bestTrade.BuyPrice),
"SellDate": new Intl.DateTimeFormat ("en-US", dateFormat).format (bestTrade.SellDate),
"SellPrice": new Intl.NumberFormat ("en-US", currencyFormat).format (bestTrade.SellPrice),
"Profit": new Intl.NumberFormat ("en-US", currencyFormat).format (bestTrade.Profit),
"Rate": new Intl.NumberFormat ().format (bestTrade.Rate)
};
console.log(`Buy ${format.BuyDate} for ${format.BuyPrice}`);
console.log(`Sell ${format.SellDate} for ${format.SellPrice}`);
console.log(`Profit ${format.Profit}`);
console.log(`Return ${format.Rate}%`);