<filename>docs/report/fa20-523-332/project/predict.py
# -*- coding: utf-8 -*-
"""predict.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1N0m7piMn_VkQHWd9tuyFfaUhQmCoXRR9
"""

#!pip install dnspython
#!pip install yahoofinancials

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf
import datetime as dt
import keras
import pymongo
import datetime
import pickle
from pytz import timezone
from yahoofinancials import YahooFinancials
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler

PASSWORD = '#######'

CRYPTO_LIST = { 'bitcoin': 'BTC-USD', 'dash' : 'DASH-USD', 'ethereum' : 'ETH-USD', 'litecoin' : 'LTC-USD', 'monero' : 'XMR-USD',
                'ripple' : 'XRP-USD' }

FOREX_LIST = { 'AustralianDollar' : 'AUDUSD=X', 'Euro' : 'EURUSD=X', 'NewZealandDollar' : 'NZDUSD=X', 'BritishPound' : 'GBPUSD=X', 
              'BrazilianReal' : 'BRLUSD=X', 'CanadianDollar' : 'CADUSD=X', 'ChineseYuan' : 'CNYUSD=X', 'HongKongDollar' : 'HKDUSD=X', 
              'IndianRupee' : 'INRUSD=X', 'KoreanWon' : 'KRWUSD=X', 'MexicanPeso' : 'MXNUSD=X', 'SouthAfricanRand' : 'ZARUSD=X', 
              'SingaporeDollar' : 'SGDUSD=X', 'DanishKrone' : 'DKKUSD=X', 'JapaneseYen' : 'JPYUSD=X', 'MalaysianRinggit' : 'MYRUSD=X', 
              'NorwegianKrone' : 'NOKUSD=X', 'SwedishKrona' : 'SEKUSD=X', 'SriLankanRupee' : 'LKRUSD=X', 'SwissFranc' : 'CHFUSD=X', 
              'NewTaiwanDollar' : 'TWDUSD=X', 'ThaiBaht' : 'THBUSD=X' }

STOCK_INDICES = { 'S&P500' : '^GSPC', 'Dow30' : '^DJI', 'NASDAQ' : '^IXIC', 'Russel2000' : '^RUT', 'S&P/TSX' : '^GSPTSE', 
                  'IBOVESPA' : '^BVSP', 'IPCMEXICO' : '^MXX', 'Nikkei225' : '^N225', 'HANGSENGINDEX' : '^HSI', 
                  'SSE' : '000001.SS', 'ShenzhenComponent' : '399001.SZ', 'TSEC' : '^TWII', 'KOSPI' : '^KS11', 'STI' : '^STI', 
                  'Jakarta' : '^JKSE', 'FTSEBursaMalaysiaKLCI' : '^KLSE', 'S&P/ASX200' : '^AXJO',  'S&P/NZX50' : '^NZ50', 
                  'S&PBSE' : '^BSESN', 'FTSE100' : '^FTSE', 'DAX' : '^GDAXI', 'CAC40' : '^FCHI', 
                  'ESTX50' : '^STOXX50E', 'EURONEXT100' : '^N100', 'BEL 20' : '^BFX' }

def get_price(ticker):
  yf = YahooFinancials(ticker)
  today = str(dt.datetime.now(timezone('EST')))[:10]
  yesterday = str((dt.datetime.now(timezone('EST')) - dt.timedelta(days=1)))[:10]
  try:
    prices = yf.get_historical_price_data(yesterday, today, 'daily')
    price = prices[ticker]['prices'][0]['close']
  except KeyError:
    print(ticker)
    today = str((dt.datetime.now(timezone('EST')) - dt.timedelta(days=1)))[:10]
    yesterday = str((dt.datetime.now(timezone('EST')) - dt.timedelta(days=2)))[:10]
    prices = yf.get_historical_price_data(yesterday, today, 'daily')
    price = prices[ticker]['prices'][0]['close']
  return price

def get_Xtest():
  X_test = []
  for ticker in (list(STOCK_INDICES.values()) + list(FOREX_LIST.values())):
    X_test.append(get_price(ticker))
  norm_path = '/Normalizers/x_norm.pkl'
  with open(norm_path, 'rb') as f:
    x_normalizer = pickle.load(f)
  X_test = np.array(X_test, dtype=np.float32).reshape(1, len(X_test))
  X_test = x_normalizer.transform(X_test)
  return X_test

X_test = get_Xtest()

def get_predictions(X_test):
  model_path = '/Models/model.h5'
  model = keras.models.load_model(model_path)
  y_norm_path = '/Normalizers/y_norm.pkl'
  with open(y_norm_path, 'rb') as f:
    y_normalizer = pickle.load(f)
  predictions = y_normalizer.inverse_transform(model.predict(X_test))
  return predictions

