def indicator_table(self) -> TableData: yahoo = YahooData() s_list = [] for stock in self.stocks: df = yahoo[stock] out = self.func(df, *self.fargs, **self.fkwargs) series = pd.Series(data=out, index=df.index, name=stock) s_list.append(series) df = pd.concat(s_list, axis=1) return TableData(df)
def _convert_to_table(self, array: np.ndarray): df = pd.DataFrame(data=array, columns=self.stocks, index=self.indicator_table.df.index) return TableData(df)
STOCKS = np.array(STOCKS) def post1(df: pd.DataFrame): series = df[DF_ADJ_CLOSE] ts = TrailingStats(series, 252 * 5) stat1 = ts.exp_growth return stat1 yahoo.symbol_names = STOCKS indicator = Indicators(yahoo) indicator.create(post1) df = indicator.extract_column('post1()') table = TableData(df) index_spy = np.where(df.columns == 'SPY')[0][0] # %% class Strat1(Strategy): def init(self): # Build long and short rate metric self.growths = [] # Arbitrarily set initial stock. Doesn't matter. self.current_stocks = [] return def next(self):
def process(df: pd.DataFrame) -> (pd.DataFrame, pd.Series): """Extract features and targets for ML.""" col_names = df.keys() feature_names = [c for c in col_names if c.endswith(')')] features = df[feature_names] targets = df['avg_future_growth'] return features, targets features, targets = process(df) feature_names = features.keys() feature_table = TableData(features) target_table = TableData(targets) # %% Machine learning reg = RandomForestRegressor() def train(features, targets, reg): scaler_x = preprocessing.StandardScaler().fit(features.values) # scaler_y = preprocessing.StandardScaler().fit(targets.values[:, None]) features1 = scaler_x.transform(features) # targets1 = scaler_y.transform(targets.values[:, None]).ravel() # x_train, x_test, y_train, y_test = train_test_split(features1,