-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCoinbase Pro Order Example 2.py
60 lines (49 loc) · 1.82 KB
/
Coinbase Pro Order Example 2.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
import cbpro
import base64
import json
from time import sleep
key = ''
secret = ''
passphrase = ''
encoded = json.dumps(secret).encode()
b64secret = base64.b64encode(encoded)
auth_client = cbpro.AuthenticatedClient(key=key, b64secret=secret, passphrase=passphrase)
c = cbpro.PublicClient()
while True:
try:
ticker_old = c.get_product_ticker(product_id='BTC-USD')
except Exception as e:
print(f'Error obtaining ticker data: {e}')
sleep(300)
try:
ticker_new = c.get_product_ticker(product_id='BTC-USD')
except Exception as e:
print(f'Error obtaining ticker data: {e}')
percent = ((float(ticker_new['price']) - float(ticker_old['price']))*100)/float(ticker_old['price'])
if percent >= 5:
try:
limit = c.get_product_ticker(product_id='ETH-USD')
except Exception as e:
print(f'Error obtaining ticker data: {e}')
try:
order=auth_client.place_limit_order(product_id='ETH-USDT',
side='buy',
price=float(limit['price'])+2,
size='0.007')
except Exception as e:
print(f'Error placing order: {e}')
sleep(2)
try:
check = order['id']
check_order = auth_client.get_order(order_id=check)
except Exception as e:
print(f'Unable to check order. It might be rejected. {e}')
if check_order['status'] == 'done':
print('Order placed successfully')
print(check_order)
break
else:
print('Order was not matched')
break
else:
print(f'The requirement is not reached. The percent change is at {percent}')