STOCK/비트코인
1% GAP
BOTTLE6
2022. 1. 17. 12:51
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는 무슨이유에서 -인지 알아봐야겠다.