-
Notifications
You must be signed in to change notification settings - Fork 22
/
odata-expectativas.R
710 lines (680 loc) · 25.9 KB
/
odata-expectativas.R
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
#' Get monthly market expectations of economic indicators
#'
#' Statistics for the monthly expectations of economic indicators.
#' All statistics are computed based on monthly expectations provided by many financial
#' institutions in Brazil: banks, funds, risk managers, so on and so forth.
#' These expections and its statistics are used to build the FOCUS Report weekly
#' released by the Brazilian Central Bank.
#'
#' There are monthly expectations available for the following indicators:
#'
#' \itemize{
#' \item Câmbio
#' \item IGP-DI
#' \item IGP-M
#' \item INPC
#' \item IPA-DI
#' \item IPA-M
#' \item IPCA
#' \item IPCA Administrados
#' \item IPCA Alimentação no domicílio
#' \item IPCA Bens industrializados
#' \item IPCA Livres
#' \item IPCA Serviços
#' \item IPCA-15
#' \item IPC-Fipe
#' \item Produção industrial
#' \item Selic
#' \item Taxa de desocupação
#' }
#'
#' Check <https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/documentacao#ExpectativaMercadoMensais>
#' for more details
#'
#' @param indic a character vector with economic indicators names.
#' They are case sensitive and don't forget the accents.
#' @param start_date series initial date. Accepts ISO character formated date and \code{Date}.
#' @param end_date series final date. Accepts ISO character formated date and \code{Date}.
#' @param ... additional parameters to be passed to the API
#'
#' \code{indic} argument must be one of indicators listed in Details.
#' Respecting the case, blank spaces and accents.
#'
#' The \code{...} is to be used with API's parameters. \code{$top} to specify
#' the maximum number of rows to be returned, this returns the \code{$top} rows,
#' in chronological order. There is also \code{$skip} to ignore the first rows.
#'
#' @return
#' A \code{data.frame} with the requested data.
#'
#' @examples
#' \dontrun{
#' indic <- c("IPCA", "IPC-Fipe")
#' end_date <- "2018-01-31"
#' x <- get_monthly_market_expectations(indic, end_date = end_date, `$top` = 10)
#'
#' # return all indicators for the specified date range
#' start_date <- "2021-01-01"
#' x <- get_monthly_market_expectations(start_date = start_date, `$top` = 20)
#' }
#' @export
get_monthly_market_expectations <- function(indic = NULL, start_date = NULL,
end_date = NULL, ...) {
df_ <- get_market_expectations("monthly", indic, start_date, end_date,
keep_names = FALSE, ...
)
refdate <- as.Date(paste0("01", df_$reference_date), "%d%m/%Y")
levels_ <- format(sort(unique(refdate)), "%Y-%m")
x_ <- format(refdate, "%Y-%m")
df_$reference_date <- factor(x_, levels = levels_, ordered = TRUE)
df_
}
#' Get quarterly market expectations of economic indicators
#'
#' Statistics for the quarterly expectations of economic indicators.
#' All statistics are computed based on quarterly expectations provided by many financial
#' institutions in Brazil: banks, funds, risk managers, so on and so forth.
#' These expections and its statistics are used to build the FOCUS Report weekly
#' released by the Brazilian Central Bank.
#'
#' There are quarterly expectations available for the following indicators:
#'
#' \itemize{
#' \item Câmbio
#' \item IPCA
#' \item IPCA Administrados
#' \item IPCA Alimentação no domicílio
#' \item IPCA Bens industrializados
#' \item IPCA Livres
#' \item IPCA Serviços
#' \item PIB Agropecuária
#' \item PIB Indústria
#' \item PIB Serviços
#' \item PIB Total
#' \item Taxa de desocupação
#' }
#'
#' Check <https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/documentacao#ExpectativasMercadoTrimestrais>
#' for more details
#'
#' @param indic a character vector with economic indicators names.
#' They are case sensitive and don't forget the accents.
#' @param start_date series initial date. Accepts ISO character formated date and \code{Date}.
#' @param end_date series final date. Accepts ISO character formated date and \code{Date}.
#' @param ... additional parameters to be passed to the API
#'
#' \code{indic} argument must be one of indicators listed in Details.
#' Respecting the case, blank spaces and accents.
#'
#' The \code{...} is to be used with API's parameters. \code{$top} to specify
#' the maximum number of rows to be returned, this returns the \code{$top} rows,
#' in chronological order. There is also \code{$skip} to ignore the first rows.
#'
#' @return
#' A \code{data.frame} with the requested data.
#'
#' @examples
#' \dontrun{
#' indic <- c("PIB Industrial", "PIB Total")
#' end_date <- "2018-01-31"
#' x <- get_quarterly_market_expectations(indic, end_date = end_date, `$top` = 10)
#'
#' # return all indicators for the specified date range
#' start_date <- "2021-01-01"
#' x <- get_quarterly_market_expectations(start_date = start_date, `$top` = 20)
#' }
#'
#' @export
get_quarterly_market_expectations <- function(indic = NULL, start_date = NULL,
end_date = NULL, ...) {
get_market_expectations("quarterly", indic, start_date, end_date,
keep_names = FALSE, ...
)
}
#' Get annual market expectations of economic indicators
#'
#' Statistics for the annual expectations of economic indicators.
#' All statistics are computed based on annual expectations provided by many financial
#' institutions in Brazil: banks, funds, risk managers, so on and so forth.
#' These expections and its statistics are used to build the FOCUS Report weekly
#' released by the Brazilian Central Bank.
#'
#' There are annual expectations available for the following indicators:
#'
#' \itemize{
#' \item Balança Comercial
#' \item Câmbio
#' \item Conta corrente
#' \item Dívida bruta do governo geral
#' \item Dívida líquida do setor público
#' \item IGP-DI
#' \item IGP-M
#' \item INPC
#' \item Investimento direto no país
#' \item IPA-DI
#' \item IPA-M
#' \item IPCA
#' \item IPCA Administrados
#' \item IPCA Alimentação no domicílio
#' \item IPCA Bens industrializados
#' \item IPCA Livres
#' \item IPCA Serviços
#' \item IPCA-15
#' \item IPC-FIPE
#' \item PIB Agropecuária
#' \item PIB Despesa de consumo da administração pública
#' \item PIB despesa de consumo das famílias
#' \item PIB Exportação de bens e serviços
#' \item PIB Formação Bruta de Capital Fixo
#' \item PIB Importação de bens e serviços
#' \item PIB Indústria
#' \item PIB Serviços
#' \item PIB Total
#' \item Produção industrial
#' \item Resultado nominal
#' \item Resultado primário
#' \item Selic
#' \item Taxa de desocupação
#' }
#'
#' Check <https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/documentacao#ExpectativasMercadoAnuais>
#' for more details
#'
#' @param indic a character vector with economic indicators names.
#' They are case sensitive and don't forget the accents.
#' @param start_date series initial date. Accepts ISO character formated date and \code{Date}.
#' @param end_date series final date. Accepts ISO character formated date and \code{Date}.
#' @param ... additional parameters to be passed to the API
#'
#' \code{indic} argument must be one of indicators listed in Details.
#' Respecting the case, blank spaces and accents.
#'
#' The \code{...} is to be used with API's parameters. \code{$top} to specify
#' the maximum number of rows to be returned, this returns the \code{$top} rows,
#' in chronological order. There is also \code{$skip} to ignore the first rows.
#'
#' @return
#' A \code{data.frame} with the requested data.
#'
#' @examples
#' \dontrun{
#' indic <- c("PIB Total", "Fiscal")
#' end_date <- "2018-01-31"
#' x <- get_annual_market_expectations(indic, end_date = end_date, `$top` = 10)
#'
#' # return all indicators for the specified date range
#' start_date <- "2021-01-01"
#' x <- get_annual_market_expectations(start_date = start_date, `$top` = 20)
#' }
#'
#' @export
get_annual_market_expectations <- function(indic = NULL, start_date = NULL,
end_date = NULL, ...) {
get_market_expectations("annual", indic, start_date, end_date,
keep_names = FALSE, ...
)
}
#' Get inflation's market expectations for the next 12 months
#'
#' Statistics of inflation's market expectations for the next 12 months.
#' All statistics are computed based on expectations provided by many financial
#' institutions in Brazil: banks, funds, risk managers, so on and so forth.
#' These expections and its statistics are used to build the FOCUS Report weekly
#' released by the Brazilian Central Bank.
#'
#' There are inflation's expectations available for the following indicators:
#'
#' \itemize{
#' \item IGP-DI
#' \item IGP-M
#' \item INPC
#' \item IPA-DI
#' \item IPA-M
#' \item IPCA
#' \item IPCA Administrados
#' \item IPCA Alimentação no domicílio
#' \item IPCA Bens industrializados
#' \item IPCA Livres
#' \item IPCA Serviços
#' \item IPCA-15
#' \item IPC-FIPE
#' }
#'
#' Check <https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/documentacao#ExpectativasMercadoInflacao12Meses>
#' for more details
#'
#' @param indic a character vector with economic indicators names.
#' They are case sensitive and don't forget the accents.
#' @param start_date series initial date.
#' Accepts ISO character formated date and \code{Date}.
#' @param end_date series final date.
#' Accepts ISO character formated date and \code{Date}.
#' @param ... additional parameters to be passed to the API
#'
#' \code{indic} argument must be one of indicators listed in Details.
#' Respecting the case, blank spaces and accents.
#'
#' The \code{...} is to be used with API's parameters. \code{$top} to specify
#' the maximum number of rows to be returned, this returns the \code{$top} rows,
#' in chronological order. There is also \code{$skip} to ignore the first rows.
#'
#' @return
#' A \code{data.frame} with the requested data.
#'
#' @examples
#' \dontrun{
#' indic <- c("IPCA", "IGP-M")
#' end_date <- "2018-06-22"
#' x <- get_twelve_months_inflation_expectations(indic, end_date = end_date, `$top` = 10)
#'
#' # return all indicators for the specified date range
#' start_date <- "2021-01-01"
#' x <- get_twelve_months_inflation_expectations(start_date = start_date, `$top` = 20)
#' }
#'
#' @export
get_twelve_months_inflation_expectations <- function(indic = NULL, start_date = NULL,
end_date = NULL, ...) {
get_market_expectations("inflation-12-months", indic, start_date, end_date,
keep_names = FALSE, ...
)
}
#' Get monthly market expectations from top 5 providers
#'
#' Statistics of monthly expectations for top 5 indicators.
#' All statistics are computed based on expectations provided by many financial
#' institutions in Brazil: banks, funds, risk managers, so on and so forth.
#' These expections and its statistics are used to build the FOCUS Report weekly
#' released by the Brazilian Central Bank.
#'
#' There are inflation's expectations available for the following indicators:
#'
#' \itemize{
#' \item Câmbio
#' \item IGP-DI
#' \item IGP-M
#' \item IPCA
#' \item Selic
#' }
#'
#' Check <https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/documentacao#ExpectativasMercadoTop5Mensais>
#' for more details
#'
#' @param indic a character vector with economic indicators names.
#' They are case sensitive and don't forget the accents.
#' @param start_date series initial date.
#' Accepts ISO character formated date and \code{Date}.
#' @param end_date series final date.
#' Accepts ISO character formated date and \code{Date}.
#' @param ... additional parameters to be passed to the API
#'
#' \code{indic} argument must be one of indicators listed in Details.
#' Respecting the case, blank spaces and accents.
#'
#' The \code{...} is to be used with API's parameters. \code{$top} to specify
#' the maximum number of rows to be returned, this returns the \code{$top} rows,
#' in chronological order. There is also \code{$skip} to ignore the first rows.
#'
#' @return
#' A \code{data.frame} with the requested data.
#'
#' @examples
#' \dontrun{
#' indic <- "IPCA"
#' end_date <- "2018-06-22"
#' x <- get_top5s_monthly_market_expectations(indic, end_date = end_date, `$top` = 10)
#'
#' # return all indicators for the specified date range
#' start_date <- "2021-01-01"
#' x <- get_top5s_monthly_market_expectations(start_date = start_date, `$top` = 20)
#' }
#'
#' @export
get_top5s_monthly_market_expectations <- function(indic = NULL, start_date = NULL,
end_date = NULL, ...) {
get_market_expectations("top5s-monthly", indic, start_date, end_date,
keep_names = FALSE, ...
)
}
#' Get annual market expectations from top 5 providers
#'
#' Statistics of annual expectations for top 5 indicators.
#' All statistics are computed based on expectations provided by many financial
#' institutions in Brazil: banks, funds, risk managers, so on and so forth.
#' These expections and its statistics are used to build the FOCUS Report weekly
#' released by the Brazilian Central Bank.
#'
#' There are inflation's expectations available for the following indicators:
#'
#' \itemize{
#' \item Câmbio
#' \item IGP-DI
#' \item IGP-M
#' \item IPCA
#' \item Selic
#' }
#'
#' Check <https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/documentacao#ExpectativasMercadoTop5Anuais>
#' for more details
#'
#' @param indic a character vector with economic indicators names.
#' They are case sensitive and don't forget the accents.
#' @param start_date series initial date.
#' Accepts ISO character formated date and \code{Date}.
#' @param end_date series final date.
#' Accepts ISO character formated date and \code{Date}.
#' @param ... additional parameters to be passed to the API
#'
#' \code{indic} argument must be one of indicators listed in Details.
#' Respecting the case, blank spaces and accents.
#'
#' The \code{...} is to be used with API's parameters. \code{$top} to specify
#' the maximum number of rows to be returned, this returns the \code{$top} rows,
#' in chronological order. There is also \code{$skip} to ignore the first rows.
#'
#' @return
#' A \code{data.frame} with the requested data.
#'
#' @examples
#' \dontrun{
#' indic <- "IPCA"
#' end_date <- "2018-06-22"
#' x <- get_top5s_annual_market_expectations(indic, end_date = end_date, `$top` = 10)
#'
#' # return all indicators for the specified date range
#' start_date <- "2021-01-01"
#' x <- get_top5s_annual_market_expectations(start_date = start_date, `$top` = 20)
#' }
#'
#' @export
get_top5s_annual_market_expectations <- function(indic = NULL, start_date = NULL,
end_date = NULL, ...) {
get_market_expectations("top5s-annual", indic, start_date, end_date,
keep_names = FALSE, ...
)
}
#' Get Selic market expectations
#'
#' Statistics of Selic market expectations.
#' All statistics are computed based on expectations provided by many financial
#' institutions in Brazil: banks, funds, risk managers, so on and so forth.
#' These expections and its statistics are used to build the FOCUS Report weekly
#' released by the Brazilian Central Bank.
#'
#' Check <https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/documentacao#ExpectativasMercadoSelic>
#' for more details
#'
#' @param start_date series initial date.
#' Accepts ISO character formated date and \code{Date}.
#' @param end_date series final date.
#' Accepts ISO character formated date and \code{Date}.
#' @param ... additional parameters to be passed to the API
#'
#' The \code{...} is to be used with API's parameters. \code{$top} to specify
#' the maximum number of rows to be returned, this returns the \code{$top} rows,
#' in chronological order. There is also \code{$skip} to ignore the first rows.
#'
#' @return
#' A \code{data.frame} with the requested data.
#'
#' @examples
#' \dontrun{
#' # return all indicators for the specified date range
#' start_date <- "2022-01-01"
#' x <- get_selic_market_expectations(start_date = start_date, `$top` = 20)
#' }
#'
#' @export
get_selic_market_expectations <- function(start_date = NULL,
end_date = NULL, ...) {
get_market_expectations("selic", NULL, start_date, end_date,
keep_names = FALSE, ...
)
}
#' Get Selic market expectations from top 5 providers
#'
#' Statistics of Selic expectations for top 5 indicators.
#' All statistics are computed based on expectations provided by many financial
#' institutions in Brazil: banks, funds, risk managers, so on and so forth.
#' These expections and its statistics are used to build the FOCUS Report weekly
#' released by the Brazilian Central Bank.
#'
#' Check <https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/documentacao#ExpectativasMercadoTop5Selic>
#' for more details
#'
#' @param start_date series initial date.
#' Accepts ISO character formated date and \code{Date}.
#' @param end_date series final date.
#' Accepts ISO character formated date and \code{Date}.
#' @param ... additional parameters to be passed to the API
#'
#' The \code{...} is to be used with API's parameters. \code{$top} to specify
#' the maximum number of rows to be returned, this returns the \code{$top} rows,
#' in chronological order. There is also \code{$skip} to ignore the first rows.
#'
#' @return
#' A \code{data.frame} with the requested data.
#'
#' @examples
#' \dontrun{
#' # return all indicators for the specified date range
#' start_date <- "2022-01-01"
#' x <- get_top5s_selic_market_expectations(start_date = start_date, `$top` = 20)
#' }
#'
#' @export
get_top5s_selic_market_expectations <- function(start_date = NULL,
end_date = NULL, ...) {
get_market_expectations("top5s-selic", NULL, start_date, end_date,
keep_names = FALSE, ...
)
}
#' Get market expectations
#'
#' General function to get statistics of market expectations.
#' The API provides requests for annual, monthly, and quarterly expectations.
#' Is is also proveided expectations for 12 months ahead, specific requests for
#' the top 5 indicators for annual and monthly expectations and data provided
#' by financial institutions.
#'
#' All statistics are computed based on expectations provided by many financial
#' institutions in Brazil: banks, funds, risk managers, so on and so forth.
#' These expections and its statistics are used to build the FOCUS Report weekly
#' released by the Brazilian Central Bank.
#'
#' There are market expectations available for the following indicators:
#'
#' \itemize{
#' \item Balança Comercial
#' \item Câmbio
#' \item Conta corrente
#' \item Dívida bruta do governo geral
#' \item Dívida líquida do setor público
#' \item IGP-DI
#' \item IGP-M
#' \item INPC
#' \item Investimento direto no país
#' \item IPA-DI
#' \item IPA-M
#' \item IPCA
#' \item IPCA Administrados
#' \item IPCA Alimentação no domicílio
#' \item IPCA Bens industrializados
#' \item IPCA Livres
#' \item IPCA Serviços
#' \item IPCA-15
#' \item IPC-FIPE
#' \item PIB Agropecuária
#' \item PIB Despesa de consumo da administração pública
#' \item PIB despesa de consumo das famílias
#' \item PIB Exportação de bens e serviços
#' \item PIB Formação Bruta de Capital Fixo
#' \item PIB Importação de bens e serviços
#' \item PIB Indústria
#' \item PIB Serviços
#' \item PIB Total
#' \item Produção industrial
#' \item Resultado nominal
#' \item Resultado primário
#' \item Selic
#' \item Taxa de desocupação
#' }
#'
#' Check <https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/documentacao>
#' for more details
#'
#' @param type a character with one of the following: \code{annual}, \code{quarterly}
#' \code{monthly}, \code{inflation-12-months}, \code{top5s-monthly}, \code{top5s-annual}.
#' @param indic a character vector with economic indicators names.
#' They are case sensitive and don't forget the accents.
#' @param start_date series initial date.
#' Accepts ISO character formated date and \code{Date}.
#' @param end_date series final date.
#' Accepts ISO character formated date and \code{Date}.
#' @param keep_names if \code{TRUE} keeps the column names returned by the API
#' (in portuguese), if \code{FALSE} the columns are renamed to standardized
#' names (in english).
#' @param ... additional parameters to be passed to the API
#'
#' \code{type} defines the API used to fetch data.
#'
#' \itemize{
#' \item \code{selic}: refers to the API *Expectativas de Mercado Selic - Estatísticas* for SELIC rate expectations
#' \item \code{annual}: refers to the API *Expectativas de Mercado Anuais* for annual market expectations
#' \item \code{quarterly}: refers to the API *Expectativas de Mercado Trimestrais* for quarterly market expectations
#' \item \code{monthly}: refers to the API *Expectativas de Mercado Mensais* for monthly market expectations
#' \item \code{inflation-12-months}: refers to the API *Expectativas de mercado para inflação nos próximos 12 meses* for market expectations of inflation indexes for the next 12 months.
#' \item \code{top5s-selic}: refers to the API *Expectativas de Mercado Selic Top5* for SELIC rate expectations of top 5's
#' \item \code{top5s-monthly}: refers to the API *Expectativas de mercado mensais para os indicadores do Top 5* for monthly market expectations of top 5's
#' \item \code{top5s-annual}: refers to the API *Expectativas de mercado anuais para os indicadores do Top 5* for annual market expectations of top 5's
#' }
#'
#' \code{indic} argument must be one of indicators listed in Details.
#' Respecting the case, blank spaces and accents.
#'
#' The \code{...} is to be used with API's parameters. \code{$top} to specify
#' the maximum number of rows to be returned, this returns the \code{$top} rows,
#' in chronological order.
#' \code{$skip} can be used to ignore the first rows.
#' If provided \code{$filter} applies filters according to <https://olinda.bcb.gov.br/olinda/servico/ajuda>.
#'
#' @return
#' A \code{data.frame} with the requested data.
#'
#' @examples
#' \dontrun{
#' indic <- c("IPCA", "Câmbio")
#' x <- get_market_expectations("annual", indic, `$top` = 10)
#'
#' x <- get_market_expectations("monthly", "Selic", `$top` = 20)
#'
#' # get monthly expectations for top 5 indicators since 2021
#' x <- get_market_expectations("top5s-monthly", start_date = "2021-01-01")
#'
#' # get annual expectations for top 5 indicators since 2021
#' x <- get_market_expectations("top5s-annual", `$top` = 20)
#'
#' # get all inflation expectations for 12 months ahead starting on 2021-01
#' x <- get_market_expectations("inflation-12-months", start_date = "2021-01-01")
#'
#' # get all SELIC expectations informed by financial institutions since 2022
#' x <- get_market_expectations("selic", start_date = "2022-01-01")
#'
#' # get TOP5 SELIC expectations starting on 2022
#' x <- get_market_expectations("top5s-selic", start_date = "2022-01-01")
#' }
#'
#' @export
get_market_expectations <- function(type = c(
"annual",
"quarterly",
"monthly",
"inflation-12-months",
"top5s-monthly",
"top5s-annual",
"selic",
"top5s-selic"
),
indic = NULL, start_date = NULL,
end_date = NULL, keep_names = TRUE, ...) {
type <- match.arg(type)
url <- .build_expectations_url(
.get_market_expectations_url(type),
indic, start_date, end_date, ...
)
f <- http_download("get", url)
data_ <- fromJSON(f)
if (!is.null(data_$value) && length(data_$value) == 0) {
return(tibble())
}
if (is.null(data_$value)) {
stop("BCB API Request error: no value attribute returned")
}
df_ <- as_tibble(data_$value)
df_$Data <- as.Date(df_$Data)
if (!keep_names) {
names(df_) <- sapply(names(df_), change_names, USE.NAMES = FALSE)
}
df_
}
.get_market_expectations_url <- function(x) {
switch(x,
"annual" = "https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativasMercadoAnuais",
"quarterly" = "https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativasMercadoTrimestrais",
"monthly" = "https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativaMercadoMensais",
"inflation-12-months" = "https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativasMercadoInflacao12Meses",
"top5s-monthly" = "https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativasMercadoTop5Mensais",
"top5s-annual" = "https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativasMercadoTop5Anuais",
"selic" = "https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativasMercadoSelic",
"top5s-selic" = "https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativasMercadoTop5Selic",
)
}
.build_expectations_url <- function(url, indic, start_date, end_date, ...) {
if (is.null(indic)) {
indic_filter <- NULL
} else {
indic_filter <- paste(sprintf("Indicador eq '%s'", indic), collapse = " or ")
indic_filter <- paste0("(", indic_filter, ")")
}
sd_filter <- if (!is.null(start_date)) {
sprintf("Data ge '%s'", start_date)
} else {
NULL
}
ed_filter <- if (!is.null(end_date)) {
sprintf("Data le '%s'", end_date)
} else {
NULL
}
q <- list(...)
q[["$format"]] <- "application/json"
# q[["$orderby"]] <- "Data desc"
filter__ <- paste(c(indic_filter, sd_filter, ed_filter, q[["$filter"]]),
collapse = " and "
)
filter__ <- if (filter__ == "") NULL else filter__
q[["$filter"]] <- filter__
modify_url(url, query = q)
}
change_names <- function(name) {
switch(name,
"Instituicao" = "institution",
"Indicador" = "indic",
"Periodicidade" = "periodicity",
"Valor" = "value",
"Data" = "date",
"DataReferencia" = "reference_date",
"Media" = "mean",
"Mediana" = "median",
"DesvioPadrao" = "sd",
"Minimo" = "min",
"Maximo" = "max",
"numeroRespondentes" = "respondents",
"CoeficienteVariacao" = "coefvar",
"baseCalculo" = "base",
"IndicadorDetalhe" = "indic_detail",
"Suavizada" = "smoothed",
"tipoCalculo" = "typeCalc",
name
)
}