Beispiel #1
0
    def __init__(self, ticker, data: pd.DataFrame, **kwargs):
        if not isinstance(data, pd.DataFrame):
            raise ValueError("data should be a pandas.DataFrame")

        if isinstance(data.columns, pd.MultiIndex):
            self._data = clean_data(data).dropna(how="all")
        else:
            self._data = data.dropna(how="all")

        if not (ticker in self._data.columns):
            raise ValueError(f"Ticker {ticker} is not provided in DataFrame")

        self._ticker = ticker
        self._data = pd.DataFrame(self._data[ticker])

        self._risk_free_rate = kwargs.get("risk_free_rate", 0.001)
        self._freq = kwargs.get("freq", 252)
        self._type = kwargs.get("type", "log")

        self._daily_returns = daily_log_returns(self._data)

        ##########PROPERTIES##########
        self._returns = mean_returns(
            self._data, freq=self._freq, type=self._type
        ).values[0]
        self._volatility = volatility(self._data).values[0]
        self._downside_volatility = negative_volatility(self._data).values[0]
        self._sharp = (self._returns - self._risk_free_rate) / self._volatility
        self._sortino = (
            self.returns - self._risk_free_rate
        ) / self._downside_volatility
        self._skew = self._data.skew().values[0]
        self._kurtosis = self._data.kurt().values[0]
Beispiel #2
0
def test_portfolio_yfinance_pass_1():
    d = d_pass_yfinance[0]
    data = download(**d)
    data = clean_data(data)
    pf = Portfolio(data=data, risk_free_rate=0.001, freq=252)
    pf.mc_simulation()
    pf.plot_mc_simulation()
    pf.print_mc_results()
Beispiel #3
0
def test_portfolio_moex_pass_0():
    d = d_pass_moex[0]
    data = download(**d)
    data = clean_data(data)
    pf = Portfolio(data=data)
    pf.mc_simulation()
    pf.plot_mc_simulation()
    pf.print_mc_results()
Beispiel #4
0
def test_download_moex_pass_4():
    d = d_pass_moex[4]
    data = download(**d)
    print(data.head())
    isinstance(data, pd.DataFrame)
    data = clean_data(data)
    tickers = data.columns.tolist()
    print(tickers)
    assert sorted(tickers) == sorted(tickers_pass_moex[4])
Beispiel #5
0
import pandas as pd
from pfo.stocks.cluster import cluster_stocks
from pfo.stocks.stock import Stock
from pathlib import Path, WindowsPath

from pfo.utils.market_data import download, Source
from pfo.utils.data_utils import clean_data

path = (Path.cwd() / ".." / "cache" / "moex_all.csv").resolve()
data = download(
    Source.MOEX,
    tickers=[],
    boards=[
        {
            "board": "TQBR",
            "shares": "shares"
        },
        {
            "board": "TQTF",
            "shares": "shares"
        },
        {
            "board": "FQBR",
            "shares": "foreignshares"
        },
    ],
)

data = clean_data(data)
data.to_csv(path)
Beispiel #6
0
def test_cluster_stocks_pass_0():
    d = d_pass_csv[0]
    data = download(**d)
    data = clean_data(data)
    data.dropna(how="all").replace([np.inf, -np.inf], np.nan)
    cluster_stocks(data, n_clusters=4, verbose=True)
Beispiel #7
0
    def __init__(self,
                 data: pd.DataFrame,
                 weights=None,
                 risk_free_rate=0.0425,
                 freq=252):
        if not isinstance(freq, int):
            raise ValueError("Frequency must be an integer")
        elif freq <= 0:
            raise ValueError("Freq must be > 0")
        else:
            self._freq = freq

        if not isinstance(risk_free_rate, (float, int)):
            raise ValueError("Risk free rate must be a float or an integer")
        else:
            self._risk_free_rate = risk_free_rate

        if not isinstance(data, pd.DataFrame):
            raise ValueError("data should be a pandas.DataFrame")

        if isinstance(data.columns, pd.MultiIndex):
            self._data = clean_data(data)
        else:
            self._data = data

        self._mc_portfolios = None
        self._mc_min_vol_port = None
        self._mc_min_downside_vol_port = None
        self._mc_max_sharpe_port = None
        self._mc_max_sortino_port = None
        self._mc_simulations_results = None
        self._weights = None
        #####################
        if weights is None:
            self._weights = np.array([
                1.0 / len(self._data.columns)
                for i in range(len(self._data.columns))
            ])
        else:
            if isinstance(weights, pd.DataFrame):
                if len(weights.columns) > 1:
                    raise ValueError(
                        "Incorrect dataframe with weights provided. Expected 1 column with weights"
                    )

                weights_list = []
                for column in data.columns:
                    stock_weight = weights.at[column, weights.columns[0]]
                    weights_list.append(stock_weight)

                self._weights = np.array(weights_list)

            elif isinstance(weights, np.ndarray):
                self._weights = weights
            else:
                raise ValueError(
                    "Weights should be numpy ndarray or pd.DataFrame")

        if len(self._weights) < len(self._data.columns) or len(
                self._weights) > len(self._data.columns):
            raise ValueError("Incorrect data or weights were provided")

        self._cvm = cov_matrix(self._data)
        self._mr = mean_returns(self._data, freq=self._freq)
        self._negative_vol = negative_volatility(data=self._data,
                                                 freq=self._freq)

        self._df_perfomamce = {}
        self._df_perfomamce["Returns"] = self.returns
        self._df_perfomamce["Volatility"] = self.volatility
        self._df_perfomamce["Down. Volatility"] = self.negative_volatility
        self._df_perfomamce["Sharp Ratio"] = self.sharp
        self._df_perfomamce["Sortino Ratio"] = self.sortino
