def update_price(ticker, interval, size, chk_for_duplicate=True): count_duplicate = 0 count_create = 0 df_prices = av.get_prices(cfg.AV_APIKEY, ticker, interval, size) if chk_for_duplicate: for i in range(0, len(df_prices)): if not pg.is_price_duplicate(ticker, interval, df_prices[pg._COL_DATETIME][i]): print( f'{ticker} ({interval}) price on {str(df_prices[pg._COL_DATETIME][i])} created : {df_prices.iloc[i, :].values}' ) pg.create_prices([df_prices.iloc[i, :].values]) count_create += 1 else: print( f'Duplicate {ticker} ({interval}) price on {str(df_prices[pg._COL_DATETIME][i])}' ) count_duplicate += 1 else: # To be used for batch imports or initialization pg.create_prices(df_prices.values) return count_create, count_duplicate
def update_price(ticker, interval, size): count_duplicate = 0 count_create = 0 df_prices = av.get_prices(cfg.AV_APIKEY, ticker, interval, size) for i in range(0, len(df_prices)): if not pg.is_price_duplicate(ticker, interval, df_prices[pg._COL_DATETIME][i]): print( f'{ticker} ({interval}) price on {str(df_prices[pg._COL_DATETIME][i])} created : {df_prices.iloc[i, :].values}' ) pg.create_prices([df_prices.iloc[i, :].values]) count_create += 1 else: print( f'Duplicate {ticker} ({interval}) price on {str(df_prices[pg._COL_DATETIME][i])}' ) count_duplicate += 1 return count_create, count_duplicate
def get_prices_with_features(ticker, interval, start, end, limit): if ticker is None: return 'Missing parameter: ticker' if interval is None: return 'Missing parameter: interval' if start is not None: error = is_datetime_string(start) if error is not None: return error else: return 'Missing parameter: ' + _PARAM_START if end is not None: error = is_datetime_string(end) if error is not None: return error if limit is not None: error = is_integer_string(limit) if error is not None: return error prices = pg.get_prices_with_features(ticker, interval, start, end, limit) return prices.to_html()
def get_prices_from_database(ticker, interval, start, end, limit): if ticker is None: return 'Missing parameter: ' + _PARAM_TICKER if interval is None: return 'Missing parameter: ' + _PARAM_INTERVAL if start is not None: error = is_datetime_string(start) if error is not None: return error else: return 'Missing parameter: ' + _PARAM_START if end is not None: error = is_datetime_string(end) if error is not None: return error if limit is not None: error = is_integer_string(limit) if error is not None: return error prices = pg.get_prices(ticker, interval, start, end, limit) return prices.to_html()
def update_date_features(ticker, interval, start_date, end_date): price_dates = pg.get_price_dates(ticker, interval, start_date, end_date) if start_date is None and end_date is None: print(f'{len(price_dates)} {ticker} ({interval}) prices found.') elif start_date is not None and end_date is None: print( f'{len(price_dates)} {ticker} ({interval}) prices on {start_date} found.' ) elif start_date is not None and end_date is not None: print( f'{len(price_dates)} {ticker} ({interval}) prices between [{start_date}, {end_date}] found.' ) features_array = [] count_duplicate = 0 count_create = 0 for i in range(0, len(price_dates)): if pg.get_features_by_price_id(price_dates[i][0]) is None: features = fe.get_date_features(price_dates[i][1]) print( f'{ticker} ({interval}) features on {price_dates[i][1]} created: {features}' ) features_array.append([price_dates[i][0], \ features[0], features[1], features[2], \ features[3], features[4], features[5], \ int(features[6][0]), int(features[6][1]), int(features[7][0]), int(features[7][1]), \ int(features[8][0]), int(features[8][1]), int(features[9][0]), int(features[9][0])]) count_create += 1 else: print( f'Duplicate {ticker} ({interval}) features on {price_dates[i][1]}' ) count_duplicate += 1 pg.create_features(features_array) return count_create, count_duplicate
def get_symbol(ticker): if ticker is None: return 'Missing parameter: ' + _PARAM_TICKER name = pg.get_symbol_name(ticker) if name is None: return f'Symbol with ticker {ticker} NOT found!' else: return name[0]
def get_all_symbols(): symbols = pg.get_symbols() if symbols is None: return 'No symbols found!' else: df = pd.DataFrame(symbols, columns=[pg._COL_NAME, pg._COL_TICKER]) print(df.info()) return df.to_html()
def get_features(ticker, interval, start, end): if ticker is None: return 'Missing parameter: ' + _PARAM_TICKER if interval is None: return 'Missing parameter: ' + _PARAM_INTERVAL if start is not None: error = is_datetime_string(start) if error is not None: return error if end is not None: error = is_datetime_string(end) if error is not None: return error df_features = pg.get_features_by_datetime(ticker, interval, start, end) return pd.DataFrame(df_features).to_html()
import dash_core_components as dcc import dash_html_components as html from dash.dependencies import Output, Input import random import plotly import plotly.graph_objs as go from collections import deque import utils.AlphaVantageUtils as av import utils.PostgresUtils as pg df_prices = pg.get_prices(av._TIC_MICROSOFT, av._INT_DAILY) cols = [pg._COL_OPEN, pg._COL_HIGH, pg._COL_LOW, pg._COL_CLOSE] arr_ohlc = df_prices.iloc[0:20, ][cols].values arr_date = df_prices.iloc[0:20, ][pg._COL_DATETIME].values X = deque(arr_ohlc, maxlen=20) Y = deque(arr_date, maxlen=20) next_price_idx = 20 max_price_idx = len(df_prices) app = dash.Dash(__name__) app.layout = html.Div([ dcc.Graph(id='live-graph', animate=True),
import numpy as np import pandas as pd import dash import dash_core_components as dcc import dash_html_components as html from sklearn.linear_model import LinearRegression import utils.AlphaVantageUtils as av import utils.PostgresUtils as pg import utils.ModelUtils as mdl name = pg.get_symbol_name(av._TIC_MICROSOFT) df_prices = pg.get_prices_with_features(av._TIC_MICROSOFT, av._INT_DAILY, None, None, None) df_prices.drop(columns=['open', 'high', 'low', 'volume'], inplace=True) print(df_prices.info()) df_train, df_test = mdl.train_test_split(df_prices, 1000) predictions = [] train = df_train.drop(pg._COL_DATETIME, axis=1) test = df_test.drop(pg._COL_DATETIME, axis=1) print(train.shape) print(test.shape)
def get_symbol_options(): return [{_OPT_LABEL: symbol[0], _OPT_VALUE: symbol[1]} for symbol in pg.get_symbols()]
import numpy as np import dash import dash_core_components as dcc import dash_html_components as html import plotly.graph_objects as go import utils.AlphaVantageUtils as av import utils.PostgresUtils as pg import utils.ModelUtils as mdl name = pg.get_symbol_name(av._TIC_MICROSOFT) df_prices = pg.get_prices(av._TIC_MICROSOFT, av._INT_DAILY) df_train, df_test = mdl.train_test_split(df_prices, 1000) print(len(df_prices), len(df_train), len(df_test)) print(min(df_train[pg._COL_DATETIME]), max(df_train[pg._COL_DATETIME])) print(min(df_test[pg._COL_DATETIME]), max(df_test[pg._COL_DATETIME])) predictions = [] def baseline(train, test): print('\n Shape of training set:') print(train.shape) print('\n Shape of testing set:') print(test.shape)