Exemplo n.º 1
0
def update_historical_fly_time_series_market_commodity_futures_tab3(
        rows_data, rows_selected, lookback_window):
    sym_root = rows_data[rows_selected[0]]['Name']
    leg1 = rows_data[rows_selected[0]]['Leg1 Actual']
    leg2 = rows_data[rows_selected[0]]['Leg2 Actual']
    leg3 = rows_data[rows_selected[0]]['Leg3 Actual']

    lookback_date = convert_date_input(lookback_window, datetime(2008, 1, 1))

    if ':' in sym_root:
        df1 = inter_comdty_spread_hist_data_dict[sym_root][leg1]
        df2 = inter_comdty_spread_hist_data_dict[sym_root][leg2]
        df3 = inter_comdty_spread_hist_data_dict[sym_root][leg3]
    else:
        df1 = futures_hist_prices_dict[sym_root][leg1]
        df2 = futures_hist_prices_dict[sym_root][leg2]
        df3 = futures_hist_prices_dict[sym_root][leg3]

    df = df1 - df2 * 2 + df3
    df = df[lookback_date.date():]

    trace = go.Scatter(x=df.index, y=df, name=f'{leg1}-{leg2}', mode='lines')
    layout_fig = go.Layout(title=sym_root,
                           xaxis={
                               'title': sym_root,
                               'type': 'date',
                               'tickformat': '%Y-%m-%d'
                           },
                           yaxis={'title': 'Price'},
                           legend=dict(orientation="h"),
                           paper_bgcolor='rgba(0,0,0,0)',
                           plot_bgcolor='rgba(0,0,0,0)')

    return go.Figure(data=[trace], layout=layout_fig)
Exemplo n.º 2
0
def update_historical_fly_scatterplot_market_commodity_futures_tab3(
        rows_data, rows_selected, lookback_window):
    sym_root = rows_data[rows_selected[0]]['Name']
    leg1 = rows_data[rows_selected[0]]['Leg1 Actual']
    leg2 = rows_data[rows_selected[0]]['Leg2 Actual']
    leg3 = rows_data[rows_selected[0]]['Leg3 Actual']

    lookback_date = convert_date_input(lookback_window, datetime(2008, 1, 1))

    if ':' in sym_root:
        df1 = inter_comdty_spread_hist_data_dict[sym_root][leg1]
        df2 = inter_comdty_spread_hist_data_dict[sym_root][leg2]
        df3 = inter_comdty_spread_hist_data_dict[sym_root][leg3]
        df0 = generic_inter_comdty_hist_prices_dict[sym_root][sym_root + '1']
    else:
        df1 = futures_hist_prices_dict[sym_root][leg1]
        df2 = futures_hist_prices_dict[sym_root][leg2]
        df3 = futures_hist_prices_dict[sym_root][leg3]
        df0 = generic_futures_hist_prices_dict[sym_root][sym_root + '1']

    df = df1 - df2 * 2 + df3
    df = pd.concat([df0, df], axis=1)
    df = df[lookback_date.date():]

    trace1 = go.Scatter(x=df.iloc[:, 0],
                        y=df.iloc[:, 1],
                        name=f'{leg1}-{leg2}',
                        mode='markers')
    trace2 = go.Scatter(x=[df.iloc[-1, 0]],
                        y=[df.iloc[-1, 1]],
                        name='today',
                        mode='markers',
                        marker=dict(color=['red'], size=[20]))
    layout_fig = go.Layout(xaxis=dict(title='Generic 1st price'),
                           yaxis=dict(title='Spread price'),
                           showlegend=False,
                           paper_bgcolor='rgba(0,0,0,0)',
                           plot_bgcolor='rgba(0,0,0,0)')

    return go.Figure(data=[trace1, trace2], layout=layout_fig)