def get_true_value():
  true_vals = []
  for crypto in CRYPTO_LIST.keys():
    yf = YahooFinancials(CRYPTO_LIST[crypto])
    today = str((dt.datetime.now(timezone('EST')) + dt.timedelta(days=1)))[:10]
    yesterday = str((dt.datetime.now(timezone('EST')) - dt.timedelta(days=0)))[:10]
    prices = yf.get_historical_price_data(yesterday, today, 'daily')
    price = prices[CRYPTO_LIST[crypto]]['prices'][0]['open']
    true_vals.append(price)
  return true_vals

predictions = get_predictions(X_test)
true_value = get_true_value()
print(predictions, true_value)

plt.scatter(range(len(predictions)), predictions, label='predicted')
plt.scatter(range(len(true_value)), true_value, label='true')
plt.xticks(np.arange(len(true_value)), labels=list(CRYPTO_LIST.keys()))
plt.legend()
plt.show()

"""# Analysis"""

def get_nn_data():
  client = pymongo.MongoClient("mongodb+srv://kmhatre:" + PASSWORD + "@crypto.<EMAIL>.mongodb.net/" + 'nn_data' + "?retryWrites=true&w=majority")
  db = client['nn_data']
  col = db['nn_data']
  result = []
  for x in col.find():
    result.append(x)
  df = pd.DataFrame(result)
  df = df.drop(['_id'], axis=1)  	
  return df

X_test = (get_nn_data()[list(STOCK_INDICES.keys()) + list(FOREX_LIST.keys())][-101:-1]).to_numpy(dtype=np.float32)
Y_test = (get_nn_data()[-100:]).to_numpy(dtype=np.float32)
norm_path = '/Normalizers/x_norm.pkl'
with open(norm_path, 'rb') as f:
  x_normalizer = pickle.load(f)
X_test = x_normalizer.transform(X_test)

model_path = '/Models/model.h5'
model = keras.models.load_model(model_path)
y_norm_path = '/Normalizers/y_norm.pkl'
with open(y_norm_path, 'rb') as f:
  y_normalizer = pickle.load(f)
predictions = y_normalizer.inverse_transform(model.predict(X_test))

plt.plot(predictions[:, 0])
plt.plot(Y_test[:, 0])
<filename>docs/report/fa20-523-332/project/predict.py
# -*- coding: utf-8 -*-
"""predict.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1N0m7piMn_VkQHWd9tuyFfaUhQmCoXRR9
"""

#!pip install dnspython
#!pip install yahoofinancials

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf
import datetime as dt
import keras
import pymongo
import datetime
import pickle
from pytz import timezone
from yahoofinancials import YahooFinancials
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler

PASSWORD = '#######'

CRYPTO_LIST = { 'bitcoin': 'BTC-USD', 'dash' : 'DASH-USD', 'ethereum' : 'ETH-USD', 'litecoin' : 'LTC-USD', 'monero' : 'XMR-USD',
                'ripple' : 'XRP-USD' }

FOREX_LIST = { 'AustralianDollar' : 'AUDUSD=X', 'Euro' : 'EURUSD=X', 'NewZealandDollar' : 'NZDUSD=X', 'BritishPound' : 'GBPUSD=X', 
              'BrazilianReal' : 'BRLUSD=X', 'CanadianDollar' : 'CADUSD=X', 'ChineseYuan' : 'CNYUSD=X', 'HongKongDollar' : 'HKDUSD=X', 
              'IndianRupee' : 'INRUSD=X', 'KoreanWon' : 'KRWUSD=X', 'MexicanPeso' : 'MXNUSD=X', 'SouthAfricanRand' : 'ZARUSD=X', 
              'SingaporeDollar' : 'SGDUSD=X', 'DanishKrone' : 'DKKUSD=X', 'JapaneseYen' : 'JPYUSD=X', 'MalaysianRinggit' : 'MYRUSD=X', 
              'NorwegianKrone' : 'NOKUSD=X', 'SwedishKrona' : 'SEKUSD=X', 'SriLankanRupee' : 'LKRUSD=X', 'SwissFranc' : 'CHFUSD=X', 
              'NewTaiwanDollar' : 'TWDUSD=X', 'ThaiBaht' : 'THBUSD=X' }

