IVtws 台灣期貨交易所報價爬蟲即時波動率計算與視覺化
python 2.7 or 3.x
pandas >= 0.18
numpy
matplotlib
selenium and PhantomJS
requests
plotly >= 1.9 (optional)
ipywidgets (optional)
bqplot (optional)
colour (optional)
git clone https://github.com/Yvictor/IVtws.git
cd IVtws
jupyter Notebook
from IVtws import IVstream
form IPython .display import display
import matplotlib .pyplot as plt
import plotly offline to build interactivate plot
#with plotly interactive plot
from plotly .tools import mpl_to_plotly
from plotly .offline import iplot ,iplot_mpl ,init_notebook_mode
init_notebook_mode ()
IVtw = IVstream ((8 ,45 ),(13 ,45 ))
IVtw .init_table (select_settled = 0 )#select_settled is the selectbox of option qoute's settlement date default is the first week option
update data and recalculate the Implied Volitity
買進
賣出
成交
成交價
漲跌
內含價值
時間價值
隱含波動率
組合價
總量
時間
TCUL
履約價
15
270.000
272.000
272.000
272.0
-37.000
268.3
3.7
13.84
9168.3
300
2016-10-14 13:44:29
3.001722
8900
17
178.000
179.000
178.000
178.0
-34.000
167.5
10.5
13.51
9167.5
988
2016-10-14 13:44:36
3.001333
9000
18
134.000
136.000
135.000
135.0
-36.000
117.0
18.0
13.24
9167.0
1142
2016-10-14 13:44:51
3.000500
9050
19
95.000
96.000
96.000
96.0
-34.000
68.0
28.0
12.54
9168.0
8657
2016-10-14 13:44:52
3.000444
9100
20
62.000
63.000
63.000
63.0
-33.000
17.0
46.0
12.26
9167.0
16032
2016-10-14 13:44:53
3.000389
9150
21
39.000
39.500
39.000
39.0
-26.000
0.0
39.0
12.05
9169.0
49552
2016-10-14 13:44:52
3.000444
9200
22
20.500
21.000
20.500
20.5
-20.500
0.0
20.5
11.72
9167.5
47770
2016-10-14 13:44:53
3.000389
9250
23
10.000
10.500
10.000
10.0
-13.000
0.0
10.0
11.58
9168.0
43563
2016-10-14 13:44:53
3.000389
9300
24
4.100
4.200
4.100
4.1
-7.400
0.0
4.1
11.29
9169.1
21033
2016-10-14 13:44:52
3.000444
9350
25
1.600
1.700
1.700
1.7
-2.800
0.0
1.7
11.57
9165.7
21693
2016-10-14 13:44:53
3.000389
9400
履約價
買進
賣出
成交價
成交
內含價值
時間價值
隱含波動率
組合價
漲跌
總量
TCUL
時間
11
8500
0.500
0.600
0.6
0.600
0.0
0.6
29.08
9189.4
-0.300
1926
3.001111
2016-10-14 13:44:40
12
8600
0.800
0.900
0.8
0.800
0.0
0.8
26.08
9189.2
-0.400
2605
3.000778
2016-10-14 13:44:46
13
8700
1.300
1.400
1.4
1.400
0.0
1.4
23.51
9181.6
-0.800
5801
3.000944
2016-10-14 13:44:43
14
8800
2.100
2.200
2.1
2.100
0.0
2.1
19.87
9167.9
-0.900
6778
3.000389
2016-10-14 13:44:53
15
8900
3.700
3.800
3.7
3.700
0.0
3.7
17.00
9168.3
-1.400
19534
3.000389
2016-10-14 13:44:53
16
8950
6.000
6.100
6.0
6.000
0.0
6.0
16.88
9182.0
-2.400
9207
3.000722
2016-10-14 13:44:47
17
9000
10.000
10.500
10.5
10.500
0.0
10.5
15.57
9167.5
-3.000
21161
3.000389
2016-10-14 13:44:53
18
9050
17.500
18.000
18.0
18.000
0.0
18.0
15.05
9167.0
-2.000
18981
3.000444
2016-10-14 13:44:52
19
9100
27.500
28.000
28.0
28.000
0.0
28.0
14.17
9168.0
-1.000
40078
3.000444
2016-10-14 13:44:52
20
9150
45.500
46.000
46.0
46.000
0.0
46.0
13.81
9167.0
2.500
37940
3.000444
2016-10-14 13:44:52
21
9200
70.000
71.000
70.0
70.000
31.0
39.0
13.57
9169.0
7.000
36969
3.000389
2016-10-14 13:44:53
22
9250
103.000
104.000
103.0
103.000
82.5
20.5
13.28
9167.5
13.000
12599
3.000500
2016-10-14 13:44:51
23
9300
141.000
142.000
142.0
142.000
132.0
10.0
13.28
9168.0
21.000
7654
3.000611
2016-10-14 13:44:49
24
9350
181.000
188.000
185.0
185.000
180.9
4.1
13.33
9169.1
24.000
1206
3.000389
2016-10-14 13:44:53
25
9400
232.000
239.000
236.0
236.000
234.3
1.7
14.29
9165.7
36.000
668
3.001722
2016-10-14 13:44:29
quick plot with matplotlib inline
fig ,ax = plt .subplots (1 )
Call = IVtw .Call [IVtw .Call ['內含價值' ]< 450 ].set_index ('履約價' ,drop = False )
Put = IVtw .Put [IVtw .Put ['內含價值' ]< 450 ].set_index ('履約價' ,drop = False )
Call ['隱含波動率' ].plot (ax = ax ,figsize = (13 ,6 ),c = 'r' ,label = 'CallIV' ,marker = 'o' )
Put ['隱含波動率' ].plot (ax = ax ,figsize = (13 ,6 ),c = 'g' ,label = 'PutIV' ,marker = 'o' )
interactive visualization with plotly
fig ,ax = plt .subplots (1 )
Call = IVtw .Call [IVtw .Call ['內含價值' ]< 500 ].set_index ('履約價' ,drop = False )
Put = IVtw .Put [IVtw .Put ['內含價值' ]< 500 ].set_index ('履約價' ,drop = False )
Call ['隱含波動率' ].plot (ax = ax ,figsize = (13 ,6 ),c = 'r' ,label = 'CallIV' )#,marker='o')
Put ['隱含波動率' ].plot (ax = ax ,figsize = (13 ,6 ),c = 'g' ,label = 'PutIV' )#,marker='o')
Call .plot .scatter (ax = ax , x = '履約價' ,y = '隱含波動率' ,s = Call ['時間價值' ]* 5 ,c = (0.7 ,0.3 ,0.3 ),edgecolor = (0.7 ,0.3 ,0.3 ))
Put .plot .scatter (ax = ax , x = '履約價' ,y = '隱含波動率' ,s = Put ['時間價值' ]* 5 ,c = (0.3 ,0.7 ,0.3 ),edgecolor = (0.3 ,0.7 ,0.3 ))
Call .plot .scatter (ax = ax , x = '履約價' ,y = '隱含波動率' ,s = Call ['內含價值' ],c = (0.7 ,0.7 ,0.9 ),edgecolor = (0.7 ,0.3 ,0.3 ))
Put .plot .scatter (ax = ax , x = '履約價' ,y = '隱含波動率' ,s = Put ['內含價值' ],c = (0.7 ,0.7 ,0.9 ),edgecolor = (0.3 ,0.7 ,0.3 ))
plotly_fig = mpl_to_plotly (fig )
plotly_fig ['layout' ]['showlegend' ] = True
iplot (plotly_fig )
IVtw .CallIVtable .drop_duplicates ().plot (figsize = (13 ,6 ),grid = True ,
color = [i .hex for i in list (Color (rgb = (0.45 ,0.55 ,0.75 )).range_to (Color (rgb = (0.75 ,0 ,0 )), len (IVtw .CallIVtable .columns .tolist ())))])
IVtw .PutIVtable .drop_duplicates ().plot (figsize = (13 ,6 ),grid = True ,
color = [i .hex for i in list (Color (rgb = (0.45 ,0.55 ,0.75 )).range_to (Color (rgb = (0.75 ,0 ,0 )), len (IVtw .PutIVtable .columns .tolist ())))])
Plot Option curve with ipywidgets
from ipywidgets import interactive ,IntSlider ,FloatSlider ,Dropdown ,Button ,fixed ,HBox ,VBox ,Layout
widg = interactive (IVtw .creatSTwithPlot ,futshare = IntSlider (min = - 5 ,max = 5 ,step = 1 ),
Cal1 = IVtw .Callless ['履約價' ].tolist (),c1share = IntSlider (min = - 5 ,max = 5 ,step = 1 ),
Cal2 = IVtw .Callless ['履約價' ].tolist (),c2share = IntSlider (min = - 5 ,max = 5 ,step = 1 ),
Put1 = IVtw .Putless ['履約價' ].tolist (),p1share = IntSlider (min = - 5 ,max = 5 ,step = 1 ),
Put2 = IVtw .Putless ['履約價' ].tolist (),p2share = IntSlider (min = - 5 ,max = 5 ,step = 1 ),
showrange = IntSlider (min = 200 , max = 500 ,step = 50 ,value = 320 ),
up = IntSlider (min = 100 ,max = 500 ,step = 50 ),
down = IntSlider (min = 100 ,max = 500 ,step = 50 ),
customcur = FloatSlider (min = 0.1 ,max = 0.9 ,step = 0.1 ,value = 0.5 ),
risk_free_rate = FloatSlider (min = 0.010 ,max = 0.050 ,step = 0.005 ,value = 0.0136 ))#,__manual=True)#0.0136
#.layout = Layout(display='display', justify_content= 'space-between', align_items='center', width='100%')
HBox ([VBox (widg .children [:5 ]),VBox (widg .children [5 :10 ]),VBox (widg .children [10 :])])
[['Call', 9400, 1.7, 9165.7000000000007, 1], ['Put', 9050, 18.0, 9167.0, -1]]