-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMA.py
49 lines (36 loc) · 1.28 KB
/
MA.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
import pandas as pd
import numpy as np
from sklearn.model_selection import GridSearchCV, TimeSeriesSplit
from sklearn.metrics import mean_squared_error
def calculate_moving_average(data, window=20):
return data.rolling(window=window).mean()
class moving_average:
def __init__(self, window):
self.window = window
def fit(self, data):
self.data = data
return self
def predict(self):
return self.data.rolling(window=self.window).mean()
def scoring(self):
return mean_squared_error(self.data[self.window:], self.data.rolling(window=self.window).mean())
def get_params(self, deep=True):
return self.window
def set_params(self, window):
self.window = window
def main():
# file with stocks
df = pd.read_csv('stock_data.csv')
# convert index (datetime)
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)
# count MA
param_grid = {
'data': list(range(2, 51))
}
grid_model = GridSearchCV(moving_average(), param_grid=param_grid, cv=TimeSeriesSplit(n_splits=10))
grid_model.fit(df['Close'])
df['Moving Average'] = calculate_moving_average(df['Close'])
print(df)
if __name__ == "__main__":
main()