Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

news.py 3.8 KB

You have to be logged in to leave a comment. Sign In
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
  1. import os
  2. import requests
  3. import sqlalchemy as db
  4. import yfinance as yf
  5. import pandas as pd
  6. from sqlalchemy import text
  7. import time
  8. from ib_insync import *
  9. # importing module
  10. import time
  11. import openai
  12. import pandas as pd
  13. def create_db():
  14. # create database:
  15. engine = db.create_engine('sqlite:///atradebot.db', echo=True)
  16. connection = engine.connect()
  17. metadata = db.MetaData()
  18. # create a single table:
  19. news = db.Table('news', metadata,
  20. db.Column('id', db.Integer(), primary_key=True),
  21. db.Column('symbol', db.String(255), nullable=True),
  22. db.Column('title', db.String(255), nullable=True),
  23. db.Column('news_date', db.String(255), nullable=True),
  24. db.Column('url', db.String(255), nullable=True),
  25. db.Column('source', db.String(255), nullable=True),
  26. db.Column('text', db.String(255), nullable=True),
  27. )
  28. # create table in database:
  29. metadata.create_all(engine)
  30. return engine, connection, news
  31. if __name__ == "__main__":
  32. engine, connection, news = create_db()
  33. connection.execute(text("PRAGMA journal_mode=WAL"))
  34. # get list of stocks:
  35. stock_df = pd.read_excel('src/atradebot/SP_500_Companies.xlsx')
  36. symbols = stock_df['Symbol'].tolist()
  37. #connecting to ib_insync
  38. ib = IB()
  39. ib.connect('127.0.0.1', 7496, clientId=1)
  40. news_providers = ib.reqNewsProviders()
  41. codes = '+'.join(news_provider.code for news_provider in news_providers)
  42. # get data for symbols:
  43. for i in symbols[:5]:
  44. trans = connection.begin_nested()
  45. try:
  46. # Fetch and store news articles
  47. try:
  48. stock = Stock(i, 'SMART', 'USD')
  49. ib.qualifyContracts(stock)
  50. headlines = ib.reqHistoricalNews(stock.conId, codes, '', '', 100)
  51. for headline in headlines:
  52. article_date = headline.time.date()
  53. article = ib.reqNewsArticle(headline.providerCode, headline.articleId)
  54. # Insert the article into the database
  55. news_info = {
  56. 'symbol': i,
  57. 'title': '', # Title not needed
  58. 'news_date': str(article_date),
  59. 'url': '', # URL not provided
  60. 'source': '', # Source not provided
  61. 'text': article.articleText
  62. }
  63. # Insert the news into the database
  64. query_news = db.insert(news)
  65. ResultProxy = connection.execute(query_news, [news_info])
  66. # Error handling for news fetching
  67. except requests.exceptions.HTTPError as error_terminal:
  68. print("HTTPError while fetching news for symbol:", i)
  69. except Exception as e: # General exception catch
  70. print("Unexpected error while fetching news for symbol:", i)
  71. trans.commit()
  72. time.sleep(1)
  73. # Error handling
  74. except Exception as e: # General exception catch
  75. print("Unexpected error:", e)
  76. trans.rollback()
  77. # Fetch and print the first 5 stocks from the database after processing
  78. #query = db.select([data]).limit(5)
  79. query = news.select().limit(5)
  80. ResultProxy = connection.execute(query)
  81. ResultSet = ResultProxy.fetchall()
  82. i = 0
  83. for result in ResultSet:
  84. print(f"\n {i}")
  85. print(result)
  86. i = i+1
  87. connection.execute(text("PRAGMA journal_mode=WAL"))
  88. # Close the connection
  89. connection.close()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...