Exemplo n.º 3
0
def update_historical_time_series_market_commodity_futures_tab1(
        rows_data, rows_selected, n_clicks, generic_start_str, generic_end_str,
        lookback_window):
    # print('historical series called')
    sym_root = rows_data[rows_selected[0]]['Contract'][:
                                                       -5]  # remove e.g. Z2018
    # sym = df_config.loc[df_config['Name'] == name]['Quandl Code'].values[0]
    lookback_date = convert_date_input(lookback_window, datetime(2008, 1, 1))

    if ':' in sym_root:
        df = generic_inter_comdty_hist_prices_dict[sym_root]
    else:
        df = generic_futures_hist_prices_dict[sym_root]

    df = df[lookback_date.date():]

    generic_start = 1
    if (generic_start_str is not None) and (not not generic_start_str):
        generic_start = int(generic_start_str)
    generic_end = df.shape[1]
    if (generic_end_str is not None) and (not not generic_end_str):
        generic_end = int(generic_end_str)

    traces = [
        go.Scatter(x=df[col].index, y=df[col], mode='lines', name=col)
        for col in df.columns[generic_start - 1:generic_end]
    ]

    layout_fig = go.Layout(xaxis=dict(title='Date',
                                      rangeslider=dict(visible=False),
                                      type='date'),
                           yaxis=dict(title='Price'),
                           legend=dict(orientation="h"),
                           height=800,
                           margin=dict(l=0, r=0, t=0, b=0),
                           paper_bgcolor='rgba(0,0,0,0)',
                           plot_bgcolor='rgba(0,0,0,0)')

    return go.Figure(data=traces, layout=layout_fig)
Exemplo n.º 4
0
def update_historical_term_structures_market_commodity_futures_tab1(
        rows_data, rows_selected, n_clicks, ione, itwo, ithree, ifour, ifive):
    # print('historical term structure called')
    sym_root = rows_data[rows_selected[0]]['Contract'][:-5]

    if ':' in sym_root:
        hist_data = inter_comdty_spread_hist_data_dict[sym_root]
        meta_data = inter_comdty_spread_contracts_meta_df[
            inter_comdty_spread_contracts_meta_df['Root'] == sym_root]
        meta_data.sort_values('Last_Trade_Date', inplace=True)
    else:
        hist_data = futures_hist_prices_dict[sym_root]
        meta_data = futures_contracts_meta_df[futures_contracts_meta_df['Root']
                                              == sym_root]
        meta_data.sort_values('Last_Trade_Date', inplace=True)

    asofdate = hist_data.index[-1]
    s0 = hist_data.loc[asofdate]
    s = s0.to_frame()

    start_idx = hist_data.shape[0] - 1
    if (ione is not None) and (not not ione):
        t1 = convert_date_input(ione, datetime.today())
        t1 = t1.date()
        dateidx1 = hist_data.index.searchsorted(
            t1)  # first one greater than or equal to
        s1 = hist_data.iloc[dateidx1]
        s = pd.concat([s, s1], axis=1)
        start_idx = min(dateidx1, start_idx)

    if (itwo is not None) and (not not itwo):
        t2 = convert_date_input(itwo, datetime.today())
        t2 = t2.date()
        dateidx2 = hist_data.index.searchsorted(
            t2)  # first one greater than or equal to
        s2 = hist_data.iloc[dateidx2]
        s = pd.concat([s, s2], axis=1)
        start_idx = min(dateidx2, start_idx)

    if (ithree is not None) and (not not ithree):
        t3 = convert_date_input(ithree, datetime.today())
        t3 = t3.date()
        dateidx3 = hist_data.index.searchsorted(
            t3)  # first one greater than or equal to
        s3 = hist_data.iloc[dateidx3]
        s = pd.concat([s, s3], axis=1)
        start_idx = min(dateidx3, start_idx)

    if (ifour is not None) and (not not ifour):
        t4 = convert_date_input(ifour, datetime.today())
        t4 = t4.date()
        dateidx4 = hist_data.index.searchsorted(
            t4)  # first one greater than or equal to
        s4 = hist_data.iloc[dateidx4]
        s = pd.concat([s, s4], axis=1)
        start_idx = min(dateidx4, start_idx)

    if (ifive is not None) and (not not ifive):
        t5 = convert_date_input(ifive, datetime.today())
        t5 = t5.date()
        dateidx5 = hist_data.index.searchsorted(
            t5)  # first one greater than or equal to
        s5 = hist_data.iloc[dateidx5]
        s = pd.concat([s, s5], axis=1)
        start_idx = min(dateidx5, start_idx)

    st = s.join(meta_data['Last_Trade_Date'], how='left')
    st = st.sort_values('Last_Trade_Date')

    # find the first common date
    # dateidx_st = st['Last_Trade_Date'].searchsorted(hist_data.index[start_idx])[0]
    dateidx_st = st['Last_Trade_Date'].searchsorted(hist_data.index[start_idx])
    st = st.iloc[dateidx_st:]
    # st.fillna(0.0, inplace=True)
    traces = [
        go.Scatter(x=st['Last_Trade_Date'],
                   y=st[c],
                   name=c.strftime('%Y-%m-%d'),
                   mode='lines+markers',
                   hovertext=st.index) for c in st.columns[:-1]
    ]
    layout_fig = go.Layout(title=sym_root,
                           xaxis={'title': sym_root},
                           yaxis={'title': 'Price'},
                           legend=dict(orientation="h"),
                           paper_bgcolor='rgba(0,0,0,0)',
                           plot_bgcolor='rgba(0,0,0,0)')

    #plotly.offline.plot({'data': traces, 'layout': layout})
    return go.Figure(data=traces, layout=layout_fig)
