基于tushare生成k線圖

tushare簡介

TuShare是一個免費慧耍、開源的python財經(jīng)數(shù)據(jù)接口包煌珊。主要實現(xiàn)對股票等金融數(shù)據(jù)從數(shù)據(jù)采集吏饿、清洗加工數(shù)據(jù)存儲的過程,能夠為金融分析人員提供快速许布、整潔、和多樣的便于分析的數(shù)據(jù)庶艾,為他們在數(shù)據(jù)獲取方面極大地減輕工作量咱揍,使他們更加專注于策略和模型的研究與實現(xiàn)上∨鹋椋考慮到Python pandas包在金融量化分析中體現(xiàn)出的優(yōu)勢题翰,TuShare返回的絕大部分的數(shù)據(jù)格式都是pandas DataFrame類型,非常便于用pandas/NumPy/Matplotlib進(jìn)行數(shù)據(jù)分析和可視化血公。當(dāng)然摔笤,如果您習(xí)慣了用Excel或者關(guān)系型數(shù)據(jù)庫做分析籍茧,您也可以通過TuShare的數(shù)據(jù)存儲功能,將數(shù)據(jù)全部保存到本地后進(jìn)行分析吮龄。應(yīng)一些用戶的請求漓帚,從0.2.5版本開始,TuShare同時兼容Python 2.x和Python 3.x昧辽,對部分代碼進(jìn)行了重構(gòu),并優(yōu)化了一些算法咕痛,確保數(shù)據(jù)獲取的高效和穩(wěn)定。 http://tushare.org/

股票數(shù)據(jù)獲取

import tushare as ts
ts.get_hist_data('600848')#一次性獲取全部日k線數(shù)據(jù)

結(jié)果顯示:

           open    high  close    low    volume    p_change  ma5 \
date
2012-01-11  6.880  7.380  7.060  6.880  14129.96    2.62  7.060
2012-01-12  7.050  7.100  6.980  6.900    7895.19    -1.13  7.020
2012-01-13  6.950  7.000  6.700  6.690    6611.87    -4.01  6.913
2012-01-16  6.680  6.750  6.510  6.480    2941.63    -2.84  6.813
2012-01-17  6.660  6.880  6.860  6.460    8642.57    5.38  6.822
2012-01-18  7.000  7.300  6.890  6.880  13075.40    0.44  6.788
2012-01-19  6.690  6.950  6.890  6.680    6117.32    0.00  6.770
2012-01-20  6.870  7.080  7.010  6.870    6813.09    1.74  6.832
           ma10    ma20      v_ma5    v_ma10    v_ma20    turnover
date
2012-01-11  7.060  7.060  14129.96  14129.96  14129.96    0.48
2012-01-12  7.020  7.020  11012.58  11012.58  11012.58    0.27
2012-01-13  6.913  6.913    9545.67    9545.67    9545.67    0.23
2012-01-16  6.813  6.813    7894.66    7894.66    7894.66    0.10
2012-01-17  6.822  6.822    8044.24    8044.24    8044.24    0.30
2012-01-18  6.833  6.833    7833.33    8882.77    8882.77    0.45
2012-01-19  6.841  6.841    7477.76    8487.71    8487.71    0.21
2012-01-20  6.863  6.863    7518.00    8278.38    8278.38    0.23

繪制K線圖

matplotlib.finance 工具包的繪制K線圖

def _candlestick(ax, quotes, width=0.2, colorup='k', colordown='r',
                 alpha=1.0, ochl=True):

    """
    Plot the time, open, high, low, close as a vertical line ranging
    from low to high.  Use a rectangular bar to represent the
    open-close span.  If close >= open, use colorup to color the bar,
    otherwise use colordown

    Parameters
    ----------
    ax : `Axes`
        an Axes instance to plot to
    quotes : sequence of quote sequences
        data to plot.  time must be in float date format - see date2num
        (time, open, high, low, close, ...) vs
        (time, open, close, high, low, ...)
        set by `ochl`
    width : float
        fraction of a day for the rectangle width
    colorup : color
        the color of the rectangle where close >= open
    colordown : color
         the color of the rectangle where close <  open
    alpha : float
        the rectangle alpha level
    ochl: bool
        argument to select between ochl and ohlc ordering of quotes

    Returns
    -------
    ret : tuple
        returns (lines, patches) where lines is a list of lines
        added and patches is a list of the rectangle patches added

    """

    OFFSET = width / 2.0

    lines = []
    patches = []
    for q in quotes:
        if ochl:
            t, open, close, high, low = q[:5]
        else:
            t, open, high, low, close = q[:5]

        if close >= open:
            color = colorup
            lower = open
            height = close - open
        else:
            color = colordown
            lower = close
            height = open - close

        vline = Line2D(
            xdata=(t, t), ydata=(low, high),
            color=color,
            linewidth=0.5,
            antialiased=True,
        )

        rect = Rectangle(
            xy=(t - OFFSET, lower),
            width=width,
            height=height,
            facecolor=color,
            edgecolor=color,
        )
        rect.set_alpha(alpha)

        lines.append(vline)
        patches.append(rect)
        ax.add_line(vline)
        ax.add_patch(rect)
    ax.autoscale_view()

    return lines, patches

tushare 的 pandas dataframe 生成K線圖


