Пример #1
0
def plot_index(index_name, benchmark_name):
    # Load Index Data File
    index_path = c.path_dict['index']
    index_file = c.file_dict['index_r'] % index_name
    df = u.read_csv(index_path + index_file)

    # Plot Figure
    fig, (ax1, ax2, ax3) = plt.subplots(3, sharex=True)
    fig.set_size_inches(32, 18)

    # Define Font
    font = {
        'family': 'serif',
        'color': 'black',
        'weight': 'normal',
        'size': 18,
    }

    # Plot Sub-figure 1
    title = '%s vs. %s' % (index_name, benchmark_name)
    ax1.set_title(title, fontdict=font)
    ax1.set_xlabel('', fontdict=font)
    ax1.set_ylabel('Ratio', fontdict=font)
    for column in ['ratio', 'b_ratio']:
        df.plot(x='date', y=column, ax=ax1)

    # Plot Sub-figure 2
    title = 'Index %s' % index_name
    ax2.set_title(title, fontdict=font)
    ax2.set_xlabel('', fontdict=font)
    ax2.set_ylabel('Close Price', fontdict=font)
    df.plot(x='date', y='index', ax=ax2)

    # Plot Sub-figure 3
    title = 'Index %s' % benchmark_name
    ax3.set_title(title, fontdict=font)
    ax3.set_xlabel('', fontdict=font)
    ax3.set_ylabel('Close Price', fontdict=font)
    df.plot(x='date', y='b_index', ax=ax3)

    # Common Format for Both Sub-figures
    for ax in [ax1, ax2, ax3]:
        ax.grid(True)
    fig.autofmt_xdate()
    fig.tight_layout()
    plt.setp(plt.gca().get_xticklabels(), rotation=30)
    plt.show()

    # Save Figure
    fig_key = 'fig_index'
    fig_path = c.path_dict[fig_key]
    fig_file = c.file_dict[fig_key] % (index_name + '_' +
                                       u.dateToStr(u.today()))
    u.saveFigure(fig, fig_path, fig_file)
Пример #2
0
def plot_index_series(index_names, series_name, benchmark_name):
    # Load Index Data Files
    series_path = c.path_dict['index']
    series_file = c.file_dict['index_r'] % series_name
    df = u.read_csv(series_path + series_file)

    # Plot Figure
    fig = plt.figure(figsize=(32, 18), dpi=72, facecolor="white")
    axes = plt.subplot(111)
    axes.cla()  # Clear Axes

    # Define Font
    font = {
        'family': 'serif',
        'color': 'black',
        'weight': 'normal',
        'size': 18,
    }

    # Plot Sub-figure 1
    title = '%s vs. %s' % (series_name, benchmark_name)
    plt.title(title, fontdict=font)
    axes.set_xlabel('', fontdict=font)
    axes.set_ylabel('Ratio', fontdict=font)
    df.plot(x='date',
            y='ratio_benchmark',
            ax=axes,
            color='grey',
            lw=2.0,
            ls='--')
    index_number = len(index_names)
    for i in range(index_number):
        index_name = index_names[i]
        column = 'ratio_' + index_name
        df.plot(x='date', y=column, ax=axes)

    # Common Format for Both Sub-figures
    axes.grid(True)
    fig.autofmt_xdate()
    fig.tight_layout()
    plt.setp(plt.gca().get_xticklabels(), rotation=30)
    plt.show()

    # Save Figure
    fig_key = 'fig_index'
    fig_path = c.path_dict[fig_key]
    fig_file = c.file_dict[fig_key] % (series_name + '_' +
                                       u.dateToStr(u.today()))
    u.saveFigure(fig, fig_path, fig_file)
