天天看點

學習心得:用python畫股票燭柱圖

今天嘗試用python分析股票資料,在網上找到一個畫圖的代碼,但是由于代碼中的pandas.io.data as web 函數不能再擷取資料,于是我在百度中找到了tushare,發現這個特别好用。

由于剛剛接觸python,在引用代碼的過程中出現了許多問題,通過百度一一進行了解決,修改後的代碼如下:(其實隻是修改了資料擷取,畫圖部分沒有修改)

import tushare as ts
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.dates import DateFormatter, WeekdayLocator, \
    DayLocator, MONDAY
from matplotlib.finance import candlestick_ohlc
import matplotlib.dates as md
import numpy as np
import datetime



def pandas_candlestick_ohlc(dat, stick="day", otherseries=None):
    """
    :param dat: pandas DataFrame object with datetime64 index, and float columns "Open", "High", "Low", and "Close", likely created via DataReader from "yahoo"
    :param stick: A string or number indicating the period of time covered by a single candlestick. Valid string inputs include "day", "week", "month", and "year", ("day" default), and any numeric input indicates the number of trading days included in a period
    :param otherseries: An iterable that will be coerced into a list, containing the columns of dat that hold other series to be plotted as lines

    This will show a Japanese candlestick plot for stock data stored in dat, also plotting other series if passed.
    """
    mondays = WeekdayLocator(MONDAY)  # major ticks on the mondays
    alldays = DayLocator()  # minor ticks on the days
    dayFormatter = DateFormatter('%d')  # e.g., 12

    # Create a new DataFrame which includes OHLC data for each period specified by stick input
    transdat = dat.loc[:, ["open", "high", "low", "close"]]

    for i in range(,len(transdat)):
        transdat.index.values[i]=datetime.datetime.strptime(transdat.index.values[i], "%Y-%m-%d")


    if (type(stick) == str):
        if stick == "day":
            plotdat = transdat
            stick =   # Used for plotting
        elif stick in ["week", "month", "year"]:
            if stick == "week":
                transdat["week"] = pd.to_datetime(transdat.index).map(lambda x: x.isocalendar()[])  # Identify weeks
            elif stick == "month":
                transdat["month"] = pd.to_datetime(transdat.index).map(lambda x: x.month)  # Identify months
            transdat["year"] = pd.to_datetime(transdat.index).map(lambda x: x.isocalendar()[])  # Identify years
            grouped = transdat.groupby(list(set(["year", stick])))  # Group by year and other appropriate variable
            plotdat = pd.DataFrame({"open": [], "high": [], "low": [],
                                    "close": []})  # Create empty data frame containing what will be plotted
            for name, group in grouped:
                plotdat = plotdat.append(pd.DataFrame({"open": group.iloc[, ],
                                                       "high": max(group.High),
                                                       "low": min(group.Low),
                                                       "close": group.iloc[-, ]},
                                                      index=[group.index[]]))
            if stick == "week":
                stick = 
            elif stick == "month":
                stick = 
            elif stick == "year":
                stick = 

    elif (type(stick) == int and stick >= ):
        transdat["stick"] = [np.floor(i / stick) for i in range(len(transdat.index))]
        grouped = transdat.groupby("stick")
        plotdat = pd.DataFrame(
            {"open": [], "high": [], "low": [], "close": []})  # Create empty data frame containing what will be plotted
        for name, group in grouped:
            plotdat = plotdat.append(pd.DataFrame({"open": group.iloc[, ],
                                                   "High": max(group.High),
                                                   "Low": min(group.Low),
                                                   "Close": group.iloc[-, ]},
                                                  index=[group.index[]]))

    else:
        raise ValueError(
            'Valid inputs to argument "stick" include the strings "day", "week", "month", "year", or a positive integer')

    # Set plot parameters, including the axis object ax used for plotting
    fig, ax = plt.subplots()
    fig.subplots_adjust(bottom=)

    if plotdat.index[-] - plotdat.index[] < pd.Timedelta('730 days'):
        weekFormatter = DateFormatter('%b %d')  # e.g., Jan 12
        ax.xaxis.set_major_locator(mondays)
        ax.xaxis.set_minor_locator(alldays)
    else:
        weekFormatter = DateFormatter('%b %d, %Y')
    ax.xaxis.set_major_formatter(weekFormatter)

    ax.grid(True)

    # Create the candelstick chart
    candlestick_ohlc(ax, list(
        zip(list( md.date2num(plotdat.index.values.tolist())), plotdat["open"].tolist(), plotdat["high"].tolist(),
            plotdat["low"].tolist(), plotdat["close"].tolist())),
                     colorup="black", colordown="red", width=stick * )

    # Plot other series (such as moving averages) as lines
    if otherseries != None:
        if type(otherseries) != list:
            otherseries = [otherseries]
        dat.loc[:, otherseries].plot(ax=ax, lw=, grid=True)

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

    plt.show()

op = ts.get_hist_data('600868', start='2017-1-1', end='2018-4-20')

pandas_candlestick_ohlc(op)
           

這次遇到的困難主要是pandas.index的修改,pandas.index.values 是數組,可以修改值。

到現在為止,還有一處不是很懂

transdat = dat.loc[:, ["open", "high", "low", "close"]]
           

為什麼這樣會把pandas的 datetime.datetime 類轉化為 str。如果有大神看到,希望能夠解答,謝謝

如有錯誤,請多多包涵

1

1

1

1

1

1

1

繼續閱讀