Ejemplo n.º 1
0
def plot_pred(pred_df,
              real_df,
              real_col,
              pred_col="fitted",
              upper_col="upper",
              lower_col="lower",
              outpath=None,
              show=True):
    import datetime
    from pathlib import Path
    from plotly.subplots import make_subplots

    # compute last date of training data
    last_date = pred_df.index[0]
    last_date -= datetime.timedelta(days=1)
    y_annotation = pred_df[upper_col].max()
    min_y_plot = real_df[-21:][real_col].min()
    max_y_plot = real_df[-21:][real_col].max()

    symbol_business = 'circle'
    symbol_holidays = 'diamond'
    sz_business = 7
    sz_holidays = 10
    marker_symbols_real = [
        symbol_business if is_business_day(d.date()) else symbol_holidays
        for d in real_df.index
    ]
    marker_sizes_real = [
        sz_business if is_business_day(d.date()) else sz_holidays
        for d in real_df.index
    ]
    marker_symbols_pred = [
        symbol_business if is_business_day(d.date()) else symbol_holidays
        for d in pred_df.index
    ]
    marker_sizes_pred = [
        sz_business if is_business_day(d.date()) else sz_holidays
        for d in pred_df.index
    ]

    fig = make_subplots(
        rows=2,
        cols=1,
        #     shared_xaxes=True,
        vertical_spacing=0.2,
        specs=[[{
            "type": "scatter"
        }], [{
            "type": "table"
        }]])

    fig.add_trace(draw_scatter(real_df, real_col, 'Reali', marker_symbols_real,
                               marker_sizes_real),
                  row=1,
                  col=1)
    fig.add_trace(draw_scatter(pred_df, pred_col, 'Previsioni',
                               marker_symbols_pred, marker_sizes_pred),
                  row=1,
                  col=1)
    fig.add_trace(draw_confidence_bands(pred_df, upper_col, 'Upper Bound'),
                  row=1,
                  col=1)
    fig.add_trace(draw_confidence_bands(pred_df, lower_col, 'Lower Bound'),
                  row=1,
                  col=1)

    fig.add_shape(type="line",
                  x0=last_date,
                  y0=0,
                  x1=last_date,
                  y1=y_annotation,
                  name="Ultimi dati training",
                  line=dict(
                      color="firebrick",
                      width=4,
                      dash="dashdot",
                  )),
    fig.add_annotation(x=last_date,
                       y=min_y_plot,
                       ax=-50,
                       ay=20,
                       xanchor="right",
                       yanchor="top",
                       text=f"Ultimi dati training:\n{last_date.date()}",
                       showarrow=True,
                       arrowhead=2),
    fig.update_layout(xaxis_title='Data',
                      xaxis=dict(range=[
                          last_date - datetime.timedelta(14),
                          last_date + datetime.timedelta(8)
                      ],
                                 autorange=False),
                      yaxis_title='N. ospedalizzazioni',
                      yaxis=dict(range=[
                          min_y_plot - y_annotation / 10,
                          max_y_plot + y_annotation / 10
                      ],
                                 autorange=False),
                      title=f'Andamento {real_col}'.title(),
                      hovermode="x",
                      font=dict(
                          family="Courier New, monospace",
                          size=16,
                      ))

    fig.add_trace(make_table(pred_df, real_col), row=2, col=1)

    fig.update_layout(width=1000,
                      height=800,
                      paper_bgcolor='rgba(0,0,0,0)',
                      plot_bgcolor='rgba(0,0,0,0)')
    if show:
        fig.show()
    else:
        if not outpath:
            outpath = f"./results/{last_date.date()}/{real_col}.html"
        Path(outpath).parent.mkdir(exist_ok=True, parents=True)
        fig.write_html(outpath)
        fig.write_image(outpath.replace(".html", ".png"))
    return fig