Exemplo n.º 5
0
def plot_cross_sectional_market_misc_data(item_selected, ione, itwo, ithree,
                                          ifour, ifive):
    df_raw = misc_data_dict[item_selected]

    if item_selected in ['USDT']:
        df = df_raw
    elif item_selected in ['PCR:VIX', 'PCR:SPX', 'PCR:SPY']:
        df = pd.concat(
            [df_raw['PV'] / df_raw['CV'], df_raw['POI'] / df_raw['COI']],
            axis=1)
        df.columns = ['PCR:V', 'PCR:OI']
    elif 'COT:' in item_selected:
        if item_selected not in ['COT:ES', 'COT:NQ', 'COT:UX']:
            df = pd.concat([
                df_raw['Open Interest:F'],
                df_raw['Producer/Merchant/Processor/User:Long:F'] -
                df_raw['Producer/Merchant/Processor/User:Short:F'],
                df_raw['Swap Dealers:Long:F'] - df_raw['Swap Dealers:Short:F'],
                df_raw['Managed Money:Long:F'] -
                df_raw['Managed Money:Short:F'],
                df_raw['Other Reportables:Long:F'] -
                df_raw['Other Reportables:Short:F']
            ],
                           axis=1)
            df.columns = [
                'Open Interest', 'Producers', 'Swap Dealers', 'Managed Money',
                'Other Report'
            ]
            df['Commercial'] = df['Producers'] + df['Swap Dealers']
            df['Large Spec'] = df['Managed Money'] + df['Other Report']
            df['Small Spec'] = 0.0 - df['Commercial'] - df['Large Spec']
        else:
            df = pd.concat([
                df_raw['Open Interest:F'],
                df_raw['Dealer Intermediary:Long:F'] -
                df_raw['Dealer Intermediary:Short:F'],
                df_raw['Asset Manager/Institutional:Long:F'] -
                df_raw['Asset Manager/Institutional:Short:F'],
                df_raw['Leveraged Funds:Long:F'] -
                df_raw['Leveraged Funds:Short:F'],
                df_raw['Other Reportables:Long:F'] -
                df_raw['Other Reportables:Short:F'],
                df_raw['Nonreportable Positions:Long:F'] -
                df_raw['Nonreportable Positions:Short:F'],
            ],
                           axis=1)
            df.columns = [
                'Open Interest', 'Dealer Intermediary', 'Asset Manager',
                'Leveraged Funds', 'Other Reportables',
                'Nonreportable Positions'
            ]
    else:
        return None

    asofdate = df.index[-1]
    s0 = df.loc[asofdate]
    s = s0.to_frame()

    if (ione is not None) and (not not ione):
        t1 = convert_date_input(ione, datetime.today())
        t1 = t1.date()
        dateidx1 = df.index.searchsorted(
            t1)  # first one greater than or equal to
        s1 = df.iloc[dateidx1]
        s = pd.concat([s, s1], axis=1)

    if (itwo is not None) and (not not itwo):
        t2 = convert_date_input(itwo, datetime.today())
        t2 = t2.date()
        dateidx2 = df.index.searchsorted(
            t2)  # first one greater than or equal to
        s2 = df.iloc[dateidx2]
        s = pd.concat([s, s2], axis=1)

    if (ithree is not None) and (not not ithree):
        t3 = convert_date_input(ithree, datetime.today())
        t3 = t3.date()
        dateidx3 = df.index.searchsorted(
            t3)  # first one greater than or equal to
        s3 = df.iloc[dateidx3]
        s = pd.concat([s, s3], axis=1)

    if (ifour is not None) and (not not ifour):
        t4 = convert_date_input(ifour, datetime.today())
        t4 = t4.date()
        dateidx4 = df.index.searchsorted(
            t4)  # first one greater than or equal to
        s4 = df.iloc[dateidx4]
        s = pd.concat([s, s4], axis=1)

    if (ifive is not None) and (not not ifive):
        t5 = convert_date_input(ifive, datetime.today())
        t5 = t5.date()
        dateidx5 = df.index.searchsorted(
            t5)  # first one greater than or equal to
        s5 = df.iloc[dateidx5]
        s = pd.concat([s, s5], axis=1)

    traces = [
        go.Scatter(x=s.index,
                   y=s[c],
                   name=c.strftime('%Y-%m-%d'),
                   mode='lines+markers',
                   hovertext=s.index) for c in s.columns
    ]
    layout_fig = go.Layout(title=item_selected,
                           xaxis={'title': item_selected},
                           yaxis={'title': 'Value'},
                           legend=dict(orientation="h"),
                           paper_bgcolor='rgba(0,0,0,0)',
                           plot_bgcolor='rgba(0,0,0,0)')

    # plotly.offline.plot({'data': traces, 'layout': layout})
    return go.Figure(data=traces, layout=layout_fig)
