-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstockprocess.py
68 lines (48 loc) · 1.52 KB
/
stockprocess.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import networkx as nx
import torch
import matplotlib.dates as mdates
def readdata():
df=pd.read_csv('data/stock/daily_data_1990.csv',sep=',')
return df
def df2data(df):
delta = 30
start = 0
Y = []
fulldata = df.iloc[:,2:].to_numpy()
N = len(fulldata[0])//delta
for i in range(N):
slicei = fulldata[:,start:(start+delta)]
Y.append(slicei)
start += delta
return Y
def loadstocks(device):
df = readdata()
Y = df2data(df)
Y=[torch.tensor(dfi,device=device).float() for dfi in Y]
return Y, df
def plotall(rawdf, lf):
fig, ax = plt.subplots()
score = torch.cat(lf).detach().numpy()
years = pd.to_datetime(list(rawdf.columns.values)[2:], format='%Y-%m-%d')
commence = 2000
fini = 6000#len(score)
ax.plot(years[commence:fini], score[commence:fini], label="Anomaly Score")
ax.tick_params(axis='y')
ax2 = ax.twinx()
dfsp500=pd.read_csv('stock/sp500.csv',sep=',')
sp500 = dfsp500['Closing'].to_numpy()
ax2.plot(years[commence:fini], sp500[commence:fini], color='orange', label='SP500')
ax2.tick_params(axis='y')
plt.legend()
plt.xticks(rotation=-45)
plt.gcf().autofmt_xdate()
plt.savefig('stock.png',bbox_inches='tight')
return
def see_stocks():
df=pd.read_csv('stock/sp500.csv',sep=',')
print(df['Return'])
plt.plot(df['Closing'])
plt.savefig('stock.png',bbox_inches='tight')