""" credits: Karthik Rangappa Spread = Closing value of stock 1 – closing value of stock 2 : change in stock price with respect to the previous close Differential = Closing Price of Stock 1 – Closing Price of Stock 2 Ratio = Stock Price of stock 1 / stock price of stock 2 todays output: basic work for pair trade """ import matplotlib.pyplot as plt import easy_algo as ea import pdb import pandas as pd hdfc_data = ea.jsonreader('HDFC', "E:\\data storage\\1 day\\2018\\") hdfc = pd.DataFrame(hdfc_data) hdfcbank_data = ea.jsonreader('HDFCBANK', "E:\\data storage\\1 day\\2018\\") hdfcbank = pd.DataFrame(hdfcbank_data) df = hdfc.merge(hdfcbank, on='date', how='left', suffixes=("_hdfc", "_hdfcbank") ) df['yesterday_hdfc'] = df['close_hdfc'].shift(1) df['closing_hdfc'] = df['close_hdfc'] -df['yesterday_hdfc'] df['yesterday_hdfcbank'] = df['close_hdfcbank'].shift(1) df['closing_hdfcbank'] = df['close_hdfcbank'] -df['yesterday_hdfcbank'] # rearrange the columns df = df[['date', 'close_hdfc', 'yesterday_hdfc', 'closing_hdfc', 'close_hdfcbank', 'yesterday_hdfcbank', 'closing_hdfcbank']] # calculate various aspects df['spread'] = df['closing_hdfc'] - df['closing_hdfcbank'] df['differential'] = df['close_hdfc'] - df['close_hdfcbank'] df['ratio'] = df['close_hdfc'] / df['close_hdfcbank'] df.to_excel("zerodha pair trade.xlsx") # subplot(row, column , present ) # # plot the output plt.subplot(311) plt.plot(df['spread']) plt.subplot(312) plt.plot(df['differential']) plt.subplot(313) plt.plot(df['ratio']) plt.show() pdb.set_trace()