bitmex / easy-data-scripts Goto Github PK
View Code? Open in Web Editor NEWA few Python scripts to easily get data from the BitMEX API.
A few Python scripts to easily get data from the BitMEX API.
Hi guys,
I am trying to retreive all the trades real time. However for whatever reason the feed does not seem to be updating anymore after running the first time. Does anyone have any idea what I can do to fix this?
`
import argparse
import json
import time
import csv
import sys
import datetime
from lib import bitmex
from settings import API_KEY, API_SECRET, API_BASE
parser = argparse.ArgumentParser(description='Fetch your full execution trade history from BitMEX.')
parser.add_argument('--apiKey', type=str, required=(not API_KEY), default=API_KEY,
help='API Key. Generate from https://www.bitmex.com/app/apiKeys')
parser.add_argument('--apiSecret', type=str, required=(not API_SECRET), default=API_SECRET,
help='API Secret.')
parser.add_argument('--fileType', type=str, default='json',
help='Output file type. Must end be json or csv.')
parser.add_argument('--filter', type=str,
help='Query filter as JSON.')
args = parser.parse_args()
fileType = args.fileType
if fileType != 'json' and fileType != 'csv':
raise Exception('Output file type must be json or csv! Given: %s' % fileType)
if not args.apiKey or not args.apiSecret:
print('Please fill in API_KEY and API_SECRET in settings.py!\n' +
'You can create an API key from https://www.bitmex.com/app/apiKeys')
sys.exit(1)
if(args.filter):
try:
json.loads(args.filter)
except ValueError as e:
raise ValueError("Filter is not valid JSON! Make sure to single-quote the string.")
connector = bitmex.BitMEX(base_url=API_BASE, apiKey=args.apiKey, apiSecret=args.apiSecret)
count = 100 # max API will allow
today = datetime.date.today()
query = {
'reverse': 'true',
'start': 0,
'startTime': today,
'count': count,
'filter': args.filter
}
out = []
firsttime = 0
z=0
timeback=0
tijd=0
while True:
data = connector._curl_bitmex(path="execution/tradeHistory", verb="GET", query=query)
out.extend(data)
trades = json.dumps(out, sort_keys=True, indent=4, separators=(',', ': '))
trades = json.loads(trades)
number = len(trades)
x=number-1
tijd = str(trades[0]["transactTime"])
if tijd!=timeback and firsttime!=0:
side = str(trades[0]["side"])
size = str(trades[0]["cumQty"])
product = str(trades[0]["symbol"])
tijd = str(trades[0]["transactTime"])
text = str(side+' '+size+' '+product+' '+tijd)
while x>=0 and firsttime==0:
side = str(trades[x]["side"])
size = str(trades[x]["cumQty"])
product = str(trades[x]["symbol"])
tijd = str(trades[x]["transactTime"])
timeback = tijd
text = str(side+' '+size+' '+product+' '+tijd)
print (text)
x=x-1
firsttime = 1
time.sleep(5)
sys.exit()`
This is great stuff. Is there a way to write to a SQL DB rather than a file?
get error using Python 3.6.2 on Win 10
command that produced error:
python fetchExecutionTradeHistory.py -h
error:
File "fetchExecutionTradeHistory.py", line 5, in <module>
from lib import bitmex
File "C:\python\BitMEX-easy-data-scripts\lib\bitmex.py", line 203
except requests.exceptions.HTTPError, e:
^
SyntaxError: invalid syntax
I'm getting this error printed in the CSV file
Unhandled Error: 429 Client Error: Too Many Requests for url: https://www.bitmex.com/api/v1/execution/tradeHistory?reverse=true&start=42000&count=500&filter=%7B%22symbol%22%3A+%22XBTUSD%22%7D {"error":{"message":"Rate limit exceeded, retry in 1 seconds.","name":"RateLimitError"}}
Endpoint was: GET execution/tradeHistory
Anyway to deal with rate limits within this project?
Z
Can't see an option to retrieve this info: https://testnet.bitmex.com/app/wallet. On their WS API page they mention ws.transact(), i tried unsuccessfully to change the code to get the info.
This info makes it a lot easier to calculate whether you are ahead or not.
Depth does not work in OrderBook.OrderBook_getL2(symbol="XBT", depth=1).result() method for test environment.
I also tried it with the following url request, the result is same:
https://testnet.bitmex.com/api/v1/orderBook/L2?symbol=XBT&depth=1
Regards
hello
can i knoe if is possible to export tick data with this code ?
thanks
Having updated settings.py
(API_KEY / API_SECRET) and executing the following I get an error message.
python fetchExecutionTradeHistory.py --apiKey mykey --apiSecret my-api-secret
Traceback (most recent call last):
File "fetchExecutionTradeHistory.py", line 60, in <module>
keys = out[0].keys()
IndexError: list index out of range
I mounted this repo using:
docker run -it -v $(pwd):/root/code python:3.5-jessie /bin/bash
The API key and secret are valid.
Hi,
It seems that recent_trades is not existing on bitmex side.
Issues
Damn need to do my taxes and cannot seem to get this working. With Python 3.6.5 running on OS X Sierra I get this error...
#python3 fetchExecutionTradeHistory.py > bitmex_records.csv
Traceback (most recent call last):
File "fetchExecutionTradeHistory.py", line 5, in <module>
from lib import bitmex
File "/Users/xxxxxxx/Scripts/easy-data-scripts/lib/bitmex.py", line 2, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
I of course inserted my API info in settings.py. Anyone have a clue?
unfortunately i observed that the stop limit order type does not work on a regular basis!?
the order is just ignored by the system
get error using Python 3.6.2 on Win 10
command that produced error:
C:\python\BitMEX-easy-data-scripts>python fetchExecutionTradeHistory.py -h
error:
File "fetchExecutionTradeHistory.py", line 5, in <module>
from lib import bitmex
File "C:\python\BitMEX-easy-data-scripts\lib\bitmex.py", line 57
print "Instrument not found: %s." % self.symbol
^
SyntaxError: Missing parentheses in call to 'print'
Q
Issues
Full of issues and errors
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.