-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
225 lines (192 loc) · 5.74 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
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
import {
addWeeks,
differenceInBusinessDays,
eachDayOfInterval,
eachMonthOfInterval,
isWeekend,
lightFormat,
max,
min,
parse,
startOfMonth,
startOfYear,
subDays,
subMonths
} from "date-fns";
import request from "request-promise";
import { format } from "util";
(async () => {
const holidays = await fetchBrazilianHolidayDates();
const dateRange = eachMonthOfInterval({
start: startOfYear(parse("2017-01-01", "yyyy-MM-dd", new Date())),
end: startOfYear(parse("2021-01-01", "yyyy-MM-dd", new Date()))
});
const dates = dateRange.map(dateOfReference => {
const startOfPreviousMonth = startOfMonth(subMonths(dateOfReference, 1));
const firstFortnightOfPreviousMonth = addWeeks(startOfPreviousMonth, 2);
const lastBusinessDayOfFirstFortnight = lastBusinessDay(
firstFortnightOfPreviousMonth,
holidays
);
return {
dateOfReference,
startOfPreviousMonth,
lastBusinessDayOfFirstFortnight
};
});
const fortnights = dates.map(
({ lastBusinessDayOfFirstFortnight }) => lastBusinessDayOfFirstFortnight
);
const minFortnight = min(fortnights);
const maxFortnight = max(fortnights);
const allQuotes = await getUsdQuotes(minFortnight, maxFortnight);
const quotesForFortnights = allQuotes.filter(candidate => {
return fortnights.some(date => {
return isEqualYmd(candidate.day, date);
});
});
console.log(
[
"Data",
"Mês Anterior",
"Último dia útil da primeira quinzena do mês anterior",
"Cotação USD Compra",
"Cotação USD Venda",
"Data Cotação"
].join("\t")
);
dates.forEach(entry => {
const quote = quotesForFortnights.find(candidate => {
return isEqualYmd(candidate.day, entry.lastBusinessDayOfFirstFortnight);
});
if (!quote) {
return;
}
console.log(
[
lightFormat(entry.dateOfReference, "MM/yyyy"),
lightFormat(entry.startOfPreviousMonth, "MM/yyyy"),
lightFormat(entry.lastBusinessDayOfFirstFortnight, "dd/MM/yyyy"),
quote.buy.toString().replace(".", ","),
quote.sell.toString().replace(".", ","),
lightFormat(quote.day, "dd/MM/yyyy")
].join("\t")
);
});
})();
function lastBusinessDay(ofDate: Date, holidays: Date[]) {
if (isWeekend(ofDate)) {
return lastBusinessDay(subDays(ofDate, 1), holidays);
}
if (holidays.length > 0) {
const isHoliday = holidays.some(holiday => {
return isEqualYmd(holiday, ofDate);
});
if (isHoliday) {
return lastBusinessDay(subDays(ofDate, 1), holidays);
}
}
return ofDate;
}
async function getUsdQuotes(startOfPeriod: Date, endOfPeriod: Date) {
const dataInicial = escape(`'${lightFormat(startOfPeriod, "MM-dd-yyyy")}'`);
const dataFinalCotacao = escape(
`'${lightFormat(endOfPeriod, "MM-dd-yyyy")}'`
);
const allQuotesOfPeriod = format(
"%s(dataInicial=@dataInicial,dataFinalCotacao=@dataFinalCotacao)?@dataInicial=%s&@dataFinalCotacao=%s&$top=%s&$format=json",
"https://olinda.bcb.gov.br/olinda/servico/PTAX/versao/v1/odata/CotacaoDolarPeriodo",
dataInicial,
dataFinalCotacao,
differenceInBusinessDays(endOfPeriod, startOfPeriod)
);
console.log("Cotações %s", allQuotesOfPeriod);
const response: OlindaCotacaoDolarPeriodo = await request.get(
allQuotesOfPeriod,
{
json: true,
resolveWithFullResponse: false
}
);
const daysOfInterval = eachDayOfInterval({
start: startOfPeriod,
end: endOfPeriod
});
return daysOfInterval
.map(day => {
const foundQuote = response.value.find(candidate => {
return isEqualYmd(
day,
parse(
candidate.dataHoraCotacao.substring(0, "yyyy-MM-dd".length),
"yyyy-MM-dd",
new Date()
)
);
});
if (!foundQuote) {
return null;
}
return {
buy: foundQuote.cotacaoCompra,
sell: foundQuote.cotacaoVenda,
day
};
})
.filter(notNull);
}
function notNull<T>(value: T | null | undefined): value is T {
return value !== null && value !== undefined;
}
function isEqualYmd(dateLeft: Date, dateRight: Date) {
return (
lightFormat(dateLeft, "yyyy-MM-dd") === lightFormat(dateRight, "yyyy-MM-dd")
);
}
type OlindaCotacaoDolarPeriodo = {
value: Array<{
cotacaoCompra: number;
cotacaoVenda: number;
dataHoraCotacao: string;
}>;
};
async function fetchBrazilianHolidayDates(): Promise<Date[]> {
const calendarUrls = [
"https://raw.githubusercontent.com/pagarme/business-calendar/master/data/brazil/2017.json",
"https://raw.githubusercontent.com/pagarme/business-calendar/master/data/brazil/2018.json",
"https://raw.githubusercontent.com/pagarme/business-calendar/master/data/brazil/2019.json",
"https://raw.githubusercontent.com/pagarme/business-calendar/master/data/brazil/2020.json",
"https://raw.githubusercontent.com/pagarme/business-calendar/master/data/brazil/2021.json"
];
const allCalendars = await Promise.all(
calendarUrls.map(async url => {
const responseData: PagarMeBusinessCalendar = await request.get(url, {
resolveWithFullResponse: false,
json: true
});
return responseData;
})
);
return allCalendars
.map(jsonData => {
return jsonData.calendar
.map(day => {
if (day.limited_financial_operation && day.holiday) {
return parse(day.date, "yyyy-MM-dd", new Date());
}
return null;
})
.filter(notNull);
})
.flat()
.sort((dateLeft, dateRight) => {
return dateLeft.getTime() - dateRight.getTime();
});
}
type PagarMeBusinessCalendar = {
calendar: Array<{
date: string;
holiday: boolean;
limited_financial_operation: boolean;
}>;
};