가상화폐 자동매매프로그램을 만들기 위해서, 파이썬을 공부 중입니다
지난번에 바이낸스 API를 신청했었는데, 처음엔 제가 주로 거래하는 선물의 경우는 활성화가 되어있지 않습니다. 선물계정을 생성하기 전에 신청한 api의 경우라면 삭제 후 새로 신청해야 합니다만, 제 경우는 선물계정이 이미 있었기에 Edit에서 바로 변경이 가능합니다.
CCXT모듈을 불러서 선물거래임을 디폴트값에 추가해줍니다. api키와 시크릿키는 본인이 발급받은 것을 입력해줍니다
import ccxt api_key = "" secret = "" binance = ccxt.binance(config={ 'apiKey': api_key, 'secret': secret, 'enableRateLimit': True, 'options': { 'defaultType': 'future' } }) |
잔고를 조회해봅시다. 그 전에, 해줘야 할 게 첫째줄엔 api키를 둘째줄엔 시크릿키를 입력한 txt파일을 주피터노트북에 업로드해줍니다. 주피터노트북? 그런 게 있답니다. 저도 오늘 처음 알았습니다. 주피터노트북 실행은 아나콘다 프롬프트에 ①pip install jupyter를 입력해 설치해준 후, ②윈도우 하단 실행창에 jupyter notebook를 입력하면 크롬 등 웹브라우저를 통해 열리는 데 ③여기다 txt파일을 업로드하면 됩니다. (주피터노트북 설치안내가 잘 설명된 블로그)
import ccxt import pprint with open("api.txt") as f: lines = f.readlines() api_key = lines[0].strip() secret = lines[1].strip() binance = ccxt.binance(config={ 'apiKey': api_key, 'secret': secret, 'enableRateLimit': True, }) balance = binance.fetch_balance(params={"type": "future"}) print(balance['USDT']) |
코딩하고 런해보면
{'free': 0.0, 'used': 56.35167481, 'total': 52.88188832} |
제가 api키를 입력한 이 테스트 계정의 경우, 선물지갑에 52.88달러(USDT) 잔고가 있고, 이중 56.35달러가 사용중이라고 나옵니다. 차액 만큼 평가손실(!)상태란 것을 알 수 있습니다.
이제 선물의 현재가를 조회해보겠습니다
import ccxt import pprint with open("api.txt") as f: lines = f.readlines() api_key = lines[0].strip() secret = lines[1].strip() binance = ccxt.binance(config={ 'apiKey': api_key, 'secret': secret, 'enableRateLimit': True, 'options': { 'defaultType': 'future' } }) btc = binance.fetch_ticker("BTC/USDT") print(btc) |
Run해보면..
{'symbol': 'BTC/USDT', 'timestamp': 1626239326425, 'datetime': '2021-07-14T05:08:46.425Z', 'high': 33336.0, 'low': 31620.0, 'bid': None, 'bidVolume': None, 'ask': None, 'askVolume': None, 'vwap': 32589.82, 'open': 33008.94, 'close': 31945.0, 'last': 31945.0, 'previousClose': None, 'change': -1063.94, 'percentage': -3.223, 'average': None, 'baseVolume': 527307.283, 'quoteVolume': 17184848586.06, 'info': {'symbol': 'BTCUSDT', 'priceChange': '-1063.94', 'priceChangePercent': '-3.223', 'weightedAvgPrice': '32589.82', 'lastPrice': '31945.00', 'lastQty': '0.010', 'openPrice': '33008.94', 'highPrice': '33336.00', 'lowPrice': '31620.00', 'volume': '527307.283', 'quoteVolume': '17184848586.06', 'openTime': '1626152880000', 'closeTime': '1626239326425', 'firstId': '1176631219', 'lastId': '1180683212', 'count': '4050541'}} |
과거값도 조회해봅시다
import ccxt import pprint import time import pandas as pd with open("api.txt") as f: lines = f.readlines() api_key = lines[0].strip() secret = lines[1].strip() binance = ccxt.binance(config={ 'apiKey': api_key, 'secret': secret, 'enableRateLimit': True, 'options': { 'defaultType': 'future' } }) btc = binance.fetch_ohlcv( symbol="BTC/USDT", timeframe='1d', since=None, limit=10) df = pd.DataFrame(btc, columns=['datetime', 'open', 'high', 'low', 'close', 'volume']) df['datetime'] = pd.to_datetime(df['datetime'], unit='ms') df.set_index('datetime', inplace=True) print(df) |
Run! 비트코인 선물의 가격데이터가 시가, 고가, 저가, 종가, 거래량 순으로 출력됩니다
open high low close volume datetime 2021-07-05 35280.17 35281.55 33032.00 33672.72 736706.409 2021-07-06 33672.72 35120.00 33500.00 34206.01 644229.007 2021-07-07 34206.00 35089.56 33756.48 33845.64 456218.909 2021-07-08 33845.63 33910.00 32066.00 32852.04 650317.664 2021-07-09 32855.00 34100.00 32250.00 33805.01 485662.879 2021-07-10 33805.01 34248.58 33010.00 33497.98 410271.979 2021-07-11 33497.98 34629.62 33306.03 34253.81 343905.350 2021-07-12 34253.88 34662.73 32650.00 33070.87 502747.758 2021-07-13 33070.86 33336.00 32186.99 32709.09 466567.713 2021-07-14 32708.66 32800.00 31620.00 31946.86 125905.957 |
파이썬을 이용한 바이낸스 선물 투자의 기초 2 (4) | 2021.07.16 |
---|---|
파이썬, 그리고 바이낸스 가상화폐투자 (0) | 2021.07.14 |
작년에 이 코인을 샀더라면... (변동성돌파전략) (0) | 2021.07.09 |
퀀트킹을 활용해서 찾은 저평가 주식종목 Top 3 (0) | 2020.09.21 |
장기성과가 우수한 소형주들 (0) | 2020.09.19 |
댓글 영역