STOCK_INDICES = { 'S&P500' : '^GSPC', 'Dow30' : '^DJI', 'NASDAQ' : '^IXIC', 'Russel2000' : '^RUT', 'S&P/TSX' : '^GSPTSE', 
                  'IBOVESPA' : '^BVSP', 'IPCMEXICO' : '^MXX', 'Nikkei225' : '^N225', 'HANGSENGINDEX' : '^HSI', 
                  'SSE' : '000001.SS', 'ShenzhenComponent' : '399001.SZ', 'TSEC' : '^TWII', 'KOSPI' : '^KS11', 'STI' : '^STI', 
                  'Jakarta' : '^JKSE', 'FTSEBursaMalaysiaKLCI' : '^KLSE', 'S&P/ASX200' : '^AXJO',  'S&P/NZX50' : '^NZ50', 
                  'S&PBSE' : '^BSESN', 'FTSE100' : '^FTSE', 'DAX' : '^GDAXI', 'CAC40' : '^FCHI', 
                  'ESTX50' : '^STOXX50E', 'EURONEXT100' : '^N100', 'BEL 20' : '^BFX' }

def get_price(ticker):
  yf = YahooFinancials(ticker)
  today = str(dt.datetime.now(timezone('EST')))[:10]
  yesterday = str((dt.datetime.now(timezone('EST')) - dt.timedelta(days=1)))[:10]
  try:
    prices = yf.get_historical_price_data(yesterday, today, 'daily')
    price = prices[ticker]['prices'][0]['close']
  except KeyError:
    print(ticker)
    today = str((dt.datetime.now(timezone('EST')) - dt.timedelta(days=1)))[:10]
    yesterday = str((dt.datetime.now(timezone('EST')) - dt.timedelta(days=2)))[:10]
    prices = yf.get_historical_price_data(yesterday, today, 'daily')
    price = prices[ticker]['prices'][0]['close']
  return price

def get_Xtest():
  X_test = []
  for ticker in (list(STOCK_INDICES.values()) + list(FOREX_LIST.values())):
    X_test.append(get_price(ticker))
  norm_path = '/Normalizers/x_norm.pkl'
  with open(norm_path, 'rb') as f:
    x_normalizer = pickle.load(f)
  X_test = np.array(X_test, dtype=np.float32).reshape(1, len(X_test))
  X_test = x_normalizer.transform(X_test)
  return X_test

X_test = get_Xtest()

def get_predictions(X_test):
  model_path = '/Models/model.h5'
  model = keras.models.load_model(model_path)
  y_norm_path = '/Normalizers/y_norm.pkl'
  with open(y_norm_path, 'rb') as f:
    y_normalizer = pickle.load(f)
  predictions = y_normalizer.inverse_transform(model.predict(X_test))
  return predictions

def get_true_value():
  true_vals = []
  for crypto in CRYPTO_LIST.keys():
    yf = YahooFinancials(CRYPTO_LIST[crypto])
    today = str((dt.datetime.now(timezone('EST')) + dt.timedelta(days=1)))[:10]
    yesterday = str((dt.datetime.now(timezone('EST')) - dt.timedelta(days=0)))[:10]
    prices = yf.get_historical_price_data(yesterday, today, 'daily')
    price = prices[CRYPTO_LIST[crypto]]['prices'][0]['open']
    true_vals.append(price)
  return true_vals

predictions = get_predictions(X_test)
true_value = get_true_value()
print(predictions, true_value)

plt.scatter(range(len(predictions)), predictions, label='predicted')
plt.scatter(range(len(true_value)), true_value, label='true')
plt.xticks(np.arange(len(true_value)), labels=list(CRYPTO_LIST.keys()))
plt.legend()
plt.show()

"""# Analysis"""

def get_nn_data():
  client = pymongo.MongoClient("mongodb+srv://kmhatre:" + PASSWORD + "@crypto.<EMAIL>.mongodb.net/" + 'nn_data' + "?retryWrites=true&w=majority")
  db = client['nn_data']
  col = db['nn_data']
  result = []
  for x in col.find():
    result.append(x)
  df = pd.DataFrame(result)
  df = df.drop(['_id'], axis=1)  	
  return df

X_test = (get_nn_data()[list(STOCK_INDICES.keys()) + list(FOREX_LIST.keys())][-101:-1]).to_numpy(dtype=np.float32)
Y_test = (get_nn_data()[-100:]).to_numpy(dtype=np.float32)
norm_path = '/Normalizers/x_norm.pkl'
with open(norm_path, 'rb') as f:
  x_normalizer = pickle.load(f)
X_test = x_normalizer.transform(X_test)

model_path = '/Models/model.h5'
model = keras.models.load_model(model_path)
y_norm_path = '/Normalizers/y_norm.pkl'
with open(y_norm_path, 'rb') as f:
  y_normalizer = pickle.load(f)
predictions = y_normalizer.inverse_transform(model.predict(X_test))

plt.plot(predictions[:, 0])
plt.plot(Y_test[:, 0])
# -*- coding: utf-8 -*- predict.ipynb  Automatically generated by Colaboratory.  Original file is located at     https://colab.research.google.com/drive/1N0m7piMn_VkQHWd9tuyFfaUhQmCoXRR9 #!pip install dnspython #!pip install yahoofinancials ######' # Analysis