-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathav.py
1351 lines (1113 loc) · 54 KB
/
av.py
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""AlphaVantage API wrapper.
The author is Zmicier Gotowka
Distributed under Fcore License 1.1 (see license.md)
"""
from datetime import datetime
import pytz
from data import stock
from data.fvalues import Timespans, SecType, Currency
from data.fdata import FdataError
import pandas as pd
import numpy as np
import json
from data.futils import get_dt, get_labelled_ndarray
import settings
class AvSubquery():
"""
Class which represents additional subqueries for optional data (fundamentals, global economic, customer data and so on).
"""
def __init__(self, table, column, condition='', title=None):
"""
Initializes the instance of Subquery class.
Args:
table(str): table for subquery.
column(str): column to obtain.
condition(str): additional SQL condition for the subquery.
title(str): optional title for the output column (the same as column name by default)
"""
self.table = table
self.column = column
self.condition = condition
# Use the default column name as the title if the title is not specified
if title is None:
self.title = column
else:
self.title = title
print(f"Warning! The datasource {type(self).__name__} is not maintained any more. Consider using YF or FMP datasources.")
def generate(self):
"""
Generates the subquery based on the provided data.
Returns:
str: SQL expression for the subquery
"""
subquery = f"""(SELECT {self.column}
FROM {self.table} report_tbl
WHERE fiscal_date_ending <= time_stamp
AND symbol_id = quotes.symbol_id
{self.condition}
ORDER BY fiscal_date_ending DESC LIMIT 1) AS {self.title}\n"""
return subquery
class AVStock(stock.StockFetcher):
"""
AlphaVantage API wrapper class.
"""
def __init__(self, **kwargs):
"""Initialize the instance of AVStock class."""
super().__init__(**kwargs)
# Default values
self.source_title = "AlphaVantage"
self.api_key = settings.AV.api_key
self.compact = False # Indicates if a limited number (100) of quotes should be obtained
# Cached EOD quotes to get dividends and split data
self._eod = None
self._eod_symbol = None
if settings.AV.plan == settings.AV.Plan.Free:
self.max_queries = 5
if settings.AV.plan == settings.AV.Plan.Plan30:
self.max_queries = 30
if settings.AV.plan == settings.AV.Plan.Plan75:
self.max_queries = 75
if settings.AV.plan == settings.AV.Plan.Plan150:
self.max_queries = 150
if settings.AV.plan == settings.AV.Plan.Plan300:
self.max_queries = 300
if settings.AV.plan == settings.AV.Plan.Plan600:
self.max_queries = 600
if settings.AV.plan == settings.AV.Plan.Plan1200:
self.max_queries = 1200
if self.api_key is None:
raise FdataError("API key is needed for this data source. Get your free API key at alphavantage.co and put it in setting.py")
# Data related to fundamental tables
self._fundamental_intervals_tbl = 'av_fundamental_intervals'
self._income_statement_tbl = 'av_income_statement'
self._balance_sheet_tbl = 'av_balance_sheet'
self._cash_flow_tbl = 'av_cash_flow'
def fetch_info(self):
raise FdataError(f"Stock info data is not supported (yet) for the source {type(self).__name__}")
def check_database(self):
"""
Database create/integrity check method for stock data related tables.
Checks if the database exists. Otherwise, creates it. Checks if the database has required tables.
Raises:
FdataError: sql error happened.
"""
super().check_database()
# Check if we need to create a table income_statement
try:
check_income_statement = f"SELECT name FROM sqlite_master WHERE type='table' AND name='{self._income_statement_tbl}';"
self.cur.execute(check_income_statement)
rows = self.cur.fetchall()
except self.Error as e:
raise FdataError(f"Can't execute a query on a table '{self._income_statement_tbl}': {e}\n{check_income_statement}") from e
if len(rows) == 0:
create_is = f"""CREATE TABLE {self._income_statement_tbl}(
av_is_report_id INTEGER PRIMARY KEY AUTOINCREMENT,
source_id INTEGER NOT NULL,
symbol_id INTEGER NOT NULL,
reported_date INTEGER,
reported_period INTEGER NOT NULL,
fiscal_date_ending INTEGER NOT NULL,
gross_profit INTEGER,
total_revenue INTEGER,
cost_of_revenue INTEGER,
cost_of_goods_and_services_sold INTEGER,
operating_income INTEGER,
selling_general_and_administrative INTEGER,
research_and_development INTEGER,
operating_expenses INTEGER,
investment_income_net INTEGER,
net_interest_income INTEGER,
interest_income INTEGER,
interest_expense INTEGER,
non_interest_income INTEGER,
other_non_operating_income INTEGER,
depreciation INTEGER,
depreciation_and_amortization INTEGER,
income_before_tax INTEGER,
income_tax_expense INTEGER,
interest_and_debt_expense INTEGER,
net_income_from_continuing_operations INTEGER,
comprehensive_income_net_of_tax INTEGER,
ebit INTEGER,
ebitda INTEGER,
net_income INTEGER,
UNIQUE(symbol_id, fiscal_date_ending, reported_period)
CONSTRAINT fk_symbols,
FOREIGN KEY (symbol_id)
REFERENCES symbols(symbol_id)
ON DELETE CASCADE
CONSTRAINT fk_sources,
FOREIGN KEY (source_id)
REFERENCES sources(source_id)
ON DELETE CASCADE
);"""
try:
self.cur.execute(create_is)
except self.Error as e:
raise FdataError(f"Can't execute a query on a table '{self._income_statement_tbl}': {e}\n{create_is}") from e
# Create index for symbol_id
create_symbol_date_is_idx = f"CREATE INDEX idx_{self._income_statement_tbl} ON {self._income_statement_tbl}(symbol_id, reported_date);"
try:
self.cur.execute(create_symbol_date_is_idx)
except self.Error as e:
raise FdataError(f"Can't create index {self._income_statement_tbl}(symbol_id, reported_date): {e}") from e
# Check if we need to create a table balance_sheet
try:
check_balance_sheet = f"SELECT name FROM sqlite_master WHERE type='table' AND name='{self._balance_sheet_tbl}';"
self.cur.execute(check_balance_sheet)
rows = self.cur.fetchall()
except self.Error as e:
raise FdataError(f"Can't execute a query on a table '{self._balance_sheet_tbl}': {e}\n{check_balance_sheet}") from e
if len(rows) == 0:
create_bs = f"""CREATE TABLE {self._balance_sheet_tbl}(
av_bs_report_id INTEGER PRIMARY KEY AUTOINCREMENT,
source_id INTEGER NOT NULL,
symbol_id INTEGER NOT NULL,
reported_date INTEGER,
reported_period INTEGER NOT NULL,
fiscal_date_ending INTEGER NOT NULL,
total_assets INTEGER,
total_current_assets INTEGER,
cash_and_cash_equivalents_at_carrying_value INTEGER,
cash_and_short_term_investments INTEGER,
inventory INTEGER,
current_net_receivables INTEGER,
total_non_current_assets INTEGER,
property_plant_equipment INTEGER,
accumulated_depreciation_amortization_ppe INTEGER,
intangible_assets INTEGER,
intangible_assets_excluding_goodwill INTEGER,
goodwill INTEGER,
investments INTEGER,
long_term_investments INTEGER,
short_term_investments INTEGER,
other_current_assets INTEGER,
other_non_current_assets INTEGER,
total_liabilities INTEGER,
total_current_liabilities INTEGER,
current_accounts_payable INTEGER,
deferred_revenue INTEGER,
current_debt INTEGER,
short_term_debt INTEGER,
total_non_current_liabilities INTEGER,
capital_lease_obligations INTEGER,
long_term_debt INTEGER,
current_long_term_debt INTEGER,
long_term_debt_noncurrent INTEGER,
short_long_term_debt_total INTEGER,
other_noncurrent_liabilities INTEGER,
other_non_current_liabilities INTEGER,
total_shareholder_equity INTEGER,
treasury_stock INTEGER,
retained_earnings INTEGER,
common_stock INTEGER,
common_stock_shares_outstanding INTEGER,
UNIQUE(symbol_id, fiscal_date_ending, reported_period)
CONSTRAINT fk_symbols,
FOREIGN KEY (symbol_id)
REFERENCES symbols(symbol_id)
ON DELETE CASCADE
CONSTRAINT fk_sources,
FOREIGN KEY (source_id)
REFERENCES sources(source_id)
ON DELETE CASCADE
);"""
try:
self.cur.execute(create_bs)
except self.Error as e:
raise FdataError(f"Can't execute a query on a table '{self._balance_sheet_tbl}': {e}\n{create_bs}") from e
# Create index for symbol_id
create_symbol_date_bs_idx = f"CREATE INDEX idx_{self._balance_sheet_tbl} ON {self._balance_sheet_tbl}(symbol_id, reported_date);"
try:
self.cur.execute(create_symbol_date_bs_idx)
except self.Error as e:
raise FdataError(f"Can't create index {self._balance_sheet_tbl}(symbol_id, reported_date): {e}") from e
# Check if we need to create a table cash_flow
try:
check_cash_flow = f"SELECT name FROM sqlite_master WHERE type='table' AND name='{self._cash_flow_tbl}';"
self.cur.execute(check_cash_flow)
rows = self.cur.fetchall()
except self.Error as e:
raise FdataError(f"Can't execute a query on a table '{self._cash_flow_tbl}': {e}\n{check_cash_flow}") from e
# TODO LOW Get rid of tabulations above
if len(rows) == 0:
create_cf = f"""CREATE TABLE {self._cash_flow_tbl}(
av_cf_report_id INTEGER PRIMARY KEY AUTOINCREMENT,
source_id INTEGER NOT NULL,
symbol_id INTEGER NOT NULL,
reported_date INTEGER,
reported_period INTEGER NOT NULL,
fiscal_date_ending INTEGER NOT NULL,
operating_cashflow INTEGER,
payments_for_operating_activities INTEGER,
proceeds_from_operating_activities INTEGER,
change_in_operating_liabilities INTEGER,
change_in_operating_assets INTEGER,
depreciation_depletion_and_amortization INTEGER,
capital_expenditures INTEGER,
change_in_receivables INTEGER,
change_in_inventory INTEGER,
profit_loss INTEGER,
cashflow_from_investment INTEGER,
cashflow_from_financing INTEGER,
proceeds_from_repayments_of_short_term_debt INTEGER,
payments_for_repurchase_of_common_stock INTEGER,
payments_for_repurchase_of_equity INTEGER,
payments_for_repurchase_of_preferred_stock INTEGER,
dividend_payout INTEGER,
dividend_payout_common_stock INTEGER,
dividend_payout_preferred_stock INTEGER,
proceeds_from_issuance_of_common_stock INTEGER,
proceeds_from_issuance_of_long_term_debt_and_capital_securities_net INTEGER,
proceeds_from_issuance_of_preferred_stock INTEGER,
proceeds_from_repurchase_of_equity INTEGER,
proceeds_from_sale_of_treasury_stock INTEGER,
change_in_cash_and_cash_equivalents INTEGER,
change_in_exchange_rate INTEGER,
net_income INTEGER,
UNIQUE(symbol_id, fiscal_date_ending, reported_period)
CONSTRAINT fk_symbols,
FOREIGN KEY (symbol_id)
REFERENCES symbols(symbol_id)
ON DELETE CASCADE
CONSTRAINT fk_sources,
FOREIGN KEY (source_id)
REFERENCES sources(source_id)
ON DELETE CASCADE
);"""
try:
self.cur.execute(create_cf)
except self.Error as e:
raise FdataError(f"Can't execute a query on a table '{self._cash_flow_tbl}': {e}\n{create_cf}") from e
# Create index for symbol_id
create_symbol_date_cf_idx = f"CREATE INDEX idx_{self._cash_flow_tbl} ON {self._cash_flow_tbl}(symbol_id, reported_date);"
try:
self.cur.execute(create_symbol_date_cf_idx)
except self.Error as e:
raise FdataError(f"Can't create index {self._cash_flow_tbl}(symbol_id, reported_date): {e}") from e
# Check if we need to create a table earnings
try:
check_earnings = "SELECT name FROM sqlite_master WHERE type='table' AND name='av_earnings';"
self.cur.execute(check_earnings)
rows = self.cur.fetchall()
except self.Error as e:
raise FdataError(f"Can't execute a query on a table 'av_earnings': {e}\n{check_earnings}") from e
if len(rows) == 0:
create_earnings = """CREATE TABLE av_earnings(
av_earnings_report_id INTEGER PRIMARY KEY AUTOINCREMENT,
source_id INTEGER NOT NULL,
symbol_id INTEGER NOT NULL,
reported_date INTEGER,
reported_period INTEGER NOT NULL,
fiscal_date_ending INTEGER NOT NULL,
reported_eps INTEGER,
estimated_eps INTEGER,
surprise INTEGER,
surprise_percentage INTEGER,
UNIQUE(symbol_id, fiscal_date_ending, reported_period)
CONSTRAINT fk_symbols,
FOREIGN KEY (symbol_id)
REFERENCES symbols(symbol_id)
ON DELETE CASCADE
CONSTRAINT fk_sources,
FOREIGN KEY (source_id)
REFERENCES sources(source_id)
ON DELETE CASCADE
);"""
try:
self.cur.execute(create_earnings)
except self.Error as e:
raise FdataError(f"Can't execute a query on a table 'av_earnings': {e}\n{create_earnings}") from e
# Create index for symbol_id
create_symbol_date_e_idx = "CREATE INDEX idx_av_earnings ON av_earnings(symbol_id, reported_date);"
try:
self.cur.execute(create_symbol_date_e_idx)
except self.Error as e:
raise FdataError(f"Can't create index av_earnings(symbol_id, reported_date): {e}") from e
# Check if we need to create table 'fundamental_intervals'
try:
check_fundamental_intervals = f"SELECT name FROM sqlite_master WHERE type='table' AND name='{self._fundamental_intervals_tbl}';"
self.cur.execute(check_fundamental_intervals)
rows = self.cur.fetchall()
except self.Error as e:
raise FdataError(f"Can't execute a query on a table '{self._fundamental_intervals_tbl}': {e}\n{check_fundamental_intervals}") from e
if len(rows) == 0:
create_fundamental_intervals = f"""CREATE TABLE {self._fundamental_intervals_tbl} (
av_f_interval_id INTEGER PRIMARY KEY AUTOINCREMENT,
symbol_id INTEGER NOT NULL,
source_id INTEGER NOT NULL,
income_statement_max_ts INTEGER,
balance_sheet_max_ts INTEGER,
cash_flow_max_ts INTEGER,
CONSTRAINT fk_source
FOREIGN KEY (source_id)
REFERENCES sources(source_id)
ON DELETE CASCADE
CONSTRAINT fk_symbols
FOREIGN KEY (symbol_id)
REFERENCES symbols(symbol_id)
ON DELETE CASCADE
UNIQUE(symbol_id, source_id)
);"""
try:
self.cur.execute(create_fundamental_intervals)
except self.Error as e:
raise FdataError(f"Can't create table {self._fundamental_intervals_tbl}: {e}") from e
# Create indexes for fundamental_intervals
create_fundamental_intervals_idx = f"CREATE INDEX idx_{self._fundamental_intervals_tbl} ON {self._fundamental_intervals_tbl}(symbol_id, source_id);"
try:
self.cur.execute(create_fundamental_intervals_idx)
except self.Error as e:
raise FdataError(f"Can't create indexes for {self._fundamental_intervals_tbl} table: {e}") from e
# TODO LOW Unite it with general fundamentals table
# Check if we need to create table 'earnings_intervals'
try:
check_earnings_intervals = "SELECT name FROM sqlite_master WHERE type='table' AND name='av_earnings_intervals';"
self.cur.execute(check_earnings_intervals)
rows = self.cur.fetchall()
except self.Error as e:
raise FdataError(f"Can't execute a query on a table 'av_earnings_intervals': {e}\n{check_earnings_intervals}") from e
if len(rows) == 0:
create_earnings_intervals = """CREATE TABLE av_earnings_intervals (
av_e_interval_id INTEGER PRIMARY KEY AUTOINCREMENT,
symbol_id INTEGER NOT NULL,
source_id INTEGER NOT NULL,
earnings_max_ts INTEGER NOT NULL,
CONSTRAINT fk_source
FOREIGN KEY (source_id)
REFERENCES sources(source_id)
ON DELETE CASCADE
CONSTRAINT fk_symbols
FOREIGN KEY (symbol_id)
REFERENCES symbols(symbol_id)
ON DELETE CASCADE
UNIQUE(symbol_id, source_id)
);"""
try:
self.cur.execute(create_earnings_intervals)
except self.Error as e:
raise FdataError(f"Can't create table av_earnings_intervals: {e}") from e
# Create indexes for earnings_intervals
create_earnings_intervals_idx = "CREATE INDEX idx_av_earnings_intervals ON av_earnings_intervals(symbol_id, source_id);"
try:
self.cur.execute(create_earnings_intervals_idx)
except self.Error as e:
raise FdataError(f"Can't create indexes for av_earnings_intervals table: {e}") from e
##########################
# Fundamental data methods
##########################
def get_earnings_num(self):
"""Get the number of earnings reports.
Returns:
int: the number of earnings entries in the database.
Raises:
FdataError: sql error happened.
"""
return self._get_data_num('av_earnings')
def add_income_statement(self, reports):
"""
Add income_statement entries to the database.
Args:
quotes_dict(list of dictionaries): income statements entries obtained from an API wrapper.
Returns:
(int, int): total number of income statements reports before and after the operation.
Raises:
FdataError: sql error happened.
"""
self.check_if_connected()
# Insert new symbols to 'symbols' table (if the symbol does not exist)
if self.get_total_symbol_quotes_num() == 0:
self.add_symbol()
num_before = self.get_income_statement_num()
for report in reports:
insert_report = f"""INSERT OR {self._update} INTO {self._income_statement_tbl} (symbol_id,
source_id,
reported_date,
reported_period,
fiscal_date_ending,
gross_profit,
total_revenue,
cost_of_revenue,
cost_of_goods_and_services_sold,
operating_income,
selling_general_and_administrative,
research_and_development,
operating_expenses,
investment_income_net,
net_interest_income,
interest_income,
interest_expense,
non_interest_income,
other_non_operating_income,
depreciation,
depreciation_and_amortization,
income_before_tax,
income_tax_expense,
interest_and_debt_expense,
net_income_from_continuing_operations,
comprehensive_income_net_of_tax,
ebit,
ebitda,
net_income)
VALUES (
(SELECT symbol_id FROM symbols WHERE ticker = '{self.symbol}'),
(SELECT source_id FROM sources WHERE title = '{self.source_title}'),
{report['reportedDate']},
(SELECT period_id FROM report_periods WHERE title = '{report['period']}'),
{report['fiscalDateEnding']},
{report['grossProfit']},
{report['totalRevenue']},
{report['costOfRevenue']},
{report['costofGoodsAndServicesSold']},
{report['operatingIncome']},
{report['sellingGeneralAndAdministrative']},
{report['researchAndDevelopment']},
{report['operatingExpenses']},
{report['investmentIncomeNet']},
{report['netInterestIncome']},
{report['interestIncome']},
{report['interestExpense']},
{report['nonInterestIncome']},
{report['otherNonOperatingIncome']},
{report['depreciation']},
{report['depreciationAndAmortization']},
{report['incomeBeforeTax']},
{report['incomeTaxExpense']},
{report['interestAndDebtExpense']},
{report['netIncomeFromContinuingOperations']},
{report['comprehensiveIncomeNetOfTax']},
{report['ebit']},
{report['ebitda']},
{report['netIncome']});"""
try:
self.cur.execute(insert_report)
except self.Error as e:
raise FdataError(f"Can't add a record to a table 'income_statement': {e}\n\nThe query is\n{insert_report}") from e
self.commit()
self._update_intervals('income_statement_max_ts', self._fundamental_intervals_tbl)
return(num_before, self.get_income_statement_num())
def add_balance_sheet(self, reports):
"""
Add balance sheet entries to the database.
Args:
quotes_dict(list of dictionaries): balance sheet entries obtained from an API wrapper.
Returns:
(int, int): total number of income statements reports before and after the operation.
Raises:
FdataError: sql error happened.
"""
self.check_if_connected()
# Insert new symbols to 'symbols' table (if the symbol does not exist)
if self.get_total_symbol_quotes_num() == 0:
self.add_symbol()
num_before = self.get_balance_sheet_num()
for report in reports:
insert_report = f"""INSERT OR {self._update} INTO {self._balance_sheet_tbl} (symbol_id,
source_id,
reported_date,
reported_period,
fiscal_date_ending,
total_assets,
total_current_assets,
cash_and_cash_equivalents_at_carrying_value,
cash_and_short_term_investments,
inventory,
current_net_receivables,
total_non_current_assets,
property_plant_equipment,
accumulated_depreciation_amortization_ppe,
intangible_assets,
intangible_assets_excluding_goodwill,
goodwill,
investments,
long_term_investments,
short_term_investments,
other_current_assets,
other_non_current_assets,
total_liabilities,
total_current_liabilities,
current_accounts_payable,
deferred_revenue,
current_debt,
short_term_debt,
total_non_current_liabilities,
capital_lease_obligations,
long_term_debt,
current_long_term_debt,
long_term_debt_noncurrent,
short_long_term_debt_total,
other_noncurrent_liabilities,
total_shareholder_equity,
treasury_stock,
retained_earnings,
common_stock,
common_stock_shares_outstanding)
VALUES (
(SELECT symbol_id FROM symbols WHERE ticker = '{self.symbol}'),
(SELECT source_id FROM sources WHERE title = '{self.source_title}'),
{report['reportedDate']},
(SELECT period_id FROM report_periods WHERE title = '{report['period']}'),
{report['fiscalDateEnding']},
{report['totalAssets']},
{report['totalCurrentAssets']},
{report['cashAndCashEquivalentsAtCarryingValue']},
{report['cashAndShortTermInvestments']},
{report['inventory']},
{report['currentNetReceivables']},
{report['totalNonCurrentAssets']},
{report['propertyPlantEquipment']},
{report['accumulatedDepreciationAmortizationPPE']},
{report['intangibleAssets']},
{report['intangibleAssetsExcludingGoodwill']},
{report['goodwill']},
{report['investments']},
{report['longTermInvestments']},
{report['shortTermInvestments']},
{report['otherCurrentAssets']},
{report['otherNonCurrentAssets']},
{report['totalLiabilities']},
{report['totalCurrentLiabilities']},
{report['currentAccountsPayable']},
{report['deferredRevenue']},
{report['currentDebt']},
{report['shortTermDebt']},
{report['totalNonCurrentLiabilities']},
{report['capitalLeaseObligations']},
{report['longTermDebt']},
{report['currentLongTermDebt']},
{report['longTermDebtNoncurrent']},
{report['shortLongTermDebtTotal']},
{report['otherNonCurrentLiabilities']},
{report['totalShareholderEquity']},
{report['treasuryStock']},
{report['retainedEarnings']},
{report['commonStock']},
{report['commonStockSharesOutstanding']});"""
try:
self.cur.execute(insert_report)
except self.Error as e:
raise FdataError(f"Can't add a record to a table 'balance_sheet': {e}\n\nThe query is\n{insert_report}") from e
self.commit()
self._update_intervals('balance_sheet_max_ts', self._fundamental_intervals_tbl)
return(num_before, self.get_balance_sheet_num())
def add_cash_flow(self, reports):
"""
Add cash flow entries to the database.
Args:
quotes_dict(list of dictionaries): cash flow entries obtained from an API wrapper.
Returns:
(int, int): total number of cash flow reports before and after the operation.
Raises:
FdataError: sql error happened.
"""
self.check_if_connected()
# Insert new symbols to 'symbols' table (if the symbol does not exist)
if self.get_total_symbol_quotes_num() == 0:
self.add_symbol()
num_before = self.get_cash_flow_num()
for report in reports:
insert_report = f"""INSERT OR {self._update} INTO {self._cash_flow_tbl} (symbol_id,
source_id,
reported_date,
reported_period,
fiscal_date_ending,
operating_cashflow,
payments_for_operating_activities,
proceeds_from_operating_activities,
change_in_operating_liabilities,
change_in_operating_assets,
depreciation_depletion_and_amortization,
capital_expenditures,
change_in_receivables,
change_in_inventory,
profit_loss,
cashflow_from_investment,
cashflow_from_financing,
proceeds_from_repayments_of_short_term_debt,
payments_for_repurchase_of_common_stock,
payments_for_repurchase_of_equity,
payments_for_repurchase_of_preferred_stock,
dividend_payout,
dividend_payout_common_stock,
dividend_payout_preferred_stock,
proceeds_from_issuance_of_common_stock,
proceeds_from_issuance_of_long_term_debt_and_capital_securities_net,
proceeds_from_issuance_of_preferred_stock,
proceeds_from_repurchase_of_equity,
proceeds_from_sale_of_treasury_stock,
change_in_cash_and_cash_equivalents,
change_in_exchange_rate,
net_income)
VALUES (
(SELECT symbol_id FROM symbols WHERE ticker = '{self.symbol}'),
(SELECT source_id FROM sources WHERE title = '{self.source_title}'),
{report['reportedDate']},
(SELECT period_id FROM report_periods WHERE title = '{report['period']}'),
{report['fiscalDateEnding']},
{report['operatingCashflow']},
{report['paymentsForOperatingActivities']},
{report['proceedsFromOperatingActivities']},
{report['changeInOperatingLiabilities']},
{report['changeInOperatingAssets']},
{report['depreciationDepletionAndAmortization']},
{report['capitalExpenditures']},
{report['changeInReceivables']},
{report['changeInInventory']},
{report['profitLoss']},
{report['cashflowFromInvestment']},
{report['cashflowFromFinancing']},
{report['proceedsFromRepaymentsOfShortTermDebt']},
{report['paymentsForRepurchaseOfCommonStock']},
{report['paymentsForRepurchaseOfEquity']},
{report['paymentsForRepurchaseOfPreferredStock']},
{report['dividendPayout']},
{report['dividendPayoutCommonStock']},
{report['dividendPayoutPreferredStock']},
{report['proceedsFromIssuanceOfCommonStock']},
{report['proceedsFromIssuanceOfLongTermDebtAndCapitalSecuritiesNet']},
{report['proceedsFromIssuanceOfPreferredStock']},
{report['proceedsFromRepurchaseOfEquity']},
{report['proceedsFromSaleOfTreasuryStock']},
{report['changeInCashAndCashEquivalents']},
{report['changeInExchangeRate']},
{report['netIncome']});"""
try:
self.cur.execute(insert_report)
except self.Error as e:
raise FdataError(f"Can't add record to a table 'cash_flow': {e}\n\nThe query is\n{insert_report}") from e
self.commit()
self._update_intervals('cash_flow_max_ts', self._fundamental_intervals_tbl)
return(num_before, self.get_cash_flow_num())
def add_earnings(self, reports):
"""
Add earnings entries to the database.
Args:
quotes_dict(list of dictionaries): earnings entries obtained from an API wrapper.
Returns:
(int, int): total number of earnings reports before and after the operation.
Raises:
FdataError: sql error happened.
"""
self.check_if_connected()
# Insert new symbols to 'symbols' table (if the symbol does not exist)
if self.get_total_symbol_quotes_num() == 0:
self.add_symbol()
num_before = self.get_earnings_num()
for report in reports:
insert_report = f"""INSERT OR {self._update} INTO av_earnings (symbol_id,
source_id,
reported_date,
reported_period,
fiscal_date_ending,
reported_eps,
estimated_eps,
surprise,
surprise_percentage)
VALUES (
(SELECT symbol_id FROM symbols WHERE ticker = '{self.symbol}'),
(SELECT source_id FROM sources WHERE title = '{self.source_title}'),
{report['reportedDate']},
(SELECT period_id FROM report_periods WHERE title = '{report['period']}'),
{report['fiscalDateEnding']},
{report['reportedEPS']},
{report['estimatedEPS']},
{report['surprise']},
{report['surprisePercentage']});"""
try:
self.cur.execute(insert_report)
except self.Error as e:
raise FdataError(f"Can't add a record to a table 'av_earnings': {e}\n\nThe query is\n{insert_report}") from e
self.commit()
self._update_intervals('earnings_max_ts', 'av_earnings_intervals')
return(num_before, self.get_earnings_num())
def get_earnings(self):
"""
Fetch all the available earnings reports if needed.
Returns:
array: the fetched reports.
int: the number of fetched reports.
"""
return self._fetch_data_if_none(column='earnings_max_ts',
interval_table='av_earnings_intervals',
data_table='av_earnings',
num_method=self.get_earnings_num,
add_method=self.add_earnings,
fetch_method=self.fetch_earnings)
##########################
# Fetching-related methods
##########################
def get_timespan_str(self):
"""
Get the timespan.
Converts universal timespan to AlphaVantage timespan.
Raises:
FdataError: incorrect/unsupported timespan requested.
Returns:
str: timespan for AV query.
"""
if self.timespan == Timespans.Minute:
return '1min'
elif self.timespan == Timespans.FiveMinutes:
return '5min'
elif self.timespan == Timespans.FifteenMinutes:
return '15min'
elif self.timespan == Timespans.ThirtyMinutes:
return '30min'
elif self.timespan == Timespans.Hour:
return '60min'
else:
raise FdataError(f"Unsupported timespan: {self.timespan.value}")
# TODO LOW Think if it is ever needed
def is_intraday(self, timespan=None):
"""
Determine if the current timespan is intraday.
Args:
timespan(Timespan): timespan to override.
Returns:
bool: if the current timespan is intraday.
"""
if timespan is None:
timespan = self.timespan
if timespan in (Timespans.Minute,
Timespans.FiveMinutes,
Timespans.FifteenMinutes,
Timespans.ThirtyMinutes,
Timespans.Hour):
return True
elif timespan == Timespans.Day:
return False
else:
raise FdataError(f"Unsupported timespan: {timespan}")
def query_and_parse(self, url, timeout=30):
"""
Query the data source and parse the response.
Args:
url(str): the url for a request.
timeout(int): timeout for the request.
Returns:
Parsed data.
"""
response = self.query_api(url, timeout)
try:
json_data = response.json()
except json.decoder.JSONDecodeError as e:
raise FdataError(f"Can't parse JSON. Likely API key limit reached: {e}") from e
return json_data
def get_intraday_url(self, year, month):
"""
Get the url for an intraday query.
Args:
year(int): The year to get data
month(int): The month to get data
Returns(string): url for an intraday query
"""
output_size = 'compact' if self.compact else 'full'
url = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={self.symbol}&interval={self.get_timespan_str()}&outputsize={output_size}&adjusted=false&month={year}-{str(month).zfill(2)}&apikey={self.api_key}'
return url
def get_quote_json(self, url, json_key):
"""
Get quote json data.
Args:
url(string): url to get data
json_key(string); json key to get data.
Raises:
FdataError: no data obtained as likely API key limit is reached.
Returns:
dictionaries: quotes data and a header
"""
json_data = self.query_and_parse(url)
try:
dict_header = dict(json_data['Meta Data'].items())
dict_results = dict(sorted(json_data[json_key].items()))
except KeyError:
# It is possible that just there is not data yet for the current month
self.log(f"Can't get data for {self.symbol} using {self.source_title}. Likely API key limit or just no data for the requested period.")
return (None, None)
return (dict_results, dict_header)
def fetch_quotes(self, first_ts=None, last_ts=None):
"""
The method to fetch quotes.
Args:
first_ts(int): overridden first ts to fetch.
last_ts(int): overridden last ts to fetch.
Raises:
FdataError: incorrect API key(limit reached), http error happened, invalid timespan or no data obtained.
Returns:
list: quotes data
"""
quotes_data = []
# At first, need to set a function depending on a timespan.
if self.is_intraday():
json_key = f'Time Series ({self.get_timespan_str()})'
# Year and month
if first_ts is None:
first_date = self.first_date
else:
first_date = get_dt(first_ts, pytz.UTC)
if last_ts is None:
last_date = self.last_date
else:
last_date = get_dt(last_ts, pytz.UTC)
year = first_date.year
month = first_date.month
if year < 2000:
year = 2000
month = 1