Python in algorithmic trading - page 24

 

How to Calculate Stock Investment Portfolio Volatility with Python, NumPy & Pandas


How to Calculate Stock Investment Portfolio Volatility with Python, NumPy & Pandas

In this video, the presenter demonstrates how to calculate the volatility of an investment portfolio using Python and includes the mathematical equations behind it. The focus is on a two-security portfolio, as it is easily calculated on a handheld calculator. However, the presenter mentions that for portfolios with more than three securities, matrix multiplication is used to calculate volatility. The presenter utilizes the NumPy library and other supporting libraries to perform the calculations.

The video begins with setting up the Python environment and importing the necessary modules. The presenter then defines variables that will be used throughout the process.

To obtain real-time data for the portfolio, the presenter mentions the use of an API and specifically demonstrates using end-of-day historical data. They explain that an API key is required for accessing the data and provide instructions on how to register and obtain the key. The starting date for the data is set to about a year ago. The presenter sets up the portfolio, assigns equal weights to each security, and creates an empty list to store the downloaded data.

Using a for loop, the presenter makes API calls for each symbol in the portfolio and retrieves the data. They demonstrate how to construct the API call by obtaining the endpoint URL from the data provider's website and making the necessary modifications. The data is retrieved as a JSON string, which is converted to a pandas DataFrame for further processing. The closing prices are extracted from the DataFrame and added to the data list.

After completing the for loop, the presenter converts the data list into a DataFrame, transposes it, renames the columns with the symbol names, and changes the index to dates. They then display the resulting DataFrame containing the closing prices for each security.

To calculate the volatility, the presenter explains that instantaneous rates of return need to be calculated. They create a new DataFrame using NumPy's logarithm function on the closing prices and then calculate the differences between consecutive values. The resulting DataFrame represents the instantaneous rates of return for each security. The presenter removes the first row of the DataFrame to handle any NaN values and demonstrates the calculation of variance for each security.

To calculate the volatility for each security, the presenter uses the square root of the variance. They display the resulting volatilities as percentages.

For portfolio volatility, the presenter explains that it can be calculated as the weighted average of the volatilities of each security. Since the securities in the example are equally weighted, the presenter only needs the mean of the volatilities. They format it as a percentage.

Next, the presenter introduces the concept of a covariance matrix and demonstrates how to calculate it using the returns DataFrame. They adjust the daily covariance to an annual covariance and display the resulting covariance matrix.

Finally, the presenter calculates the portfolio volatility using matrix multiplication. They multiply the weights by the covariance matrix and take the square root of the result. The portfolio volatility is displayed as a percentage, representing the standard deviation.

In conclusion, the video provides a step-by-step demonstration of how to calculate portfolio volatility using Python. The presenter explains the necessary calculations, introduces relevant libraries such as NumPy, and emphasizes the benefits of diversification in reducing portfolio volatility.

How to Calculate Stock Investment Portfolio Volatility with Python, NumPy & Pandas
How to Calculate Stock Investment Portfolio Volatility with Python, NumPy & Pandas
  • 2021.08.18
  • www.youtube.com
#python #numpy #pandaslearn how to use Python and NumPy to calculate investment portfolio volatilityhttps://alphabench.com/data/python-portfolio-volatil...
 

Getting Started with Steamlit for Python - Build a Functioning Web App in Minutes



Getting Started with Steamlit for Python - Build a Functioning Web App in Minutes

In this video, I will demonstrate how to use Streamlit for Python to create a web app. Streamlit is a web-based UI that is free to use and allows you to build and share high-quality apps without any front-end development experience. We will be building a simple app that allows us to download stock price data using an API and graph it. Let's get started!

First, I will be using PyCharm as my IDE, but you can use any IDE of your choice. If you're not familiar with PyCharm, I have provided a link in the video description for a "Getting Started with PyCharm" video. Feel free to check it out if you want to learn more.

I have already set up a new project in PyCharm called "Streamlit Demo" and added a Python file. Since we are starting with a fresh virtual environment, we need to install some dependencies. We will need Pandas, the Requests library, and Streamlit. Depending on your PyCharm configuration, you may already have Pandas and Requests installed, but you will definitely need to install Streamlit. Additionally, we will use the "End of Day Historical Data" API for our app, which allows free API calls with a limit of about 20 per day. If you need more than that, they offer subscription options on their website.

