|
52 | 52 | ###### Looking at realized and implied volatility over GBPUSD in the overnight (ON) tenor |
53 | 53 | if run_example == 1 or run_example == 0: |
54 | 54 | # Download the whole all market data for GBPUSD for pricing options (vol surface) |
55 | | - md_request = MarketDataRequest(start_date='01 Jun 2016', finish_date='02 Jul 2016', |
56 | | - data_source='bloomberg', cut='10AM', category='fx-vol-market', |
| 55 | + md_request = MarketDataRequest(start_date='01 Jun 2016', |
| 56 | + finish_date='02 Jul 2016', |
| 57 | + data_source='bloomberg', cut='10AM', |
| 58 | + category='fx-vol-market', |
57 | 59 | tickers=['GBPUSD'], |
58 | 60 | cache_algo='cache_algo_return') |
59 | 61 |
|
60 | 62 | market_df = market.fetch_market(md_request) |
61 | 63 |
|
62 | 64 | # Download FX tick data for GBPUSD over Brexit vote and then convert into 1 minute data (open/high/low/close) |
63 | 65 | # which are necessary for calculating realised volatility |
64 | | - md_request = MarketDataRequest(start_date='01 Jun 2016', finish_date='02 Jul 2016', |
65 | | - data_source='dukascopy', freq='tick', category='fx', fields=['bid', 'ask'], |
| 66 | + md_request = MarketDataRequest(start_date='01 Jun 2016', |
| 67 | + finish_date='02 Jul 2016', |
| 68 | + data_source='dukascopy', freq='tick', |
| 69 | + category='fx', fields=['bid', 'ask'], |
66 | 70 | tickers=['GBPUSD'], |
67 | 71 | cache_algo='cache_algo_return') |
68 | 72 |
|
69 | 73 | from findatapy.timeseries import Calculations |
| 74 | + |
70 | 75 | calc = Calculations() |
71 | 76 |
|
72 | 77 | tick_data = market.fetch_market(md_request) |
73 | | - intraday_spot_df = calc.resample_tick_data_ohlc(tick_data, 'GBPUSD', freq='1min') |
| 78 | + intraday_spot_df = calc.resample_tick_data_ohlc(tick_data, 'GBPUSD', |
| 79 | + freq='1min') |
74 | 80 |
|
75 | | - vol_stats = VolStats(market_df=market_df, intraday_spot_df=intraday_spot_df) |
| 81 | + vol_stats = VolStats(market_df=market_df, |
| 82 | + intraday_spot_df=intraday_spot_df) |
76 | 83 |
|
77 | | - realized_vol = vol_stats.calculate_realized_vol('GBPUSD', tenor_label="ON", freq='intraday', freq_min_mult=1, |
78 | | - hour_of_day=10, minute_of_day=0, field='close', timezone_hour_minute='America/New_York') * 100 |
| 84 | + realized_vol = vol_stats.calculate_realized_vol('GBPUSD', tenor_label="ON", |
| 85 | + freq='intraday', |
| 86 | + freq_min_mult=1, |
| 87 | + hour_of_day=10, |
| 88 | + minute_of_day=0, |
| 89 | + field='close', |
| 90 | + timezone_hour_minute='America/New_York') * 100 |
79 | 91 |
|
80 | 92 | implied_vol = pd.DataFrame(market_df['GBPUSDVON.close']) |
81 | 93 |
|
82 | | - vrp = vol_stats.calculate_vol_risk_premium('GBPUSD', tenor_label='ON', implied_vol=implied_vol, realized_vol=realized_vol) |
| 94 | + vrp = vol_stats.calculate_vol_risk_premium('GBPUSD', tenor_label='ON', |
| 95 | + implied_vol=implied_vol, |
| 96 | + realized_vol=realized_vol) |
83 | 97 |
|
84 | 98 | style = Style() |
85 | 99 |
|
|
99 | 113 |
|
100 | 114 | # Download FX tick data for GBPUSD over Brexit vote and then convert into 1 minute data (open/high/low/close) |
101 | 115 | # which are necessary for calculating realised volatility |
102 | | - md_request = MarketDataRequest(start_date='01 Jun 2016', finish_date='02 Jul 2016', |
103 | | - data_source='dukascopy', freq='tick', category='fx', fields=['bid', 'ask'], |
| 116 | + md_request = MarketDataRequest(start_date='01 Jun 2016', |
| 117 | + finish_date='02 Jul 2016', |
| 118 | + data_source='dukascopy', freq='tick', |
| 119 | + category='fx', fields=['bid', 'ask'], |
104 | 120 | tickers=['GBPUSD'], |
105 | 121 | cache_algo='cache_algo_return') |
106 | 122 |
|
107 | 123 | from findatapy.timeseries import Calculations |
| 124 | + |
108 | 125 | calc = Calculations() |
109 | 126 |
|
110 | | - intraday_spot_df = calc.resample_tick_data_ohlc(market.fetch_market(md_request), 'GBPUSD', freq='1min')['GBPUSD.close'] |
| 127 | + intraday_spot_df = \ |
| 128 | + calc.resample_tick_data_ohlc(market.fetch_market(md_request), 'GBPUSD', |
| 129 | + freq='1min')['GBPUSD.close'] |
111 | 130 |
|
112 | 131 | vol_stats = VolStats() |
113 | 132 |
|
|
117 | 136 | realized_vol = [] |
118 | 137 |
|
119 | 138 | for min in minute_frequencies: |
120 | | - min_df = pd.DataFrame(intraday_spot_df.resample(str(min) + 'min').last().dropna()) |
| 139 | + min_df = pd.DataFrame( |
| 140 | + intraday_spot_df.resample(str(min) + 'min').last().dropna()) |
121 | 141 |
|
122 | 142 | rv = vol_stats.calculate_realized_vol('GBPUSD', spot_df=min_df, |
123 | | - tenor_label="ON", freq='intraday', freq_min_mult=min, |
124 | | - hour_of_day=10, minute_of_day=0, field='close', timezone_hour_minute='America/New_York') * 100 |
| 143 | + tenor_label="ON", |
| 144 | + freq='intraday', |
| 145 | + freq_min_mult=min, |
| 146 | + hour_of_day=10, minute_of_day=0, |
| 147 | + field='close', |
| 148 | + timezone_hour_minute='America/New_York') * 100 |
125 | 149 |
|
126 | | - rv.columns=[str(min) + 'min'] |
| 150 | + rv.columns = [str(min) + 'min'] |
127 | 151 |
|
128 | 152 | realized_vol.append(rv) |
129 | 153 |
|
|
144 | 168 | # Download the whole all market data for GBPUSD for pricing options (vol surface) |
145 | 169 | # Note: 10AM prints for vol no longer published by Bloomberg, so later values are a weighted average of TOK and LDN |
146 | 170 | # closes |
147 | | - md_request = MarketDataRequest(start_date='01 May 2016', finish_date='02 Jul 2016', |
148 | | - data_source='bloomberg', cut='10AM', category='fx-vol-market', |
| 171 | + md_request = MarketDataRequest(start_date='01 May 2016', |
| 172 | + finish_date='02 Jul 2016', |
| 173 | + data_source='bloomberg', cut='10AM', |
| 174 | + category='fx-vol-market', |
149 | 175 | tickers=['GBPUSD'], |
150 | 176 | cache_algo='cache_algo_return') |
151 | 177 |
|
152 | 178 | market_df = market.fetch_market(md_request) |
153 | 179 |
|
154 | 180 | from findatapy.timeseries import Calculations |
| 181 | + |
155 | 182 | calc = Calculations() |
156 | 183 |
|
157 | 184 | vol_stats = VolStats(market_df=market_df) |
158 | 185 |
|
159 | | - implied_addon = vol_stats.calculate_implied_vol_addon('GBPUSD', tenor_label='ON').dropna() |
| 186 | + implied_addon = vol_stats.calculate_implied_vol_addon('GBPUSD', |
| 187 | + tenor_label='ON').dropna() |
160 | 188 |
|
161 | 189 | style = Style() |
162 | 190 |
|
|
169 | 197 |
|
170 | 198 | ###### Look at the statistics for recent period for EURUSD comparing implied vs realized |
171 | 199 | if run_example == 4 or run_example == 0: |
172 | | - |
173 | 200 | import datetime |
174 | 201 | from datetime import timedelta |
175 | 202 |
|
|
185 | 212 |
|
186 | 213 | # Download the whole all market data for pricing options (vol surface) |
187 | 214 | md_request = MarketDataRequest(start_date=month_before, finish_date=today, |
188 | | - data_source='bloomberg', freq='intraday', fields='open', |
189 | | - tickers=[asset + 'VON'], vendor_tickers=[asset + 'VON BGN Curncy'], |
| 215 | + data_source='bloomberg', freq='intraday', |
| 216 | + fields='open', |
| 217 | + tickers=[asset + 'VON'], |
| 218 | + vendor_tickers=[asset + 'VON BGN Curncy'], |
190 | 219 | cache_algo='cache_algo_return') |
191 | 220 |
|
192 | 221 | from findatapy.timeseries import Calculations, Filter |
| 222 | + |
193 | 223 | calc = Calculations() |
194 | 224 | filter = Filter() |
195 | 225 |
|
196 | 226 | freq_min_mult = 5 |
197 | 227 |
|
198 | 228 | # Resample into 1 minute data and fill down all points |
199 | | - implied_vol_df = market.fetch_market(md_request)[asset +'VON.open'].resample('1min').first().fillna(method='ffill') |
| 229 | + implied_vol_df = market.fetch_market(md_request)[ |
| 230 | + asset + 'VON.open'].resample('1min').first().fillna(method='ffill') |
200 | 231 |
|
201 | 232 | # Filter data by 1000 New York time, and return back to UTC, remove any out of trading hours |
202 | 233 | # Then strip of time of day from the timestamp |
203 | | - implied_vol_df = filter.filter_time_series_by_time_of_day_timezone(10, 0, implied_vol_df, timezone_of_snap='America/New_York') |
| 234 | + implied_vol_df = filter.filter_time_series_by_time_of_day_timezone(10, 0, |
| 235 | + implied_vol_df, |
| 236 | + timezone_of_snap='America/New_York') |
204 | 237 | implied_vol_df = filter.remove_out_FX_out_of_hours(implied_vol_df) |
205 | 238 | implied_vol_df.index = pd.to_datetime(implied_vol_df.index.date) |
206 | 239 | implied_vol_df = pd.DataFrame(implied_vol_df) |
207 | 240 | implied_vol_df.columns = [asset + 'VON.close'] |
208 | 241 |
|
209 | 242 | # Download FX intraday spot data, which will be used to calculate realized volatility |
210 | | - md_request.tickers = asset; md_request.vendor_tickers = asset + ' BGN Curncy' |
211 | | - intraday_spot_df = market.fetch_market(md_request).resample(str(freq_min_mult) + 'min').first() |
212 | | - intraday_spot_df = filter.remove_out_FX_out_of_hours(intraday_spot_df).dropna() |
| 243 | + md_request.tickers = asset; |
| 244 | + md_request.vendor_tickers = asset + ' BGN Curncy' |
| 245 | + intraday_spot_df = market.fetch_market(md_request).resample( |
| 246 | + str(freq_min_mult) + 'min').first() |
| 247 | + intraday_spot_df = filter.remove_out_FX_out_of_hours( |
| 248 | + intraday_spot_df).dropna() |
213 | 249 | intraday_spot_df.columns = [asset + '.close'] |
214 | 250 |
|
215 | 251 | vol_stats = VolStats() |
216 | 252 |
|
217 | 253 | # Calculate realized vol with the intraday data, with daily cutoffs |
218 | 254 | realized_vol = vol_stats.calculate_realized_vol( |
219 | | - asset, tenor_label='ON', spot_df=intraday_spot_df, hour_of_day=10, minute_of_day=0, |
220 | | - freq='intraday', timezone_hour_minute='America/New_York', freq_min_mult=freq_min_mult) * 100.0 |
221 | | - implied_vol_addon = vol_stats.calculate_implied_vol_addon(asset, implied_vol=implied_vol_df, tenor_label='ON', |
222 | | - adj_ON_friday=True).dropna() |
223 | | - |
224 | | - vrp = vol_stats.calculate_vol_risk_premium(asset, tenor_label='ON', implied_vol=implied_vol_df, realized_vol=realized_vol, |
225 | | - adj_ON_friday=True) |
| 255 | + asset, tenor_label='ON', spot_df=intraday_spot_df, hour_of_day=10, |
| 256 | + minute_of_day=0, |
| 257 | + freq='intraday', timezone_hour_minute='America/New_York', |
| 258 | + freq_min_mult=freq_min_mult) * 100.0 |
| 259 | + implied_vol_addon = vol_stats.calculate_implied_vol_addon(asset, |
| 260 | + implied_vol=implied_vol_df, |
| 261 | + tenor_label='ON', |
| 262 | + adj_ON_friday=True).dropna() |
| 263 | + |
| 264 | + vrp = vol_stats.calculate_vol_risk_premium(asset, tenor_label='ON', |
| 265 | + implied_vol=implied_vol_df, |
| 266 | + realized_vol=realized_vol, |
| 267 | + adj_ON_friday=True) |
226 | 268 |
|
227 | 269 | style = Style() |
228 | 270 |
|
229 | 271 | style.title = asset + ' ON implied volatility vs realized' |
230 | 272 | style.scale_factor = 3 |
231 | 273 | style.source = 'Bloomberg' |
232 | 274 |
|
233 | | - to_plot = vrp[[asset + 'UON.close', asset +'HON.close']].dropna() |
| 275 | + to_plot = vrp[[asset + 'UON.close', asset + 'HON.close']].dropna() |
234 | 276 |
|
235 | 277 | chart.plot(to_plot, style=style) |
236 | 278 |
|
|
0 commit comments