''' R-Squared coefficient of determination R**2 = 1 - SSres/SStot ''' def r_squared(series: pd.Series, predicted_values: pd.Series)->np.float64: ss_res_series = ss_res(series, predicted_values) ss_tot_series = ss_tot(series) r_squared = 1 - (ss_res_series/ss_tot_series) return r_squared if __name__ == '__main__': # load data ticker = 'HECP' series = load_sample_data(ticker) close_price = series.iloc[-30:, 1] print('Symbol: ', ticker) print('naive prediction--') predicted_values = series.iloc[-30:, 1].shift(1) r2 = r_squared(close_price, predicted_values).round(3) print('R squared: ', r2) exp_range = ExponentialRange(4, 8, 1/4) test_columns = ['date', 'open', 'close', 'low', 'high', 'volume'] test_df = pd.DataFrame( np.random.rand(exp_range.max, 6), columns=test_columns, index=pd.date_range('01-01-1900', periods=exp_range.max, freq=pd.Timedelta(seconds=10)) )
axs[0].grid(True) axs[1].set_title(ticker+' RSI') axs[1].xaxis.set_major_locator(locator) axs[1].xaxis.set_major_formatter(formatter) axs[1].set_ylim(0, 100) axs[1].plot(dataframe[['rsi']], c='k', lw=1.5, ls='dotted', label="RSI") axs[1].set_xlabel('Date') axs[1].set_ylabel('rsi') axs[1].legend(loc='lower right') axs[1].grid(True) plt.show() if __name__ == '__main__': ticker = 'HRVC' dataframe = load_sample_data(ticker) window = 30 rsi = compute_rsi(dataframe, window) to_plot = dataframe.copy() compute_bb(to_plot) x = -window # define the date range for plot to plot to_plot = to_plot.iloc[x:] to_plot['rsi'] = rsi[x:] _plot(ticker, to_plot) exit
# Indicators module contains core qttk functionality from qttk.indicators import compute_rsi # qttk is 'batteries included' with simulated data from qttk.utils.sample_data import load_sample_data # Load a ticker from csv into dataframe df = load_sample_data('AWU') print(df.head()) rsi = compute_rsi(df) print(rsi[-10:])
# Multiply sign by the boolean. This will cast the boolean to an integer # either 1 or 0 and then multiply by the sign either (-1, 0, 1). return macd_sign * (macd_sign != macd_shifted_sign) if __name__ == '__main__': required_ohlcv_columns = pd.Series( ['open', 'high', 'low', 'close', 'volume']) # removed date as a required column because it is set as the dataframe index # when the csv is read # required_ohlcv_columns = pd.Series(['date', 'open', 'high', 'low', 'close', 'volume']) data = 'AWU.csv' # name of data file to use demo_macd(required_columns=required_ohlcv_columns) # optional loop # script_dir = os.path.dirname(__file__) # csv_files = os.path.join(script_dir, 'data', 'eod', '*.csv') # for csv_file in glob.glob(csv_files): # main(csv_file) # try signal from qttk.utils.sample_data import load_sample_data csv_file = load_sample_data('AWU') macd = compute_macd(csv_file) macd_signal = create_macd_signal(macd['MACD']) macd_signal = macd_signal.iloc[-180:] macd_signal.plot() plt.show()
right=0.989, hspace=0.918, wspace=0.2) axs[0].set_title = 'Close Prices: {ticker}'.format(ticker=ticker) axs[0].plot(close_price.loc[close_price.index > cutoff]) axs[1].set_title = f'Chaikin Money Flow: {ticker}' axs[1].plot(cmf.loc[chaikin_money_flow.index > cutoff]) axs[2].set_title = f'Signal: {ticker}' axs[2].plot(cmf_signal.loc[signal.index > cutoff]) return fig, axs if __name__ == '__main__': ticker = 'AWU' demo_plot = True if sys.argv[1:]: demo_plot = False demo_df = load_sample_data(ticker) cmf = calculate_chaikin_money_flow(demo_df) cmf_signal = calculate_chaikin_money_flow_signal(cmf) if demo_plot: fig, axs = _format_chaikin_plot(demo_df.close, cmf, cmf_signal, ticker) plt.show(block=False)