추세를 따르는 매매전략
(반대 - Mean Reversion Strategy ; 평균을 중심으로 추세를 역행하는 매매 전략 )
Momentum behavior and mean reversion behavior can both occur in the same asset.
Each momentum measure will be predictive over different time horizons. Some over years and some over days
추세/회귀성을 발견하기 위해 timeframe을 단기부터 장기까지 모두 적용해보아야 한다. 과적합 주의!
# Momentum behavior이란?
자기상관성(<->unit root를 가짐)이 있으면서 가격에 trend가 발생하는 현상을 momentum behavior라고 한다.
주식가격(P)이 autocorrelated 되어 있고, 미지의 변수(X)에 의해 결정된다면 주식가격 결정모형은 아래와 같다.
P_t = T_t + e
T_t = X + P_{1, t-1}
P_t = X + P_{1, t-1} + e
주식가격의 차분 ( Pt - Pt-1 ; Returns ; X+e )이 random하게 변동하는 Pt의 움직임이 Momentum behavior임.
from statsmodels.tsa.stattools import adfuller
_, pvalue, _, _, _, _ = adfuller(P)
# Mean reversion behavior이란?
주식가격의 추세는 일시적이며 장기적으로 평균에 회귀하는 현상을 Mean Reversion behavior라고 한다.
from statsmodels import regression
import statsmodels.api as sm
# Find the line of best fit to illustrate the trend
X = np.arange(len(dates))
x = sm.add_constant(X) # Add a column of ones so that line can have a y-intercept
model = regression.linear_model.OLS(prices['AAPL'], x).fit()
a = model.params[0] # Get coefficients of line
b = model.params[1]
prices['Y_hat'] = X * b + a
plt.plot((prices['AAPL'] - prices['Y_hat']).values)
# Development path:
- Hypothesize that based on investor psychology, certain stocks should exhibit momentum behavior.
- Construct a mathemtical model that follows this hypothesis.
- Test your hypothesis by seeing if that model fits your data well.
- Test your model out of sample on new data you've never seen before.
# How to measure momentum
1. Absolute momentum
short_mavg = asset.rolling(window=30, center=False).mean()
long_mavg = asset.rolling(window=200, center=False).mean()
2. Cross sectional momentum (Relative momentum; stock to stock)
# How to apply momentum to strategies
1. Trade by Entry and Exit Signals
: Moving average crossover strategies
--> Moving Average Crossover Ribbons 이동평균선의 역/정배열
1) hamming distance : 벡터간의 거리 계산 ( 불일치 개수 )
2) spearman correlation : 벡터간의 상관계수 계산 ( 랭킹기반, 비모수 상관계수 <-> pearson )
3) 낙폭 범위 : thickness
import scipy.stats as stats
import scipy.spatial.distance as distance
scores = pd.Series(index=asset.index)
for date in rolling_means.index:
mavg_values = rolling_means_df.loc[date]
# 1. measures of ribbon shape
ranking = stats.rankdata(mavg_values.values)
# hamming distance
d = distance.hamming(ranking, range(1, 11))
# spearman correlation
d, _ = stats.spearmanr(ranking, range(1, 11))
# 2. measures of ribbon thickness
d = np.max(mavg_values) - np.min(mavg_values)
scores[date] = d
--> Momentum in physics = mass(거래량;1/거래회전율; 1/수익률의 표준편차) * velocity(수익률)
the more volatile or the less liquid an asset (the smaller its mass),
the easier it is to move its price (i.e. change its position)
# x(t) : 주식가격의 로그변환
# v(t) : 일일 수익률; x(t)의 차분
x = np.log(asset)
v = x.diff()
m = data['VOLUME'].iloc[:,8]
p0 = v.rolling(window=k, center=False).sum()
p1 = m*v.rolling(window=k, center=False).sum()
p2 = p1/m.rolling(window=k, center=False).sum()
p3 = v.rolling(window=k, center=False).mean()/v.rolling(window=k, center=False).std()
2. Use momentum as a factor for ranking stocks
: Long-Short Equity and Ranking Universes by Factors
'Trading > Strategies' 카테고리의 다른 글
ALT - Short position setup (trend strat.) (0) | 2020.12.19 |
---|---|
ALT - Long position setup (trend strat.) (0) | 2020.12.18 |
ALT - Pairs Trading Strategy with Kalman filter (0) | 2020.12.04 |
ALT - Mean reversion Strategy (0) | 2020.11.27 |
ALT - Long-Short Equity Strategy (0) | 2020.11.27 |