백테스팅을 위해 코인데이터 엑셀로 저장하기
import pybithumb
df = pybithumb.get_ohlcv("BTC")
df.to_excel("btc.xlsx")
변동성 돌파 전략 백테스팅
# 엑셀데이터를 약간 수정하자
import pybithumb
df = pybithumb.get_ohlcv("BTC")
df['range'] = (df['high'] - df['low']) * 0.5
df.to_excel("btc.xlsx")
# 목표가 구하기
import pybithumb
df = pybithumb.get_ohlcv("BTC")
df['range'] = (df['high'] - df['low']) * 0.5
df['target'] = df['open'] + df['range'].shift(1) # shift(1) 1행 내려달라
# 다음날 시가에 직전의 레인지를 더한다 = 타겟프라이스 나옴
'''
Ex)
range range.shift(1)
1000
2000 1000
3000 2000
... 3000
'''
df.to_excel("btc.xlsx")
import pybithumb
df = pybithumb.get_ohlcv("BTC")
df['range'] = (df['high'] - df['low']) * 0.5
df['range_shift1'] = df['range'].shift(1)
df['target'] = df['open'] + df['range'].shift(1)
df.to_excel("btc.xlsx")
매수 매도 그리고 수익률
import pybithumb
import numpy as np
df = pybithumb.get_ohlcv("BTC")
df['range'] = (df['high'] - df['low']) * 0.5
df['target'] = df['open'] + df['range'].shift(1)
df['ror'] = np.where(df['high'] > df['target'],
df['close'] / df['target'],
1)
# ror를 모두 곱해서 누적수익률 계산
ror = df['ror'].cumprod()[-2]
# print(ror)
df.to_excel("trade.xlsx")
# 특정년도의 수익률 계산
import pybithumb
import numpy as np
df = pybithumb.get_ohlcv("BTC")
# 2018년 수익률 계산
df = df['2018']
df['range'] = (df['high'] - df['low']) * 0.5
df['target'] = df['open'] + df['range'].shift(1)
df['ror'] = np.where(df['high'] > df['target'],
df['close'] / df['target'],
1)
ror = df['ror'].cumprod()[-2]
df.to_excel("trade.xlsx")
# 수수료 추가
import pybithumb
import numpy as np
df = pybithumb.get_ohlcv("BTC")
# df = df['2018']
df['range'] = (df['high'] - df['low']) * 0.5
df['target'] = df['open'] + df['range'].shift(1)
fee = 0.0032
df['ror'] = np.where(df['high'] > df['target'],
df['close'] / df['target'] - fee,
1)
ror = df['ror'].cumprod()[-2]
df.to_excel("trade.xlsx")
# k값을 변경해가며 구해보자
import pybithumb
import numpy as np
def get_ror(k=0.5):
df = pybithumb.get_ohlcv("BTC")
df['range'] = (df['high'] - df['low']) * k
df['target'] = df['open'] + df['range'].shift(1)
fee = 0.0032
df['ror'] = np.where(df['high'] > df['target'],
df['close'] / df['target'] - fee,
1)
ror = df['ror'].cumprod()[-2]
return ror
for k in np.arange(0.1, 1.0, 0.1):
ror = get_ror(k)
print("%.1f %f" % (k, ror))
# MDD구하기
import pybithumb
import numpy as np
df = pybithumb.get_ohlcv("BTC")
df['range'] = (df['high'] - df['low']) * 0.5
df['target'] = df['open'] + df['range'].shift(1)
fee = 0.0032
df['ror'] = np.where(df['high'] > df['target'],
df['close'] / df['target'] - fee,
1)
df['hpr'] = df['ror'].cumprod()
df['dd'] = (df['hpr'].cummax() - df['hpr']) / df['hpr'].cummax() * 100
print("MDD(%): ", df['dd'].max())
df.to_excel("dd.xlsx")