# lib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns;sns.set()
# homemade
from features import tautil
from labeling import labeling
머신러닝을 이용한 트레이딩: (2) 가격 모멘텀 라벨링
Price Momentum Labeling
trading
labeling
momentum
삼성전자 종가를 기준으로 가격의 트렌드와 트렌드 강도(모멘텀)을 측정하여 이를 라벨(label)로 사용한다.
- Trend scanning 기법
- 향후 N 기간동안의 선형 회귀 시 t통계량의 절댓값의 최대값을 사용
- 즉, 일마다 향후 주어진 기간 중 가장 강한 트렌드를 예측하고자 하는 그날의 정답 트렌드로 지정
이 실험에서는 측정한 트렌드(\(|\hat{t}|\))를 분위 수로 나누어 이산적인 값으로 지정해주었다. 이는 다음에 분류(classification) 작업을 하기 위함이다.
아래 그래프에서 기간 N을 달리했을 때를 비교한 뒤, 투자자의 트레이딩 기간을 고려해 이를 정해준다.
import FinanceDataReader as fdr
= fdr.DataReader('005930','2010-1-1','2021-6-1')
df_ = tautil.ohlcv(df_) df
=df.close close
=[60] windows
= []
trend_scanning_regime_q3 for i in windows:
=i,q=3)[0].abs()) trend_scanning_regime_q3.append(labeling.trend_scanning_label(close,window
= []
trend_scanning_q3 for i in windows:
=i,q=3)[0]) trend_scanning_q3.append(labeling.trend_scanning_label(close,window
for j in range(len(windows)):
= trend_scanning_q3[j][:'2020']
y = windows[j]
i = plt.subplots(2, gridspec_kw={'height_ratios': [5, 1]}, figsize=(15,5))
f, (a0, a1) "Trend Scanning Labels {}span 3cut".format(i))
f.suptitle('2020'],alpha=0.4)
a0.plot(close[:'2020'].index,close[:'2020'],c=y, cmap='vlag')
a0.scatter(close[:0))
a1.plot(y.fillna( f.show()
for j in range(len(windows)):
= np.sign(trend_scanning_q3[j]-1)+1
y = windows[j]
i = plt.subplots(2, gridspec_kw={'height_ratios': [5, 1]}, figsize=(15,5))
f, (a0, a1) "Trend Scanning (long position) Labels {}span 4cut".format(i))
f.suptitle(=0.4)
a0.plot(close,alpha=y, cmap='vlag')
a0.scatter(close.index,close,c0))
a1.plot(y.fillna("c:image/labeling/trend_scanning_long_pos_{}.png".format(i))
f.savefig( f.show()