'Populate stock predictions in django database with models
I have a code which outputs the prediction of stocks. I've been trying to figure out how to populate the predictions into the database. Every time the code runs, it should modify the database with the new predictions.
This is my 'predictions.py':
import yfinance as yf
import numpy as np # Processing data as arrays
import pandas as pd # Used to create a dataframe that holds all data
from keras.models import load_model # Used to load existing models
import datetime
import joblib
from Dashboards.models import Predictions
def models_loader(folder, name, days = [1, 5, 30]):
model = []
for i in days:
model.append(load_model(folder+'/'+ name + '_' + str(i) + '.h5'))
return model
days = [1,5,14,30,90] #Day intervals
scaler = joblib.load('scaler.sav')
models = models_loader('ML Model','Model', days) #Load models
has_Run = False
companies = ['TSLA', 'AAPL','SIRI','GGB','PLUG']
while True:
print("Has started")
time = datetime.datetime.today()
schedule = datetime.time(17,0,0)
if time.hour == schedule.hour and has_Run==False: #change second time hour to schedule hour
last = datetime.date.today()
td = datetime.timedelta(100)
start = last - td
for symbols in companies:
print(symbols)
predINV= []
data = yf.download(symbols,start = start, end = last)
data = data.filter(['Close'])
if len(data.index) > 59:
INPUT = data.values
INPUT = INPUT[-60:]
scaled_input = scaler.fit_transform(INPUT)
scaled_input = np.reshape(scaled_input, (-1,60,1))
for i in range(len(days)):
pred = models[i].predict(scaled_input)
predINV.append(scaler.inverse_transform(pred))
predINV[i] = predINV[i].round(decimals = 2)
print(f'Day {days[i]}: {predINV[i]}')
pred_to_db = Predictions(symbol=symbols, day1=predINV[0], day5=predINV[1], day14=predINV[2], day30=predINV[3], day90=predINV[4])
pred_to_db.save()
stocks = Predictions.objects.all()
for stock in stocks:
stock.symbol = symbols
stock.day1 = predINV[0]
stock.day5 = predINV[1]
stock.day14 = predINV[2]
stock.day30 = predINV[3]
stock.day90 = predINV[4]
stock.save(update_fields=['symbol', 'day1', 'day5', 'day14', 'day30', 'day90'])
del data
has_Run = True
elif has_Run==True and time.hour != schedule.hour:
has_Run=False
(This is what I want to populate to DB. It should populate all the data and every time the code runs modify the rows with the new values.) Output of 'predictions.py' looks like this:
Has started
TSLA
[*********************100%***********************] 1 of 1 completed
Day 1: [[836.88]]
Day 5: [[836.88]]
Day 14: [[836.07]]
Day 30: [[835.21]]
Day 90: [[827.17]]
AAPL
[*********************100%***********************] 1 of 1 completed
Day 1: [[163.32]]
Day 5: [[163.32]]
Day 14: [[163.69]]
Day 30: [[163.57]]
Day 90: [[163.04]]
SIRI
[*********************100%***********************] 1 of 1 completed
Day 1: [[6.17]]
Day 5: [[6.17]]
Day 14: [[6.16]]
Day 30: [[6.15]]
Day 90: [[6.12]]
....
This is my 'models.py':
class Predictions(models.Model):
'''Predictions Table to maintain stock predictions'''
symbol = models.CharField(default='', max_length=1)
day1 = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
day5 = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
day14 = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
day30 = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
day90 = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
