J-Quants APIを経由して様々な日本株データを取得できるようなり、そのデータを分析するべく、テクニカル分析ライブラリを実装してみました。 …と言っても一から実装するのではなく、この分野ではかなり有名なTA-LIBというC言語で実装されたライブラリを CFFI(the Common Foreign Function Interface)経由でLisp関数として呼び出します(以下実装参考)。
TA-LIBで定義されたEMA(指数平滑移動平均線)をCFFIを通じて外部アクセス関数を定義しLisp上からアクセスする感じです。
(cffi:defcfun ("TA_EMA" %ema) :ta-ret-code (start-idx :int) (end-idx :int) (in-real :pointer) (opt-in-time-period :int) (out-beg-idx :pointer) (out-nb-element :pointer) (out-real :pointer))
次に、呼び出し関数を定義してみました(もちろん、%ema関数を直接呼び出すことも可能です)。
(defun ema (series-1 start-idx end-idx &key (time-period 10)) "Exponential Moving Average (EMA) The Exponential Moving Average is more responsive moving average compared to the Simple Moving Average (SMA). The weights are determined by alpha which is proportional to it's length. There are several different methods of calculating EMA. One method uses just the standard definition of EMA and another uses the SMA to generate the initial value for the rest of the calculation. Returns: ema. " (unless (eq (type-of series-1) 'ta-series-1) (error 'ta-lib-type-error :error-text "Invalid series type, expect 1 series")) (%with-ta-command-1 %ema series-1 start-idx end-idx :in-args `(,time-period)))
今回も、Lispを使ったテクニカル分析の動画をYouTubeにアップしました。前回に比べ、ビデオ編集ソフト(Shotcut)となるものも利用しました。😀 こちらも、機会を見て紹介したいと思います。
↓アップした動画、👍もよろしくお願いします! www.youtube.com