Once Streamlit is installed, let's take a look at their sample application. I will open the terminal and run Streamlit by calling Python from the command line and then executing the Streamlit command. Their sample application is in a file called "hello". After a few seconds, the application will start, and you will see a web page with a header, some text, and hyperlinks. On the left side, there are various demonstrations you can explore. The nice thing about their demos is that they provide the code used to generate the output. For example, there is a demo with a chart, and below it, you can see the code used to generate that chart. Streamlit takes care of formatting and placing content on the web page, so you only need to focus on writing the code to produce the desired output.

Once you are satisfied with your application, you can request permission from Streamlit to publish it live. They still offer free publishing, but you need to contact them to set it up. Afterward, you can push your files to GitHub and share your live application within seconds.

Now let's move on to creating our own application. I will demonstrate a few features of Streamlit before we start writing our application code. Streamlit provides various elements that you can use, such as buttons, color pickers, sidebars, text boxes, and date pickers. The most commonly used element is the "write" function, which allows you to display text on the web page. You can also use markdown syntax within the "write" function to format the text. Additionally, if you want to display free-form text, you can simply use a docstring, and Streamlit will render it as markdown.

Let's test some of these features by writing a basic app. I will define a few dependencies and then show you how to use the Streamlit elements. We will start with the "st" function and explore its different capabilities. For example, we can use "st.button" to display a button, "st.color_picker" for a color picker, and "st.sidebar" to create a sidebar. The "write" function is also quite useful for displaying text, and you can use markdown syntax within it. I will demonstrate these common features, but keep in mind that Streamlit offers many more options.

To run the application, I will stop the current Streamlit server and start it again by executing the Python command with the name.

Here's an example of a simple Streamlit app that allows you to download and graph stock price data:

import streamlit as st
import pandas as pd
import requests

# Set the title and page layout
st.title("Stock Price Data")
st.sidebar.header("Settings")

# Create input fields in the sidebar
symbol = st.sidebar.text_input("Enter a stock symbol (e.g., AAPL)", value="AAPL")
start_date = st.sidebar.text_input("Enter the start date (YYYY-MM-DD)", value="2022-01-01")
end_date = st.sidebar.text_input("Enter the end date (YYYY-MM-DD)", value="2022-12-31")

# Create a button to trigger the data retrieval
if st.sidebar.button("Get Data"):
    # Make an API request to retrieve the stock price data
    url = f"https://api.example.com/stock/{symbol}/history?start_date={start_date}&end_date={end_date}"
    response = requests.get(url)
    
    # Check if the API request was successful
    if response.status_code == 200:
        data = response.json()
        df = pd.DataFrame(data)
        
        # Display the downloaded data
        st.write(df)
        
        # Create a line chart of the stock prices
        st.line_chart(df["close"])
    else:
        st.write("Error retrieving data from the API")

# Add some additional information to the sidebar
st.sidebar.info("This is a simple app to download and graph stock price data.")
st.sidebar.info("Enter the stock symbol and date range, then click 'Get Data' to retrieve the data.")

In this example, we first import the necessary libraries: Streamlit, Pandas, and Requests. We then set the title and page layout using the st.title() and st.sidebar.header() functions.

Next, we create input fields in the sidebar using the st.sidebar.text_input() function. Users can enter a stock symbol, start date, and end date in these fields.

We then create a button using the st.sidebar.button() function. When the button is clicked, it triggers the data retrieval process. We construct the API request URL using the entered symbol, start date, and end date. We make a GET request to the API using the requests.get() function.

If the API request is successful (status code 200), we retrieve the data from the response and create a Pandas DataFrame. We display the downloaded data using the st.write() function, and then create a line chart of the stock prices using the st.line_chart() function.

If the API request fails, we display an error message using the st.write() function.

Finally, we add some additional information to the sidebar using the st.sidebar.info() function.

To run this app, you can save it in a Python file (e.g., stock_app.py) and execute it using the command streamlit run stock_app.py in your terminal.

