Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # coding: utf-8
- # In[54]:
- #!/usr/bin/bash python
- # coding: utf-8
- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- import csv
- import warnings
- df = pd.read_csv('/Users/King/TA_SPY.csv')
- df1 = pd.read_csv('/Users/King/SPY_var_analysis.csv')
- import warnings
- warnings.filterwarnings("ignore")
- df1['PVP'] = 100*round(df1.PVS / df1.Price, 4)
- df1['PVS'] = df1.PVP
- del df1['PVP']
- df['PVP'] = 100*round(df.PVS / df.Price, 4)
- df['PVS'] = df.PVP
- del df['PVP']
- with open('/Users/King/SPY_var_analysis.csv', 'r') as csv_file:
- reader = csv.reader(csv_file)
- header = next(reader)
- for row in reader:
- Date = str(row[0])
- Time = str(row[1])
- ref_Num = float(row[2])
- Price = float(row[3])
- ChgO = float(row[4])
- PG = float(row[5])
- MTR = float(row[6])
- DR = float(row[7])
- GR = float(row[8])
- GDR = float(row[9])
- PVS = float(row[10])
- avgVol = float(row[11])
- ITDS = float(row[12])
- NS = float(row[13])
- SS = float(row[14])
- AQR = float(row[15])
- ALQ = float(row[16])
- UDR = float(row[17])
- CVSR = float(row[18])
- QRS = float(row[19])
- AVSR = float(row[20])
- DRC = int(row[21])
- CashOpen = float(row[22])
- Liquidity = float(row[23])
- MTR_Strength = float(row[24])
- UDR_Strength = float(row[25])
- Chg45 = float(row[26])
- Chg60 = float(row[27])
- Chg120 = float(row[28])
- PVP = 100*round(PVS / Price, 4)
- VOL_QR = round((avgVol / AQR) - CVSR, 3)
- match_time = df.ref_Num == ref_Num
- avg_bin = 5
- matches = df[['Date', 'Time', 'ChgO', 'PG', 'MTR', 'DR', 'GR', 'GDR', 'PVS', 'avgVol', 'ITDS', 'NS', 'SS', 'avgQR', 'QRS_Index', 'ALQ', 'UDR', 'VSR', '60Min', '120Min', '180Min', '240Min', '1D', '3D']][match_time]
- chgo_max = matches.iloc[:, 2].max()
- chgo_min = matches.iloc[:, 2].min()
- chgo_range = abs(chgo_max - chgo_min)
- chgo_move = abs(chgo_range / 9)
- cur_ChgO_bin = round((ChgO - chgo_min) / chgo_move, 0)
- scen_ChgO_bin = round((df.ChgO - chgo_min) / chgo_move, 0)
- ChgOMatch = scen_ChgO_bin == cur_ChgO_bin
- pm = df[['Date', 'Time', 'ChgO', 'PG', 'MTR', 'DR', 'GR', 'GDR', 'PVS', 'avgVol', 'ITDS', 'NS', 'SS', 'avgQR', 'QRS_Index', 'ALQ', 'UDR', 'VSR', '60Min', '120Min', '180Min', '240Min']][match_time][ChgOMatch]
- pg_max = matches.iloc[:, 3].max()
- pg_min = matches.iloc[:, 3].min()
- pg_range = abs(pg_max - pg_min)
- pg_move = abs(pg_range / avg_bin)
- cur_PG_bin = round((PG - pg_min) / pg_move, 0)
- scen_PG_bin = round((df.PG - pg_min) / pg_move, 0)
- PGMatch = scen_PG_bin == cur_PG_bin
- mtr_max = matches.iloc[:, 4].max()
- mtr_min = matches.iloc[:, 4].min()
- mtr_range = abs(mtr_max - mtr_min)
- mtr_move = abs(mtr_range / avg_bin)
- cur_MTR_bin = round((MTR - mtr_min) / mtr_move, 0)
- scen_MTR_bin = round((df.MTR - mtr_min) / mtr_move, 0)
- MTRMatch = scen_MTR_bin == cur_MTR_bin
- dr_max = matches.iloc[:, 5].max()
- dr_min = matches.iloc[:, 5].min()
- dr_range = abs(dr_max - dr_min)
- dr_move = abs(dr_range / avg_bin)
- cur_DR_bin = round((DR - dr_min) / dr_move, 0)
- scen_DR_bin = round((df.DR - dr_min) / dr_move, 0)
- DRMatch = scen_DR_bin == cur_DR_bin
- gr_max = matches.iloc[:, 6].max()
- gr_min = matches.iloc[:, 6].min()
- gr_range = abs(gr_max - gr_min)
- gr_move = abs(gr_range / avg_bin)
- cur_GR_bin = round((GR - gr_min) / gr_move, 0)
- scen_GR_bin = round((df.GR - gr_min) / gr_move, 0)
- GRMatch = scen_GR_bin == cur_GR_bin
- gdr_max = matches.iloc[:, 7].max()
- gdr_min = matches.iloc[:, 7].min()
- gdr_range = abs(gdr_max - gdr_min)
- gdr_move = abs(gdr_range / avg_bin)
- cur_GDR_bin = round((GDR - gdr_min) / gdr_move, 0)
- scen_GDR_bin = round((df.GDR - gdr_min) / gdr_move, 0)
- GDRMatch = scen_GDR_bin == cur_GDR_bin
- #set6 = df[match_time][GDRMatch]
- ##set6
- pvs_max = matches.iloc[:, 8].max()
- pvs_min = matches.iloc[:, 8].min()
- pvs_range = abs(pvs_max - pvs_min)
- pvs_move = abs(pvs_range / avg_bin)
- cur_PVS_bin = round((PVP - pvs_min) / pvs_move, 0)
- scen_PVS_bin = round((df.PVS - pvs_min) / pvs_move, 0)
- PVSMatch = scen_PVS_bin == cur_PVS_bin
- vol_max = matches.iloc[:, 9].max()
- vol_min = matches.iloc[:, 9].min()
- vol_range = abs(vol_max - vol_min)
- vol_move = abs(vol_range / avg_bin)
- cur_VOL_bin = round((avgVol - vol_min) / vol_move, 0)
- scen_VOL_bin = round((df.avgVol - vol_min) / vol_move, 0)
- VOLMatch = scen_VOL_bin == cur_VOL_bin
- itds_max = matches.iloc[:, 10].max()
- itds_min = matches.iloc[:, 10].min()
- itds_range = abs(itds_max - itds_min)
- itds_move = abs(itds_range / avg_bin)
- cur_ITDS_bin = round((ITDS - itds_min) / itds_move, 0)
- scen_ITDS_bin = round((df.ITDS - itds_min) / itds_move, 0)
- ITDSMatch = scen_ITDS_bin == cur_ITDS_bin
- ns_max = matches.iloc[:, 11].max()
- ns_min = matches.iloc[:, 11].min()
- ns_range = abs(ns_max - ns_min)
- ns_move = abs(ns_range / avg_bin)
- cur_NS_bin = round((NS - ns_min) / ns_move, 0)
- scen_NS_bin = round((df.NS - ns_min) / ns_move, 0)
- NSMatch = scen_NS_bin == cur_NS_bin
- ss_max = matches.iloc[:, 12].max()
- ss_min = matches.iloc[:, 12].min()
- ss_range = abs(ss_max - ss_min)
- ss_move = abs(ss_range / avg_bin)
- cur_SS_bin = round((SS - ss_min) / ss_move, 0)
- scen_SS_bin = round((df.SS - ss_min) / ss_move, 0)
- SSMatch = scen_SS_bin == cur_SS_bin
- aqr_max = matches.iloc[:, 13].max()
- aqr_min = matches.iloc[:, 13].min()
- aqr_range = abs(aqr_max - aqr_min)
- aqr_move = abs(aqr_range / avg_bin)
- cur_AQR_bin = round((AQR - aqr_min) / aqr_move, 0)
- scen_AQR_bin = round((df.avgQR - aqr_min) / aqr_move, 0)
- AQRMatch = scen_AQR_bin == cur_AQR_bin
- qrs_max = matches.iloc[:, 14].max()
- qrs_min = matches.iloc[:, 14].min()
- qrs_range = abs(qrs_max - qrs_min)
- qrs_move = abs(qrs_range / avg_bin)
- cur_QRS_bin = round((QRS - qrs_min) / qrs_move, 0)
- scen_QRS_bin = round((df.QRS_Index - qrs_min) / qrs_move, 0)
- QRSMatch = scen_QRS_bin == cur_QRS_bin
- alq_max = matches.iloc[:, 15].max()
- alq_min = matches.iloc[:, 15].min()
- alq_range = abs(alq_max - alq_min)
- alq_move = abs(alq_range / avg_bin)
- cur_ALQ_bin = round((AQR - alq_min) / alq_move, 0)
- scen_ALQ_bin = round((df.ALQ - alq_min) / alq_move, 0)
- ALQMatch = scen_ALQ_bin == cur_ALQ_bin
- udr_max = matches.iloc[:, 16].max()
- udr_min = matches.iloc[:, 16].min()
- udr_range = abs(udr_max - udr_min)
- udr_move = abs(udr_range / avg_bin)
- cur_UDR_bin = round((UDR - udr_min) / udr_move, 0)
- scen_UDR_bin = round((df.UDR - udr_min) / udr_move, 0)
- UDRMatch = scen_UDR_bin == cur_UDR_bin
- vsr_max = matches.iloc[:, 17].max()
- vsr_min = matches.iloc[:, 17].min()
- vsr_range = abs(vsr_max - vsr_min)
- vsr_move = abs(vsr_range / avg_bin)
- cur_VSR_bin = round((CVSR - vsr_min) / vsr_move, 0)
- scen_VSR_bin = round((df.VSR - vsr_min) / vsr_move, 0)
- VSRMatch = scen_VSR_bin == cur_VSR_bin
- avg_vsr_max = matches.iloc[:, 18].max()
- avg_vsr_min = matches.iloc[:, 18].min()
- avg_vsr_range = abs(avg_vsr_max - avg_vsr_min)
- avg_vsr_move = abs(avg_vsr_range / avg_bin)
- cur_avg_vsr_bin = round((AVSR - avg_vsr_min) / avg_vsr_move, 0)
- scen_avg_vsr_bin = round((df.avgVSR - avg_vsr_min) / avg_vsr_move, 0)
- AVGVSRMatch = scen_avg_vsr_bin == cur_avg_vsr_bin
- ##################################################################################################
- def outlier():
- outlier_count = int()
- for i in dist:
- if i >= .74:
- outlier_count += 1
- if outlier_count < 2 and outlier_count == 1:
- print('HAS OUTLIER')
- if outlier_count >= 2:
- print('HAS %s OUTLIERS' % outlier_count)
- return
- ###############################################################################
- avg_df_chgo = np.mean(df[match_time].ChgO)
- avg_df_chgo_dist = round(abs(ChgO - avg_df_chgo) / avg_df_chgo, 2)
- avg_df_mtr = np.mean(df[match_time].MTR)
- avg_df_mtr_dist = round(abs(MTR - avg_df_mtr) / avg_df_mtr, 2)
- avg_df_pg = np.mean(df[match_time].PG)
- avg_df_pg_dist = round(abs(PG - avg_df_pg) / avg_df_pg, 2)
- avg_df_dr = np.mean(df[match_time].DR)
- avg_df_dr_dist = round(abs(DR - avg_df_dr) / avg_df_dr, 2)
- avg_df_gr= np.mean(df[match_time].GR)
- avg_df_gr_dist = round(abs(GR - avg_df_gr) / avg_df_gr, 2)
- avg_df_gdr = np.mean(df[match_time].GDR)
- avg_df_gdr_dist = round(abs(GDR - avg_df_gdr) / avg_df_gdr, 2)
- avg_df_pvs = np.mean(df[match_time].PVS)
- avg_df_pvs_dist = round(abs(PVP - avg_df_pvs) / avg_df_pvs, 2)
- avg_df_alq = np.mean(df[match_time].ALQ)
- avg_df_alq_dist = round(abs(ALQ - avg_df_alq) / avg_df_alq, 2)
- avg_df_ss = np.mean(df[match_time].SS)
- avg_df_ss_dist = round(abs(SS - avg_df_ss) / avg_df_ss, 2)
- avg_df_vol = np.mean(df[match_time].avgVol)
- avg_df_avol_dist = round(abs(avgVol - avg_df_vol) / avg_df_vol, 2)
- avg_df_aqr = np.mean(df[match_time].avgQR)
- avg_df_aqr_dist = round(abs(AQR - avg_df_aqr) / avg_df_aqr, 2)
- avg_df_qrs = np.mean(df[match_time].QRS_Index)
- avg_df_qrs_dist = round(abs(QRS - avg_df_qrs) / avg_df_qrs, 2)
- avg_df_vsr = np.mean(df[match_time].VSR)
- avg_df_vsr_dist = round(abs(CVSR - avg_df_vsr) / avg_df_vsr, 2)
- avg_df_udr = np.mean(df[match_time].UDR)
- avg_df_udr_dist = round(abs(UDR - avg_df_udr) / avg_df_udr, 2)
- avg_df_drc = np.mean(df[match_time].DRC)
- avg_df_drc_dist = round(abs(DRC - avg_df_drc) / avg_df_drc, 2)
- #####################################################################
- avg_chgo = np.mean(df[match_time][ChgOMatch].ChgO)
- avg_chgo_dist = round(abs(ChgO - avg_chgo) / avg_chgo, 2)
- avg_mtr = np.mean(df[match_time][ChgOMatch].MTR)
- avg_mtr_dist = round(abs(MTR - avg_mtr) / avg_mtr, 2)
- avg_pg = np.mean(df[match_time][ChgOMatch].PG)
- avg_pg_dist = round(abs(PG - avg_pg) / avg_pg, 2)
- avg_dr = np.mean(df[match_time][ChgOMatch].DR)
- avg_dr_dist = round(abs(DR - avg_dr) / avg_dr, 2)
- avg_gr= np.mean(df[match_time][ChgOMatch].GR)
- avg_gr_dist = round(abs(GR - avg_gr) / avg_gr, 2)
- avg_gdr = np.mean(df[match_time][ChgOMatch].GDR)
- avg_gdr_dist = round(abs(GDR - avg_gdr) / avg_gdr, 2)
- avg_pvs = np.mean(df[match_time][ChgOMatch].PVS)
- avg_pvs_dist = round(abs(PVP - avg_pvs) / avg_pvs, 2)
- avg_alq = np.mean(df[match_time][ChgOMatch].ALQ)
- avg_alq_dist = round(abs(ALQ - avg_alq) / avg_alq, 2)
- avg_ss = np.mean(df[match_time][ChgOMatch].SS)
- avg_ss_dist = round(abs(SS - avg_ss) / avg_ss, 2)
- avg_vol = np.mean(df[match_time][ChgOMatch].avgVol)
- avg_avol_dist = round(abs(avgVol - avg_vol) / avg_vol, 2)
- avg_aqr = np.mean(df[match_time][ChgOMatch].avgQR)
- avg_aqr_dist = round(abs(AQR - avg_aqr) / avg_aqr, 2)
- avg_qrs = np.mean(df[match_time][ChgOMatch].QRS_Index)
- avg_qrs_dist = round(abs(QRS - avg_qrs) / avg_qrs, 2)
- avg_vsr = np.mean(df[match_time][ChgOMatch].VSR)
- avg_vsr_dist = round(abs(CVSR - avg_vsr) / avg_vsr, 2)
- avg_udr = np.mean(df[match_time][ChgOMatch].UDR)
- avg_udr_dist = round(abs(UDR - avg_udr) / avg_udr, 2)
- avg_drc = np.mean(df[match_time][ChgOMatch].DRC)
- avg_drc_dist = round(abs(DRC - avg_drc) / avg_drc, 2)
- ################################################################################
- oval_mtr_dist = round(avg_df_mtr_dist + avg_mtr_dist, 2)
- oval_pg_dist = round(avg_df_mtr_dist + avg_mtr_dist, 2)
- oval_dr_dist = round(avg_df_dr_dist + avg_dr_dist, 2)
- oval_gr_dist = round(avg_df_gr_dist + avg_gr_dist, 2)
- oval_gdr_dist = round(avg_df_gdr_dist + avg_gdr_dist, 2)
- oval_pvs_dist = round(avg_df_pvs_dist + avg_pvs_dist, 2)
- oval_alq_dist = round(avg_df_alq_dist + avg_alq_dist, 2)
- oval_avol_dist = round(avg_df_avol_dist + avg_avol_dist, 2)
- oval_ss_dist = round(avg_df_ss_dist + avg_ss_dist, 2)
- oval_aqr_dist = round(avg_df_aqr_dist + avg_aqr_dist, 2)
- oval_qrs_dist = round(avg_df_qrs_dist + avg_qrs_dist, 2)
- oval_vsr_dist = round(avg_df_vsr_dist + avg_vsr_dist, 2)
- oval_udr_dist = round(avg_df_udr_dist + avg_udr_dist, 2)
- oval_drc_dist = round(avg_df_drc_dist + avg_drc_dist, 2)
- dist = [avg_mtr_dist, avg_pg_dist, avg_dr_dist, avg_gr_dist, avg_gdr_dist, avg_avol_dist, avg_ss_dist, avg_aqr_dist, avg_qrs_dist, avg_vsr_dist, avg_udr_dist, avg_drc_dist]
- indent = ' '
- print('MTR'+indent+'PG'+indent+'DR'+indent+'GR'+indent+'GDR'+indent+'Vol'+indent+'ALQ'+indent+'SS'+indent+'QR'+indent+'QRS'+indent+'VSR'+indent+'UDR'+indent+'DRC')
- print(avg_mtr_dist, avg_pg_dist, avg_dr_dist, avg_gr_dist, avg_gdr_dist, avg_avol_dist, avg_alq_dist, avg_ss_dist, avg_aqr_dist, avg_qrs_dist, avg_vsr_dist, avg_udr_dist, avg_drc_dist)
- outlier()
- indt = ' '
- print('mtr'+indt+' pg'+indt+' dr'+indt+'gr'+indt+'gdr'+indt+'alq'+indt+'vol'+indt+' ss'+indt+'aqr'+indt+'qrs'+indt+'vsr'+indt+'udr'+indt+'drc')
- print(oval_mtr_dist, oval_pg_dist, oval_dr_dist, oval_gr_dist, oval_gdr_dist, oval_alq_dist, oval_avol_dist, oval_ss_dist, oval_aqr_dist, oval_qrs_dist, oval_vsr_dist, oval_udr_dist,oval_drc_dist)
- ChgORank = abs(ChgO - matches.ChgO).rank(ascending=True)
- ChgOMatch = ChgORank < 10
- ALQRank = abs(ALQ - matches.ALQ).rank(ascending=True)
- ALQMatch = ALQRank < 10
- QRSRank = abs(QRS - matches.QRS_Index).rank(ascending=True)
- QRSMatch = QRSRank < 10
- SSRank = abs(SS - matches.SS).rank(ascending=True)
- SSMatch = SSRank < 10
- DRRank = abs(DR - matches.DR).rank(ascending=True)
- DRMatch = DRRank < 10
- PGRank = abs(PG - matches.PG).rank(ascending=True)
- PGMatch = PGRank < 10
- VOLRank = abs(avgVol - matches.avgVol).rank(ascending=True)
- VOLMatch = VOLRank < 10
- VSRRank = abs(CVSR - matches.VSR).rank(ascending=True)
- VSRMatch = VSRRank < 10
- dict = {'MTR':oval_mtr_dist, 'PG':oval_pg_dist, 'DR':oval_dr_dist, 'GR':oval_gr_dist, 'GDR':oval_gdr_dist, 'ALQ':oval_alq_dist, 'AVOL':oval_avol_dist, 'SS':oval_ss_dist, 'AQR':oval_aqr_dist, 'QRS':oval_qrs_dist, 'VSR':oval_vsr_dist, 'UDR':oval_udr_dist,'DRC':oval_drc_dist}
- pm = []
- print('\n\n')
- for k,v in dict.items():
- if v >= 1.5:
- print(k, v)
- if k+'Match' == 'ALQMatch':
- print(matches[ALQMatch])
- if k+'Match' == 'QRSMatch':
- print(matches[QRSMatch])
- if k+'Match' == 'VSRMatch':
- print(matches[VSRMatch])
- if k+'Match' == 'AQRMatch':
- print(matches[AQRMatch])
- matches['VOL_QR'] = round(matches.avgVol / matches.avgQR) - matches.VSR
- matches['AQVSR_cor'] = round((matches.VSR * matches.avgQR) / matches.avgVol, 3)
- nvRank = abs(matches.AQVSR_cor - 0).rank(ascending=True)
- nvMatch = nvRank < 10
- print('\n\n\n')
- del matches['ITDS']
- del matches['NS']
- print(matches[nvMatch])
- print('\n')
- # curf = df1[['Date', 'Time', 'ChgO', 'PG', 'MTR', 'DR', 'GR', 'GDR', 'PVS', 'avgVol', 'ITDS', 'NS', 'SS', 'avgQR', 'QRS_Index', 'ALQ', 'UDR', 'VSR', 'VOL_QR']]
- print(df1[-1:])
- matches['VOL_QRRank'] = abs(matches.VOL_QR - VOL_QR).rank(ascending=True)
- VOL_QRMatch = matches.VOL_QRRank < 10
- print(matches[VOL_QRMatch])
- # print(matches[QRSMatch])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement