· 7 years ago · Aug 06, 2018, 04:20 AM
1import gdax, time
2import json, hmac, hashlib, time, requests, base64
3from requests.auth import AuthBase
4
5API_KEY = "xxxxxxx"
6API_SECRET = "xxxx"
7API_PASS = "xxxx"
8
9
10
11key = API_KEY
12b64secret = API_SECRET
13passphrase = API_PASS
14
15
16# Create custom authentication for Exchange
17class CoinbaseExchangeAuth(AuthBase):
18 def __init__(self, api_key, secret_key, passphrase):
19 self.api_key = api_key
20 self.secret_key = secret_key
21 self.passphrase = passphrase
22
23 def __call__(self, request):
24 timestamp = str(time.time())
25 message = timestamp + request.method + request.path_url + (request.body or '')
26 hmac_key = base64.b64decode(self.secret_key)
27 signature = hmac.new(hmac_key, message, hashlib.sha256)
28 signature_b64 = signature.digest().encode('base64').rstrip('\n')
29
30 request.headers.update({
31 'CB-ACCESS-SIGN': signature_b64,
32 'CB-ACCESS-TIMESTAMP': timestamp,
33 'CB-ACCESS-KEY': self.api_key,
34 'CB-ACCESS-PASSPHRASE': self.passphrase,
35 'Content-Type': 'application/json'
36 })
37 return request
38
39api_url = 'https://api.pro.coinbase.com/'
40auth_client = gdax.AuthenticatedClient(key, b64secret, passphrase,
41 api_url="https://api.pro.coinbase.com/")
42
43auth = CoinbaseExchangeAuth(API_KEY, API_SECRET, API_PASS)
44
45# Get accounts
46r = requests.get(api_url + 'accounts', auth=auth)
47
48
49
50
51auth_client = gdax.AuthenticatedClient(key, b64secret, passphrase)
52
53# Use the sandbox API (requires a different set of API access credentials)
54auth_client = gdax.AuthenticatedClient(key, b64secret, passphrase,
55 api_url="https://api.pro.coinbase.com/")
56
57# print auth_client.buy(price=l,size='0.001',product_id='BTC-USD',type='limit',post_only="true")
58
59
60
61l = 7000.20
62while True:
63 print auth_client.buy(price=l,size='0.001',product_id='BTC-USD',type='limit',post_only="true")
64
65 print(l)
66 time.sleep(7)
67 auth_client.cancel_all(0,1)
68
69 if l >= 7110.00:
70 l = 7000.00
71
72 else:
73 print auth_client.buy(price=l,size='0.001',product_id='BTC-USD',type='limit',post_only="true")
74 l + 1
75 time.sleep(7)
76 auth_client.cancel_all(0,1)
77
78
79
80#print auth_client.buy(price=l,size='0.001',product_id='BTC-USD',type='limit',post_only="true")
81#print auth_client.cancel_all(0,1)
82#print auth_client.buy(price='6000.99',product_id='BTC-USD',size='0.001',type='limit',post_only='true)')
83#print auth_client.buy(price=l,size='0.001',product_id='BTC-USD',type='limit',post_only="true")
84#print auth_client.buy(price=l,size='0.001',product_id='BTC-USD',type='limit',post_only="true")
85
86#print auth_client.sell(price='7020.92',size='0.001',product_id='BTC-USD',type='limit',post_only="true")