- 변동성 돌파 전략 간단 정리
- 가격 변동폭 계산 : ‘전일 고가’ 에서 ‘전일 저가’를 빼서 가격 변동폭을 구한다
- 매수 기준 : 당일 시간에서 변동폭 * 0.5 이상 상승시 매수
- 매도 기준 : 당일 종가에 매도
# 주기적으로 현재가를 받아보자
import pybithumb
import time
while True:
price = pybithumb.get_current_price("BTC")
print(price)
time.sleep(0.2)
# 목표가 계산하기
import pybithumb
df = pybithumb.get_ohlcv("BTC")
yesterday = df.iloc[-2]
today_open = yesterday['close']
yesterday_high = yesterday['high']
yesterday_low = yesterday['low']
target = today_open + (yesterday_high - yesterday_low) * 0.5
print(target)
함수로 묶어보자
def get_target_price(ticker):
df = pybithumb.get_ohlcv(ticker)
yesterday = df.iloc[-2]
today_open = yesterday['close']
yesterday_high = yesterday['high']
yesterday_low = yesterday['low']
target = today_open + (yesterday_high - yesterday_low) * 0.5
return target
# 매수시도
import time
import pybithumb
import datetime
def get_target_price(ticker):
df = pybithumb.get_ohlcv(ticker)
yesterday = df.iloc[-2]
today_open = yesterday['close']
yesterday_high = yesterday['high']
yesterday_low = yesterday['low']
target = today_open + (yesterday_high - yesterday_low) * 0.5
return target
# 변수 초기화
now = datetime.datetime.now()
mid = datetime.datetime(now.year, now.month, now.day) + datetime.timedelta(1) # datetime.timedelta(1) = 1일 후
target_price = get_target_price("BTC")
while True:
now = datetime.datetime.now()
if mid < now < mid + datetime.timedelta(seconds=10):
target_price = get_target_price("BTC")
mid = datetime.datetime(now.year, now.month, now.day) + datetime.timedelta(1)
current_price = pybithumb.get_current_price("BTC")
# 목표가 보다 시장가가 높다면
if current_price > target_price:
# 보유중인 원화 조회
krw = bithumb.get_balance("BTC")[2]
orderbook = pybithumb.get_orderbook("BTC")
# 최우선 매도 호가를 조회
sell_price = orderbook['asks'][0]['price']
unit = krw/float(sell_price)
# 시장가가 되기에 시장가로 매수
bithumb.buy_market_order("BTC", unit)
time.sleep(1)
# 매도시도
import time
import pybithumb
import datetime
def get_target_price(ticker):
df = pybithumb.get_ohlcv(ticker)
yesterday = df.iloc[-2]
today_open = yesterday['close']
yesterday_high = yesterday['high']
yesterday_low = yesterday['low']
target = today_open + (yesterday_high - yesterday_low) * 0.5
return target
def buy_crypto_currency(ticker):
krw = bithumb.get_balance(ticker)[2]
orderbook = pybithumb.get_orderbook(ticker)
sell_price = orderbook['asks'][0]['price']
unit = krw/float(sell_price)
bithumb.buy_market_order(ticker, unit)
def sell_crypto_currency(ticker):
unit = bithumb.get_balance(ticker)[0]
bithumb.sell_market_order(ticker, unit)
now = datetime.datetime.now()
mid = datetime.datetime(now.year, now.month, now.day) + datetime.timedelta(1)
target_price = get_target_price("BTC")
while True:
now = datetime.datetime.now()
if mid < now < mid + datetime.timedelta(seconds=10):
target_price = get_target_price("BTC")
mid = datetime.datetime(now.year, now.month, now.day) + datetime.timedelta(1)
# 매도
sell_crypto_currency("BTC")
current_price = pybithumb.get_current_price("BTC")
if current_price > target_price:
buy_crypto_currency("BTC")
time.sleep(1)
Connect Key, Secret Key txt에 저장하기
# 매도시도
import time
import pybithumb
import datetime
def get_target_price(ticker):
df = pybithumb.get_ohlcv(ticker)
yesterday = df.iloc[-2]
today_open = yesterday['close']
yesterday_high = yesterday['high']
yesterday_low = yesterday['low']
target = today_open + (yesterday_high - yesterday_low) * 0.5
return target
def buy_crypto_currency(ticker):
krw = bithumb.get_balance(ticker)[2]
orderbook = pybithumb.get_orderbook(ticker)
sell_price = orderbook['asks'][0]['price']
unit = krw/float(sell_price)
bithumb.buy_market_order(ticker, unit)
def sell_crypto_currency(ticker):
unit = bithumb.get_balance(ticker)[0]
bithumb.sell_market_order(ticker, unit)
now = datetime.datetime.now()
mid = datetime.datetime(now.year, now.month, now.day) + datetime.timedelta(1)
target_price = get_target_price("BTC")
# bithumb.txt에 저장된 키를 읽어온다
with open("bithumb.txt") as f:
lines = f.readlines()
key = lines[0].strip()
secret = lines[1].strip()
bithumb = pybithumb.Bithumb(key, secret)
while True:
try: # 혹시나 모를 예외처리
now = datetime.datetime.now()
if mid < now < mid + datetime.timedelta(seconds=10):
target_price = get_target_price("BTC")
mid = datetime.datetime(now.year, now.month, now.day) + datetime.timedelta(1)
sell_crypto_currency("BTC")
current_price = pybithumb.get_current_price("BTC")
if current_price > target_price:
buy_crypto_currency("BTC")
except:
print("에러 발생")
time.sleep(1)
변동성 돌파 + 상승장 투자 복합
# 복습삼아 5일 이평선 구하기
import pybithumb
df = pybithumb.get_ohlcv("BTC")
close = df['close']
ma5 = close.rolling(5).mean()
print(ma5)
함수로 묶자
def get_yesterday_ma5(ticker):
df = pybithumb.get_ohlcv(ticker)
close = df['close']
ma = close.rolling(window=5).mean()
return ma[-2]
적용해보자
def get_yesterday_ma5(ticker):
df = pybithumb.get_ohlcv(ticker)
close = df['close']
ma = close.rolling(5).mean()
return ma[-2]
now = datetime.datetime.now()
mid = datetime.datetime(now.year, now.month, now.day) + datetime.timedelta(1)
ma5 = get_yesterday_ma5("BTC")
target_price = get_target_price(“BTC”)
while True:
try:
now = datetime.datetime.now()
if mid < now < mid + datetime.delta(seconds=10):
target_price = get_target_price("BTC")
mid = datetime.datetime(now.year, now.month, now.day) + datetime.timedelta(1)
ma5 = get_yesterday_ma5("BTC")
sell_crypto_currency("BTC")
current_price = pybithumb.get_current_price("BTC")
# 현재가가 5일 이평을 넘어야 매수한다.
if (current_price > target_price) and (current_price > ma5):
buy_crypto_currency("BTC")
except:
print("에러 발생")
time.sleep(1)