先來個(gè)Tushare簡介
Tushare是一個(gè)免費(fèi)
注冊鏈接(帶本人推廣碼
https://tushare.pro/register?reg=133066
注冊完成后
網(wǎng)站上有API調(diào)用說明
下面介紹一個(gè)均線畫圖實(shí)現(xiàn)
步驟1
這里根據(jù)個(gè)人喜好進(jìn)行安裝
步驟2
#包含庫
import tushare as ts
import os
#自己的token放這里
ts.set_token('xxxxxxxxxx')
pro = ts.pro_api()
#根據(jù)個(gè)人喜好填個(gè)代碼
code = '002304'
#輸出文件名
output_file = code + '.csv'
data = ts.get_hist_data(code)
#日期是保留的
data.to_csv(output_file, columns=['open','high','low','close'])
第一個(gè)腳本完成
步驟3
import csv
from datetime import datetime
from matplotlib import pyplot as plt
file = '002304.csv'
with open(file) as f_stock_price:
data = csv.reader(f_stock_price)
stock_price = next(data)
#轉(zhuǎn)換日期和各種價(jià)格
dates, open_prices, high_prices, low_prices, close_prices = [], [], [], [], []
for row in data:
try:
date = datetime.strptime(row[0], "%Y-%m-%d")
open_p = float(row[1])
high_p = float(row[2])
low_p = float(row[3])
close_p = float(row[4])
except ValueError:
print("data missing")
else:
dates.append(date)
open_prices.append(open_p)
high_prices.append(high_p)
low_prices.append(low_p)
close_prices.append(close_p)
#MA50計(jì)算
ma_50 = []
close_price_len = len(close_prices)
print(close_price_len)
interval = 49
count = 0
for i in range(0, close_price_len):
if (i + interval) >= close_price_len:
break;
else:
last = i + interval
ma_50_price = round(float(sum(close_prices[i:last]) / 50), 2)
ma_50.append(ma_50_price)
count += 1
#創(chuàng)建線圖
#不喜自己動手的
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates, close_prices, c='blue', alpha=0.5)
plt.plot(dates[0:count], ma_50, c='red', alpha=0.5)
title = "002304 MA50"
plt.title(title, fontsize=20)
plt.xlabel('', fontsize=16)
fig.autofmt_xdate()
ax = plt.gca()
ax.yaxis.set_major_locator(plt.MultipleLocator(10))
plt.show()
最后一步
雖然丑了點(diǎn)
聯(lián)系客服