-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoinbase_advanced_trader_tester.py
More file actions
122 lines (94 loc) · 3.25 KB
/
Copy pathcoinbase_advanced_trader_tester.py
File metadata and controls
122 lines (94 loc) · 3.25 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
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
import http.client
import hmac
import hashlib
import json
import time
import uuid
from enum import Enum
import math
# Load sensitive information from config.json
with open('config.json') as config_file:
config = json.load(config_file)
api_key = config['API_KEY']
api_secret = config['API_SECRET']
creds = [api_key, api_secret]
class Side(Enum):
BUY = 1
SELL = 0
class Method(Enum):
POST = 1
GET = 0
def generate_client_order_id():
return uuid.uuid4()
def coinbase_request(method, path, body):
conn = http.client.HTTPSConnection("api.coinbase.com")
timestamp = str(int(time.time()))
message = timestamp + method + path.split('?')[0] + str(body)
signature = hmac.new(creds[1].encode(
'utf-8'), message.encode('utf-8'), digestmod=hashlib.sha256).hexdigest()
headers = {
"accept": "application/json",
"CB-ACCESS-KEY": creds[0],
"CB-ACCESS-SIGN": signature,
"CB-ACCESS-TIMESTAMP": timestamp
}
conn.request(method, path, body, headers)
res = conn.getresponse()
data = res.read()
response_data = json.loads(data.decode("utf-8"))
print(json.dumps(response_data, indent=2))
return response_data
def place_limit_order(side, pair, size, limit_price):
method = Method.POST.name
path = f'/api/v3/brokerage/orders'
payload = json.dumps({
"client_order_id": str(generate_client_order_id()),
"side": side,
"product_id": pair,
"order_configuration": {
"limit_limit_gtc": {
"post_only": False,
"limit_price": limit_price,
"base_size": size
}
}
})
coinbase_request(method, path, payload)
def get_all_product_info():
method = Method.GET.name
path = f'/api/v3/brokerage/products'
payload = ''
response = coinbase_request(method, path, payload)
for product in response['products']:
print(product['product_id'])
def get_product_info(pair):
method = Method.GET.name
path = f'/api/v3/brokerage/products/{pair}'
payload = ''
response = coinbase_request(method, path, payload)
return {"price": response['price'],
"quote_increment": response['quote_increment'],
"base_increment": response['base_increment']}
def tester():
my_side = Side.SELL.name
my_trading_pair = "BTC-USD"
usd_order_size = 5
factor = .998 if my_side == Side.SELL.name else 1.002
product_info = get_product_info(my_trading_pair)
quote_currency_price_increment = abs(
round(math.log(float(product_info['quote_increment']), 10)))
base_currency_price_increment = abs(
round(math.log(float(product_info['base_increment']), 10)))
my_limit_price = str(
round(float(product_info['price']) * factor, quote_currency_price_increment))
my_order_size = str(
round(usd_order_size/float(my_limit_price), base_currency_price_increment))
place_limit_order(my_side, my_trading_pair, my_order_size, my_limit_price)
print(f'The spot price of {my_trading_pair} is ${product_info["price"]}')
print(f"Your limit price is {my_limit_price}")
print(f"Your order size is {my_order_size}")
return {
'statusCode': 200,
'body': json.dumps('End of script.')
}
tester()