<jupyter_start><jupyter_text>Stock Downside Risk Chart<jupyter_code># Library
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

import warnings
warnings.filterwarnings("ignore")

from pandas_datareader import data as pdr
import yfinance as yf
yf.pdr_override()
start = '2019-01-01' #input
end = '2020-07-01' #input
symbol = 'AMD' #input
stocks = yf.download(symbol, start=start, end=end)['Adj Close']
stocks_returns = stocks.pct_change().dropna()
def downside_risk(stock_returns):
    dr = stock_returns[stock_returns < stock_returns.mean()].std(skipna = True) * np.sqrt(252)
    return dr
# Compute the running Downside Risk
running = [downside_risk(stocks_returns[i-90:i]) for i in range(90, len(stocks_returns))]

# Plot running Downside Risk up to 100 days before the end of the data set
_, ax1 = plt.subplots(figsize=(12,8))
ax1.plot(range(90, len(stocks_returns)-100), running[:-100])
ticks = ax1.get_xticks()
ax1.set_xticklabels([stocks.index[int(i)].date() for i in ticks[:-1]]) # Label x-axis with dates
plt.title(symbol + ' Downside Risk')
plt.xlabel('Date')
plt.ylabel('Downside Risk')
stock_dr = downside_risk(stocks_returns)
stock_dr
running = [downside_risk(stocks_returns[i-90:i]) for i in range(90, len(stocks_returns))]
running<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12962962962962962, 'fraction_numerical': 0.035493827160493825, 'mean_word_length': 6.411428571428571, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '328561', 'license': 'MIT', 'n_tokens_mistral': 509, 'n_tokens_neox': 464, 'n_words': 116, 'path': 'Python_Stock/Stock_Measurement_Ratio_Chart/Stock_Downside_Risk_Chart.ipynb', 'repo_name': 'eu90h/Stock_Analysis_For_Quant', 'size': '1296'}