Start, End and Duration of Maximum Drawdown in Python

Start, End and Duration of Maximum Drawdown in Python

The maximum drawdown of a time series is the largest drop (peak to trough) in value that occurs during a specific period. You can calculate the start, end, and duration of the maximum drawdown in Python using various libraries like NumPy and pandas. Here's a step-by-step example:

import numpy as np import pandas as pd # Simulated time series data data = [100, 105, 110, 95, 80, 90, 70, 110, 95, 120] dates = pd.date_range(start='2023-01-01', periods=len(data), freq='D') ts = pd.Series(data, index=dates) # Calculate cumulative maximum of the time series cum_max = np.maximum.accumulate(ts) # Calculate drawdown as the percentage drop from the cumulative maximum drawdown = (cum_max - ts) / cum_max * 100 # Find the maximum drawdown value and its index (start date) max_drawdown = drawdown.max() max_drawdown_idx = drawdown.idxmax() # Find the end date (recovery) of the maximum drawdown end_date_idx = drawdown[max_drawdown_idx:].idxmin() # Calculate the duration of the maximum drawdown duration = (end_date_idx - max_drawdown_idx).days # Print the results print("Maximum Drawdown: {:.2f}%".format(max_drawdown)) print("Start Date: {}".format(max_drawdown_idx)) print("End Date: {}".format(end_date_idx)) print("Duration: {} days".format(duration)) 

In this example, the ts Series represents the time series data. The code calculates the cumulative maximum (cum_max) of the time series, then calculates the drawdown by comparing the cumulative maximum with the time series values. The maximum drawdown value, start date, end date, and duration are then calculated and printed.

Remember to replace the sample data with your actual time series data for accurate results.

