2020-04-17 16:16:58 +00:00
|
|
|
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):
|
2020-04-18 20:52:27 +00:00
|
|
|
date = DateTimeField(index=True, default=datetime.datetime.utcnow, null=False)
|
2020-04-17 16:16:58 +00:00
|
|
|
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) \
|
2020-04-18 20:52:27 +00:00
|
|
|
.where(Stocks.ticker == ticker) \
|
2020-04-17 16:16:58 +00:00
|
|
|
.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()
|
2020-04-18 20:52:27 +00:00
|
|
|
|
2020-04-17 16:16:58 +00:00
|
|
|
except BaseException as e:
|
|
|
|
print(e)
|