Ejemplo n.º 2
0
def trade_signal(signal_date, commodity, months_ahead):

    #Confirms signal_date is not a business day or holiday
    if not is_business_day(signal_date) or (is_holiday(signal_date)):
        return -2

    #Finds the near and far commodity ticker
    #Crude oil
    if commodity == "WTI":
        #WTI crude
        near_commodity = "CHRIS/CME_CL1"
        far_commodity = ''.join(["CHRIS/CME_CL", str(months_ahead + 1)])
    elif commodity == "BRENT":
        #Brent crude
        near_commodity = "CHRIS/ICE_B1"
        far_commodity = ''.join(["CHRIS/ICE_B", str(months_ahead + 1)])
    #Natural gas
    elif commodity == "NG":
        #US natural gas
        near_commodity = "CHRIS/CME_NG1"
        far_commodity = ''.join(["CHRIS/CME_NG", str(months_ahead + 1)])
    elif commodity == "UKNG":
        #UK natural gas
        near_commodity = "CHRIS/ICE_M1"
        far_commodity = ''.join(["CHRIS/ICE_M", str(months_ahead + 1)])
    #Cattle
    elif commodity == "LC":
        #Live cattle
        near_commodity = "CHRIS/CME_LC1"
        far_commodity = ''.join(["CHRIS/CME_LC", str(months_ahead + 1)])
    elif commodity == "FC":
        #Feeder cattle
        near_commodity = "CHRIS/CME_FC1"
        far_commodity = ''.join(["CHRIS/CME_FC", str(months_ahead + 1)])
    #Distillates
    elif commodity == "RBOBGAS":
        #RBOB Gas
        near_commodity = "CHRIS/CME_RB1"
        far_commodity = ''.join(["CHRIS/CME_RB", str(months_ahead + 1)])
    elif commodity == "GASOIL":
        #Gas oil
        near_commodity = "CHRIS/ICE_G1"
        far_commodity = ''.join(["CHRIS/ICE_G", str(months_ahead + 1)])
    elif commodity == "HEATOIL":
        #NY Harbor heating oil (CME)
        near_commodity = "CHRIS/CME_HO1"
        far_commodity = ''.join(["CHRIS/CME_HO", str(months_ahead + 1)])
    #Wheat
    elif commodity == "WHEAT":
        #Plain ol' wheat
        near_commodity = "CHRIS/CME_W1"
        far_commodity = ''.join(["CHRIS/CME_W", str(months_ahead + 1)])
    elif commodity == "KWHEAT":
        #Kansas City hard red wheat
        #Only goes out to the fifth month (sixth future)
        near_commodity = "CHRIS/CME_KW1"
        far_commodity = ''.join(["CHRIS/CME_KW", str(months_ahead + 1)])
    elif commodity == "MWHEAT":
        #Milling wheat
        #Only goes out to the fifth month (sixth future)
        near_commodity = "CHRIS/LIFFE_EBM1"
        far_commodity = ''.join(["CHRIS/LIFFE_EBM", str(months_ahead + 1)])
    #Catch all for commodities not on the list
    else:
        return -3

    #Converts the date to the right format and fetches the near and far future
    if signal_date.month < 10:
        signal_date_month_string = ''.join(["0", str(signal_date.month)])
    else:
        signal_date_month_string = str(signal_date.month)

    if signal_date.day < 10:
        signal_date_day_string = ''.join(["0", str(signal_date.day)])
    else:
        signal_date_day_string = str(signal_date.day)

    signal_date_string = ''.join([
        '"',
        str(signal_date.year), '-', signal_date_month_string, '-',
        signal_date_day_string, '"'
    ])
    ##    print(signal_date_string)

    try:
        near = quandl.get(near_commodity,
                          start_date=signal_date_string,
                          end_date=signal_date_string,
                          returns="numpy")
        for item in near:
            near_price = float(item[4])
##            print(near_price)
    except:
        return -4

    try:
        far = quandl.get(far_commodity,
                         start_date=signal_date_string,
                         end_date=signal_date_string,
                         returns="numpy")
        for item in far:
            far_price = float(item[4])
##        print(far_price)
    except:
        return -5

    #Figures out the dates of the the near and far futures, days between the two dates
    near_date = expiration_date(signal_date.year, signal_date.month, commodity)
    far_date = near_date + relativedelta(months=+months_ahead)
    far_date = expiration_date(far_date.year, far_date.month, commodity)
    ##    print(near_date)
    ##    print(far_date)
    if near_date.date() <= signal_date:
        ##        print(True)
        near_date = near_date + relativedelta(months=+1)
        far_date = far_date + relativedelta(months=+1)
##        print(near_date)
##        print(far_date)
    delta = far_date - near_date
    ##    print(delta.days)

    #Calculates the signal and returns it
    try:
        signal = (math.log(near_price) -
                  math.log(far_price)) * 365 / delta.days
    except:
        return -6
    return signal
Ejemplo n.º 3
0
            },
            ignore_index=True)
        for j in range(df1.shape[0]):
            df_output['pct_chg_date_%d' % (j + 1)][i] = df1['pct_chg'][j]
        i += 1
    print(df_output)
    logging.debug(df_output)


if __name__ == '__main__':
    logging.debug('start...')
    print('analyze daily data')

    start_date = '20210514'
    end_date = '20210514'
    start = datetime.datetime.strptime(start_date, '%Y%m%d')
    end = datetime.datetime.strptime(end_date, '%Y%m%d')

    #end = datetime.datetime.now()
    #start=end -datetime.timedelta(days = 2)

    for i in range((end - start).days + 1):
        date = start + datetime.timedelta(days=i)
        date_str = date.strftime('%Y%m%d')
        print(date_str)
        if utils.is_business_day(date):
            data = chaodiefantan(date_str)
            #measuremnet(data)
        else:
            print('Not Business day!')
    print('end')
Ejemplo n.º 4
0
# b2 = read_data('ICE', 'B', 2)
b7 = read_data(second_exchange, second_commodity_code, 7)
cb = pd.concat([cl1, b1], axis=1, join='inner')

# Initialization of Parameters
cl_pos = 0
b_pos = 0
pnl = 0
pnl_ls = []
day_count = 0  # The way the code structured now, only trading days are counted.
previous_price1 = 0
previous_price2 = 0
position = 0  # This is the indicator of whether we are holding positions, if
# we are during rolling period and not holding anything, this would also be 1

if is_holiday(start_date) or not is_business_day(start_date):
    dt = next_business_nonholilday(start_date, 1)
else:
    dt = start_date

# Executing the Roll Yield Strategy
while (dt < termination_date):
    # Calculating PnL
    if dt in cb.index:
        if day_count != 0:
            pnl += cl_pos * (cl1.loc[dt][0] - previous_price1)
            +b_pos * (b1.loc[dt][0] - previous_price2)
            pnl_ls.append([dt, pnl])

            # Check if it's time to roll
            # If both contracts have non-zero positions