forked from weiliuv/fcoin-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfcoin3.py
137 lines (105 loc) · 4.44 KB
/
fcoin3.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import hmac
import hashlib
import requests
import sys
import time
import base64
import json
from collections import OrderedDict
class Fcoin():
def __init__(self,base_url = 'https://api.fcoin.com/v2/'):
self.base_url = base_url
def auth(self, key, secret):
self.key = bytes(key,'utf-8')
self.secret = bytes(secret, 'utf-8')
def public_request(self, method, api_url, **payload):
"""request public url"""
r_url = self.base_url + api_url
try:
r = requests.request(method, r_url, params=payload)
r.raise_for_status()
except requests.exceptions.HTTPError as err:
print(err)
if r.status_code == 200:
return r.json()
def get_signed(self, sig_str):
"""signed params use sha512"""
sig_str = base64.b64encode(sig_str)
signature = base64.b64encode(hmac.new(self.secret, sig_str, digestmod=hashlib.sha1).digest())
return signature
def signed_request(self, method, api_url, **payload):
"""request a signed url"""
param=''
if payload:
sort_pay = sorted(payload.items())
#sort_pay.sort()
for k in sort_pay:
param += '&' + str(k[0]) + '=' + str(k[1])
param = param.lstrip('&')
timestamp = str(int(time.time() * 1000))
full_url = self.base_url + api_url
if method == 'GET':
if param:
full_url = full_url + '?' +param
sig_str = method + full_url + timestamp
elif method == 'POST':
sig_str = method + full_url + timestamp + param
signature = self.get_signed(bytes(sig_str, 'utf-8'))
headers = {
'FC-ACCESS-KEY': self.key,
'FC-ACCESS-SIGNATURE': signature,
'FC-ACCESS-TIMESTAMP': timestamp
}
try:
r = requests.request(method, full_url, headers = headers, json=payload)
r.raise_for_status()
except requests.exceptions.HTTPError as err:
print(err)
print(r.text)
if r.status_code == 200:
return r.json()
def get_server_time(self):
"""Get server time"""
return self.public_request('GET','/public/server-time')['data']
def get_currencies(self):
"""get all currencies"""
return self.public_request('GET', '/public/currencies')['data']
def get_symbols(self):
"""get all symbols"""
return self.public_request('GET', '/public/symbols')['data']
def get_market_ticker(self, symbol):
"""get market ticker"""
return self.public_request('GET', 'market/ticker/{symbol}'.format(symbol=symbol))
def get_market_depth(self, level, symbol):
"""get market depth"""
return self.public_request('GET', 'market/depth/{level}/{symbol}'.format(level=level, symbol=symbol))
def get_trades(self,symbol):
"""get detail trade"""
return self.public_request('GET', 'market/trades/{symbol}'.format(symbol=symbol))
def get_balance(self):
"""get user balance"""
return self.signed_request('GET', 'accounts/balance')
def list_orders(self, **payload):
"""get orders"""
return self.signed_request('GET','orders', **payload)
def create_order(self, **payload):
"""create order"""
return self.signed_request('POST','orders', **payload)
def buy(self,symbol, price, amount):
"""buy someting"""
return self.create_order(symbol=symbol, side='buy', type='limit', price=str(price), amount=amount)
def sell(self, symbol, price, amount):
"""sell someting"""
return self.create_order(symbol=symbol, side='sell', type='limit', price=str(price), amount=amount)
def get_order(self,order_id):
"""get specfic order"""
return self.signed_request('GET', 'orders/{order_id}'.format(order_id=order_id))
def cancel_order(self,order_id):
"""cancel specfic order"""
return self.signed_request('POST', 'orders/{order_id}/submit-cancel'.format(order_id=order_id))
def order_result(self, order_id):
"""check order result"""
return self.signed_request('GET', 'orders/{order_id}/match-results'.format(order_id=order_id))
def get_candle(self,resolution, symbol, **payload):
"""get candle data"""
return self.public_request('GET', 'market/candles/{resolution}/{symbol}'.format(resolution=resolution, symbol=symbol), **payload)