Last active
May 16, 2022 18:23
-
-
Save lcharles123/c7b8af78be63d840d78a20a0725dd48b to your computer and use it in GitHub Desktop.
simple ftx client python, provides basic iIterations with FTX.com crypto exchange
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/rlwrap python3 | |
#-*- coding:utf-8 -*- | |
# apt install rlwrap, add console history support | |
"""Since FTX site uses a lot of CPU with markets with high activity | |
You can use this simple script to iteract. | |
Order cancelation is better using the site https://ftx.com/orders | |
""" | |
import time | |
import hmac | |
from requests import Request, Session | |
import json | |
MARKET="LUNA/USDT" | |
KEY="apiKey" | |
SECRET="apiSecret" | |
def loop(req): | |
req = req.split() | |
res = '' | |
if req == []: return | |
ts = int(time.time() * 1000) | |
s = Session() | |
if req[0] == "s" or req[0] == "b": | |
print("$ " + str(float(req[2]) * float(req[1]))) | |
try: | |
input("CTRL + C to cancel\n") | |
except KeyboardInterrupt: | |
print("cancelado") | |
return | |
side = "sell" if req[0] == 's' else "buy" | |
data = '{"market": "'+MARKET+'", "side": "'+ side #modify this string to add more options | |
data += '", "price": '+ str(float(req[2])) | |
data += ', "size": '+ str(float(req[1])) +', "type": "limit", "postOnly": true}' #postOnly : avoid market price sells | |
data = bytes(data.encode()) | |
request = Request('POST', 'https://ftx.com/api/orders', data = data) | |
prepared = request.prepare() | |
signature_payload = bytes(str(ts).encode()) + b'POST/api/orders' + data | |
elif req[0] == "l": #list book | |
request = Request('GET', 'https://ftx.com/api/markets/'+MARKET+'/orderbook?depth=1') | |
prepared = request.prepare() | |
signature_payload = bytes(str(ts).encode()) + b'GET/api/markets/'+bytes(MARKET.encode())+b'/orderbook?depth=1' | |
elif req[0] == "lo": #list open orders | |
request = Request('GET', 'https://ftx.com/api/orders?market='+MARKET) | |
prepared = request.prepare() | |
signature_payload = bytes(str(ts).encode()) + b'GET/api/orders?market='+bytes(MARKET.encode()) | |
elif req[0] == "c": #cancel order | |
request = Request('DELETE', 'https://ftx.com/api/orders/'+req[1]) | |
prepared = request.prepare() | |
signature_payload = bytes(str(ts).encode()) + b'DELETE/api/orders/' + bytes(req[1].encode()) | |
else: | |
raise IndexError | |
print(signature_payload) | |
signature = hmac.new(SECRET.encode(), signature_payload, 'sha256').hexdigest() #secret | |
prepared.headers['FTX-KEY'] = KEY | |
prepared.headers['FTX-SIGN'] = signature | |
prepared.headers['FTX-TS'] = str(ts) | |
res = s.send(prepared) | |
print(json.dumps(json.loads(res.text), indent=2)) | |
def main(): | |
while True: | |
try: | |
loop(input("l\tlist orderbook\nlo\tlist open orders\nc\tcancel <order_id>\n[b]uy\t<qtd> <price>\n[s]ell\t<qtd> <price>\n> ")) | |
except (IndexError) as e: #add others errors | |
print("ERRO, try again") | |
continue | |
except (KeyboardInterrupt, EOFError) as e: | |
print("exit") | |
break | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment