question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FTX fetch_open_orders and fetch_orders don't fetch trigger orders

See original GitHub issue
  • OS: Windows
  • Programming Language version: Python
  • CCXT version: 1.41.82

I set up a test account and opened three orders: A low limit buy, a market stop loss and a trailing stop loss order - of those three only the limit order gets returned by fetch_orders and fetch_open_orders.

This is not the correct behavior according to #6669 (see https://github.com/ccxt/ccxt/issues/6669#issuecomment-603703325) and apparently has been fixed/implemented there - it seems to be broken again (or I am just dumb, which is always an option)

    exchange = ccxt.ftx()
    exchange.apiKey = "<key>"
    exchange.secret = "<secret>"
    exchange.headers = {'FTX-SUBACCOUNT': "<mysubaccount>", }

    pp(exchange.fetch_orders('BULL/USD'))
    pp(exchange.fetch_open_orders('BULL/USD'))

EDIT: cancel_all_orders also does NOT cancel trigger orders. Seems to be a bug in this piece of code:

    def cancel_all_orders(self, symbol=None, params={}):
        self.load_markets()
        conditionalOrdersOnly = self.safe_value(params, 'conditionalOrdersOnly')
        request = {
            # 'market': market['id'],  # optional
            # 'conditionalOrdersOnly': False,  # cancel conditional orders only
            # 'limitOrdersOnly': False,  # cancel existing limit orders(non-conditional orders) only
        }
        if conditionalOrdersOnly:
            request['conditionalOrdersOnly'] = conditionalOrdersOnly
        else:
            request['limitOrdersOnly'] = True

request['limitOrdersOnly'] in the last line should probably NOT be set to true just because conditionalOrdersOnly is not set to true.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
no-trick-ponycommented, Feb 23, 2021

I checked that, and while the former (“FTX cannot return both the regular orders and the stop orders at the same time”) seems to be true, the latter (“The same goes to canceling them.”) does not seem to be the case.

I tested it with the vanilla FTX-API and their endpoint cancelled all my orders for all markets. Here is some code with the vanilla FTX-API that calls all three endpoints. I created a limit order and a trailing stop order for this.

import time
import hmac
from requests import Request, Session
from urllib.parse import urljoin
from pprint import pp

session = Session()

def sign_request(request: Request):
    prepared = request.prepare()
    ts = int(time.time() * 1000)
    signature_payload = f'{ts}{prepared.method}{prepared.path_url}'.encode()
    signature = hmac.new('<api_secret>'.encode(), signature_payload, 'sha256').hexdigest()

    prepared.headers['FTX-KEY'] = '<api_key>'
    prepared.headers['FTX-SIGN'] = signature
    prepared.headers['FTX-TS'] = str(ts)
    return prepared

def request(endpoint, params=[], account: str=None, method: str='GET'):
    url = urljoin('https://ftx.com/api/', endpoint)
    print(url)

    request = Request(method, url, params)
    request = sign_request(request)
    if account:
        request.headers['FTX-SUBACCOUNT'] = account
    response = session.send(request)
    print(response.status_code)
    pp(response.json())

subaccount = '<your_subaccount>'
# Only prints my limit order
request('orders', account=subaccount )

# Only prints my trailing stop order
request('conditional_orders', account=subaccount )

# Deletes all my orders (limit AND trailing stop)
request('orders', account=subaccount, method='DELETE')

It would probably be a good idea to add a comment about the special behavior of the fetch_open_orders and fetch_orders method in ftx.py - I would have honestly expected for ccxt to just make 2 API calls and merge the result, since https://github.com/ccxt/ccxt/wiki/Manual#all-orders says “Some exchanges don’t have an endpoint for fetching all orders, ccxt will emulate it where possible”. I guess that’s problematic in terms of API call counting to not run into rate limiting?

Thanks for the reply though - at least I now know how to fetch all orders (make 2 calls and merge them manually). I need this to first get all open orders, then make a new trailing stop order for the amount I already have plus the amount I plan to buy, then make a market buy order for the amount I want to buy and finally delete all the orders i first fetched - I think that’s the safest way to make buying failsafe (In case a failure occurs at any point I am not ending up with bought currency without a stop order).

Thanks for your help again!

0reactions
AlgoQcommented, Mar 2, 2021

@kroitor Also, when I try to use the fetchAllOrders with the privateGetConditionalOrders method on FTX Futures, I get an empty list as return (btw, the stop order went through).

Code:

slOrder = connection.create_order('AAVE-PERP', 'stop', 'sell', 0.01, None, {'triggerPrice':300, 'reduceOnly': True})
time.sleep(5)
openTriggerOrders = connection.fetch_open_orders('AAVE-PERP', {'method':'privateGetConditionalOrders'})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Exchanges — ccxt 2.4.71 documentation
The CCXT library currently supports the following 104 cryptocurrency exchange markets and trading APIs: Besides making basic market and limit orders, some ...
Read more >
Advanced Order Types - FTX.US
These orders do not enter the order-book until the market price reaches a trigger price, at which point they are sent as orders...
Read more >
python/ccxt/async_support/ftx.py - searchcode
1# -*- coding: utf-8 -*- 2 3# PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED ... 58 'fetchOHLCV': True, 59 'fetchOpenOrders': True,...
Read more >
ccxt-dev/ccxt - Gitter
the corresponding unified method would be fetchOrder. or fetchOpenOrders ← if you have seen your order here and don't see it here anymore,...
Read more >
python - Get Executed Orders with CCXT - FTX - Stack Overflow
I wonder if there is a way to get notified when an order is executed by using the CCXT with FTX Exchange. I've...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found