Closed popomomo closed 3 years ago
Hi! Binance does not support a side
parameter for the fetchOrders
call (https://binance-docs.github.io/apidocs/spot/en/#current-open-orders-user_data). You should not expect that any arbitrary undocumented parameter will work. Only the documented params will actually work correctly. In order to solve this, you just fetch all orders, and then filter them by their side.
import ccxt
from pprint import pprint
print('CCXT Version:', ccxt.__version__)
exchange = ccxt.binance({
'apiKey': 'YOUR_API_KEY',
'secret' : 'YOUR_SECRET',
})
if exchange.has['fetchOrders']:
symbol = 'DOGE/USDT'
try:
orders = exchange.fetch_orders(symbol)
side = 'buy'
filtered_orders = [order for order in orders if order['side'] == side]
print(len(filtered_orders), side, 'orders:')
pprint(filtered_orders)
except Exception as e:
print(type(e).__name__, str(e))
We highly recommend reading the entire CCXT Manual from top to bottom – it will really save your time:
Hope that answers your question, let us know if not.
Thanks for your fast reply!!
I just trying to use the same code to fetch a order/trade history on margin market instead of spot and it work fine. Why spot can't? haha
Anyway I will try your code that you suggest on a spot market and will let you know.
Thank you
@popomomo I am trying to do the exact same thing, did you figure this out?
@samgermain what have you tried already? Have you tried the snippet above?
@kroitor
I've tried this
import ccxt
config = {
"api_key": "xxxxxxxxx",
"api_secret": "xxxxxxxx"
}
exchange_id = 'binance'
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({
'apiKey': config["api_key"],
'secret': config["api_secret"],
'timeout': 30000,
'enableRateLimit': True,
"useServerTime": True, # If you get timestamp errors, synchronize to server time at startup
# "test": True # If you want to use sandbox mode where orders are simulated
})
spot_balance = exchange.fetch_balance({'type':'spot'})
exchange.create_order(symbol='ADA/USDT', type='market', side='buy', amount=8, price=1.59, params={'leverage': 3})
it works for regular, just not for leverage
@samgermain as far as I understand, you want to apply some leverage on futures contracts, is that correct? Also, what is your version of CCXT ?
Not futures, just regular margin leverage. It would make sense if I also had to make a transfer to a margin account, but that's not anywhwere in the manual.
If that were the problem, I feel that the error would be Inssfficient funds" though anyway
Versions 1.51.3
@samgermain afaik, Binance margin API does not let you change your leverage at all. Instead, you just choose your margin mode, either cross-margin (and your max leverage is fixed to 3x in that case, this is the default) or isolated margin (and your max leverage is fixed at 10x in that case). The actual leverage will depend on how much money you borrow against your own funds.
Thus, the following should work:
import ccxt
from pprint import pprint
print('CCXT Version:', ccxt.__version__)
exchange = ccxt.binance({
'apiKey': 'YOUR_API_KEY',
'secret': 'YOUR_SECRET',
'options': {
'defaultType': 'margin'
},
})
markets = exchange.load_markets()
# exchange.verbose = True # uncomment for debugging purposes
# transfer HOW_MUCH USDT from Main account to Margin account
# you must have a transfer permission on the API key
response = exchange.transfer('USDT', HOW_MUCH, 'main', 'margin')
pprint(response)
print('-------------------------------------------------------------------------')
margin_balance = exchange.fetch_balance()
pprint(margin_balance)
print('-------------------------------------------------------------------------')
# this will place an order to open a margin position with leverage up to 3 (crossed margin by default)
# you must have at least some USDT balance on your Margin account to place this order
order = exchange.create_order(symbol='ADA/USDT', type='market', side='buy', amount=8, price=1.59)
pprint(order)
UPD. minor edits
So it turns out you also have to borrow in order to trade with any leverage, this is what a POST request to binance would look like in order to do that. This is what that would looks like
exchange.sapi_post_margin_loan({"asset": "USDT", "isIsolated": False, "symbol": "ADAUSDT", "amount": 27})
But this is specific to binance, is there a general method which can do this for multiple exchanges or is this the only way?
But this is specific to binance, is there a general method which can do this for multiple exchanges or is this the only way?
This aspect of margin trading is not fully unified yet, it's a work in progress. But you can call any endpoint of any exchange using the implicit methods as described in the Manual: https://github.com/ccxt/ccxt/wiki/Manual#implicit-api. We will do our best to unify the borrowing and lending aspects asap. In the meantime it is exchange-specific.
It looks like response = exchange.transfer('USDT', HOW_MUCH, 'main', 'margin')
is always going to transfer to spot margin
Is there a way to transfer to isolated margin with this method?
I see that there's a params argument
def transfer(self, code, amount, fromAccount, toAccount, params={}):
Could one of these params be isIsolated: true
or something?
If not, i see that there is sapi_post_margin_isolated_transfer
for binance
Could one of these params be
isIsolated: true
or something?
We will look into adding something like that.
If not, i see that there is
sapi_post_margin_isolated_transfer
for binance
In the meantime, yes, you have to do this via the implicit API:
Hi, I'm trying to fetch latest order/trade history query by 'side' parameter (buy or sell). As you can see in code below I always get a BadRequest error " binance {"code":-1104,"msg":"Not all sent parameters were read; read '5' parameter(s) but was sent '6'."} " if I use parameter with 'side'. But if I change it to 'type' with 'market/limit' it work fine.
Test and trying with fetchOrders, fetchClosedOrders, fetchMyTrades all of these got same error