Beispiel #8
0
def cluster_stocks(data: pd.DataFrame, n_clusters=5, verbose=False):
    """Gets the number of clusters and tries to cluster(KMeans) stocks based on
    the mean returns and volatility. The decision about optimal number
    of clusters can be made based on an elbow curve. Max number of cluster is
    20.
    Good article about elbow curve:
    https://blog.cambridgespark.com/how-to-determine-the-optimal-number-of-clusters-for-k-means-clustering-14f27070048f
    The function creates following plots:
     1. Elbow curve to make decision about optimal number of clusters
     2. A plot with K-Means clustered by return and volatility stocks and centroids.
     3. Plots with clusters and their daily return cumulative sum over the given period
    :Input:
         : data: ``pandas.DataFrame`` stock prices
         :n_clusters: ``int`` (default: 5), should be > 2 and less than number of stocks in
         pf
         :verbose: ``boolean`` (default= ``False``), whether to print out clusters
    :Output:
         :clusters: ``list`` of (Stocks) tickers.
    """

    if not isinstance(n_clusters, int):
        raise ValueError("Total number of clusters must be integer.")
    elif n_clusters < 2:
        raise ValueError(
            f"Total number of clusters({len(data.columns)}) must be > 2.")
    elif len(data.columns) < 3:
        raise ValueError(
            f"Total number of stocks in pf({len(data.columns)}) must be > 2.")
    elif n_clusters > len(data.columns):
        raise ValueError(
            f"Total number of clusters({n_clusters}) "
            f"must be <= number of stocks({len(data.columns)}) in pf")

    if isinstance(data.columns, pd.MultiIndex):
        data = clean_data(data)

    pf_return_means = mean_returns(data, type="log")
    pf_daily_returns = daily_log_returns(data)
    pf_volatility = volatility(data)
    # format the data as a numpy array to feed into the K-Means algorithm
    data_ret_vol = np.asarray(
        [np.asarray(pf_return_means),
         np.asarray(pf_volatility)]).T

    distorsions = []
    max_n_clusters = min(20, len(data.columns))

    for k in range(2, max_n_clusters):
        k_means = KMeans(n_clusters=k)
        k_means.fit(X=data_ret_vol)
        distorsions.append(k_means.inertia_)

    plt.plot(
        range(2, max_n_clusters),
        distorsions,
        linestyle="-",
        color="red",
        lw=2,
        label="Elbow curve",
    )
    plt.title("Elbow curve")
    plt.xlabel("Number of clusters")
    plt.ylabel("Distortion")
    plt.grid(True)
    plt.legend()

    # Step size of the mesh. Decrease to increase the quality of the VQ.
    h = 0.002  # point in the mesh [x_min, x_max]x[y_min, y_max].

    x_min, x_max = data_ret_vol[:, 0].min() - 0.1, data_ret_vol[:,
                                                                0].max() + 0.1
    y_min, y_max = data_ret_vol[:, 1].min() - 0.1, data_ret_vol[:,
                                                                1].max() + 0.1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))

    km = KMeans(n_clusters=n_clusters)
    km.fit(data_ret_vol)

    centroids = km.cluster_centers_

    # Obtain labels for each point in mesh. Use last trained model.
    Z = km.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape)

    # some plotting using numpy's logical indexing
    plt.figure(figsize=(10, 6))
    plt.imshow(
        Z,
        interpolation="nearest",
        extent=(xx.min(), xx.max(), yy.min(), yy.max()),
        cmap=plt.cm.Paired,
        aspect="auto",
        origin="lower",
    )

    # Plot the centroids as a white X
    plt.scatter(centroids[:, 0],
                centroids[:, 1],
                marker="*",
                s=420,
                color="white",
                zorder=10)
    # Plot stocks
    plt.plot(data_ret_vol[:, 0], data_ret_vol[:, 1], "o", markersize=12)

    plt.title("K-means clustering\n" "Centroids are marked with white star")
    plt.xlabel("Returns")
    plt.ylabel("Volatility")

    idx, _ = vq(data_ret_vol, centroids)
    clusters = {}

    for i in list(set(idx)):
        clusters[i] = []

    for name, cluster in zip(pf_return_means.index, idx):
        clusters[cluster].append(name)

    # Calculating avg comulative daily return for each cluster and store
    # in pf_daily_returns under special stock name - avg{Cluster index}
    for i in list(set(idx)):
        s = "avg" + str(i)
        pf_daily_returns[s] = pf_daily_returns[clusters[i]].mean(axis=1)

    for n in range(n_clusters):
        # plot clusters
        plt.figure(figsize=(10, 6))

        for stock in clusters[n]:
            # plot stocks as grey lines
            plt.plot(pf_daily_returns[stock].cumsum(), "gray", linewidth=1)

        plt.title(f"Cluster #{n}")
        plt.ylabel("Daily returns cumulative sum")
        # plot average to see cluster dynamic
        s = "avg" + str(n)
        plt.plot(pf_daily_returns[s].cumsum(), "red", linewidth=3)
        plt.xticks(rotation=30)
        plt.grid(True)

        if verbose:
            print(f"Cluster #{n}")
            print(clusters[n])

    return clusters