Files
AI-Stock-Trader/main.py
T

64 lines
2.0 KiB
Python

import sqlite3
from datetime import date, datetime
from datapuller import DataPuller
def main():
# Initilize SqLite
dbconn = sqlite3.connect("./data/appdata.db")
dbcursor = dbconn.cursor()
# Create keystore table if it doesnt exist already
dbcursor.execute('''
CREATE TABLE IF NOT EXISTS keystore (
key TEXT PRIMARY KEY,
value TEXT
)
''')
def SetKey(Key, Value):
dbcursor.execute('INSERT OR REPLACE INTO keystore (key, value) VALUES (?, ?)', (Key, Value))
dbconn.commit()
def GetKey(Key):
dbcursor.execute('SELECT value FROM keystore WHERE key = ?', (Key, ))
result = dbcursor.fetchone()
return result[0] if result else None
# Pull the last run data
lastrundata = GetKey("LastRun")
lastrun = None
if lastrundata != None:
lastrun = datetime.strptime( lastrundata, "%Y-%m-%d" )
else:
lastrun = datetime.strptime( "2000-01-01", "%Y-%m-%d" )
# If the strings dont match becuase were only checking day it has to be a new day so update the data
if lastrun != str(date.today()):
# This will update the data store in the data folder
DataPuller.pull()
# Update the last run to today
SetKey("LastRun", str(date.today()))
# Load in the AI algorithm late so yfinance works properly
import tensorflow as tf
import keras
from keras.layers import Dense, Flatten, Conv2D
from keras import Model
mnist = keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# Add a channels dimension
x_train = x_train[..., tf.newaxis].astype("float32")
x_test = x_test[..., tf.newaxis].astype("float32")
# batch and shuffle the dataset
train_ds = tf.data.Dataset.from_tensor_slices(
(x_train, y_train)).shuffle(10000).batch(32)
test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)
if __name__ == "__main__":
main()