Examples

  1. "Calculate Maximum Drawdown in Python"

    • This query is about finding the maximum drawdown in a series, which measures the largest peak-to-trough decline in a financial time series.
    import numpy as np prices = np.array([100, 110, 105, 95, 90, 120, 80]) cumulative_max = np.maximum.accumulate(prices) drawdowns = (prices - cumulative_max) / cumulative_max max_drawdown = drawdowns.min() # Get the maximum drawdown (most negative value) print("Maximum Drawdown:", max_drawdown) 
  2. "Find Start and End of Maximum Drawdown in Python"

    • This query explores locating the start and end points of the maximum drawdown in a time series, indicating where the decline began and ended.
    import numpy as np prices = np.array([100, 110, 105, 95, 90, 120, 80]) cumulative_max = np.maximum.accumulate(prices) drawdowns = (prices - cumulative_max) / cumulative_max # Find the index of maximum drawdown max_drawdown_index = np.argmin(drawdowns) # Find the start of the drawdown (when prices last hit the cumulative max before the drawdown) start_drawdown_index = np.where(cumulative_max == prices[max_drawdown_index])[0][0] # Find the end of the drawdown (when prices recover to the cumulative max after the drawdown) end_drawdown_index = np.where(cumulative_max[max_drawdown_index:] == cumulative_max[start_drawdown_index])[0][0] + max_drawdown_index print("Maximum Drawdown Start:", start_drawdown_index) print("Maximum Drawdown End:", end_drawdown_index) 
  3. "Calculate Duration of Maximum Drawdown in Python"

    • This query discusses finding the duration of the maximum drawdown, indicating how long it took for the price to recover.
    import numpy as np prices = np.array([100, 110, 105, 95, 90, 120, 80]) cumulative_max = np.maximum.accumulate(prices) drawdowns = (prices - cumulative_max) / cumulative_max # Find the start and end of the maximum drawdown max_drawdown_index = np.argmin(drawdowns) start_drawdown_index = np.where(cumulative_max == prices[max_drawdown_index])[0][0] end_drawdown_index = np.where(cumulative_max[max_drawdown_index:] == cumulative_max[start_drawdown_index])[0][0] + max_drawdown_index # Calculate duration max_drawdown_duration = end_drawdown_index - start_drawdown_index print("Maximum Drawdown Duration:", max_drawdown_duration, "periods") 
  4. "Calculate Drawdown Series in Python"

    • This query involves calculating the drawdown for each point in a time series, which measures the deviation from the historical high.
    import numpy as np prices = np.array([100, 110, 105, 95, 90, 120, 80]) cumulative_max = np.maximum.accumulate(prices) drawdown_series = (prices - cumulative_max) / cumulative_max print("Drawdown Series:", drawdown_series) 
  5. "Plot Drawdown and Maximum Drawdown in Python"

    • This query is about visualizing the drawdown series and highlighting the maximum drawdown on a plot for better understanding.
    import numpy as np import matplotlib.pyplot as plt prices = np.array([100, 110, 105, 95, 90, 120, 80]) cumulative_max = np.maximum.accumulate(prices) drawdown_series = (prices - cumulative_max) / cumulative_max plt.plot(drawdown_series, label='Drawdown') plt.axhline(y=drawdown_series.min(), color='r', linestyle='--', label='Maximum Drawdown') plt.xlabel("Period") plt.ylabel("Drawdown") plt.title("Drawdown and Maximum Drawdown") plt.legend() plt.show() 
  6. "Calculate Maximum Drawdown with Rolling Window in Python"

    • This query explores finding the maximum drawdown over a rolling window, providing insight into drawdowns within a specific timeframe.
    import numpy as np import pandas as pd prices = np.array([100, 110, 105, 95, 90, 120, 80, 130, 140]) window = 3 # Create a rolling window of cumulative maximums rolling_max = pd.Series(prices).rolling(window=window, min_periods=1).max() drawdowns = (prices - rolling_max) / rolling_max max_drawdown = drawdowns.min() print("Maximum Drawdown with Rolling Window:", max_drawdown) 
  7. "Calculate Maximum Drawdown with Date Index in Python"

    • This query involves finding the maximum drawdown in a time series with a date index, often used for financial data with specific timestamps.
    import numpy as np import pandas as pd # Create a time series with a date index dates = pd.date_range(start="2023-01-01", periods=7, freq="D") prices = pd.Series([100, 110, 105, 95, 90, 120, 80], index=dates) cumulative_max = prices.cummax() # Cumulative maximum drawdowns = (prices - cumulative_max) / cumulative_max max_drawdown = drawdowns.min() print("Maximum Drawdown with Date Index:", max_drawdown) 
  8. "Calculate Maximum Drawdown Percentage in Python"

    • This query explores calculating the maximum drawdown as a percentage of the cumulative maximum, which helps understand the relative magnitude of the drawdown.
    import numpy as np prices = np.array([100, 110, 105, 95, 90, 120, 80]) cumulative_max = np.maximum.accumulate(prices) drawdowns = (prices - cumulative_max) / cumulative_max max_drawdown_percentage = drawdowns.min() * 100 # Convert to percentage print("Maximum Drawdown Percentage:", max_drawdown_percentage, "%") 
  9. "Identify Recovery Period after Maximum Drawdown in Python"

    • This query is about finding the recovery period, which is the time it takes for prices to recover to their previous high after a maximum drawdown.
    import numpy as np prices = np.array([100, 110, 105, 95, 90, 120, 80, 130, 140]) cumulative_max = np.maximum.accumulate(prices) drawdowns = (prices - cumulative_max) / cumulative_max # Find the maximum drawdown and its recovery max_drawdown_index = np.argmin(drawdowns) start_drawdown_index = np.where(cumulative_max == prices[max_drawdown_index])[0][0] end_drawdown_index = np.where(cumulative_max[max_drawdown_index:] == cumulative_max[start_drawdown_index])[0][0] + max_drawdown_index recovery_period = len(prices) - end_drawdown_index print("Recovery Period after Maximum Drawdown:", recovery_period, "periods") 

More Tags

powercli heading contentpresenter android-animation 32bit-64bit formats semantic-segmentation nmake ngx-translate popover

More Python Questions

More Weather Calculators

More Various Measurements Units Calculators

More Chemical reactions Calculators

More Geometry Calculators