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
- 빅데이터분석기사
- 데이터분석
- PolynomialFeatures
- docker
- backtest
- 볼린저밴드
- TimeSeries
- Python
- 실기
- hackerrank
- 비트코인
- GridSearchCV
- 파이썬 주식
- lstm
- randomforest
- Programmers
- 데이터분석전문가
- 프로그래머스
- 변동성돌파전략
- Quant
- 백테스트
- 주식
- 파이썬
- SQL
- 파트5
- ADP
- Crawling
- 토익스피킹
- sarima
- 코딩테스트
Archives
- Today
- Total
데이터 공부를 기록하는 공간
1% GAP 본문
1분봉 기준
시가보다 1% 상승시 매수하여,
시가보다 2% 상승시 매도하여, 1% gap으로 수익 창출 백테스트
데이터는 엑셀에 저장한 후 사용
캔들스틱은 plotly 활용. 느림.
import pyupbit
import matplotlib.pyplot as plt
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
import numpy as np
def get_ohlcv(ticker, interval = 'minute1'):
df = pyupbit.get_ohlcv(ticker, interval = interval, count=10000)
return df
def short_traiding_for_1percent(df):
ma15 = df['close'].rolling(15).mean().shift(1)
ma50 = df['close'].rolling(50).mean().shift(1)
ma120 = df['close'].rolling(120).mean().shift(1)
## 1) 매수일자 판별
cond_0 = df['high'] >= df['open']*1.01
cond_1 = (ma15 >= ma50) & (ma15 <= ma50*1.03)
cond_2 = ma50> ma120
cond_buy = cond_0 & cond_1 & cond_2
acc_ror = 1 #수익률 저장변수 : 1은 원금
sell_date = None #초기화
ax_ror = []
ay_ror = []
## 2) 매도 조건 탐색 및 수익률 계산
for buy_date in df.index[cond_buy]:
if sell_date != None and buy_date <= sell_date:
continue # 아래 코드를 무시하고 다시 for 문으로
target = df.loc[buy_date:]
cond = target['high'] >= target['open']*1.02
sell_candidate = target.index[cond]
if len(sell_candidate) == 0: #매수했지만 매도할 조건이 없다는 뜻
buy_price = df.loc[buy_date, 'open']*(1.01+0.0005+0.00005)
sell_price = df.iloc[-1,3]*(1-0.0005-0.00005)
acc_ror *= (sell_price / buy_price)
ax_ror.append(df.index[-1])
ax_ror.append(acc_ror)
break
else:
sell_date = sell_candidate[0]
acc_ror *= 1.01-0.001-0.0001 # slippage & fee
candle = go.Candlestick(
x = df.index,
open = df['open'],
high = df['high'],
low = df['low'],
close = df['close'],
)
ror_chart = go.Scatter(
x = ax_ror,
y = ay_ror,
)
fig = make_subplots(specs=[ [{"secondary_y" : True}] ])
fig.add_trace(candle)
fig.add_trace(ror_chart, secondary_y=True)
for idx in df.index[cond_buy]:
fig.add_annotation(
x = idx,
y = df.loc[idx,'open'],
)
fig.show()
return acc_ror
for ticker in ["KRW-BTC","KRW-LTC","KRW-ETH", "KRW-ADA","KRW-POWR","KRW-DOGE","KRW-NEAR"]:
df = get_ohlcv(ticker)
df.to_excel(f"./data_1percent/{ticker}.xlsx")
for ticker in ["KRW-BTC","KRW-LTC","KRW-ETH", "KRW-ADA","KRW-POWR","KRW-DOGE","KRW-NEAR"]:
#for ticker in ["KRW-ADA"]:
df = pd.read_excel(f"./data_1percent/{ticker}.xlsx", index_col=0)
ror = short_traiding_for_1percent(df)
기간수익률 = df.iloc[-1,3] / df.iloc[0,0]
print(ticker, f"{ror:.2f}", f"{기간수익률:.2f}")
좌) 1%Gap 수익률
우) 기간수익률
DOGE와 NEAR를 제외하고는 기간수익률보다 1%Gap 수익률보다 좋다.
DOGE와 NEAR는 무슨이유에서 -인지 알아봐야겠다.
'STOCK > 비트코인' 카테고리의 다른 글
finance datareader (0) | 2022.02.08 |
---|---|
50% 리밸런싱 (0) | 2022.02.05 |
plotly 캔들스틱, 저항선 그리기 (0) | 2022.01.16 |
볼린저밴드-찾기 (0) | 2022.01.16 |
볼린저밴드-추세추종-문제점 (0) | 2022.01.16 |
Comments