<jupyter_start><jupyter_text>AboutRoughwork code for comparing BTC and ETH returns.<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

import fattails.metrics as fattails<jupyter_output><empty_output><jupyter_text>Datasource:* BTC: https://finance.yahoo.com/quote/BTC-USD/history/* ETH: https://finance.yahoo.com/quote/ETH-USD/history/<jupyter_code>btc_filepath = '../data/crypto/BTC-USD.csv'
eth_filepath = '../data/crypto/ETH-USD.csv'<jupyter_output><empty_output><jupyter_text>Setup<jupyter_code>def prices_to_returns(prices):
    """ Convert price data into percentage return data.

    Each data entry is replaced by it's percentage change 
    vs. the entry before it.
    
    Parameters
    ----------
    sr : pd.Series
        Price data.

    Returns
    -------
    returns : pd.Series
        Percentage return data.
        
    Example
    -------
    >>> # Initialise a price series
    >>> prices = pd.Series([100, 101, 100, 125])
    0    100
    1    101
    2    100
    3    125
    dtype: int64
    >>> # Convert Prices to Returns
    >>> convert_prices_to_returns(prices)
    1    0.010000
    2   -0.009901
    3    0.250000
    dtype: float64
    """
    # Caluculate Returns
    previous_prices = prices.shift(1)
    returns = prices / previous_prices - 1
    
    # Remove First Period
    returns = returns.iloc[1:]  # The first entry has no prior day so return is NaN
        
    return returns
prices = pd.Series([100, 101, 100, 125])

prices_to_returns(prices)<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code># Load Prices
btc_prices = pd.read_csv(btc_filepath, index_col=0, parse_dates=[0])
btc_prices = btc_prices.Close.rename('prices')
display(btc_prices)

eth_prices = pd.read_csv(eth_filepath, index_col=0, parse_dates=[0])
eth_prices = eth_prices.Close.rename('prices')

display(eth_prices)
# Calculate Returns
btc_returns = prices_to_returns(btc_prices)
eth_returns = prices_to_returns(eth_prices)

basket_returns = (btc_returns + eth_returns)/2  # Basket where you rebalance to 50:50 at end of each day.
basket_returns
# Create DataFrame
returns_df = pd.concat([btc_returns, eth_returns], \
                       keys=['btc_returns','eth_returns'], \
                       axis=1, join='inner')

# Add Mean Return Column
returns_df['mean_returns'] = returns_df.mean(axis=1)

returns_df.head()<jupyter_output><empty_output><jupyter_text>Missing Data Check<jupyter_code># NaN Values in Returns Data
returns_df.loc[returns_df.isna().any(axis=1)]
# NaN Values in ETH Prices
eth_prices.iloc[1:][returns_df.isna().any(axis=1)]
# NaN Values in BTC Prices
btc_prices.loc[returns_df.index][returns_df.isna().any(axis=1)]
# Drop Nan Values from Returns
returns_df.dropna(inplace=True)<jupyter_output><empty_output><jupyter_text>Analyse  Simple Plot<jupyter_code>returns_df.max()
# Plot
(returns_df*100).loc[:,['btc_returns','eth_returns']] \
                .plot(figsize=[15,5], alpha=0.5, grid=True, \
                      ylim=[-100,100],  ylabel='% Move since last close', \
                      title='Daily USD Returns');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12217626471524022, 'fraction_numerical': 0.03181673560292714, 'mean_word_length': 3.576419213973799, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 37, 'https://': 2, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '276005', 'license': 'MIT', 'n_tokens_mistral': 1172, 'n_tokens_neox': 1087, 'n_words': 223, 'path': 'notebooks/NB31 - Daily Returns of BTC vs. ETH.ipynb', 'repo_name': 'FergM/fattails', 'size': '3143'}