You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
def ticker_list(file) -> list:
with open(file, 'r') as fil:
data = fil.readlines()
for i in range(len(data)):
data[i] = data[i].strip()
return data
warning this function will take around 40 - 60 minutes to complete if comparing ~500 securities
def find_all_pairs(days: int, data_dir: str):
symbols = ticker_list()
# symbols = symbols[:10] # testing not 124000 possibilities
correlated = []
cointegrated = []
for i in range(len(symbols)):
for j in range(i + 1, len(symbols)):
if .95 < pearson_coor(Stock(symbols[i]).ranged_data_list('close', days),
Stock(symbols[j]).ranged_data_list('close', days)):
correlated.append([symbols[i], symbols[j]])
for i in range(len(correlated)):
pvalue = coint(Stock(correlated[i][0]).ranged_data_list('close', days),
Stock(correlated[i][1]).ranged_data_list('close', days))[1]
if pvalue < .005:
cointegrated.append([pvalue, correlated[i][0],correlated[i][1]])
return cointegrated
`
in function find_all_pairs, the only thing that needs to be changed is the way it accesses the data. I was using a class named Stock and that could return the last n days of close or open prices in a list.
`
MMM
ABT
ABBV
ABMD
ACN
ATVI
ADBE
AMD
AAP
AES
AET
AMG
AFL
A
APD
AKAM
ALK
ALB
ARE
ALXN
ALGN
ALLE
AGN
ADS
LNT
ALL
GOOGL
GOOG
MO
AMZN
AEE
AAL
AEP
AXP
AIG
AMT
AWK
AMP
ABC
AME
AMGN
APH
APC
ADI
ANSS
ANTM
AON
AOS
APA
AIV
AAPL
AMAT
APTV
ADM
ARNC
ANET
AJG
AIZ
T
ADSK
ADP
AZO
AVB
AVY
BHGE
BLL
BAC
BK
BAX
BBT
BDX
BRK.B
BBY
BIIB
BLK
HRB
BA
BKNG
BWA
BXP
BSX
BHF
BMY
AVGO
BR
BF.B
CHRW
COG
CDNS
CPB
COF
CAH
KMX
CCL
CAT
CBOE
CBRE
CBS
CELG
CNC
CNP
CTL
CERN
CF
SCHW
CHTR
CVX
CMG
CB
CHD
CI
XEC
CINF
CTAS
CSCO
C
CFG
CTXS
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
PSX
PNW
PXD
PNC
RL
PPG
PPL
PFG
PG
PGR
PLD
PRU
PEG
PSA
PHM
PVH
QRVO
PWR
QCOM
DGX
RJF
RTN
O
RHT
REG
REGN
RF
RSG
RMD
RHI
ROK
COL
ROL
ROP
ROST
RCL
CRM
SBAC
SCG
SLB
STX
SEE
SRE
SHW
SPG
SWKS
SLG
SNA
SO
LUV
SPGI
SWK
SBUX
STT
SRCL
SYK
STI
SIVB
SYMC
SYF
SNPS
SYY
TROW
TTWO
TPR
TGT
TEL
FTI
TXN
TXT
TMO
TIF
TWTR
TJX
TMK
TSS
TSCO
TDG
TRV
TRIP
FOXA
FOX
TSN
UDR
ULTA
USB
UAA
UA
UNP
UAL
UNH
UPS
URI
UTX
UHS
UNM
VFC
VLO
VAR
VTR
VRSN
VRSK
VZ
VRTX
VIAB
V
VNO
VMC
WMT
WBA
DIS
WM
WAT
WEC
WCG
WFC
WELL
WDC
WU
WRK
WY
WHR
WMB
WLTW
WYNN
XEL
XRX
XLNX
XYL
YUM
ZBH
ZION
ZTS
`
This is the file where it pulls the names of the stocks to compare
THanks! Lmk how it works
The text was updated successfully, but these errors were encountered:
`
@author Bryson Meiling
input is two tickers. The output is a correlation factor between -1 and 1
makes the two list the same length for the correlation calculation between -1 and 1
if one is longer, it will cut off the oldest data of the longer set
from statsmodels.tsa.stattools import coint
import math
def pearson_coor(data1: list, data2: list) -> float: # find the correlation of 2 list of even length
def average(x: list) -> float:
assert len(x) > 0
return float(sum(x)) / len(x)
def coint_test(ticker1: list, ticker2: list) -> float:
return coint(ticker1, ticker2)
def ticker_list(file) -> list:
with open(file, 'r') as fil:
data = fil.readlines()
for i in range(len(data)):
data[i] = data[i].strip()
return data
warning this function will take around 40 - 60 minutes to complete if comparing ~500 securities
def find_all_pairs(days: int, data_dir: str):
symbols = ticker_list()
# symbols = symbols[:10] # testing not 124000 possibilities
correlated = []
cointegrated = []
for i in range(len(symbols)):
for j in range(i + 1, len(symbols)):
if .95 < pearson_coor(Stock(symbols[i]).ranged_data_list('close', days),
Stock(symbols[j]).ranged_data_list('close', days)):
correlated.append([symbols[i], symbols[j]])
`
in function find_all_pairs, the only thing that needs to be changed is the way it accesses the data. I was using a class named Stock and that could return the last n days of close or open prices in a list.
`
MMM
ABT
ABBV
ABMD
ACN
ATVI
ADBE
AMD
AAP
AES
AET
AMG
AFL
A
APD
AKAM
ALK
ALB
ARE
ALXN
ALGN
ALLE
AGN
ADS
LNT
ALL
GOOGL
GOOG
MO
AMZN
AEE
AAL
AEP
AXP
AIG
AMT
AWK
AMP
ABC
AME
AMGN
APH
APC
ADI
ANSS
ANTM
AON
AOS
APA
AIV
AAPL
AMAT
APTV
ADM
ARNC
ANET
AJG
AIZ
T
ADSK
ADP
AZO
AVB
AVY
BHGE
BLL
BAC
BK
BAX
BBT
BDX
BRK.B
BBY
BIIB
BLK
HRB
BA
BKNG
BWA
BXP
BSX
BHF
BMY
AVGO
BR
BF.B
CHRW
COG
CDNS
CPB
COF
CAH
KMX
CCL
CAT
CBOE
CBRE
CBS
CELG
CNC
CNP
CTL
CERN
CF
SCHW
CHTR
CVX
CMG
CB
CHD
CI
XEC
CINF
CTAS
CSCO
C
CFG
CTXS
CLX
CME
CMS
KO
CTSH
CL
CMCSA
CMA
CAG
CXO
COP
ED
STZ
COO
CPRT
GLW
COST
COTY
CCI
CSX
CMI
CVS
DHI
DHR
DRI
DVA
DE
DAL
XRAY
DVN
DLR
DFS
DISCA
DISCK
DISH
DG
DLTR
D
DOV
DWDP
DTE
DRE
DUK
DXC
ETFC
EMN
ETN
EBAY
ECL
EIX
EW
EA
EMR
ETR
EOG
EFX
EQIX
EQR
ESS
EL
EVRG
ES
RE
EXC
EXPE
EXPD
ESRX
EXR
XOM
FFIV
FB
FAST
FRT
FDX
FIS
FITB
FE
FISV
FLT
FLIR
FLS
FLR
FMC
FL
F
FTNT
FTV
FBHS
BEN
FCX
GPS
GRMN
IT
GD
GE
GIS
GM
GPC
GILD
GPN
GS
GT
GWW
HAL
H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K
VZ
VRTX
VIAB
V
VNO
VMC
WMT
WBA
DIS
WM
WAT
WEC
WCG
WFC
WELL
WDC
WU
WRK
WY
WHR
WMB
WLTW
WYNN
XEL
XRX
XLNX
XYL
YUM
ZBH
ZION
ZTS
`
This is the file where it pulls the names of the stocks to compare
THanks! Lmk how it works
The text was updated successfully, but these errors were encountered: