tushare庫簡介
tushare是免費開源的財經數據接口,網站http://tushare.org/。
通過tushare庫,我們可以獲取相應的股票信息,如下展示瞭如何獲取創業板的指數。
b = ts.get_k_data("399006", index=True)
print(b)
股票分析題目
1)獲得000063股票的k線數據
2)輸出所有收盤比開盤上漲3%的日期
3)輸出開盤比上一日收盤跌幅超2%的日期
5)假如每月的1日買一手,每年的最後一天賣出,算出2017到2019年每年的收入。
import tushare as ts
import pandas as pd
def get_k_data(stock_code, stock_file_name):
ts.get_k_data(stock_code).to_csv(stock_file_name)
if __name__ == '__main__':
stock_file_name = "d:/mystock.csv"
get_k_data("000063", stock_file_name)
df = pd.read_csv(stock_file_name, index_col = "date", parse_dates=["date"])[["open", "close", "high", "low"]]
print(df[((df["close"] - df["open"]) / df["open"]) >= 0.03].index)
print(df[((df["open"] - df["close"].shift(1)) / df["close"].shift(1) <= -0.02)])
df = df["2017-01-01":"2019-12-31"]
df_monthly = df.resample("MS").first()
print(df_monthly)
df_yearly = df.resample("A").last()
print(df_yearly)
cost_money = 0
hold = 0
for year in range(2017,2020):
cost_money += df_monthly[str(year)]["open"].sum() * 100
hold += 100 * len(df_monthly[str(year)])
cost_money -= df_yearly[str(year)]["open"][0] * hold
hold = 0
print("cost money:", cost_money)
如果您喜歡這篇文章,別忘了點贊和評論哦!