1.獲取資料

罗论明發表於2024-04-01
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
import tushare as ts
import os


def get_data(code,start='1990-1-1',end='2021-1-1'):
    df=ts.get_k_data(code,autype='qfq',start=start,end=end)
    # df = ts.get_k_data(code, autype='qfq', ktype="30",start=start, end=end)
    print(df)
    df.index=pd.to_datetime(df.date)
    df['ma']=0.0  #Backtrader需要用到
    df['openinterest'] = 0.0  # Backtrader需要用到
    df=df[['open','high','low','close','volume','openinterest',"ma"]]
    return df

def acquire_code():   #只下載一隻股票資料,且只用CSV儲存   未來可以有自己的資料庫
    inp_code =input("請輸入股票程式碼:\n")
    inp_start = input("請輸入開始時間:\n")
    inp_end = input("請輸入結束時間:\n")
    df = get_data(inp_code,inp_start,inp_end)
    print(df.info())
    print("—"*30)
    print(df.describe())

    path = os.path.join(os.path.join(os.getcwd(),"資料地址"),inp_code+".csv")
    # path = os.path.join(os.path.join(os.getcwd(),"資料地址"),inp_code+"_30M.csv")
    df.to_csv(path )

acquire_code()

相關文章