Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- sarima
- PolynomialFeatures
- Quant
- randomforest
- 빅데이터분석기사
- 프로그래머스
- docker
- 파이썬 주식
- 데이터분석전문가
- 파이썬
- Programmers
- lstm
- backtest
- 백테스트
- 변동성돌파전략
- 코딩테스트
- 볼린저밴드
- Crawling
- Python
- 비트코인
- TimeSeries
- hackerrank
- 파트5
- GridSearchCV
- 데이터분석
- 실기
- ADP
- SQL
- 주식
- 토익스피킹
Archives
- Today
- Total
데이터 공부를 기록하는 공간
백테스트 - MLP 5분봉 본문
pred.sum()
# df = pyupbit.get_ohlcv("KRW-BTC", "minute5", count=12*24*30*3)
df = pd.read_csv("20220103_btc_minute5_90days.csv").rename(columns = {"Unnamed: 0":'datetime'}).set_index("datetime")
df = df[['close','volume']]
df
data = df.copy()
data['return'] = np.log(data['close']/data['close'].shift(1))
data.dropna(inplace=True)
data['direction'] = np.where(data['return']>0, 1, 0)
lags = 5
cols = []
for lag in range(1, lags+1):
col = "lag_{}".format(lag)
data[col] = data['return'].shift(lag)
cols.append(col)
data.dropna(inplace=True)
data
data['momentum'] = data['return'].rolling(5).mean().shift(1)
data['volatility'] = data['return'].rolling(20).std().shift(1)
data['distance'] = (data['close']-data['close'].rolling(50).mean()).shift(1)
data.dropna(inplace=True)
cols.extend(['momentum','volatility','distance'])
cutoff = '2021-12-01'
training_data = data[data.index<cutoff].copy()
mu, std = training_data.mean(), training_data.std()
training_data_ = (training_data-mu)/std
test_data = data[data.index>=cutoff].copy()
test_data_ = (test_data-mu)/std
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.optimizers import Adam, RMSprop
optimizer = Adam(learning_rate=0.0001)
def set_seeds(seed=100):
#random.seed(seed)
np.random.seed(seed)
tf.random.set_seed(100)
set_seeds()
model = Sequential()
model.add(Dense(32, activation='relu',
input_shape=(len(cols),)))
model.add(Dropout(0.2))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(32, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer=optimizer,
loss='binary_crossentropy',
metrics=['accuracy'])
%%time
model.fit(training_data_[cols], training_data['direction'],
epochs=20, verbose=False,
validation_split=0.2, shuffle=False)
res = pd.DataFrame(model.history.history)
res[['accuracy','val_accuracy']].plot(figsize=(10,6), style='--')
cutoff_value = 0.5
pred = np.where(model.predict(training_data_[cols])>cutoff_value, 1,0)
training_data['prediction'] = np.where(pred > 0, 1, 0)
training_data['strategy'] = (training_data['prediction']*
training_data['return'])
training_data[['return','strategy']].sum().apply(np.exp)
training_data[['return','strategy']].cumsum().apply(np.exp).plot(figsize=(10,6))
cutoff_value = 0.5
pred = np.where(model.predict(test_data_[cols])>cutoff_value, 1,0)
pred[:30].flatten()
test_data['prediction'] = np.where(pred > 0, 1, 0)
test_data['strategy'] = (test_data['prediction']*
test_data['return'])
test_data[['return','strategy']].sum().apply(np.exp)
test_data[['return','strategy']].cumsum().apply(np.exp).plot(figsize=(10,6))
▶ 테스트 데이터는 하락장을 버티지 못하였다. 수수료가 없음에도 훈련데이터보다 뒤떨어지는 5% 수익률
○ precision으로 평가지표 변경하기
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.optimizers import Adam, RMSprop
from keras.metrics import Precision
optimizer = Adam(learning_rate=0.0001)
def set_seeds(seed=100):
#random.seed(seed)
np.random.seed(seed)
tf.random.set_seed(100)
set_seeds()
model = Sequential()
model.add(Dense(32, activation='relu',
input_shape=(len(cols),)))
model.add(Dropout(0.2))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(32, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer=optimizer,
loss='binary_crossentropy',
metrics=['Precision'])
%%time
model.fit(training_data_[cols], training_data['direction'],
epochs=20, verbose=False,
validation_split=0.2, shuffle=False)
res = pd.DataFrame(model.history.history)
res[['precision','val_precision']].plot(figsize=(10,6), style='--')
cutoff_value = 0.5
pred = np.where(model.predict(training_data_[cols])>cutoff_value, 1,0)
training_data['prediction'] = np.where(pred > 0, 1, 0)
training_data['strategy'] = (training_data['prediction']*
training_data['return'])
training_data[['return','strategy']].sum().apply(np.exp)
training_data[['return','strategy']].cumsum().apply(np.exp).plot(figsize=(10,6))
cutoff_value = 0.5
pred = np.where(model.predict(test_data_[cols])>cutoff_value, 1,0)
test_data['prediction'] = np.where(pred > 0, 1, 0)
test_data['strategy'] = (test_data['prediction']*
test_data['return'])
test_data[['return','strategy']].sum().apply(np.exp)
test_data[['return','strategy']].cumsum().apply(np.exp).plot(figsize=(10,6))
'STOCK > 비트코인' 카테고리의 다른 글
백테스트 - SMA 1시간봉 (0) | 2022.01.03 |
---|---|
백테스트 - MLP 5분봉 volume 변수추가 (0) | 2022.01.03 |
백테스트 - MLP (0) | 2022.01.03 |
백테스트 - logistic regression (0) | 2022.01.03 |
백테스트 - 모멘텀 (0) | 2022.01.02 |
Comments