-
Notifications
You must be signed in to change notification settings - Fork 16
/
predictorGenerator.py
executable file
·45 lines (35 loc) · 1.4 KB
/
predictorGenerator.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
__author__ = 'kknet'
import numpy as np
import pandas as pd
import os
import statistics as stats
def symbol_to_path(symbol, base_dir=os.path.join(".", "data")):
"""Return CSV file path given ticker symbol."""
return os.path.join(base_dir, "{}.csv".format(str(symbol)))
def get_adj_closing(symbols, dates, addSPY=True):
"""Read stock data (adjusted close) for given symbols from CSV files."""
df = pd.DataFrame(index=dates)
for symbol in symbols:
df_temp = pd.read_csv(symbol_to_path(symbol), index_col='Date',
parse_dates=True, usecols=['Date', 'Adj Close'], na_values=['nan'])
df_temp = df_temp.rename(columns={'Adj Close': symbol})
df = df.join(df_temp)
df = df.dropna()
return df
def getData(symbols, dates):
price_df = get_adj_closing(symbols, dates)
Y_df = stats.Y(price_df).fillna(method='ffill')
BB_df = stats.BollingerBand(price_df).fillna(0)
V_df = stats.Volatility(price_df).fillna(0)
M_df = stats.Momentum(price_df).fillna(0)
total_rows = len(Y_df)
data = np.zeros(shape=(total_rows,4))
price_array = np.zeros(shape=(total_rows,1))
#prices = price_df.fillna(method='ffill')
for i in xrange(total_rows):
data[i,0]=BB_df.ix[i]
data[i,1]=V_df.ix[i]
data[i,2]=M_df.ix[i]
data[i,3]=Y_df.ix[i]
price_array[i,0] = price_df.ix[i]
return data , price_array , price_df