ML and FinTech: Project by 徐培修

KeyWords: 證券交易、股價、籌碼、券商分點

Motivations

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

證交所每日盤後會提供每個券商分點對個股的買賣張籌碼資訊,在一些歷史資料來看,某些分點可能對某些個股會有特別好的掌握度,故想學習用更科學的方式分析此情況。

  • 掌握籌碼,掌握先機

    在投資市場中,有基本面、技術面、籌碼面等面相的交易策略。進入市場進行交易,無論是主力、法人亦或是散戶,目標不外乎就是獲利。而若在主力或法人等進場布局前期,能有效發現跡象,並跟隨著進場投資,便能有機會跟著主力獲利,因此如何從籌碼中找到蛛絲馬跡,便是重要的學問。

  • 關鍵券商分點

    在證交所盤後可以看到券商在個股的買賣訊息,希望透過觀測各分點的籌碼進出與股價關係,找出對歷史股價有相對關鍵的券商分點,進而監控其動作,並作為投資進出場依據。

EDA

(資料來源: CMoney資料庫)

由於此組Data Set中有許多資料,而其中此次研究中最想著重在分點日買賣超對股價的影響,故先將Data做以下整理。

首先將1477.csv導入python,並用pandas.read_csv指令萃取出日期、券商代號、買張、賣張以及買賣超資訊。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Data Preprocessing

由於data本身格式,index有很多重複的日期以及券商代號
重新解構,讓data架構可以更清晰

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

如此一來,Y軸的index則可呈現每個交易日,X軸為所有分點,值則為該分點在該日的買賣超數量,若當日無買賣則呈現NaN

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

我們將上表以時間為X軸,買賣超為Y軸畫出如上圖。
由於分點的數量太多,以至於整體圖面過於密集,然而也可以看出其中也不乏許多分點在日買賣超會超過±500張的情形。

過濾部分較無價值data

Seven Techniques for Data Dimensionality Reduction

https://www.kdnuggets.com/2015/05/7-methods-data-dimensionality-reduction.html

  • Missing Values Ratio.

    Data columns with too many missing values are unlikely to carry much useful information. Thus data columns with number of missing values greater than a given threshold can be removed. The higher the threshold, the more aggressive the reduction.

    各分點對於此檔股票的交易頻率可以定義為

    (1m)100 其中
    m
    為Missing Value Ratio。

    • 過濾交易頻率過低的分點
      當分點交易頻率過低,則也意味該分點對股價的影響力應也較低,故過濾該分點data應對分析影響不大,甚至可能因為過濾掉一些雜訊而讓訓練分析結果更好。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

從上圖中可以看出,在1184個券商分點中,有許多分點參與交易並留倉的天數比不到1%,故透過給定一個threshold參數來過濾掉交易比例較少的分點,且亦可作為後續動態調整的參數。

最後,將股價資料與籌碼資料進行concat後可得以下結果:
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • Data Discription:
    縱軸: 從2011/01/03至2021/10/18之交易日,共2650天
    橫軸: 前85項為在這些交易日中,實際交易天數超過50%的券商分點;
     後3項為這些交易日該檔股票的開盤價、收盤價以及漲跌幅

Problem formulation

本篇將用三種交易策略進行比較:

項次 策略 備註
1 黃金交叉策略 Benchmark
2 羅吉斯回歸分析 利用前80%交易日(2011-01-03至2019-8-14)的籌碼及股價漲跌作為訓練資料,用訓練結果預測後20%的交易日(2019-8-15至2021-10-18)當日漲跌
3 LSTM模型分析 利用前80%交易日(2011-01-03至2019-8-14)的籌碼及股價漲跌幅作為訓練資料,用訓練結果預測後20%的交易日(2019-8-15至2021-10-18)當日漲跌幅

Remark:

  1. 為求簡單的比較,本篇將忽略每次交易所需成本(eg: 交易手續費、交易稅)
  2. 交易策略中不討論放空;且報酬率定義如下:
    Returni=(SellPriceiBuyPriceiBuyPricei)×100%, ii

    Total Return=i=1N(1+Returni)1, N

1. Benchmark: MA5/MA20黃金交叉策略

  • 交易策略: 當5MA向上穿越20MA線時進行買入;反之則賣出

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

上圖為以每日收盤價格畫出的5MA及20MA的曲線圖。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

使用5MA搭配20MA的黃金交叉策略,在2019-08-15至2021-10-18間得到的總報酬率為33.07%, 共交易16次。

2. Logistic Regression預測漲跌

首先將股價漲跌二元化:

Yi={1, ifcloseiclosei1>00, ifcloseiclosei10
Yi
表示個股當日漲/跌,當日收盤相較前日收盤為漲時,
Yi=1,Yi=0

日籌碼預測漲跌結果

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

由羅吉斯回歸分析結果的confusion matrix可以看到:
利用前80%的籌碼資料來預測後20%的股價漲跌Accuracy為79%

  • 交易策略:
    • 若手中無持股: 當日預測為漲則開盤買入
    • 若手中有持股: 當日預測為跌則開盤賣出
    • 其他狀況則不作交易動作

Action[i]={Buy at Open[i], ifPosition[i1]=0 and Pred[i]=1Sell at Open[i], ifPosition[i1]=1 and Pred[i]=0Hold the Postion[i1], else

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

羅吉斯回歸預測漲跌的策略交易在2019-08-15至2021-10-18間得到的總報酬率為:24.32%, 共交易137次

3. LSTM預測模型:

  • 交易策略:
    • 若手中無持股: 當日預測為漲幅超過1%則於開盤買入
    • 若手中有持股: 當日預測為跌幅超過1%則於開盤賣出
    • 其他狀況則不作交易動作

Action[i]={Buy at Open[i], ifPosition[i1]=0 and Pred[i]>0.01Sell at Open[i], ifPosition[i1]=1 and Pred[i]<0.01Hold the Postion[i1], else

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

LSTM預測模型的交易策略在2019-08-15至2021-10-18間得到的總報酬率為:57.57%, 共交易72次

Analysis

項次 策略 總報酬率 交易次數 MDD
1 黃金交叉策略 33.07% 16 14%
2 羅吉斯回歸分析 24.32% 137 23%
3 LSTM模型預測分析 57.57% 72 26%

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Conclusion

  • 券商分點籌碼交易資料有機會反映出個股股價的漲跌,若能有效運用,搭配不同投資策略可能可以提升投資獲利率
  • Future work:
    1. 對其他個股進行分析,或許只有針對某些個股有較明顯的主力分點才能有較顯著的效果
    2. 運用不同Machine Learning模型分析
    3. 加入針對突發性暴跌事件作出相對應的措施的方法