[DRAFT] Compare Strategy

Multiple Portfolio Analysis Dashboard Cookbook

This cookbook will guide you through the process of setting up a simple portfolio analysis dashboard using Python libraries such as finter, gradio, and plotly. The dashboard will allow you to visualize the performance of a portfolio based on different alpha models over time.

Prerequisites

Before you begin, ensure you have the following installed:

  • Python 3.x

  • finter (a financial data analysis library)

  • gradio (for creating web UIs for Python scripts)

  • plotly (for interactive plotting)

  • dotenv (for environment variable management)

You can install these packages using pip:

pip install finter gradio plotly python-dotenv

Step-by-Step Guide

Step 1: Import Libraries

Start by importing the necessary Python libraries:

from functools import lru_cache
import dotenv
import gradio as gr
import plotly.graph_objs as go
from plotly.subplots import make_subplots
from finter.data import ModelData
from finter.framework_model import BaseAlpha

Step 2: Load Environment Variables

Load environment variables using dotenv:

dotenv.load_dotenv(dotenv.find_dotenv())

Step 3: Define Constants and Functions

Set the end date for the analysis and define functions to load returns and alpha data:

END = 20240401

def cached_returns_loader():
    # Load and calculate daily returns
    return (
        BaseAlpha.get_cm("content.handa.dataguide.price_volume.krx-spot-price_close.1d")
        .get_df(20150101, 20240309)
        .pct_change()
    )

@lru_cache(maxsize=128)
def cached_alpha_loader(identity_name: str):
    # Load alpha data and cache it
    df = ModelData.load(identity_name)
    df.columns = df.columns.astype(int)
    return df

Step 4: Define Portfolio Analysis Function

Create a function to run the portfolio analysis and generate plots:

def run_portfolio_analysis(start_int, identity_names: str):
    # ... (code from the provided script)

This function takes a start date and a string of identity names separated by newlines, loads the data, calculates PnL, drawdown, positive counts, and absolute sum, and then generates a series of plots.

Step 5: Set Up Gradio Interface

Create a Gradio interface to input parameters and display the analysis:

if __name__ == "__main__":
    with gr.Blocks() as app:
        # ... (code from the provided script)

This block of code sets up sliders and text areas for user input and a button to trigger the analysis.

Step 6: Launch the Dashboard

Finally, launch the dashboard:

    app.launch(server_name="0.0.0.0", server_port=7860)

Expected Output

When you run the script, a web interface will be available at http://localhost:7860 (or another port if you change it). You can input the start date and alpha identities, then click "Run Analysis" to see the portfolio performance plots.

The output will include four plots:

  1. Portfolio PnL (Profit and Loss)

  2. Positive Counts (number of positive returns)

  3. Absolute Sum (sum of absolute alpha values)

  4. Drawdown (peak-to-trough decline)

Each plot will be interactive, allowing you to zoom in and out and hover over data points for more information.

Conclusion

This cookbook provides a straightforward way to set up a portfolio analysis dashboard. You can modify the code to include additional metrics or to analyze different data sets as per your requirements. The use of caching and environment variables ensures efficiency and security, respectively. With gradio, you can easily share your analysis with others by hosting it on a web server.

Last updated