Created
April 11, 2023 12:13
-
-
Save nfl0/01db5816a30b57486a8872e4d73935f3 to your computer and use it in GitHub Desktop.
Don't sell in May, stay invested! #SellinMayAndGoAway
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import pandas as pd | |
from scipy.stats import ttest_ind | |
# set up Coingecko API endpoint | |
base_url = "https://api.coingecko.com/api/v3" | |
endpoint = "/coins/bitcoin/market_chart" | |
# set number of days to retrieve historical data for | |
days = "max" | |
# make API request for historical price data | |
params = { | |
"vs_currency": "usd", | |
"days": days | |
} | |
response = requests.get(base_url + endpoint, params=params) | |
data = response.json() | |
# extract price data from API response | |
prices = pd.DataFrame(data["prices"], columns=["timestamp", "price"]) | |
prices["date"] = pd.to_datetime(prices["timestamp"], unit="ms").dt.date | |
prices.set_index("date", inplace=True) | |
# set datetime index to prices dataframe | |
prices.index = pd.to_datetime(prices.index) | |
# compute daily returns | |
daily_returns = prices["price"].pct_change() | |
# compute average daily returns for each month | |
monthly_returns = daily_returns.resample("M").mean() | |
# compute average returns for May-October and November-April periods | |
may_oct_avg = monthly_returns[(monthly_returns.index.month >= 5) & (monthly_returns.index.month <= 10)].mean() | |
nov_apr_avg = monthly_returns[(monthly_returns.index.month >= 11) | (monthly_returns.index.month <= 4)].mean() | |
# perform t-test to compare returns for May-October and November-April periods | |
t, p = ttest_ind(monthly_returns[(monthly_returns.index.month >= 5) & (monthly_returns.index.month <= 10)], | |
monthly_returns[(monthly_returns.index.month >= 11) | (monthly_returns.index.month <= 4)], | |
equal_var=False) | |
# print results | |
print("Average monthly returns:") | |
print(monthly_returns) | |
print() | |
print("Average returns for May-October period:", may_oct_avg) | |
print("Average returns for November-April period:", nov_apr_avg) | |
print("T-test results: t =", t, ", p =", p) | |
# interpret results | |
if may_oct_avg > nov_apr_avg: | |
print("Sell in May and go away!") | |
else: | |
print("Don't sell in May, stay invested!") | |
if p < 0.05: | |
print("The difference in average returns between May-October and November-April is statistically significant.") | |
else: | |
print("The difference in average returns between May-October and November-April is not statistically significant.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Average monthly returns:
date
2013-04-30 0.001155
2013-05-31 0.000289
2013-06-30 -0.009457
2013-07-31 0.006666
2013-08-31 0.007502
...
2022-12-31 0.000457
2023-01-31 0.010613
2023-02-28 0.001359
2023-03-31 0.006307
2023-04-30 0.005894
Freq: M, Name: price, Length: 121, dtype: float64
Average returns for May-October period: 0.001985500916276666
Average returns for November-April period: 0.002644712465366056
T-test results: t = -0.40210273038850486 , p = 0.6884141759230908
Don't sell in May, stay invested!
The difference in average returns between May-October and November-April is not statistically significant.