iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 9
2
Elastic Stack on Cloud

Elastic 戰台股系列 第 9

[Day09] 選股系統架構修改 - 歷史資料移轉 ES Cloud

一面工作,小寶寶才三個月大,一面趕鐵人賽的稿,真的很想撞牆。

今天總算可以達成一個小里程碑 - 把先前建立的歷史盤後資料上 ES 雲。

系統架構修改

Day01 時,我介紹過目前本機選股程式的架構,經過 Day02-Day08 的 ES 基礎學習,我把原本的系統架構修改如下:
https://ithelp.ithome.com.tw/upload/images/20200917/20129624X9kIMa5rvG.png
首先,我將撰寫一隻 Python Uploader 結果 elasticsearch-py 把過去建立的盤後資料庫 ( pure csv-based ) 轉換到 ES Cloud上。然後把每日從証交所取得的收盤資料直接更新到 ES Cloud。

for stock_id in stock_list:
    stock_id_price_path = "{0}/{1}-raw.csv".format(Const.STOCK_HISTORY_FOLDER_PATH, stock_id)
    print(stock_id_price_path)
    try:
        with open(stock_id_price_path, newline='') as csvfile:
            rows = csv.reader(csvfile)
            documents = []
            for row in rows:
                document = {}
                document['stock_id'] = row[0]
                document['date']     = row[1]
                document['volume']   = row[2]
                document['open']     = row[3]
                document['high']     = row[4]
                document['low']      = row[5]
                document['close']    = row[6]
    
                action = {}
                actionProperties = {}
                actionProperties["_id"] = document['stock_id'] + document['date']
                action["index"] = actionProperties
                documents.append(action)
                documents.append(document)
                
                result = es.bulk(body=documents, index='history-prices-python')
                
    except IOError:
        print("File not accessible")

驗証

來隨機抽驗一下資料正確性:
https://ithelp.ithome.com.tw/upload/images/20200924/20129624dLlkirApYL.png

初學 Elasticsearch 到今天是第 9 天,完全已經感受到這套工作的方便性以及容易上手。當初設計初版的選股程式時,就是因為設計資料庫的麻煩,讓我選擇用快速解法,但導入 ES 幫我解決了這個問題!太棒啦!

為了繼續完善我的選股系統,接下來預計的探索包含:

  • 不同的資料上傳方式:Logstash
  • ES 的強大搜尋能力
  • 資料視覺化

繼續努力!


上一篇
[Day08] Python 與 Elasticsearch
下一篇
[Day10] 技術指標計算 - 搜尋練習 using Kibana
系列文
Elastic 戰台股30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0

哈哈哈辛苦了,喬叔不會虧待你的 XDD

我要留言

立即登入留言