Setup dotnet wrapper around Python AI project for UI
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,27 @@
|
||||
import sqlite3
|
||||
|
||||
class DataBase:
|
||||
|
||||
def __init__(self):
|
||||
# Initilize SqLite
|
||||
self.conn = sqlite3.connect("./data/appdata.db")
|
||||
self.cursor = self.conn.cursor()
|
||||
|
||||
# Create keystore table if it doesnt exist already
|
||||
self.cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS keystore (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT
|
||||
)
|
||||
''')
|
||||
|
||||
# Set a key in the keystore
|
||||
def SetKey(self, Key, Value):
|
||||
self.cursor.execute('INSERT OR REPLACE INTO keystore (key, value) VALUES (?, ?)', (Key, Value))
|
||||
self.conn.commit()
|
||||
|
||||
# Get a key from the keystore
|
||||
def GetKey(self, Key):
|
||||
self.cursor.execute('SELECT value FROM keystore WHERE key = ?', (Key, ))
|
||||
result = self.cursor.fetchone()
|
||||
return result[0] if result else None
|
||||
@@ -0,0 +1,53 @@
|
||||
import yfinance as yf
|
||||
import pandas as pd
|
||||
|
||||
class DataPuller:
|
||||
|
||||
@staticmethod
|
||||
def pull():
|
||||
# Import the S&P 500 symbols
|
||||
symbols = pd.read_excel("./data/stock_symbols.xlsx")
|
||||
symbols.columns = symbols.columns.str.strip()
|
||||
tickers = symbols['Symbol'].tolist()
|
||||
|
||||
# Scrape the data
|
||||
all_data = []
|
||||
for i, symbol in enumerate(tickers): # Try first 20
|
||||
print(f"Processing: {i} of {len(tickers)}")
|
||||
df = yf.download(symbol, period="max", auto_adjust=True)
|
||||
if not df.empty:
|
||||
# Remove the ticker column
|
||||
df.columns = df.columns.get_level_values(0)
|
||||
|
||||
# Make sure Date is actually a Date Object
|
||||
df = df.reset_index()
|
||||
df['Date'] = pd.to_datetime(df['Date'], format="%Y-%m-%d")
|
||||
df.set_index('Date', inplace=True)
|
||||
|
||||
# Add the Symbol column for tracking
|
||||
df['Symbol'] = symbol
|
||||
|
||||
# Add feature Spread
|
||||
df['Spread'] = abs( df['High'] - df['Low'] )
|
||||
|
||||
# Add feature for Returns
|
||||
df['Return'] = df['Close'].pct_change()
|
||||
|
||||
# Add feature for volitility last 5
|
||||
df['Volatility_5'] = df['Return'].transform(lambda x: x.rolling(5).std())
|
||||
|
||||
# Add feature for volitility last 20
|
||||
df['Volatility_20'] = df['Return'].transform(lambda x: x.rolling(20).std())
|
||||
|
||||
all_data.append(df)
|
||||
|
||||
# Concatinate into a combined list and cache
|
||||
print("Processing data")
|
||||
final_df = pd.concat(all_data)
|
||||
|
||||
# Drop rows with null values
|
||||
final_df.dropna(inplace=True)
|
||||
|
||||
print("Writing data to file")
|
||||
final_df.to_parquet("./data/stocks.parquet")
|
||||
final_df.head(200).to_csv("./data/stocks_preview.csv")
|
||||
@@ -0,0 +1,39 @@
|
||||
from datetime import date, datetime
|
||||
from datapuller import DataPuller
|
||||
from database import DataBase
|
||||
|
||||
def main():
|
||||
|
||||
# Load the database object
|
||||
db = DataBase()
|
||||
|
||||
# If we havent already pulled the stock data today
|
||||
if db.GetKey("LastRun") != str(date.today()):
|
||||
# Update the data store in the data folder
|
||||
DataPuller.pull()
|
||||
# Update the last run to today
|
||||
db.SetKey("LastRun", str(date.today()))
|
||||
|
||||
# Lazy Load in the AI algorithms 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()
|
||||
Reference in New Issue
Block a user