FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
QuantSoftwareToolkit/Examples/Basic/tutorial1.py at master · testmana2/QuantSoftwareToolkit · GitHub
testmana2
/
QuantSoftwareToolkit
Public
forked from
QuantSoftware/QuantSoftwareToolkit
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
QuantSoftwareToolkit
/
Examples
/
Basic
/
tutorial1.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
114 lines (89 loc) · 3.57 KB
Breadcrumbs
QuantSoftwareToolkit
/
Examples
/
Basic
/
tutorial1.py
Copy path
File metadata and controls
114 lines (89 loc) · 3.57 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'''
(c) 2011, 2012 Georgia Tech Research Corporation
This source code is released under the New BSD license. Please see
http://wiki.quantsoftware.org/index.php?title=QSTK_License
for license details.
Created on January, 24, 2013
@author: Sourabh Bajaj
@contact: sourabhbajaj@gatech.edu
@summary: Example tutorial code.
'''
# QSTK Imports
import
QSTK
.
qstkutil
.
qsdateutil
as
du
import
QSTK
.
qstkutil
.
tsutil
as
tsu
import
QSTK
.
qstkutil
.
DataAccess
as
da
# Third Party Imports
import
datetime
as
dt
import
matplotlib
.
pyplot
as
plt
import
pandas
as
pd
print
"Pandas Version"
,
pd
.
__version__
def
main
():
''' Main Function'''
# List of symbols
ls_symbols
=
[
"AAPL"
,
"GLD"
,
"GOOG"
,
"$SPX"
,
"XOM"
]
# Start and End date of the charts
dt_start
=
dt
.
datetime
(
2006
,
1
,
1
)
dt_end
=
dt
.
datetime
(
2010
,
12
,
31
)
# We need closing prices so the timestamp should be hours=16.
dt_timeofday
=
dt
.
timedelta
(
hours
=
16
)
# Get a list of trading days between the start and the end.
ldt_timestamps
=
du
.
getNYSEdays
(
dt_start
,
dt_end
,
dt_timeofday
)
# Creating an object of the dataaccess class with Yahoo as the source.
c_dataobj
=
da
.
DataAccess
(
'Yahoo'
)
# Keys to be read from the data, it is good to read everything in one go.
ls_keys
=
[
'open'
,
'high'
,
'low'
,
'close'
,
'volume'
,
'actual_close'
]
# Reading the data, now d_data is a dictionary with the keys above.
# Timestamps and symbols are the ones that were specified before.
ldf_data
=
c_dataobj
.
get_data
(
ldt_timestamps
,
ls_symbols
,
ls_keys
)
d_data
=
dict
(
zip
(
ls_keys
,
ldf_data
))
# Filling the data for NAN
for
s_key
in
ls_keys
:
d_data
[
s_key
]
=
d_data
[
s_key
].
fillna
(
method
=
'ffill'
)
d_data
[
s_key
]
=
d_data
[
s_key
].
fillna
(
method
=
'bfill'
)
d_data
[
s_key
]
=
d_data
[
s_key
].
fillna
(
1.0
)
# Getting the numpy ndarray of close prices.
na_price
=
d_data
[
'close'
].
values
# Plotting the prices with x-axis=timestamps
plt
.
clf
()
plt
.
plot
(
ldt_timestamps
,
na_price
)
plt
.
legend
(
ls_symbols
)
plt
.
ylabel
(
'Adjusted Close'
)
plt
.
xlabel
(
'Date'
)
plt
.
savefig
(
'adjustedclose.pdf'
,
format
=
'pdf'
)
# Normalizing the prices to start at 1 and see relative returns
na_normalized_price
=
na_price
/
na_price
[
0
, :]
# Plotting the prices with x-axis=timestamps
plt
.
clf
()
plt
.
plot
(
ldt_timestamps
,
na_normalized_price
)
plt
.
legend
(
ls_symbols
)
plt
.
ylabel
(
'Normalized Close'
)
plt
.
xlabel
(
'Date'
)
plt
.
savefig
(
'normalized.pdf'
,
format
=
'pdf'
)
# Copy the normalized prices to a new ndarry to find returns.
na_rets
=
na_normalized_price
.
copy
()
# Calculate the daily returns of the prices. (Inplace calculation)
# returnize0 works on ndarray and not dataframes.
tsu
.
returnize0
(
na_rets
)
# Plotting the plot of daily returns
plt
.
clf
()
plt
.
plot
(
ldt_timestamps
[
0
:
50
],
na_rets
[
0
:
50
,
3
])
# $SPX 50 days
plt
.
plot
(
ldt_timestamps
[
0
:
50
],
na_rets
[
0
:
50
,
4
])
# XOM 50 days
plt
.
axhline
(
y
=
0
,
color
=
'r'
)
plt
.
legend
([
'$SPX'
,
'XOM'
])
plt
.
ylabel
(
'Daily Returns'
)
plt
.
xlabel
(
'Date'
)
plt
.
savefig
(
'rets.pdf'
,
format
=
'pdf'
)
# Plotting the scatter plot of daily returns between XOM VS $SPX
plt
.
clf
()
plt
.
scatter
(
na_rets
[:,
3
],
na_rets
[:,
4
],
c
=
'blue'
)
plt
.
ylabel
(
'XOM'
)
plt
.
xlabel
(
'$SPX'
)
plt
.
savefig
(
'scatterSPXvXOM.pdf'
,
format
=
'pdf'
)
# Plotting the scatter plot of daily returns between $SPX VS GLD
plt
.
clf
()
plt
.
scatter
(
na_rets
[:,
3
],
na_rets
[:,
1
],
c
=
'blue'
)
# $SPX v GLD
plt
.
ylabel
(
'GLD'
)
plt
.
xlabel
(
'$SPX'
)
plt
.
savefig
(
'scatterSPXvGLD.pdf'
,
format
=
'pdf'
)
if
__name__
==
'__main__'
:
main
()
Back
|
FazBrowse Home
|
New Git URL