python+Sqlite+Dataframe打造金融股票數據結構


5. 本地數據庫

很簡單的用本地Sqlite查找股票數據。

DataSource類,返回的是Dataframe物件。這個Dataframe物件,在之后的業務,如計算股票指標,還需要特別處理。

import os
import sqlite3 as sqlite3
import numpy as np
import pandas as pd


# 數據源
class DataSource:
    def __init__(self):
        self.db = None              # 數據庫
        self.cursor = None          # 指針
        self.stocks = {}            # 股票池
        self.indexs = {}            # 指數池
        self.name = 'unit_test.db'  # 數據源名稱

    def connect(self):
        self.db = sqlite3.connect(os.path.abspath(self.name))
        self.cursor = self.db.cursor()

    def get_stocks(self, ucodes):
        # 股票池
        try:
            self.stocks = {}
            self.connect()
            self.db.row_factory = lambda cursor, row: row[0]
            for ucode in ucodes:
                sql = """SELECT t.code, t.lot, t.nmll, t.stime, t.high, t.low, t.open, t.close, t.volume
                            FROM (SELECT n.code, n.lot, n.nmll, c.stime, c.high, c.low, c.open, c.close, c.volume 
                                FROM s_{} AS c INNER JOIN name AS n 
                                    ON c.code=n.code ORDER BY c.stime DESC LIMIT 365*20) AS t 
                                /*INNER JOIN financial AS f 
                            ON t.code=f.code AND substr(t.stime,1,4)=f.year*/
                        ORDER BY t.stime""".format(ucode)
                self.cursor.execute(sql)
                columns = ['code', 'lot', 'nmll', 'sdate', 'high', 'low', 'open', 'last', 'vol']
                self.stocks[ucode] = pd.DataFrame(self.cursor.fetchall(), columns=columns)
            self.db.commit()
            self.cursor.close()
            self.db.close()
            return self.stocks
        except sqlite3.Error as e:
            print(e)

    def get_indexs(self, indexs):
        try:
            # 指數池
            self.indexs = {}
            self.connect()
            self.db.row_factory = lambda cursor, row: row[0]
            for index in indexs:
                sql = """SELECT t.code, t.lot, t.nmll, t.stime, t.high, t.low, t.open, t.close, t.volume
                            FROM (SELECT n.code, n.lot, n.nmll, c.stime, c.high, c.low, c.open, c.close, c.volume 
                                FROM s_{} AS c INNER JOIN name AS n 
                                    ON c.code=n.code ORDER BY c.stime DESC LIMIT 365*20) AS t 
                                /*INNER JOIN financial AS f 
                            ON t.code=f.code AND substr(t.stime,1,4)=f.year*/
                        ORDER BY t.stime""".format(index.upper())
                self.cursor.execute(sql)
                columns = ['code', 'lot', 'nmll', 'sdate', 'high', 'low', 'open', 'last', 'vol']
                self.indexs[index] = pd.DataFrame(self.cursor.fetchall(), columns=columns)
            self.db.commit()
            self.cursor.close()
            self.db.close()
            return self.indexs
        except sqlite3.Error as e:
            print(e)


data_source = DataSource()
df1 = data_source.get_stocks(['00700'])
df2 = data_source.get_indexs(['hsi'])

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM