-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindicators.py
120 lines (85 loc) · 3.3 KB
/
indicators.py
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
"""from preprocessing import getdataset
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
from sklearn.model_selection import train_test_split
import random
import pickle
classifier = RandomForestClassifier(n_estimators=100, criterion='gini', oob_score=True)
print(pickle_model.predict([[60]]))
#Price Rate of Change
#df50['Close Rateofchange'] = df50['Close'].transform(lambda x: x.pct_change(periods = 20))
#On Balance Volume
obv= []
obv.append(0)
for i in range(1, len(df50['Close'])):
if df50['Close'][i] > df50['Close'][i-1]:
obv.append( obv[-1] + df50['Volume'][i])
elif df50['Close'][i] < df50['Close'][i-1]:
obv.append( obv[-1] - df50['Volume'][i])
else:
obv.append(obv[-1])
df50['OBV'] = obv
#Volume weighted average price
close_mean = df50['Close'].mean()
print("Mean closing price: {:.2f} Rs".format(close_mean))
period = 20
vwap = []
for i in range(0,len(df50)):
if(i>=period):
numerator = sum(df50["Close"][i-period:i]*df50["Volume"][i-period:i])
denomenator = sum(df50["Volume"][i-period:i])
vwap.append(numerator/denomenator)
else:
vwap.append(None)
df50['VWAP'] = vwap
df50['Prediction Value'] = 0
for i in range(len(df50)):
if df50['Up Days'][i]>0:
df50['Prediction Value'][i] = 1
elif df50['Down Days'][i]>0:
df50['Prediction Value'][i] = 0
"""
"""y_pred = classifier.predict(x1_test)
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
print("Accuracy of the Classifier: {}\n".format(accuracy_score(y1_test, y_pred)))
print("Confusion Matrix:\n")
cm = confusion_matrix(y1_test, y_pred)
print(cm)
print("\n")
print("Classification Report:\n")
cr = classification_report(y1_test, y_pred)
print(cr)
pkl_filename = "pickle_model.pkl"
with open(pkl_filename, 'wb') as file:
pickle.dump(classifier, file)
def buildclassifier(df50, rsi):
df50['TrendValue'][0] = np.nan
upday, downday = df50.copy(), df50.copy()
upday.loc['TrendValue'] = upday.loc[(upday['TrendValue']<0), 'TrendValue' ] = 0
downday.loc['TrendValue'] = downday.loc[(downday['TrendValue']>0), 'TrendValue' ] = 0
downday['TrendValue'] = downday['TrendValue'].abs()
ewmup = upday['TrendValue'].transform(lambda x: x.ewm(span = 20).mean())
ewmdown = downday['TrendValue'].transform(lambda x: x.ewm(span = 20).mean())
relative_strength = ewmup/ewmdown
RSI = (100.0 - (1.0/100+relative_strength))
df50['Up Days'] = upday['TrendValue']
df50['Down Days'] = downday['TrendValue']
df50['RSI'] = RSI
df50['Prediction'] = np.nan
df = df50.copy()
df.drop(columns=['Prediction'], inplace=True)
df.dropna(how='any', inplace=True)
df50['Prediction Value'] = 0
for i in range(0, len(df50)):
if df50['Up Days'][i]>0:
df50['Prediction Value'][i] = 1
elif df50['Down Days'][i]>0:
df50['Prediction Value'][i] = 0
x = df[['RSI']]
y = df[['Prediction Value']]
x1_train, x1_test, y1_train, y1_test = train_test_split(x, y, test_size = 0.2, random_state=0)
classifier.fit(x1_train, y1_train.values.ravel())
return classifier.predict(rsi)
"""