<jupyter_start><jupyter_text>Initial data exploration and visualization<jupyter_code>from datetime import date, timedelta

import altair as alt
import numpy as np
import pandas as pd
import pandas_market_calendars as mcal
import yfinance as yf

import finstar.evaluation.returns as returns
import finstar.strategies.sma as sma
# determine NYSE market days
end = date.today()
start = date(year=end.year - 10, month=end.month, day=end.day)

nyse = mcal.get_calendar('NYSE')
nyse_days= nyse.schedule(start_date=start, end_date=end).index
days_df = pd.DataFrame(index=nyse_days)
days_df.index.set_names("Date", inplace=True)
ticker_df = pd.read_parquet("../data/raw/20210620-SP500-max/20210620/AAPL.snappy.parquet")
ticker_df
joined = days_df.join(ticker_df.loc[:, "Close"], on="Date", how="left")
joined
joined.isna
hist_df = pd.DataFrame(joined.loc[:, "Close"])
hist_df = hist_df.reset_index()
hist_df.head()
hist_df.tail()
short_sma = 1
long_sma = 200
hist_df["Position"] = sma.sma(hist_df["Close"], short_sma, long_sma)
hist_df.dropna(inplace=True)

hist_df["Returns"] = returns.log_returns(hist_df["Close"])
hist_df["Strategy"] = returns.strategy_returns(hist_df["Position"], hist_df["Returns"])
hist_df["Returns_Sum"] = hist_df["Returns"].cumsum().apply(np.exp)
hist_df["Strategy_Sum"] = hist_df["Strategy"].cumsum().apply(np.exp)
hist_df.loc[:, ["Returns", "Strategy"]].sum().apply(np.exp)
hist_df
plot_df = hist_df.loc[:, ["Date", "Close"]].melt("Date", var_name="Type", value_name="Price")

base = alt.Chart(plot_df).mark_line().encode(
    x="Date:T",
    y="Price:Q",
    color="Type:N"
).interactive()

base
len(hist_df)
plot_df = hist_df.loc[:, ["Date", "Position", "Returns_Sum", "Strategy_Sum"]].melt("Date", var_name="Type", value_name="Returns")

base = alt.Chart(plot_df).mark_line().encode(
    x="Date:T",
    y="Returns:Q",
    color="Type:N"
)

base<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17290458618871904, 'fraction_numerical': 0.013178703215603585, 'mean_word_length': 6.974789915966387, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '567792', 'license': 'Apache-2.0', 'n_tokens_mistral': 742, 'n_tokens_neox': 722, 'n_words': 124, 'path': 'notebooks/20210612-initial_eda.ipynb', 'repo_name': 'JungeAlexander/finstar', 'size': '1897'}