Skip to content

Instantly share code, notes, and snippets.

@rmcxm
Last active June 12, 2020 01:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmcxm/2eef66c6f5da153f1e2bfeaf65d4e9c1 to your computer and use it in GitHub Desktop.
Save rmcxm/2eef66c6f5da153f1e2bfeaf65d4e9c1 to your computer and use it in GitHub Desktop.
ccxt api samples
import ccxt
exchange_id = 'bitmex'
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({'apiKey': 'jyZEyWNzfRLaT', 'secret': 'hMwffHAZDiEg0iewrV51k', 'timeout': 60000})
exchange.fetch_order_book('BTC/USD',limit=2))['bids'][0][0]
exchange.fetch_trades('BTC/USD',limit=1)
print(exchange.fetch_ticker('BTC/USD'))
exchange.createLimitBuyOrder ('ETH/USD', 1, 148, { 'execInst': 'ParticipateDoNotInitiate' })
exchange.createLimitBuyOrder ('ETH/USD', 1, 149, { 'execInst': 'ParticipateDoNotInitiate' })['status']
'canceled'
'open'
['id']
stop:
exchange.create_order(symbol = 'ETH/USD', type = 'stop', side = 'sell', amount = 1, params = { 'ordType': 'Stop', 'stopPx': 147, 'execInst': 'Close,LastPrice' })
exchange.create_order(symbol = 'ETH/USD', type = 'stop', side = 'buy', amount = 1, params = { 'ordType': 'Stop', 'stopPx': 147, 'execInst': 'Close,LastPrice' })
trailing:
exchange.create_order(symbol = 'ETH/USD', type = 'stop', side = 'sell', amount = 1, params = { 'ordType': 'Stop', 'pegPriceType': 'TrailingStopPeg', 'pegOffsetValue': -5.0, 'execInst': 'Close,LastPrice' })
exchange.create_order(symbol = 'ETH/USD', type = 'stop', side = 'buy', amount = 1, params = { 'ordType': 'Stop', 'pegPriceType': 'TrailingStopPeg', 'pegOffsetValue': 5.0, 'execInst': 'Close,LastPrice' })
position:
exchange.private_get_position({'filter': exchange.json({"isOpen":True, "symbol":"XBTUSD"})})
exchange.private_get_position({'filter': exchange.json({"isOpen":True, "symbol":"ETHUSD"})})
ret = self.bitmexengine.exchange.cancelOrder(db_order['bitmexId'])
exchange.fetchOrder (id='a93e01a4-74fd-ac4b-75fd-10262c7840c7')['status']
'closed'
x=exchange.fetchOrders (symbol='BTC/USD', since=(exchange.milliseconds ()- 50000000), limit=500, params={'orderID': '28c55431-7c97-d9b0-e674-d4b47b95b6ce'})
edit order:
exchange.editOrder(id='c1245e46-0257-c547-b8c6-7f676581930b',symbol='ETH/USD',type='limit',side='buy',amount = 1, price=140,params = { })
fetchorder return value:
{'lastTradeTimestamp': 1579689767593, 'status': 'open', 'timestamp': 1579689767593, 'symbol': u'ETH/USD', 'datetime': '2020-01-22T10:42:47.593Z', 'price': 140.0, 'cost': 0.0, 'id': 'c1245e46-0257-c547-b8c6-7f676581930b', 'info': {u'ordStatus': u'New', u'exDestination': u'XBME', u'text': u'Amended orderQty price: Amended via API.\nSubmitted via API.', u'timeInForce': u'GoodTillCancel', u'currency': u'USD', u'pegPriceType': u'', u'simpleLeavesQty': None, u'ordRejReason': u'', u'transactTime': u'2020-01-22T10:42:47.593Z', u'clOrdID': u'', u'settlCurrency': u'XBt', u'cumQty': 0, u'displayQty': None, u'avgPx': None, u'price': 140, u'simpleOrderQty': None, u'contingencyType': u'', u'triggered': u'', u'timestamp': u'2020-01-22T10:42:47.593Z', u'symbol': u'ETHUSD', u'pegOffsetValue': None, u'execInst': u'ParticipateDoNotInitiate', u'simpleCumQty': None, u'orderID': u'c1245e46-0257-c547-b8c6-7f676581930b', u'multiLegReportingType': u'SingleSecurity', u'account': 656334, u'stopPx': None, u'leavesQty': 1, u'orderQty': 1, u'workingIndicator': True, u'ordType': u'Limit', u'clOrdLinkID': u'', u'side': u'Buy'}, 'fee': None, 'average': None, 'side': 'buy', 'amount': 1.0, 'type': 'limit', 'remaining': 1.0, 'filled': 0.0}
limit = 60
multiplier = 1*60
since = exchange.milliseconds () - limit * multiplier * 1000
candles = exchange.fetch_ohlcv('BTC/USD', '1m', since, limit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment