Added stocks plugin
This commit is contained in:
@ -22,6 +22,9 @@ class RoborPlugin(Plugin):
|
||||
def __init__(self):
|
||||
self.__table = None
|
||||
|
||||
def get_interval(self):
|
||||
return config.ROBOR_INTERVAL
|
||||
|
||||
def get_column_index(self, table, column_name):
|
||||
header_row = table.find('tr')
|
||||
for elem in header_row.iter('th'):
|
||||
@ -74,7 +77,6 @@ class RoborPlugin(Plugin):
|
||||
entry.field = field
|
||||
entry.value = value
|
||||
entry.save()
|
||||
print(model_to_dict(entry))
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,56 @@
|
||||
import datetime
|
||||
|
||||
from peewee import *
|
||||
from playhouse.shortcuts import model_to_dict
|
||||
|
||||
import config
|
||||
from database import BaseModel
|
||||
from plugins.plugin import Plugin
|
||||
import yfinance as yf
|
||||
|
||||
|
||||
class Stocks(BaseModel):
|
||||
date = DateTimeField(index=True, default=datetime.datetime.now(), null=False)
|
||||
ticker = TextField(null=False)
|
||||
label = TextField(null=False)
|
||||
value_open = FloatField(null=False)
|
||||
value_close = FloatField(null=False)
|
||||
value_high = FloatField(null=False)
|
||||
value_low = FloatField(null=False)
|
||||
|
||||
|
||||
class StocksPlugin(Plugin):
|
||||
models = [Stocks]
|
||||
|
||||
def get_interval(self):
|
||||
return config.STOCKS_INTERVAL
|
||||
|
||||
def execute(self):
|
||||
for ticker, label in config.STOCKS_TICKERS.items():
|
||||
# Get last existing date
|
||||
latest_date = Stocks.select(Stocks.date) \
|
||||
.order_by(Stocks.date.desc()) \
|
||||
.limit(1) \
|
||||
.scalar()
|
||||
|
||||
try:
|
||||
yfticker = yf.Ticker(ticker)
|
||||
|
||||
if latest_date is None:
|
||||
data = yfticker.history(period='max')
|
||||
else:
|
||||
data = yfticker.history(start=latest_date + datetime.timedelta(seconds=1))
|
||||
|
||||
for row in data.itertuples():
|
||||
entry = Stocks()
|
||||
entry.date = row.Index.to_pydatetime()
|
||||
entry.ticker = ticker
|
||||
entry.label = label
|
||||
entry.value_open = row.Open
|
||||
entry.value_close = row.Close
|
||||
entry.value_high = row.High
|
||||
entry.value_low = row.Low
|
||||
entry.save()
|
||||
print(model_to_dict(entry))
|
||||
except BaseException as e:
|
||||
print(e)
|
Reference in New Issue
Block a user