Market-Direction-Predictor 0.1.0__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,149 @@
1
+ from sklearn.neural_network import MLPClassifier
2
+ from sklearn.metrics import accuracy_score,confusion_matrix
3
+ import numpy as np
4
+
5
+ def percent_change(data):return [100.0*(data[i]-data[i-1])/(data[i-1]) for i in range(1,len(data))]
6
+
7
+ class MDPC:
8
+ """use source closing price to predict next closing price UP/DOWN"""
9
+ def __init__(self,clf:MLPClassifier=MLPClassifier(),wl=7):
10
+ self.clf=clf
11
+ self.wl=wl
12
+ def train_predict(self,closing:list[float]):
13
+ """trains the model based on %change of closing. return tuple of accuracy,confusion matrix,next prediction probability"""
14
+ data=percent_change(closing)
15
+ assert len(data)==len(closing)-1
16
+ xdata=[]
17
+ ydata=[]
18
+ T=len(data)
19
+ for i in range(T-self.wl):
20
+ current_window=data[i:i+self.wl]
21
+ next_value=data[i+self.wl]
22
+ xdata.append(current_window)
23
+ ydata.append(int(next_value>0.0))
24
+
25
+ self.clf.fit(xdata,ydata)
26
+ ypred=self.clf.predict(xdata)
27
+ accuracy=accuracy_score(ydata,ypred)
28
+ cm=confusion_matrix(ydata,ypred)
29
+
30
+ lastwindow=data[T-self.wl:T]
31
+ predprob=self.clf.predict_proba([lastwindow])
32
+ return accuracy,cm,predprob[0]
33
+
34
+ class MDPHLC:
35
+ """use source HLC to predict next closing price UP/DOWN"""
36
+ def __init__(self,clf:MLPClassifier=MLPClassifier(),wl=7):
37
+ self.clf=clf
38
+ self.wl=wl
39
+ def train_predict(self,high:list[float],low:list[float],closing:list[float]):
40
+ """trains the model based on %change of HLC data. return tuple of accuracy,confusion matrix,next prediction probability"""
41
+ assert len(high) == len(low) == len(closing), "All input lists must have the same length."
42
+ data=np.array([
43
+ percent_change(high),
44
+ percent_change(low),
45
+ percent_change(closing),
46
+ ])
47
+ assert data.shape==(3,len(closing)-1)
48
+ xdata=[]
49
+ ydata=[]
50
+ T=data.shape[1]
51
+ for i in range(T-self.wl):
52
+ current_window=data[:,i:i+self.wl].flatten(order="F")
53
+ next_close=data[2,i+self.wl]
54
+ xdata.append(current_window)
55
+ ydata.append(int(next_close>0.0))
56
+
57
+ self.clf.fit(xdata,ydata)
58
+ ypred=self.clf.predict(xdata)
59
+ accuracy=accuracy_score(ydata,ypred)
60
+ cm=confusion_matrix(ydata,ypred)
61
+
62
+ lastwindow=data[:,T-self.wl:T].flatten(order="F")
63
+ predprob=self.clf.predict_proba([lastwindow])
64
+ return accuracy,cm,predprob[0]
65
+
66
+ class MDPMORE:
67
+ """use multiple source/indicators (maybe volume) to predict next closing price UP/DOWN"""
68
+ def __init__(self,clf:MLPClassifier=MLPClassifier(),wl=7):
69
+ self.clf=clf
70
+ self.wl=wl
71
+ def train_predict(self,sources:list):
72
+ """trains the model based on %change of sources. return tuple of accuracy,confusion matrix,next prediction probability. note that sources is a list of list[float]. the last list in sources is the source that needs to be predicted(closing price)"""
73
+ closing_length = len(sources[-1])
74
+ for src in sources:
75
+ assert len(src) == closing_length, "All sources must match the length of the closing prices."
76
+ data=np.array([
77
+ percent_change(src) for src in sources
78
+ ])
79
+ assert data.shape==(len(sources),len(sources[-1])-1)
80
+ xdata=[]
81
+ ydata=[]
82
+ T=data.shape[1]
83
+ for i in range(T-self.wl):
84
+ current_window=data[:,i:i+self.wl].flatten(order="F")
85
+ next_close=data[-1,i+self.wl]
86
+ xdata.append(current_window)
87
+ ydata.append(int(next_close>0.0))
88
+
89
+ self.clf.fit(xdata,ydata)
90
+ ypred=self.clf.predict(xdata)
91
+ accuracy=accuracy_score(ydata,ypred)
92
+ cm=confusion_matrix(ydata,ypred)
93
+
94
+ lastwindow=data[:,T-self.wl:T].flatten(order="F")
95
+ predprob=self.clf.predict_proba([lastwindow])
96
+ return accuracy,cm,predprob[0]
97
+
98
+ if __name__=="__main__": # simple tests
99
+ d=[1,2,3 ,2,3,4 ,3,4,5 ,4,5,6]
100
+ mdpc=MDPC(wl=2,clf=MLPClassifier())
101
+ ac,cm,npp=mdpc.train_predict(d)
102
+ print(ac)
103
+ print(cm)
104
+ print(npp)
105
+ assert npp[0]>npp[1]
106
+ d=[1,2,3 ,2,3,4 ,3,4,5 ,4,5]
107
+ mdpc=MDPC(wl=2,clf=MLPClassifier())
108
+ ac,cm,npp=mdpc.train_predict(d)
109
+ print(ac)
110
+ print(cm)
111
+ print(npp)
112
+ assert npp[0]<npp[1]
113
+ print("----------------------------------")
114
+ h=[1,2, 1,2, 1,2]
115
+ l=[2,1, 2,1, 2,1]
116
+ c=[3,2, 3,2, 3,2] # next is 3
117
+ mdphlc=MDPHLC(wl=2)
118
+ ac,cm,npp=mdphlc.train_predict(h,l,c)
119
+ print(ac)
120
+ print(cm)
121
+ print(npp)
122
+ assert npp[0]<npp[1]
123
+ h=[1,2, 1,2, 1,2, 1]
124
+ l=[2,1, 2,1, 2,1, 2]
125
+ c=[3,2, 3,2, 3,2, 3]
126
+ mdphlc=MDPHLC(wl=2)
127
+ ac,cm,npp=mdphlc.train_predict(h,l,c)
128
+ print(ac)
129
+ print(cm)
130
+ print(npp)
131
+ assert npp[0]>npp[1]
132
+ print("----------------------------------")
133
+ v=[1,2,3,4, 3,4,5,6, 5,6,7,8, ]
134
+ c=[4,3,2,1, 3,2,1,.5, 2,1,.5,.25, ] # pred should give 1
135
+ mdpmore=MDPMORE(wl=3)
136
+ ac,cm,npp=mdpmore.train_predict([v,c])
137
+ print(ac)
138
+ print(cm)
139
+ print(npp)
140
+ assert npp[0]<npp[1]
141
+ h=[1,2, 1,2, 1,2]
142
+ l=[2,1, 2,1, 2,1]
143
+ c=[3,2, 3,2, 3,2] # next is 3
144
+ mdphlc=MDPMORE(wl=2)
145
+ ac,cm,npp=mdphlc.train_predict([h,l,c])
146
+ print(ac)
147
+ print(cm)
148
+ print(npp)
149
+ assert npp[0]<npp[1]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: Market-Direction-Predictor
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: simple python code for predicting markets
5
5
  Author: navidpgg
6
6
  Description-Content-Type: text/markdown
@@ -1,5 +1,7 @@
1
1
  README.md
2
2
  setup.py
3
+ Market Direction Predictor/__init__.py
4
+ Market Direction Predictor/mdp.py
3
5
  Market_Direction_Predictor.egg-info/PKG-INFO
4
6
  Market_Direction_Predictor.egg-info/SOURCES.txt
5
7
  Market_Direction_Predictor.egg-info/dependency_links.txt
@@ -0,0 +1 @@
1
+ Market Direction Predictor
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: Market-Direction-Predictor
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: simple python code for predicting markets
5
5
  Author: navidpgg
6
6
  Description-Content-Type: text/markdown
@@ -7,7 +7,7 @@ long_description = (this_directory / "README.md").read_text()
7
7
 
8
8
  setup(
9
9
  name="Market-Direction-Predictor",
10
- version="0.1.0",
10
+ version="0.2.0",
11
11
  description="simple python code for predicting markets",
12
12
  author="navidpgg",
13
13
  packages=find_packages(),