This is just a basic example, and you can customize it further based on your needs. Streamlit provides many more capabilities for creating interactive web apps, including dropdowns, checkboxes, sliders, and more. You can explore the Streamlit documentation (https://docs.streamlit.io/) for more information and examples.

Getting Started with Steamlit for Python - Build a Functioning Web App in Minutes
Getting Started with Steamlit for Python - Build a Functioning Web App in Minutes
  • 2021.10.18
  • www.youtube.com
#python #streamlitHow to use streamlit - full-featured GUI for Pythonhttps://alphabench.com/data/python-streamlit-app.html*Please SUBSCRIBE:https://www.yo...
 

Use End of Day (EOD) Historical Data API to Find Time of Daily High/Low



Use End of Day (EOD) Historical Data API to Find Time of Daily High/Low

In this video, we're going to explore the End of Day Historical Data API, which allows us to extract the daily high and low trading prices for a stock. This API is part of a freemium model for financial data, offering 20 free API calls per day. The advantage of this API is that it provides access to data from over 70 global exchanges. You can find the link to their website and special pricing in the video description.

To begin, we need to set up our environment. We'll be using the datetime module to manipulate dates, the helper library for accessing historical data, pandas for data manipulation and filtering, and requests to make API calls. Next, we need to obtain our API key, which can be stored in a file or directly in the notebook for simplicity.

We'll also define the start date for our data, typically going back about 10 days. The API has different limitations based on the type of data, with minute-by-minute data limited to 120 days and five-minute interval data limited to 600 days. The API indexes data using the Unix timestamp, which represents the number of seconds elapsed since January 1st, 1970. We convert the desired start date to a Unix timestamp for the API request.

With the necessary setup complete, we can create an instance of the "End of Day Historical Data" object using our API key. This object allows us to retrieve the data we need. The helper library provides various endpoints that can be explored for different types of data. In this example, we will focus on retrieving intraday prices for Tesla using one-minute intervals, starting from the specified start date.

To visualize the data, we can wrap the API call in a constructor for a Pandas DataFrame. This makes it easier to work with and visualize the data. We display the first few rows of the DataFrame to verify the retrieved data, including the Unix timestamp, date, and time.

If you prefer not to use the helper library, the video provides an alternative approach using the requests library to directly access the API endpoint and retrieve the JSON data.

Next, we perform some data cleanup. We replace the integer-based index with the date and time from the data. We add separate columns for the time and date, and remove unnecessary columns such as GMT offset and the original date time. The resulting DataFrame shows the revised structure with the added columns.

To focus on regular trading hours, we create a copy of the original data. Since the index is a datetime object, we can use the between_time method to filter for the desired times. As the video mentions, the timezone for this data is GMT-5, so we adjust the times accordingly. We also convert the times to Eastern Time (ET) for easier interpretation.

To filter only the time from the datetime column, we perform some additional steps. Since we already performed a calculation, we need to format the data again before applying the datetime method to extract the time. The resulting DataFrame displays the adjusted times in New York City.

Finally, we address the original question of when the highs and lows occur during the trading day. We first retrieve the general high and low for each day using the groupby function and the min and max methods. To determine the specific time of these highs and lows, we locate the index of the minimum and maximum values within the DataFrame, respectively. This provides insights into the timing of these price points, allowing us to observe any patterns or trends.

Overall, this video provides a step-by-step guide to using the End of Day Historical Data API, retrieving stock price data, and analyzing the timing of high and low trading prices.

Use End of Day (EOD) Historical Data API to Find Time of Daily High/Low
Use End of Day (EOD) Historical Data API to Find Time of Daily High/Low
  • 2021.11.22
  • www.youtube.com
@MattMacarty #python #pandas #EODHistoricalDataUse a financial services API to find the time a stock trades at it lowest or highest each dayUse a financi...
 

How to Backtest Trading Algorithms and Portfolio Metrics with Python and QuantStats



How to Backtest Trading Algorithms and Portfolio Metrics with Python and QuantStats

Today, I'm going to walk you through a comprehensive overview of Quant stats, an extensive Financial metrics Library built as a portfolio profiling tool. With Quant stats, you can generate detailed reports effortlessly using just a simple one-liner. This tool allows you to effectively measure portfolio performance and backtest trading algorithms.

Quant stats relies on well-known data science libraries that you may already be familiar with and use. For this demonstration, I'm working in PyCharm and have started a new project. If you're following along, make sure you have the necessary dependencies installed. I'm using a slightly older version of Pandas because Quant stats hasn't been updated in about six months, and there might be some compatibility issues with the latest Pandas versions.

To ensure you have the required dependencies, you can add them to a requirements.txt file and install them using Pip. Additionally, Quant stats consists of three modules: stats, plots, and reports. Due to the extensive functionality, I won't be able to cover everything in this single video. However, I'll provide you with an overview and guide you on how to get started. You can then explore further based on your specific needs and interests.

Before we dive into the details, let's take a moment to hear from our sponsor. If you're interested in learning how to create trading algorithms with Python, allow Looming Wealth to assist you with their algorithmic trading course. In this course, you'll learn to create bots that automatically trade on your behalf, implement and utilize technical and financial metrics, and use machine learning to develop unique trading algorithms. You can learn at your own pace or join live classes for a collaborative learning experience. Follow the link below to learn more about Looming Wealth and get a 15% discount on any of their trading courses.

Now, let's get back to Quant stats. In a new blank file, start by importing the Quant stats library. Next, set a stock symbol to analyze. For this example, I'll use the S&P 500 ETF (SPY). We'll use Quant stats to examine its performance over the last three years.

To download the returns data, we'll utilize Quant stats utils, which relies on Yahoo Finance data. You can use any data source you prefer; there's no magic here. Let's print out the returns to see what we get. The output will be a Pandas Series with daily returns spanning the specified time period.

Once we have the returns data, we can calculate various statistics using Quant stats. There are numerous available functions, far more than we can cover in this video. You can obtain a list of available statistics by printing the available_stats attribute. Let's display a list comprehension of the available stats, excluding the private functions.

I'll comment out some of the list comprehension to keep it concise. We'll proceed to print a couple of common statistics for the S&P 500 over the past three years. Let's calculate the Sharpe ratio using qs.stats.sharpe_ratio(). Additionally, we'll find the best day and month by calling qs.stats.best_day() and qs.stats.best_month() respectively. You can modify these calls to aggregate data differently if needed.

Alternatively, Quant stats provides a wrapper for Pandas, allowing you to make method calls directly on the portfolio object. By extending Pandas, you can use one-liners to access various statistics. For example, you can call Quant stats.extend_pandas() and then use the portfolio object to retrieve statistics such as compounded annual gross return and maximum drawdown.

In addition to statistics, Quant stats also offers the ability to build portfolios.

Next, I'm going to calculate the signals based on the moving average crossover strategy. If the fast moving average is above the slow moving average, it's a buy signal. Otherwise, it's a sell signal. I'll add these signals to the data frame.

After that, I'm going to calculate the positions based on the signals. If it's a buy signal, I'll set the position to 1 (indicating a long position), and if it's a sell signal, I'll set the position to -1 (indicating a short position). All other cases will be set to 0 (indicating no position).

Now that we have the positions, I'm going to calculate the strategy returns. To do this, I'll multiply the positions by the daily returns of the asset. This will give us the daily strategy returns.

Next, I'll calculate the cumulative returns of the strategy by taking the cumulative product of the daily returns. This will give us the growth of the strategy over time.

Finally, I'm going to generate the report using the quantstats.reports.html function. This function takes the cumulative returns data and generates an HTML report with various performance metrics and visualizations. I'll save the report as an HTML file.

To view the report automatically, I'll open the file using the web browser. And that's it for the code.

Now, when you run this code with your chosen parameters, it will generate the moving average crossover strategy, calculate the performance metrics, and generate an HTML report with the results. You can customize the strategy parameters, such as the moving average periods or the time window, to suit your needs.

How to Backtest Trading Algorithms and Portfolio Metrics with Python and QuantStats
How to Backtest Trading Algorithms and Portfolio Metrics with Python and QuantStats
  • 2022.12.07
  • www.youtube.com
​@MattMacarty #python #trading #algotrading How to Backtest Trading Strategies and Algorithms, Generate Portfolio Metrics✅ Please SUBSCRIBE:https://w...
Reason: