-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
128 lines (89 loc) · 2.8 KB
/
index.ts
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
/* * * * * * * * * function made to fetch API by url * * * * * * */
export async function http(
request: RequestInfo
): Promise<any> {
const response = await fetch(request);
const body = await response.json();
return body;
}
/* * * * * * * * * * * * Date Protoype extend * * * * * * * * * * * */
declare global {
interface Date {
addDays(days: number): Date;
getDashedDate(): string;
}
}
Date.prototype.addDays = function(days: number): Date {
let date = this;
date.setDate(date.getDate() + days);
return date;
}
/* this prototype only returns the date in a YYYY-MM-DD format */
Date.prototype.getDashedDate = function(): string {
const month: number = this.getMonth() + 1;
const year: number = this.getFullYear();
const day: number = this.getDate();
return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`
}
/* * * * * * * */
/* * * * * * * * * * * * STOCK CLASS * * * * * * * * * * * */
class Stock {
date: Date;
price: number;
constructor(date:string) {
this.date = new Date(date);
this.price = 0;
}
printInfo() {
console.log(`📈 ${this.date.getDashedDate()} -> $${this.price}`);
}
async setPrice() {
/* API CREDENTIALS */
const url = 'https://eodhistoricaldata.com/api/eod/MCD.US';
const api_token = 'OeAFFmMliFG5orCUuwAKQ8l4WWFQ67YX';
/* Read from API to get the stock price on that day */
const data = await http(
`${url}?api_token=${api_token}&from=${this.date.getDashedDate()}&to=${this.date.getDashedDate()}&fmt=json`
);
/* return the price only */
this.price = data.length > 0 ? Number(data[0].close) : 0;
}
}
/* * * * * * * * * * * * PORTFOLIO CLASS * * * * * * * * * * * */
class Portfolio {
stocks: Array<Stock>;
constructor(){
this.stocks = []
}
async profit(d1: string, d2: string){
console.log('\nRANGE:', d1, ' - ', d2, '\n')
console.log('🕑 Fetching data from API...\n')
let count = 0;
try{
const date1 = new Date(`${d1}`);
const date2 = new Date(`${d2}`);
/* Making sure that day1 is less than day2 */
while(date1 <= date2) {
const temp = new Stock(date1.getDashedDate());
await temp.setPrice();
// since the API that I am using doesnt have all the days, I am making sure that they return a price value
if(temp.price > 0) {
this.stocks.push(temp);
this.stocks[count].printInfo();
count ++
}
/* Go to next day */
date1.addDays(1);
}
console.log('\n', count, ' results found...\n')
}catch(err)
{
console.error(err);
}
}
}
/* * * * * * * * * * * * PROGRAM STARTS HERE * * * * * * * * * * * */
const p = new Portfolio();
/* TYPE HERE RANGE OF DATES */
// example: 2019-12-31 2020-01-31
p.profit('2017-01-05', '2017-01-10');