def get_atr(input_df, params=None): n = params["n"] # Set temp dataframe keys h_l_key = Ct.get_key("H-L") h_pc_key = Ct.get_key("H-PC") l_pc_key = Ct.get_key("L-PC") tr_key = Ct.get_key("TR") df_data = input_df.copy() df_data.columns = df_data.columns.droplevel(1) df_data[h_l_key] = abs(df_data[Ct.high_key()] - df_data[Ct.low_key()]) df_data[h_pc_key] = abs(df_data[Ct.high_key()] - df_data[Ct.prices_key()].shift(1)) df_data[l_pc_key] = abs(df_data[Ct.low_key()] - df_data[Ct.prices_key()].shift(1)) df_data[tr_key] = df_data[[h_l_key, h_pc_key, l_pc_key]].max(axis=1, skipna=False) df_data[Ct.atr_key()] = df_data[tr_key].rolling(n).mean() # df[Ct.atr_key()] = df[tr_key].ewm(span=n,adjust=False,min_periods=n).mean() # df_data.dropna(inplace=True, axis=0) df_data.drop([h_l_key, h_pc_key, l_pc_key], axis=1, inplace=True) result_df = pd.DataFrame() result_df.append(df_data.loc[:, [Ct.atr_key(), tr_key]]) return result_df
def get_prices_data(self, keys=None): if keys is None or len(keys) == 0: print("No keys has been specified. All keys were selected. ") keys = { Ct.high_key(): True, Ct.low_key(): True, Ct.open_key(): True, Ct.close_key(): True, Ct.adj_close_key(): True, Ct.volume_key(): True } method_tag = "get_prices_data" if self.data_source_historical is not None: if self.data_source_historical.prices is None or self.data_source_historical.prices.empty is True: raise ValueError( "No historical data available, call method self.get_historical_data() first" ) key_titles = self.get_key_titles(keys) if len(key_titles) > 0: prices = self.data_source_historical.get_prices( self.tickers, key_titles) else: print("{} - There are no prices information, for ticker:{}". format(method_tag, self.tickers)) raise ValueError else: print("There has been an error in {}".format(method_tag)) raise ValueError # Validate Price dataframe if prices.empty == True: print("There has been an error in {}".format(method_tag)) raise ValueError self.price_info = prices return prices