<jupyter_start><jupyter_text>Price Relative https://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:price_relative<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

import warnings
warnings.filterwarnings("ignore")

# fix_yahoo_finance is used to fetch data 
import fix_yahoo_finance as yf
yf.pdr_override()
# input
symbol1 = 'AAPL'
symbol2 = '^GSPC'
start = '2018-10-01'
end = '2019-01-01'

# Read data 
df1 = yf.download(symbol1,start,end)
df2 = yf.download(symbol2,start,end)
df = pd.concat([df1['Adj Close'], df2['Adj Close']],axis=1)
# Rename columns
df.columns = [symbol1,symbol2]
df['Price Relative'] = df['AAPL']/df['^GSPC']
df['Percentage Change in Price Relative'] = ((df['Price Relative']-df['Price Relative'].shift())/df['Price Relative'].shift())*100
df.head()
fig = plt.figure(figsize=(14,10))
ax1 = plt.subplot(2, 1, 1)
ax1.plot(df1['Adj Close'])
ax1.set_title('Stock '+ symbol1 +' Closing Price')
ax1.set_ylabel('Price')
ax1.legend(loc='best')

ax2 = plt.subplot(2, 1, 2)
ax2.plot(df['Percentage Change in Price Relative'], label='Price Relative', color='black')
ax2.grid()
ax2.legend(loc='best')
ax2.set_ylabel('Price Relative')
ax2.set_xlabel('Date')<jupyter_output><empty_output><jupyter_text>Candlestick with Price Relative<jupyter_code>from matplotlib import dates as mdates
import datetime as dt

dfc = df1.copy()
dfc['VolumePositive'] = dfc['Open'] < dfc['Adj Close']
#dfc = dfc.dropna()
dfc = dfc.reset_index()
dfc['Date'] = mdates.date2num(dfc['Date'].astype(dt.date))
dfc.head()
from mpl_finance import candlestick_ohlc

fig = plt.figure(figsize=(14,10))
ax1 = plt.subplot(2, 1, 1)
candlestick_ohlc(ax1,dfc.values, width=0.5, colorup='g', colordown='r', alpha=1.0)
ax1.xaxis_date()
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
ax1.grid(True, which='both')
ax1.minorticks_on()
ax1v = ax1.twinx()
colors = dfc.VolumePositive.map({True: 'g', False: 'r'})
ax1v.bar(dfc.Date, dfc['Volume'], color=colors, alpha=0.4)
ax1v.axes.yaxis.set_ticklabels([])
ax1v.set_ylim(0, 3*df1.Volume.max())
ax1.set_title('Stock '+ symbol1 +' Closing Price')
ax1.set_ylabel('Price')

ax2 = plt.subplot(2, 1, 2)
ax2.plot(df['Percentage Change in Price Relative'], label='Price Relative', color='black')
ax2.grid()
ax2.legend(loc='best')
ax2.set_ylabel('Price Relative')
ax2.set_xlabel('Date')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17897371714643304, 'fraction_numerical': 0.0392156862745098, 'mean_word_length': 7.184300341296928, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '485840', 'license': 'MIT', 'n_tokens_mistral': 1016, 'n_tokens_neox': 964, 'n_words': 177, 'path': 'Python_Stock/Technical_Indicators/Price_Relative.ipynb', 'repo_name': 'chunsj/Stock_Analysis_For_Quant', 'size': '2397'}