Skip to content

Commit

Permalink
v2.11 - Nifty Prediction Bugfixed - TensorFlow model train/infer forc…
Browse files Browse the repository at this point in the history
…ed to CPU
  • Loading branch information
pranjal-joshi committed Nov 16, 2023
1 parent 3f7c879 commit 5a34151
Show file tree
Hide file tree
Showing 7 changed files with 1,022 additions and 848 deletions.
5 changes: 4 additions & 1 deletion src/classes/Changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from classes.ColorText import colorText

VERSION = "2.10"
VERSION = "2.11"

changelog = colorText.BOLD + '[ChangeLog]\n' + colorText.END + colorText.BLUE + '''
[1.00 - Beta]
Expand Down Expand Up @@ -253,5 +253,8 @@
[2.10]
1. Position Size Calculator added as a new tab
[2.11]
1. Nifty Prediction issue fixed - Model is now trained on CPU instead of Apple-M1 GPU
''' + colorText.END
17 changes: 14 additions & 3 deletions src/classes/Screener.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def validateLTP(self, data, screenDict, saveDict, minLTP=None, maxLTP=None):
data = data.replace([np.inf, -np.inf], 0)
recent = data.head(1)

pct_change = (data[::-1]['Close'].pct_change() * 100).iloc[-1]
pct_change = (data[::-1]['Close'].pct_change(fill_method=None) * 100).iloc[-1]
if pct_change > 0.2:
pct_change = colorText.GREEN + (" (%.1f%%)" % pct_change) + colorText.END
elif pct_change < -0.2:
Expand Down Expand Up @@ -598,12 +598,23 @@ def validateVCP(self, data, screenDict, saveDict, stockName=None, window=3, perc
def getNiftyPrediction(self, data, proxyServer):
import warnings
warnings.filterwarnings("ignore")
# Disable GPUs as this causes wrong preds in Docker
import tensorflow as tf
physical_devices = tf.config.list_physical_devices('GPU')
try:
tf.config.set_visible_devices([], 'GPU')
visible_devices = tf.config.get_visible_devices()
for device in visible_devices:
assert device.device_type != 'GPU'
except:
pass
#
model, pkl = Utility.tools.getNiftyModel(proxyServer=proxyServer)
with SuppressOutput(suppress_stderr=True, suppress_stdout=True):
data = data[pkl['columns']]
### v2 Preprocessing
for col in pkl['columns']:
data[col] = data[col].pct_change() * 100
data[col] = data[col].pct_change(fill_method=None) * 100
data = data.iloc[-1]
###
data = pkl['scaler'].transform([data])
Expand Down Expand Up @@ -697,7 +708,7 @@ def monitorFiveEma(self, proxyServer, fetcher, result_df, last_signal, risk_rewa
# Add data to vector database
def addVector(self, data, stockCode, daysToLookback):
data = data[::-1] # Reinverting preprocessedData for pct_change
data = data.pct_change()
data = data.pct_change(fill_method=None)
# data = data[::-1] # Do we need to invert again? No we dont - See operation after flatten
data = data[['Open', 'High', 'Low', 'Close']]
data = data.reset_index(drop=True)
Expand Down
12 changes: 12 additions & 0 deletions src/ml/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
import keras
import matplotlib.pyplot as plt

import tensorflow as tf
physical_devices = tf.config.list_physical_devices('GPU')
try:
# Disable all GPUS
tf.config.set_visible_devices([], 'GPU')
visible_devices = tf.config.get_visible_devices()
for device in visible_devices:
assert device.device_type != 'GPU'
except:
# Invalid device or cannot modify virtual devices once initialized.
pass

TEST_DAYS = 50
PERIOD = '5y'

Expand Down

0 comments on commit 5a34151

Please sign in to comment.