Exemplo n.º 6
0
def plot_time_series_market_misc_data(item_selected, lookback_window):
    lookback_date = convert_date_input(lookback_window, datetime(2008, 1, 1))
    df_raw = misc_data_dict[item_selected][lookback_date.date():]

    if item_selected in ['USDT']:
        df = df_raw
    elif item_selected in ['PCR:VIX', 'PCR:SPX', 'PCR:SPY']:
        df = pd.concat(
            [df_raw['PV'] / df_raw['CV'], df_raw['POI'] / df_raw['COI']],
            axis=1)
        df.columns = ['PCR:V', 'PCR:OI']
    elif 'COT:' in item_selected:
        if item_selected not in ['COT:ES', 'COT:NQ', 'COT:UX']:
            df = pd.concat([
                df_raw['Open Interest:F'],
                df_raw['Producer/Merchant/Processor/User:Long:F'] -
                df_raw['Producer/Merchant/Processor/User:Short:F'],
                df_raw['Swap Dealers:Long:F'] - df_raw['Swap Dealers:Short:F'],
                df_raw['Managed Money:Long:F'] -
                df_raw['Managed Money:Short:F'],
                df_raw['Other Reportables:Long:F'] -
                df_raw['Other Reportables:Short:F']
            ],
                           axis=1)
            df.columns = [
                'Open Interest', 'Producers', 'Swap Dealers', 'Managed Money',
                'Other Report'
            ]
            df['Commercial'] = df['Producers'] + df['Swap Dealers']
            df['Large Spec'] = df['Managed Money'] + df['Other Report']
            df['Small Spec'] = 0.0 - df['Commercial'] - df['Large Spec']
        else:
            df = pd.concat([
                df_raw['Open Interest:F'],
                df_raw['Dealer Intermediary:Long:F'] -
                df_raw['Dealer Intermediary:Short:F'],
                df_raw['Asset Manager/Institutional:Long:F'] -
                df_raw['Asset Manager/Institutional:Short:F'],
                df_raw['Leveraged Funds:Long:F'] -
                df_raw['Leveraged Funds:Short:F'],
                df_raw['Other Reportables:Long:F'] -
                df_raw['Other Reportables:Short:F'],
                df_raw['Nonreportable Positions:Long:F'] -
                df_raw['Nonreportable Positions:Short:F'],
            ],
                           axis=1)
            df.columns = [
                'Open Interest', 'Dealer Intermediary', 'Asset Manager',
                'Leveraged Funds', 'Other Reportables',
                'Nonreportable Positions'
            ]
        # sym_root = item_selected.split(':')[1]
        # hist_price = generic_futures_hist_prices_dict[sym_root][lookback_date.date():].iloc[:,0]
    else:
        return None

    traces = [
        go.Scatter(x=df.index, y=df[col], mode='lines', name=col)
        for col in df.columns
    ]

    layout_fig = go.Layout(title=item_selected,
                           xaxis=dict(title=item_selected,
                                      rangeslider=dict(visible=False),
                                      type='date'),
                           yaxis=dict(title='Value'),
                           legend=dict(orientation="h"),
                           height=800,
                           margin=dict(l=0, r=0, t=0, b=0),
                           paper_bgcolor='rgba(0,0,0,0)',
                           plot_bgcolor='rgba(0,0,0,0)')

    return go.Figure(data=traces, layout=layout_fig)