Esempio n. 1
0
def plottingPositionACF(positions,
                        ax,
                        title='Position auto correlation function'):
    positions = aggregatePositons(positions, convert='raw')
    if 'cash' in positions:
        positions = positions.drop('cash', axis='columns')

    nlags = 100
    acf_mat = np.zeros((len(positions.columns), nlags + 1))
    cols = positions.columns

    for i, col in enumerate(cols):
        acfs = acf(positions[col], nlags=nlags)
        acf_mat[i, 0:len(acfs)] = acfs

    acf_mean = np.nanmean(acf_mat, axis=0)
    ax.plot(
        acf_mean,
        color='orangered',
        alpha=0.5,
        lw=2,
    )
    ax.set_title(title)
    ax.set_ylabel('Auto correlation')
    ax.set_xlabel('lags')
    return ax
Esempio n. 2
0
def plottingHodings(positions, ax, freq='M', title="Holdings Analysis"):
    positions = aggregatePositons(positions, convert='daily')
    if 'cash' in positions:
        positions = positions.drop('cash', axis='columns')
    df_holdings = positions.apply(lambda x: np.sum(x != 0), axis='columns')
    df_holdings_by_freq = df_holdings.resample(freq).mean()
    df_holdings.plot(color='steelblue', alpha=0.6, lw=0.5, ax=ax)

    if freq == 'M':
        freq = 'monthly'
    else:
        freq = 'daily'

    df_holdings_by_freq.plot(color='orangered', alpha=0.5, lw=2, ax=ax)
    ax.axhline(df_holdings.values.mean(),
               color='steelblue',
               ls='--',
               lw=3,
               alpha=1.0)

    ax.set_xlim((positions.index[0], positions.index[-1]))

    ax.legend([
        'Holdings on each bar', 'Average {0} holdings'.format(freq),
        'Average whole peirod {0} holdings'.format(freq)
    ],
              loc="best")
    ax.set_title(title)
    ax.set_ylabel('Number of securities holdings')
    ax.set_xlabel('')
    return ax
Esempio n. 3
0
def createPostionTearSheet(position, plot=True):
    positions = aggregatePositons(position)
    positions_weiget = calculatePosWeight(positions)
    if plot:
        verticalSections = 3
        plt.figure(figsize=(16, 7 * verticalSections))
        gs = gridspec.GridSpec(verticalSections, 3, wspace=0.5, hspace=0.5)

        axExposure = plt.subplot(gs[0, :])
        axTopExposure = plt.subplot(gs[1, :], sharex=axExposure)
        axHoldings = plt.subplot(gs[2, :])

        plottingExposure(positions_weiget, axExposure)
        plottingTopExposure(positions_weiget, axTopExposure)
        plottingHodings(positions_weiget, axHoldings)
    return positions
Esempio n. 4
0
def createPostionTearSheet(position, plot=True):
    positions = aggregatePositons(position)
    positions_weiget = calculatePosWeight(positions)
    if plot:
        verticalSections = 3
        plt.figure(figsize=(16, 7 * verticalSections))
        gs = gridspec.GridSpec(verticalSections, 3, wspace=0.5, hspace=0.5)

        axExposure = plt.subplot(gs[0, :])
        axTopExposure = plt.subplot(gs[1, :], sharex=axExposure)
        axHoldings = plt.subplot(gs[2, :])

        plottingExposure(positions_weiget, axExposure)
        plottingTopExposure(positions_weiget, axTopExposure)
        plottingHodings(positions_weiget, axHoldings)
    return positions
Esempio n. 5
0
def plottingTopExposure(positions,
                        ax,
                        top=10,
                        title="Top 10 securities exposure (%)"):
    positions = aggregatePositons(positions, convert='daily')
    y_axis_formatter = FuncFormatter(two_dec_places)
    ax.yaxis.set_major_formatter(FuncFormatter(y_axis_formatter))
    df_mean = positions.abs().mean()
    df_top = df_mean.nlargest(top)
    (positions[df_top.index] * 100.).plot(ax=ax)
    ax.legend(loc='upper center',
              frameon=True,
              bbox_to_anchor=(0.5, -0.14),
              ncol=5)
    ax.set_title(title)
    return ax
Esempio n. 6
0
def createTranscationTearSheet(transactions, positions, plot=True):
    positions = aggregatePositons(positions)
    transcations = aggregateTranscations(transactions)

    if plot:
        verticalSections = 1
        plt.figure(figsize=(16, 7 * verticalSections))
        gs = gridspec.GridSpec(verticalSections, 3, wspace=0.5, hspace=0.5)
        axTurnOver = plt.subplot(gs[0, :])
    else:
        axTurnOver = None

    turnOverRate = plottingTurnover(transcations, positions, axTurnOver)[1]
    turnOverRate.name = 'turnover_rate'
    turnOverRate.index.name = 'date'

    return pd.DataFrame(turnOverRate)
Esempio n. 7
0
def createTranscationTearSheet(transactions, positions, plot=True):
    positions = aggregatePositons(positions)
    transcations = aggregateTranscations(transactions)

    if plot:
        verticalSections = 1
        plt.figure(figsize=(16, 7 * verticalSections))
        gs = gridspec.GridSpec(verticalSections, 3, wspace=0.5, hspace=0.5)
        axTurnOver = plt.subplot(gs[0, :])
    else:
        axTurnOver = None

    turnOverRate = plottingTurnover(transcations, positions, axTurnOver)[1]
    turnOverRate.name = 'turnover_rate'
    turnOverRate.index.name = 'date'

    return pd.DataFrame(turnOverRate)
Esempio n. 8
0
def plottingExposure(positions, ax, title="Total non cash exposure (%)"):
    positions = aggregatePositons(positions, convert='daily')
    y_axis_formatter = FuncFormatter(two_dec_places)
    ax.yaxis.set_major_formatter(FuncFormatter(y_axis_formatter))
    if 'cash' in positions:
        positions_without_cash = positions.drop('cash', axis='columns')
    else:
        positions_without_cash = positions
    longs = positions_without_cash[positions_without_cash > 0] \
                .sum(axis=1).fillna(0) * 100
    shorts = positions_without_cash[positions_without_cash < 0] \
                 .abs().sum(axis=1).fillna(0) * 100
    df_long_short = pd.DataFrame({'long': longs, 'short': shorts})
    df_long_short.plot(kind='area',
                       stacked=True,
                       color=['blue', 'green'],
                       linewidth=0.,
                       ax=ax)
    ax.set_title(title)
    return ax