forked from NerdyBurner/SAStocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
120 lines (109 loc) · 4.26 KB
/
database.py
File metadata and controls
120 lines (109 loc) · 4.26 KB
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
#Build as of 6.5.23
import sqlite3
from config import Config
import datetime
from typing import List
class Database:
def __init__(self):
self.connection = sqlite3.connect(Config.DATABASE_NAME)
self.create_tables()
self.cursor = self.connection.cursor()
def create_tables(self):
cursor = self.connection.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
summary TEXT,
source TEXT,
ticker TEXT,
timestamp DATETIME NOT NULL
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS sentiments (
id INTEGER PRIMARY KEY,
ticker TEXT NOT NULL,
score REAL NOT NULL,
timestamp DATETIME NOT NULL
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS financial_data (
id INTEGER PRIMARY KEY,
ticker TEXT NOT NULL,
rsi REAL,
macd REAL,
historical_price_high REAL,
historical_price_low REAL,
historical_price_avg REAL,
recent_price REAL,
vader_score REAL,
gpt3_score REAL,
final_score REAL,
timestamp DATETIME NOT NULL
)
''')
self.connection.commit()
def save_article(self, article):
try:
cursor = self.connection.cursor()
cursor.execute('''
INSERT INTO articles (title, summary, source, ticker, timestamp)
VALUES (?, ?, ?, ?, ?)
''', (article['title'], article['summary'], article.get('source'), article['ticker'], article['timestamp']))
self.connection.commit()
except Exception as e:
print(f"Error saving article: {str(e)}")
def save_sentiment(self, sentiment):
try:
cursor = self.connection.cursor()
cursor.execute('''
INSERT INTO sentiments (ticker, score, timestamp)
VALUES (?, ?, ?)
''', (sentiment['ticker'], sentiment['score'], sentiment['timestamp']))
self.connection.commit()
except Exception as e:
print(f"Error saving sentiment: {str(e)}")
def save_financial_data(self, ticker: str, rsi: float, macd: float, prices: List[float], final_score: float):
data = {
'ticker': ticker,
'rsi': rsi,
'macd': macd,
'close_price': prices[-1] if prices else None,
'final_score': final_score,
'date': datetime.date.today().isoformat(),
}
self.cursor.execute("""
INSERT INTO financial_data (ticker, rsi, macd, close_price, final_score, date)
VALUES (:ticker, :rsi, :macd, :close_price, :final_score, :date)
""", data)
self.connection.commit()
# Rest of the methods (get_article, get_sentiment, get_all_articles, get_all_sentiments, get_financial_data) go here...
def get_article(self, id):
cursor = self.connection.cursor()
cursor.execute('SELECT * FROM articles WHERE id = ?', (id,))
return cursor.fetchone()
def get_sentiment(self, id):
cursor = self.connection.cursor()
cursor.execute('SELECT * FROM sentiments WHERE id = ?', (id,))
return cursor.fetchone()
def get_all_articles(self):
cursor = self.connection.cursor()
cursor.execute('SELECT * FROM articles')
return cursor.fetchall()
def get_all_sentiments(self):
cursor = self.connection.cursor()
cursor.execute('SELECT * FROM sentiments')
return cursor.fetchall()
def get_financial_data(self, ticker):
cursor = self.connection.cursor()
cursor.execute('SELECT * FROM financial_data WHERE ticker = ?', (ticker,))
return cursor.fetchone()
def fetch_financial_report(self):
self.cursor.execute("""
SELECT ticker, recent_price, rsi, macd, vader_score, gpt3_score, final_score
FROM financial_data
ORDER BY final_score DESC
""")
return self.cursor.fetchall()