具有日期时间轴的散景图显示了数据帧中不存在的时间戳数据

时间:2019-07-18 04:32:45

标签: python python-3.x pandas bokeh

我试图绘制一个烛台图,以获取股票的日内交易数据。数据集包含最近五个工作日的数据。除了烛台,图表还将显示重要的地板枢轴水平和简单的移动平均值,以进行分析。

使用以下代码生成的图显示周末和非交易时间的数据。如何从地块中删除这些区域? PS:数据集不包含非交易期间的任何数据。

from statistics import mean
from bokeh.plotting import figure, output_file, show
import pandas as pd


def central_pivot(close, low, high):
    return mean([close, low, high])


def first_resistance(pivot, low):
    return pivot * 2 - low


def second_resistance(pivot, high, low):
    return pivot + (high - low)


def third_resistance(resistance_1, high, low):
    return resistance_1 + (high - low)


def first_support(pivot, high):
    return pivot * 2 - high


def second_support(pivot, high, low):
    return pivot - (high - low)


def third_support(support_1, high, low):
    return support_1 - (high - low)


def cpr_lower_boundary(high, low):
    return mean([high, low])


def cpr_upper_boundary(pivot_point, central_pivot_range_lower_boundary):
    return central_pivot_range_lower_boundary * 2 - pivot_point


def find_opening_and_closing_time(data_frame):
    return {
        'opening': data_frame.index.min(),
        'closing': data_frame.index.max()
    }


def daily_open(data_frame, timestamp):
    return data_frame.loc[timestamp, 'Open']


def daily_close(data_frame, timestamp):
    return data_frame.loc[timestamp, 'Close']


def daily_low(data_frame):
    return data_frame['Low'].min()


def daily_high(data_frame):
    return data_frame['High'].max()


def get_pivot_points(data_frame):
    timings = find_opening_and_closing_time(data_frame)

    low = daily_low(data_frame)
    high = daily_high(data_frame)
    close = daily_close(data_frame, timings['closing'])

    pivot_point = central_pivot(close, low, high)
    resistance_1 = first_resistance(pivot_point, low)
    resistance_2 = second_resistance(pivot_point, high, low)
    resistance_3 = third_resistance(resistance_1, high, low)
    support_1 = first_support(pivot_point, high)
    support_2 = second_support(pivot_point, high, low)
    support_3 = third_support(support_1, high, low)

    central_pivot_range_lower_boundary = cpr_lower_boundary(high, low)
    central_pivot_range_upper_boundary = cpr_upper_boundary(pivot_point, central_pivot_range_lower_boundary)

    return {'resistance_3': resistance_3,
            'resistance_2': resistance_2,
            'resistance_1': resistance_1,
            'central_pivot_range_upper_boundary': central_pivot_range_upper_boundary,
            'pivot_point': pivot_point,
            'central_pivot_range_lower_boundary': central_pivot_range_lower_boundary,
            'support_1': support_1,
            'support_2': support_2,
            'support_3': support_3
            }


def prepare_dataframe(data_root):
    df = pd.read_csv(data_root, index_col=['Date'], parse_dates=True)
    frames = []
    for date, dataframe in df.groupby(df.index.date):
        pivot_values = get_pivot_points(dataframe)
        print(date, pivot_values)
        dataframe['resistance_3'] = pivot_values['resistance_3']
        dataframe['resistance_2'] = pivot_values['resistance_2']
        dataframe['resistance_1'] = pivot_values['resistance_1']
        dataframe['central_pivot_range_upper_boundary'] = pivot_values['central_pivot_range_upper_boundary']
        dataframe['pivot_point'] = pivot_values['pivot_point']
        dataframe['central_pivot_range_lower_boundary'] = pivot_values['central_pivot_range_lower_boundary']
        dataframe['support_1'] = pivot_values['support_1']
        dataframe['support_2'] = pivot_values['support_2']
        dataframe['support_3'] = pivot_values['support_3']
        frames.append(dataframe)
    result_frame = pd.concat(frames)
    result_frame['sma_20'] = result_frame.Close.rolling(window=20).mean()
    result_frame['sma_50'] = result_frame.Close.rolling(window=50).mean()
    result_frame['sma_200'] = result_frame.Close.rolling(window=200).mean()
    return result_frame


def plot_analysis(data_frame):
    output_file('test_plot.html')
    tools = 'box_select, box_zoom, lasso_select, pan, reset, save, crosshair'
    p = figure(plot_width=1366, plot_height=768, tools=tools, x_axis_type='datetime', output_backend='webgl')
    inc = data_frame.Close > data_frame.Open
    dec = data_frame.Open > data_frame.Close
    candle_width = 60 * 1000
    p.line(data_frame.index, data_frame.resistance_3, color='red')
    p.line(data_frame.index, data_frame.resistance_2, color='orange')
    p.line(data_frame.index, data_frame.resistance_1, color='green')
    p.line(data_frame.index, data_frame.central_pivot_range_upper_boundary, color='blue')
    p.line(data_frame.index, data_frame.pivot_point, color='blue')
    p.line(data_frame.index, data_frame.central_pivot_range_lower_boundary, color='blue')
    p.line(data_frame.index, data_frame.support_1, color='green')
    p.line(data_frame.index, data_frame.support_2, color='orange')
    p.line(data_frame.index, data_frame.support_3, color='red')
    p.line(data_frame.index, data_frame.sma_20, color='yellow')
    p.line(data_frame.index, data_frame.sma_50, color='cyan')
    p.line(data_frame.index, data_frame.sma_200, color='pink')
    p.grid.grid_line_alpha = 0.3
    p.segment(data_frame.index, data_frame.High, data_frame.index, data_frame.Low, color="black")
    p.vbar(data_frame.index[inc], candle_width, data_frame.Open[inc], data_frame.Close[inc], fill_color="#D5E1DD",
           line_color="black")
    p.vbar(data_frame.index[dec], candle_width, data_frame.Open[dec], data_frame.Close[dec], fill_color="#F2583E",
           line_color="black")

    show(p)


if __name__ == '__main__':
    DATA_ROOT = '../data/banban18-7.csv'
    data = prepare_dataframe(DATA_ROOT)
    plot_analysis(data)

实际结果:Plot from this code显示非交易时间的数据。

预期结果:该图应仅包含交易时间/仅适用于数据框中显示的值的数据。

日内数据:Raw data from stock exchange

0 个答案:

没有答案