簡單入門python預測股價-使用stocker套件(evaluate_prediction)

網誌好讀版 https://hn28082251.blogspot.com/2019/05/python-stocker-evaluate-prediction.html 沒有看過基礎概念的請先看下方連結
最近實在忙爆,本來想好好寫完整的教學,現在看來還是先慢慢寫片段,找時間再整合成一篇。 這次來講講evaluate_prediction這個函式,目的式來模擬我們跟著程式預測去下單的結果,也就是說我們將使用模型預測給出的策略,與我們在整個期間純粹買進並持有股票的策略進行一個對比。
megapx
本次資料以VTI(Vanguard Total Stock Market ETF)為例,這是我在美股的持股之一,資料集幫大家整理好了 ,請點下方文中連結 https://hn28082251.blogspot.com/2019/05/python-stocker-evaluate-prediction.html 前半段程式都一樣,就是把資料讀進來
megapx
相關除錯經驗可參考下方文章留言
接下來輸入這行程式碼並且執行 vti.evaluate_prediction(nshares=1000) nshares=1000代表我們用1000股去做模擬 評估時間為2018-05-14 00:00:00 to 2019-05-14
megapx
本函式的原理如下 The strategy from the model states that for a given day, we buy a stock if the model predicts it will increase. If the model predicts a decrease, we do not play the market on that day. Our earnings, if we bought the stock, will be the change in the price of the stock over that day multiplied by the number of shares. Therefore, if we predict the stock will go up and the price does go up, we will make the change in price times the number of shares. If the price goes down, we lose the change in price times the number of shares.  1、當模型預測股價會上漲的那一天,會買入,並在一天結束時賣出。當模型預測股價下跌時,我們就不買入任何股票; The strategy from the model states that for a given day, we buy a stock if the model predicts it will increase. If the model predicts a decrease, we do not play the market on that day.   2、如果購買股票價格在當天上漲,那麼我們就把股票上漲的幅度乘以我們購買的股票的數量; if we predict the stock will go up and the price does go up, we will make the change in price times the number of shares. 3、如果購買的股票價格下跌,我們就把下跌的幅度乘以股票的數量,計作我們的損失。If the price goes down, we lose the change in price times the number of shares. 執行後Stocker就會以數字和圖表顯示的方式告訴我們這個策略是如何進行的 從上方的模型預測可看到 The total profit using the Prophet model = $23360.00. The Buy and Hold strategy profit = $6890.00. 有點出乎我意料的,在這個資料集,模型的預測竟然跑贏了買進並且持有的狀況,我在測試的時候有用台積電股價做測試,結果買進並且持有的獲利勝過頻繁進出。 不過在台股大家要知道買進賣出光是手續費跟稅就會讓人自己扣血了,這個程式並沒有把這些算進去,如果要直接拿來應用在台股上,我只能說要三思阿XD 本文章並沒有推薦任何投資標的,單純是個人的一些觀察與見解,為作者自行查看相關資料後整理而成,資料之正確性以各官方公告為主,任何人觀看本文之後,而有投資該股票基金或ETF之行為,自行對所有後果負責。  祝大家都能夠有更富足的生活~
留言
B7 python 有個twstock套件 可以讓你不用寫爬蟲就可以抓到歷史股價 我有空來寫一篇 B8 你很認真在看我每一篇文章耶~google試算表是最快上手且不太需要程式基礎的方法 另外近期收到德州大學計資中心主任Dr.Ho的邀請 如果順利成行的話 月底會在中興大學舉辦的工作坊與大家分享python的簡單應用 有興趣的人可以去報名