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
- 데이터분석
- Python
- backtest
- 실기
- Quant
- 파트5
- TimeSeries
- 백테스트
- Crawling
- 데이터분석전문가
- lstm
- 볼린저밴드
- 코딩테스트
- 프로그래머스
- 비트코인
- SQL
- PolynomialFeatures
- 파이썬 주식
- 파이썬
- ADP
- randomforest
- 빅데이터분석기사
- GridSearchCV
- hackerrank
- sarima
- 주식
- 변동성돌파전략
- 토익스피킹
- docker
- Programmers
Archives
- Today
- Total
데이터 공부를 기록하는 공간
백테스트 - SMA 1시간봉 본문
data = df.copy()
data = data[['close']]
data['SMA1'] = data['close'].rolling(42).mean()
data['SMA2'] = data['close'].rolling(180).mean()
data.plot(title="2years BTC", figsize=(10,4))
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import pyupbit
df = pyupbit.get_ohlcv("KRW-BTC","hour1", count=24*30*24)
data['position'] = np.where(data['SMA1']>data['SMA2'],1,0)
data.dropna(inplace=True)
data['position'].plot(ylim=[-1.1, 1.1], title='market positioning', figsize=(10,4))
;
# 로그수익률
data['strategy'] = data['position'].shift(1)*data['returns']
data[['returns','strategy']].sum()
data[['returns','strategy']].sum().apply(np.exp)
data[['returns','strategy']].cumsum().apply(np.exp).plot(figsize=(10,4));
매도조건 변경 : SMA1 > SMA2 & CLOSE < SMA1
data = df.copy()
data = data[['close']]
data['SMA1'] = data['close'].rolling(42).mean()
data['SMA2'] = data['close'].rolling(180).mean()
data.dropna(inplace=True)
#cond = data['SMA1']>data['SMA2']
#data.loc[cond, 'position'] = 1
data['position'] = np.where(data['SMA1']>data['SMA2'],1,np.NaN)
data['position'].plot(ylim=[-0.1, 1.1], title='market positioning', figsize=(10,4))
;
cond = ( data['position']==1 ) & ( data['close'] < data['SMA1'])
data.loc[cond,'position'] = 0
data['position'].plot(ylim=[-0.1, 1.1], title='market positioning', figsize=(10,4))
;
data['position'] = data['position'].fillna(method='ffill')
data['position'].plot(ylim=[-0.1, 1.1], title='market positioning', figsize=(10,4))
;
data['returns'] = np.log(data['close']/data['close'].shift(1))
data['strategy'] = data['position'].shift(1)*data['returns']
data[['returns','strategy']].sum()
data[['returns','strategy']].sum().apply(np.exp)
data[['returns','strategy']].cumsum().apply(np.exp).plot(figsize=(10,4));
▶ 매도조건을 바꾸니 조금 더 수익률이 MDD가 낮아지는 것 같다. but....
data['cumret'] = data['strategy'].cumsum().apply(np.exp)
data['cummax'] = data['cumret'].cummax()
data[['cumret','cummax']].dropna().plot(figsize=(10,4));
# strategy의 MDD
drawdown = data['cummax'] - data['cumret']
drawdown.max()
data['cumret_returns'] = data['returns'].cumsum().apply(np.exp)
data['cummax_returns'] = data['cumret_returns'].cummax()
data[['cumret_returns','cummax_returns']].dropna().plot(figsize=(10,4));
# 보유 시 MDD
drawdown = data['cummax_returns'] - data['cumret_returns']
drawdown.max()
보유시 MDD 217% 보다 전략 MDD 109%가 작게 느껴지지만...........
'STOCK > 비트코인' 카테고리의 다른 글
볼린저밴드-추세추종 (0) | 2022.01.10 |
---|---|
백테스트 - MLP 일봉 4년데이터 (0) | 2022.01.03 |
백테스트 - MLP 5분봉 volume 변수추가 (0) | 2022.01.03 |
백테스트 - MLP 5분봉 (0) | 2022.01.03 |
백테스트 - MLP (0) | 2022.01.03 |
Comments