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
- randomforest
- 볼린저밴드
- Crawling
- lstm
- 데이터분석
- 주식
- 코딩테스트
- 파이썬
- Programmers
- 토익스피킹
- 실기
- 파트5
- 파이썬 주식
- 프로그래머스
- 빅데이터분석기사
- sarima
- 백테스트
- PolynomialFeatures
- SQL
- GridSearchCV
- 비트코인
- Python
- docker
- TimeSeries
- 변동성돌파전략
- backtest
- hackerrank
- 데이터분석전문가
- Quant
- ADP
Archives
- Today
- Total
데이터 공부를 기록하는 공간
백테스트 - MLP 5분봉 volume 변수추가 본문
df = pd.read_csv("20220103_btc_minute5_90days.csv").rename(columns = {"Unnamed: 0":'datetime'}).set_index("datetime")
df = df[['close','volume']]
data = df.copy()
data['return'] = np.log(data['close']/data['close'].shift(1))
data['volumeR'] = np.log(data['volume']/data['volume'].shift(1))
data.dropna(inplace=True)
data['direction'] = np.where(data['return']>0, 1, 0)
lags = 3
cols = []
for lag in range(1, lags+1):
col = "return_{}".format(lag)
data[col] = data['return'].shift(lag)
cols.append(col)
for lag in range(1, lags+1):
col = "volumeR_{}".format(lag)
data[col] = data['volumeR'].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-07'
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
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(128, activation='relu',
input_shape=(len(cols),)))
model.add(Dropout(0.3))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(64, 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']*0.9995)
training_data[['return','strategy']].sum().apply(np.exp)
training_data[['return','strategy']].cumsum().apply(np.exp).plot(figsize=(10,6))
cutoff_value = 0.6
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']*0.9995)
test_data[['return','strategy']].sum().apply(np.exp)
test_data[['return','strategy']].cumsum().apply(np.exp).plot(figsize=(10,6))
▶
volume의 lag데이터를 포함시켜도 소용이 없었다.
테스트 데이터를 하락폭이 큰 데이터를 제외시켜도 소용이 없었다.
cutoff_value를 높게 변경시켜서 거래를 줄여보아도 소용이 없었다.
fbprophet을 활용해야할까? ask, bid데이터를 추가하면 조금 달라질까?
'STOCK > 비트코인' 카테고리의 다른 글
백테스트 - MLP 일봉 4년데이터 (0) | 2022.01.03 |
---|---|
백테스트 - SMA 1시간봉 (0) | 2022.01.03 |
백테스트 - MLP 5분봉 (0) | 2022.01.03 |
백테스트 - MLP (0) | 2022.01.03 |
백테스트 - logistic regression (0) | 2022.01.03 |
Comments