freq_min_mult = 5

    # Resample into 1 minute data and fill down all points
    implied_vol_df = market.fetch_market(md_request)[asset +'VON.open'].resample('1min').first().fillna(method='ffill')

    # Filter data by 1000 New York time, and return back to UTC, remove any out of trading hours
    # Then strip of time of day from the timestamp
    implied_vol_df = filter.filter_time_series_by_time_of_day_timezone(10, 0, implied_vol_df, timezone_of_snap='America/New_York')
    implied_vol_df = filter.remove_out_FX_out_of_hours(implied_vol_df)
    implied_vol_df.index = pd.to_datetime(implied_vol_df.index.date)
    implied_vol_df = pd.DataFrame(implied_vol_df)
    implied_vol_df.columns = [asset + 'VON.close']

    # Download FX intraday spot data, which will be used to calculate realized volatility
    md_request.tickers = asset; md_request.vendor_tickers = asset + ' BGN Curncy'
    intraday_spot_df = market.fetch_market(md_request).resample(str(freq_min_mult) + 'min').first()
    intraday_spot_df = filter.remove_out_FX_out_of_hours(intraday_spot_df).dropna()
    intraday_spot_df.columns = [asset + '.close']

    vol_stats = VolStats()

    # Calculate realized vol with the intraday data, with daily cutoffs
    realized_vol = vol_stats.calculate_realized_vol(
        asset, tenor_label='ON', spot_df=intraday_spot_df, hour_of_day=10, minute_of_day=0,
        freq='intraday', timezone_hour_minute='America/New_York', freq_min_mult=freq_min_mult) * 100.0
    implied_vol_addon = vol_stats.calculate_implied_vol_addon(asset, implied_vol=implied_vol_df, tenor_label='ON',
                                                adj_ON_friday=True).dropna()

    vrp = vol_stats.calculate_vol_risk_premium(asset, tenor_label='ON', implied_vol=implied_vol_df, realized_vol=realized_vol,
                                                adj_ON_friday=True)
示例#2
0
# Fill in your own API keys for Quandl and FRED here
# md_request.QUANDL_API_KEY = "TYPE HERE"
# md_request.FRED_API_KEY = "TYPE HERE"

df_fx = market.fetch_market(md_request=md_request)

##### Download deposit rate data
rates_tickers = ['USD3M', 'CAD3M', 'EUR3M', 'AUD3M', 'CHF3M', 'SEK3M', 'GBP3M', 'NOK3M', 'JPY3M', 'NZD3M']

rates_vendor_tickers = ['IR3TIB01USM156N', 'IR3TIB01CAM156N', 'IR3TIB01EZM156N', 'IR3TIB01AUM156N', 'IR3TIB01CHM156N',
'IR3TIB01SEM156N', 'IR3TIB01GBM156N', 'IR3TIB01NOM156N', 'IR3TIB01JPM156N', 'IR3TIB01NZM156N']

md_request.data_source = 'alfred'
md_request.tickers = rates_tickers
md_request.vendor_tickers = rates_vendor_tickers

df_rates = market.fetch_market(md_request=md_request)
df_rates = df_rates.resample('BM').last()

df = df_fx.join(df_rates, how='left')
df = df.fillna(method='ffill')
df.to_csv("fx_rates.csv")

##### Download US stocks data
equities_tickers = ['TWTR', 'GOOG', 'FB', 'AAPL', 'NFLX', 'AMZN', 'TSLA', 'S&P500']
equities_vendor_tickers = ['TWTR', 'GOOG', 'FB', 'AAPL', 'NFLX', 'AMZN', 'TSLA', '^GSPC']

md_request.data_source = 'yahoo'
md_request.tickers = equities_tickers
md_request.vendor_tickers = equities_vendor_tickers