I am using it simply to gather crypto market data, compute alpha, beta and R-squared with statsmodels, and then create a crypto = input("Cryptocurrency: ") function with a while loop that allows me to ask the user for specific crypto and output its respective statistics, followed by showing the input again.
With the following code, I receive the error: ValueError: If using all scalar values, you must pass an index
import san import numpy as np import matplotlib.pyplot as plt import pandas as pd import datetime import statsmodels.api as sm from statsmodels import regression cryptos = ["bitcoin", "ethereum", "ripple", "bitcoin-cash", "tether", "bitcoin-sv", "litecoin", "binance-coin", "eos", "chainlink", "monero", "bitcoin-gold"] def get_and_process_data(c): raw_data = san.get("daily_closing_price_usd/" + c, from_date="2014-12-31", to_date="2019-12-31", interval="1d") # "query/slug" return raw_data.pct_change()[1:] df = pd.DataFrame({c: get_and_process_data(c) for c in cryptos}) df['MKT Return'] = df.mean(axis=1) # avg market return #print(df) # show dataframe with all data def model(x, y): # Calculate r-squared X = sm.add_constant(x) # artificially add intercept to x, as advised in the docs model = sm.OLS(y,X).fit() rsquared = model.rsquared # Fit linear regression and calculate alpha and beta X = sm.add_constant(x) model = regression.linear_model.OLS(y,X).fit() alpha = model.params[0] beta = model.params[1] return rsquared, alpha, beta results = pd.DataFrame({c: model(df[df[c].notnull()]['MKT Return'], df[df[c].notnull()][c]) for c in cryptos}).transpose() results.columns = ['rsquared', 'alpha', 'beta'] print(results)
The error is in the following line:
df = pd.DataFrame({c: get_and_process_data(c) for c in cryptos})
I tried solving the issue by changing it to:
df = {c: get_and_process_data(c) for c in cryptos} df['MKT Return'] = df.mean(axis=1) # avg market return print(df) # show dataframe with all data
But with that, it gave me a different error: AttributeError: 'dict' object has no attribute 'mean'.
The goal is to create a single DataFrame with the DateTime column, columns for the cryptos and their pct.change data, an additional column for MKT Return with the daily mean from all cryptos' pct.change. Then, use all this data to calculate each crypto's statistics and finally create the input function mentioned at the beginning.
I hope I made myself clear and that someone is able to help me with this matter.