-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
135 lines (113 loc) · 4.45 KB
/
functions.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 requests
import sqlite3
import base64
import hashlib
import hmac
def getDepth(direction='ask', pair='BTCUSD'):
# Set the endpoint for the API call
endpoint = f"https://api.binance.com/api/v3/ticker/price?symbol={pair}"
# Make the GET request
response = requests.get(endpoint)
# Check the status code of the response
if response.status_code == 200:
# If the request was successful, parse the JSON data
data = response.json()
# Check the direction parameter to determine which price to return
if direction == 'ask':
return data['askPrice']
elif direction == 'bid':
return data['bidPrice']
else:
return None
def getOrderBook(pair='BTCUSD'):
# Set the endpoint for the API call
endpoint = f"https://api.binance.com/api/v3/depth?symbol={pair}"
response = requests.get(endpoint)
# Check the status code of the response
if response.status_code == 200:
# If the request was successful, parse the JSON data
return response.json()
else:
return None
def refreshDataCandle(pair='BTCUSD', duration='5m'):
# Set the endpoint for the API call
endpoint = f"https://api.binance.com/api/v1/klines?symbol={pair}&interval={duration}"
response = requests.get(endpoint)
if response.status_code == 200:
return response.json()
else:
return None
def createCandleTable():
# Connect to the database
conn = sqlite3.connect("trading.db")
c = conn.cursor()
# Create the table
c.execute("CREATE TABLE IF NOT EXISTS candles (timestamp INTEGER, open REAL, high REAL, low REAL, close REAL, volume REAL)")
conn.commit()
conn.close()
def refreshDataCandle(pair='BTCUSD', duration='5m'):
# Set the endpoint for the API call
endpoint = f"https://api.binance.com/api/v1/klines?symbol={pair}&interval={duration}"
# Make the GET request
response = requests.get(endpoint)
if response.status_code == 200:
data = response.json()
conn = sqlite3.connect("trading.db")
c = conn.cursor()
# Check if the latest candle in the database is the same as the latest candle in the data
c.execute("SELECT * FROM candles ORDER BY timestamp DESC LIMIT 1")
latest_candle_db = c.fetchone()
latest_candle_api = data[-1]
if latest_candle_db[0] != latest_candle_api[0]:
# If the latest candle is not the same, update the database with the new candle data
for candle in data:
c.execute("INSERT INTO candles VALUES (?, ?, ?, ?, ?, ?)", candle)
conn.commit()
conn.close()
return True
else:
return False
def refreshData(pair='BTCUSD'):
endpoint = f"https://api.binance.com/api/v1/trades?symbol={pair}"
response = requests.get(endpoint)
if response.status_code == 200:
# If succesfull parson json
data = response.json()
# Connect to the db
conn = sqlite3.connect("trading.db")
c = conn.cursor()
# Iterate through the data and insert it into the table
for trade in data:
c.execute("INSERT INTO trades VALUES (?, ?, ?, ?)", (trade['id'], trade['price'], trade['quantity'], trade['timestamp']))
conn.commit()
conn.close()
return True
else:
return False
def createOrder(api_key, secret_key, direction, price, amount, pair='BTCUSD_d', orderType='LimitOrder'):
# Set the endpoint for the API call
endpoint = "https://api.binance.com/api/v3/order"
params = {
"symbol": pair,
"side": direction,
"type": orderType,
"timeInForce": "GTC",
"price": price,
"quantity": amount
}
# Encode the parameters as a query string
query_string = "&".join([f"{key}={value}" for key, value in params.items()])
# Generate the signature for the request
signature = hmac.new(secret_key.encode(), query_string.encode(), hashlib.sha256).hexdigest()
# Set the headers for the request
headers = {
"X-MBX-APIKEY": api_key,
"Content-Type": "application/x-www-form-urlencoded"
}
# Make the POST request
response = requests.post(endpoint, data=query_string, headers=headers)
# Check the status code of the response
if response.status_code == 200:
return response.json()
else:
return None