-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
54 lines (49 loc) · 1.64 KB
/
main.py
File metadata and controls
54 lines (49 loc) · 1.64 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import requests
import hmac
import hashlib
import time
import json
class BitflyerAPI:
def __init__(self, api_key, api_secret):
self.base_url = "https://api.bitflyer.com/v1/"
self.api_key = api_key
self.api_secret = api_secret
def get_ticker(self, product_code="BTC_JPY"):
"""
Get ticker data.
"""
endpoint = "ticker"
params = {
"product_code": product_code
}
response = requests.get(self.base_url + endpoint, params=params)
return response.json()
def place_market_order(self, product_code, side, size):
"""
Place a market order.
"""
endpoint = "me/sendchildorder"
method = "POST"
body = {
"product_code": product_code,
"child_order_type": "MARKET",
"side": side,
"size": size
}
headers = self._get_private_headers(endpoint, method, body)
response = requests.post(self.base_url + endpoint, headers=headers, data=json.dumps(body))
return response.json()
def _get_private_headers(self, endpoint, method, body):
"""
Create headers for private API requests.
"""
timestamp = str(time.time())
text = timestamp + method + "/v1/" + endpoint + json.dumps(body)
sign = hmac.new(bytes(self.api_secret.encode('ascii')), bytes(text.encode('ascii')), hashlib.sha256).hexdigest()
headers = {
'ACCESS-KEY': self.api_key,
'ACCESS-TIMESTAMP': timestamp,
'ACCESS-SIGN': sign,
'Content-Type': 'application/json'
}
return headers