import numpy as np import matplotlib.pyplot as plt import pandas as pd from matplotlib import interactive, cm from time import time #from math import log10 interactive(True) def dataset(nr): if (nr==1): name = "AAPL-84-19.csv" c = 0 elif(nr==2): name = "INTC-84-19.csv" c = 1900 elif(nr==3): name = "IXIC-84-19.csv" c = 1900 x = pd.read_csv(name) print('dataset: ',name) # preview the first and last lines of data print(x.head(2)) print(x.tail(2)) print('type & size of x ',type(x),np.size(x)) y = x.as_matrix() print('type & size of y ',type(y),np.size(y)) print('y[0,...]',y[0,:] ) Nd = np.size(y)//7 print('Nd =',Nd) t = np.zeros(Nd,dtype=int) ymd = np.ndarray((Nd,3),dtype=int) po = y[:,1]*100; pc = y[:,4]*100 ph = y[:,2]*100; vol = y[:,6] pl = y[:,3]*100 for n in range(Nd): # decode dates, store numeric values in ymd array date = y[n,0] ymd[n,0] = yy = int(np.array(date.split('-')[0])) - c ymd[n,1] = mm = int(np.array(date.split('-')[1])) ymd[n,2] = dd = int(np.array(date.split('-')[2])) t[n] = n print('n=0 (beg.):', ymd[0,0], ymd[0,1], ymd[0,2],'\n') return(Nd,y,ymd) ''' read and process AAPL, INTC, and IXIC historical data in .csv format, 1984-2019. ''' # AAPL NdA,yA,ymdA = dataset(1) # INTC NdI,yI,ymdI = dataset(2) # IXIC = NASDAQ NdX,yX,ymdX = dataset(3) N = np.size(yA)//7 # of rows, numbered from 0 to N-1 print('N=',N) pA = np.array(yA[:,4],dtype=float) # without explicit dtype=float there will be pI = np.array(yI[:,4],dtype=float) # problems with math functions like np.log10(pA) pX = np.array(yX[:,4],dtype=float) print('logA',type(pA),type(np.array([1,3.3])),np.log10(np.array([1,3.3]))) A = (pA/pA[0]) I = (pI/pI[0]) X = (pX/pX[0]) print(np.size(A),np.size(ymdA)) print(np.size(I),np.size(ymdI)) print(np.size(X),np.size(ymdX)) t = np.linspace(-N/240,0,N) # take a look at the data plt.semilogy(t,A,color=(1,0,0)) plt.semilogy(t,I,color=(0,0,1)) plt.semilogy(t,X,color=(0,0,0)) plt.title('INTC, IXIC=NASDAQ, AAPL') plt.xlabel(' time [yr]') plt.ylabel(' price normalizd to initial price') plt.grid() plt.show() input(' ok?') # plt.cla() plt.semilogy(t,A/X,color=(1,0,0)) plt.semilogy(t,I/X,color=(0,0,1)) plt.semilogy(t,X/X,color=(0,0,0)) plt.title('INTC/IXIC, AAPL/IXIC') plt.xlabel(' time [yr]') plt.ylabel(' price relative to market average') plt.grid() plt.show() input(' ok?') # a bit of analysis lA = np.log10(A) # (...)