def _candlestick(ax, df, width=0.2, colorup='k', colordown='r',
                 alpha=1.0):

    """
    Plot the time, open, high, low, close as a vertical line ranging
    from low to high.  Use a rectangular bar to represent the
    open-close span.  If close >= open, use colorup to color the bar,
    otherwise use colordown

    Parameters
    ----------
    ax : `Axes`
        an Axes instance to plot to
    df : pandas data from tushare
    width : float
        fraction of a day for the rectangle width
    colorup : color
        the color of the rectangle where close >= open
    colordown : color
         the color of the rectangle where close <  open
    alpha : float
        the rectangle alpha level
    ochl: bool
        argument to select between ochl and ohlc ordering of quotes

    Returns
    -------
    ret : tuple
        returns (lines, patches) where lines is a list of lines
        added and patches is a list of the rectangle patches added

    """

    OFFSET = width / 2.0

    lines = []
    patches = []
    for date_string,row in df.iterrows():
        date_time = datetime.datetime.strptime(date_string,'%Y-%m-%d')
        t = date2num(date_time)
        open, high, close, low = row[:4]

        if close >= open:
            color = colorup
            lower = open
            height = close - open
        else:
            color = colordown
            lower = close
            height = open - close

        vline = Line2D(
            xdata=(t, t), ydata=(low, high),
            color=color,
            linewidth=0.5,
            antialiased=True,
        )

        rect = Rectangle(
            xy=(t - OFFSET, lower),
            width=width,
            height=height,
            facecolor=color,
            edgecolor=color,
        )
        rect.set_alpha(alpha)

        lines.append(vline)
        patches.append(rect)
        ax.add_line(vline)
        ax.add_patch(rect)
    ax.autoscale_view()

    return lines, patches


def drawPic(df, code, name):
    mondays = WeekdayLocator(MONDAY)            # 主要刻度
    alldays = DayLocator()                      # 次要刻度
    #weekFormatter = DateFormatter('%b %d')     # 如:Jan 12
    mondayFormatter = DateFormatter('%m-%d-%Y') # 如:2-29-2015
    dayFormatter = DateFormatter('%d')          # 如:12
    fig, ax = plt.subplots()
    fig.subplots_adjust(bottom=0.2)
    ax.xaxis.set_major_locator(mondays)
    ax.xaxis.set_minor_locator(alldays)
    ax.xaxis.set_major_formatter(mondayFormatter)

    _candlestick(ax, df, width=0.6, colorup='r', colordown='g')

    ax.xaxis_date()
    ax.autoscale_view()
    plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')

    
    ax.grid(True)
    plt.title(name + '  ' + code, fontproperties=zhfont)
    plt.show()


def makePicture(code, name):
    df = ts.get_hist_data(code, start=begin_time, end=end_time)
    df = df.sort_index(0)
#    df.plot()
    drawPic(df, code, name)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末头滔,一起剝皮案震驚了整個濱河市坤检,隨后出現(xiàn)的幾起案子早歇,更是在濱河造成了極大的恐慌晨另,老刑警劉巖借尿,帶你破解...
    沈念sama閱讀 216,843評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件茄靠,死亡現(xiàn)場離奇詭異,居然都是意外死亡儡蔓,警方通過查閱死者的電腦和手機(jī)喂江,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,538評論 3 392
  • 文/潘曉璐 我一進(jìn)店門吉嚣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來尝哆,“玉大人秋泄,你說我怎么就攤上這事瘦麸∽趟牵” “怎么了了赌?”我有些...
    開封第一講書人閱讀 163,187評論 0 353
  • 文/不壞的土叔 我叫張陵逢并,是天一觀的道長砍聊。 經(jīng)常有香客問我,道長俯树,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,264評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮秽晚,結(jié)果婚禮上瓦糟,老公的妹妹穿的比我還像新娘。我一直安慰自己赴蝇,他們只是感情好狸页,可當(dāng)我...
    茶點故事閱讀 67,289評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般芍耘。 火紅的嫁衣襯著肌膚如雪址遇。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,231評論 1 299
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死息裸,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播捏雌,決...
    沈念sama閱讀 40,116評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼净嘀,長吁一口氣:“原來是場噩夢啊……” “哼膜眠!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,945評論 0 275
  • 序言:老撾萬榮一對情侶失蹤绰沥,失蹤者是張志新(化名)和其女友劉穎涧衙,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體两踏,經(jīng)...
    沈念sama閱讀 45,367評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡晶姊,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,581評論 2 333
  • 正文 我和宋清朗相戀三年馋袜,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,754評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡厢塘,死狀恐怖画机,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情技健,我是刑警寧澤纤子,帶...
    沈念sama閱讀 35,458評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站古话,受9級特大地震影響悉抵,放射性物質(zhì)發(fā)生泄漏审磁。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,068評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望跋涣。 院中可真熱鬧缨睡,春花似錦、人聲如沸陈辱。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,692評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽沛贪。三九已至拾并,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間鹏浅,已是汗流浹背嗅义。 一陣腳步聲響...
    開封第一講書人閱讀 32,842評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留隐砸,地道東北人之碗。 一個月前我還...
    沈念sama閱讀 47,797評論 2 369
  • 正文 我出身青樓,卻偏偏與公主長得像季希,于是被迫代替她去往敵國和親褪那。 傳聞我的和親對象是個殘疾皇子幽纷,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,654評論 2 354

推薦閱讀更多精彩內(nèi)容