Пример #3
0
def getDailyHFQ(stock_id, is_index, date_start, date_end, time_to_market, incremental):
    hfq = None
    # For incremental update, use the next day of last update as the date start.
    if incremental:
        hfq = loadDailyHFQ(stock_id, is_index)
        if not u.isNoneOrEmpty(hfq):
            hfq.set_index('date',inplace=True)
            hfq.sort_index(ascending=True,inplace=True)
            last_day = hfq.index[len(hfq)-1]
            date_start = u.nextDayFromStr(last_day)
            # Check if existing data file is already up-to-date
            if date_end < date_start:
                return

    # Download
    if gs.is_debug:
        print('Download Daily HFQ: %s, start=%s, end=%s' % (stock_id, u.dateToStr(date_start), u.dateToStr(date_end)))
    df = get_daily_hfq(stock_id=stock_id, is_index=is_index, date_start=date_start, 
                       date_end=date_end, time_to_market=time_to_market)
    if not u.isNoneOrEmpty(df):
        # Default df has 'date' set as index with type pandas.tslib.Timestamp,
        # so merged results will contain 00:00:00 along with date.
        # With below series of operation, index of df will be converted to type
        # str, thus avoid above issue.
        df.reset_index(inplace=True)
        df['date'] = df['date'].map(lambda x:str(x.date()))
        df.set_index('date',inplace=True)
        df.sort_index(ascending=True,inplace=True)

    # For incremental update, merge data
    if incremental:
        if (not u.isNoneOrEmpty(df)) and (not u.isNoneOrEmpty(hfq)):
            df = hfq.append(df)

    # Format Columns
    if not u.isNoneOrEmpty(df):
        for column in ['open', 'high', 'close', 'low', 'volume', 'amount', 'factor']:
            df[column] = df[column].map(lambda x:'%.3f' % float(x))

    # Save to CSV File
    if not u.isNoneOrEmpty(df):
        u.to_csv(df, c.path_dict['lshq'], c.file_dict['lshq'] % u.stockFileName(stock_id, is_index))
Пример #4
0
def plot_coefficient_price(stock_ids, allprice, postfix, series_name,
                           benchmark_name):
    # If want to debug benchmark only (without stocks), set below flag to True.
    debug_benchmark_only = False

    # Extract Stock Prices and Normalize Them
    row_number = len(allprice)
    stock_number = len(stock_ids)
    columns = ['date', benchmark_name]
    if not debug_benchmark_only:
        for i in range(stock_number):
            stock_id = u.stockID(stock_ids[i])
            columns.append(stock_id)
    prices = u.createDataFrame(row_number, columns)
    prices['date'] = allprice['date']
    prices[benchmark_name] = allprice['close']
    if not debug_benchmark_only:
        for i in range(stock_number):
            stock_id = u.stockID(stock_ids[i])
            prices[stock_id] = allprice['close_' + stock_id]
    if debug_benchmark_only:
        print('Original Price')
        print(prices)

    # Normalize Price
    for i in range(1, len(columns)):
        column = columns[i]
        prices[column] = normalize_price(prices[column])
    if debug_benchmark_only:
        print('Normalized Price')
        print(prices)

    # Calculate Relative Price w.r.t. First Valid Price
    for i in range(1, len(columns)):
        column = columns[i]
        row = -1
        for j in range(row_number):
            if not np.isnan(prices.ix[j, column]):  # Find first valid price
                row = j
                break
        if row != -1:
            if debug_benchmark_only:
                print('Row =', row)
            ref_price = prices.ix[
                row,
                column]  # Need to be cached in the first place as it will be normalized to one later.
            for j in range(row, row_number):
                cur_price = prices.ix[j, column]
                if not np.isnan(cur_price):
                    prices.ix[
                        j, column] = 1.0 + (cur_price - ref_price) / ref_price
    if debug_benchmark_only:
        print('Relative Price')
        print(prices)

    # Plot Figure
    fig = plt.figure(figsize=(32, 18), dpi=72, facecolor="white")
    axes = plt.subplot(111)
    axes.cla()  # Clear Axes

    # Define Font
    font = {
        'family': 'serif',
        'color': 'black',
        'weight': 'normal',
        'size': 18,
    }

    # Plot Sub-figure 1
    title = '%s vs. %s' % (series_name, benchmark_name)
    plt.title(title, fontdict=font)
    axes.set_xlabel('', fontdict=font)
    axes.set_ylabel('Ratio', fontdict=font)
    prices.plot(x='date',
                y=benchmark_name,
                ax=axes,
                color='grey',
                lw=2.0,
                ls='--')
    if not debug_benchmark_only:
        for i in range(stock_number):
            column = u.stockID(stock_ids[i])
            prices.plot(x='date', y=column, ax=axes)

    # Common Format for Both Sub-figures
    axes.grid(True)
    fig.autofmt_xdate()
    fig.tight_layout()
    plt.setp(plt.gca().get_xticklabels(), rotation=30)
    plt.show()

    # Save Figure
    fig_key = 'fig_coef'
    fig_path = c.path_dict[fig_key]
    fig_name = '_'.join(
        [postfix, series_name, 'vs', benchmark_name,
         u.dateToStr(u.today())])
    fig_file = c.file_dict[fig_key] % fig_name
    u.saveFigure(fig, fig_path, fig_file)