File size: 3,917 Bytes
9bf26a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import yfinance as yf
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.compose import ColumnTransformer
import joblib
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'

INCLUDE_COMMODITIES = True

def preprocessBeforeScaling(df):
    df['High'] = df['High'].pct_change() * 100
    df['Low'] = df['Low'].pct_change() * 100
    df['Open'] = df['Open'].pct_change() * 100
    df['Close'] = df['Close'].pct_change() * 100 

    if INCLUDE_COMMODITIES:
        df['gold_High'] = df['gold_High'].pct_change() * 100
        df['gold_Low'] = df['gold_Low'].pct_change() * 100
        df['gold_Open'] = df['gold_Open'].pct_change() * 100
        df['gold_Close'] = df['gold_Close'].pct_change() * 100

        df['crude_High'] = df['crude_High'].pct_change() * 100
        df['crude_Low'] = df['crude_Low'].pct_change() * 100
        df['crude_Open'] = df['crude_Open'].pct_change() * 100
        df['crude_Close'] = df['crude_Close'].pct_change() * 100
    return df

metrics = {
    "TP": 0, "FP": 0, "TN": 0, "FN": 0
}

endpoint = keras.models.load_model('nifty_model_v3.h5')
try:
    scaler
except NameError:
    pkl = joblib.load('nifty_model_v3.pkl')
    scaler = pkl['scaler']
today = yf.download(
                tickers="^NSEI",
                period=f'{TEST_DAYS}d',
                interval='1d',
                progress=False,
                timeout=10
            )
if INCLUDE_COMMODITIES:
    gold = yf.download(
                    tickers="GC=F",
                    period=f'{TEST_DAYS}d',
                    interval='1d',
                    progress=False,
                    timeout=10
                ).add_prefix(prefix='gold_')
    crude = yf.download(
                tickers="CL=F",
                period=f'{TEST_DAYS}d',
                interval='1d',
                progress=False,
                timeout=10
            ).add_prefix(prefix='crude_')

    today = pd.concat([today, gold, crude], axis=1)
    today = today.drop(columns=['Adj Close', 'Volume', 'gold_Adj Close', 'gold_Volume', 'crude_Adj Close', 'crude_Volume'])
else:
    today = today.drop(columns=['Adj Close', 'Volume'])

###
today = preprocessBeforeScaling(today)
today = today.drop(columns=['gold_Open', 'gold_High', 'gold_Low', 'crude_Open', 'crude_High', 'crude_Low'])
###

cnt_correct, cnt_wrong = 0, 0
for i in range(-TEST_DAYS,0):
    df = today.iloc[i]
    twr = today.iloc[i+1]['Close']
    df = scaler.transform([df])
    pred = endpoint.predict([df], verbose=0)

    if twr > today.iloc[i]['Open']:
        fact = "BULLISH"
    else:
        fact = "BEARISH"

    if pred > 0.5:
        out = "BEARISH"
    else:
        out = "BULLISH"

    if out == fact:
        cnt_correct += 1
        if out == "BULLISH":
            metrics["TP"] += 1
        else:
            metrics["TN"] += 1
    else:
        cnt_wrong += 1
        if out == "BULLISH":
            metrics["FN"] += 1
        else:
            metrics["FP"] += 1

        
    print("{} Nifty Prediction -> Market may Close {} on {}! Actual -> {}, Prediction -> {}, Pred = {}".format(
            today.iloc[i].name.strftime("%d-%m-%Y"),
            out,
            (today.iloc[i].name + pd.Timedelta(days=1)).strftime("%d-%m-%Y"),
            fact,
            "Correct" if fact == out else "Wrong",
            str(np.round(pred[0][0], 2))
            )
        )

print("Correct: {}, Wrong: {}, Accuracy: {}".format(cnt_correct, cnt_wrong, cnt_correct/(cnt_correct+cnt_wrong)))
print(metrics)