Hi, Is there a plan to integrate margin trading in the wrapper? I really need to adapt my bot to trade on margin.
Please advise.
Thanks
I did it myself ..
Feel free to submit a pull request @trinhvv
@trinhvv are you able to share the margin integration you did?
It would be great if we could have margin trading endpoints integrated to this wrapper.
I'm willing to help, I'm just a little confused about how this wrapper works and I haven't touched the actual code of wrappers before.
Maybe I'm overcomplicating this.
I'm really confused because the methods here use **params, meaning you pass keyworded arguments like quantity=7. But nowhere in the code on this repo does it seem to check for whether the keywords are valid ones.
For example, in the comment in def_create_order, it lists arguments required by the API. What's stopping a user from doing
client.create_order(mistyped_arg="some incorrect type") through this wrapper ?
Is there anything in the code preventing functions being called with invalid arguments, or is an API request still made and then the user just deals with the error returned from the API?
It would be great if we could have margin trading endpoints integrated to this wrapper.
I'm willing to help, I'm just a little confused about how this wrapper works and I haven't touched the actual code of wrappers before.Maybe I'm overcomplicating this.
I'm really confused because the methods here use **params, meaning you pass keyworded arguments like quantity=7. But nowhere in the code on this repo does it seem to check for whether the keywords are valid ones.
For example, in the comment in def_create_order, it lists arguments required by the API. What's stopping a user from doing
client.create_order(mistyped_arg="some incorrect type") through this wrapper ?Is there anything in the code preventing functions being called with invalid arguments, or is an API request still made and then the user just deals with the error returned from the API?
Parameters are defined in API docs, for instance:

My work is for personal use not for production as I created an independent client for margin only.
The thing I was confused about was how the wrapper knows which params are needed for each endpoint, if this is even the case. That way I can add the endpoints to client.py. I'd also like to know if only client.py needs to be updated; it seems so but I want to make sure.
@ikubay I implemented a couple of calls to enable margin trading.
Yes, you only need to add code to client.py. While it looks complicated at first you'll find your way around the code.
Params are passed via the kwargs, you need to give them the same name as in the Binance documentation.
Endpoints that that need to be signed (and timestamped) are taken care of by the wrapper. Just pass True so that the call gets signed, the existing code takes care of the rest.
Here is an example to get you started:
response = self.api_client.loan_asset(
asset=self.symbol,
amount=quantity
)
@trinhivv
Would you mind sharing your code?
@ikubay I implemented a couple of calls to enable margin trading.
Yes, you only need to add code to
client.py. While it looks complicated at first you'll find your way around the code.Params are passed via the kwargs, you need to give them the same name as in the Binance documentation.
Endpoints that that need to be signed (and timestamped) are taken care of by the wrapper. Just pass
Trueso that the call gets signed, the existing code takes care of the rest.Here is an example to get you started:
response = self.api_client.loan_asset( asset=self.symbol, amount=quantity )@trinhivv
Would you mind sharing your code?
Wonderful implementation, please pr
Thanks everyone for their input here. Had been on vacation!
I've updated to v0.7.2 which includes all the margin trading endpoints. Some docs here https://python-binance.readthedocs.io/en/latest/margin.html
@sammchardy thanks for this release. I noticed the info = client.get_margin_account() is returning error: 'data'. Would you mind looking into this?
@sammchardy thanks for this release. I noticed the info = client.get_margin_account() is returning error: 'data'. Would you mind looking into this?
It should be like this and worked.
def get_margin_account(self, **params):
"""Query margin account details
https://github.com/binance-exchange/binance-official-api-docs/blob/master/margin-api.md#query-margin-account-details-user_data
:returns: API response
.. code-block:: python
{
"borrowEnabled": true,
"marginLevel": "11.64405625",
"totalAssetOfBtc": "6.82728457",
"totalLiabilityOfBtc": "0.58633215",
"totalNetAssetOfBtc": "6.24095242",
"tradeEnabled": true,
"transferEnabled": true,
"userAssets": [
{
"asset": "BTC",
"borrowed": "0.00000000",
"free": "0.00499500",
"interest": "0.00000000",
"locked": "0.00000000",
"netAsset": "0.00499500"
},
{
"asset": "BNB",
"borrowed": "201.66666672",
"free": "2346.50000000",
"interest": "0.00000000",
"locked": "0.00000000",
"netAsset": "2144.83333328"
},
{
"asset": "ETH",
"borrowed": "0.00000000",
"free": "0.00000000",
"interest": "0.00000000",
"locked": "0.00000000",
"netAsset": "0.00000000"
},
{
"asset": "USDT",
"borrowed": "0.00000000",
"free": "0.00000000",
"interest": "0.00000000",
"locked": "0.00000000",
"netAsset": "0.00000000"
}
]
}
:raises: BinanceRequestException, BinanceAPIException
"""
return self._request_margin_api('get', 'margin/account', True, data=params)
Thanks @trinhvv
@sammchardy thanks for this release. I noticed the info = client.get_margin_account() is returning error: 'data'. Would you mind looking into this?
It should be like this and worked.
def get_margin_account(self, **params): """Query margin account details https://github.com/binance-exchange/binance-official-api-docs/blob/master/margin-api.md#query-margin-account-details-user_data :returns: API response .. code-block:: python { "borrowEnabled": true, "marginLevel": "11.64405625", "totalAssetOfBtc": "6.82728457", "totalLiabilityOfBtc": "0.58633215", "totalNetAssetOfBtc": "6.24095242", "tradeEnabled": true, "transferEnabled": true, "userAssets": [ { "asset": "BTC", "borrowed": "0.00000000", "free": "0.00499500", "interest": "0.00000000", "locked": "0.00000000", "netAsset": "0.00499500" }, { "asset": "BNB", "borrowed": "201.66666672", "free": "2346.50000000", "interest": "0.00000000", "locked": "0.00000000", "netAsset": "2144.83333328" }, { "asset": "ETH", "borrowed": "0.00000000", "free": "0.00000000", "interest": "0.00000000", "locked": "0.00000000", "netAsset": "0.00000000" }, { "asset": "USDT", "borrowed": "0.00000000", "free": "0.00000000", "interest": "0.00000000", "locked": "0.00000000", "netAsset": "0.00000000" } ] } :raises: BinanceRequestException, BinanceAPIException """ return self._request_margin_api('get', 'margin/account', True, data=params)
Thanks @trinhvv. When I used argument "symbol = USDT" or "asset = USDT" does not return only USDT details.
@quadricanna @trinhvv Could you share how are you calling client.get_margin_account()? I'm also returning KeyError: 'data'.
Thanks @trinhvv
@sammchardy thanks for this release. I noticed the info = client.get_margin_account() is returning error: 'data'. Would you mind looking into this?
It should be like this and worked.
def get_margin_account(self, **params): """Query margin account details https://github.com/binance-exchange/binance-official-api-docs/blob/master/margin-api.md#query-margin-account-details-user_data :returns: API response .. code-block:: python { "borrowEnabled": true, "marginLevel": "11.64405625", "totalAssetOfBtc": "6.82728457", "totalLiabilityOfBtc": "0.58633215", "totalNetAssetOfBtc": "6.24095242", "tradeEnabled": true, "transferEnabled": true, "userAssets": [ { "asset": "BTC", "borrowed": "0.00000000", "free": "0.00499500", "interest": "0.00000000", "locked": "0.00000000", "netAsset": "0.00499500" }, { "asset": "BNB", "borrowed": "201.66666672", "free": "2346.50000000", "interest": "0.00000000", "locked": "0.00000000", "netAsset": "2144.83333328" }, { "asset": "ETH", "borrowed": "0.00000000", "free": "0.00000000", "interest": "0.00000000", "locked": "0.00000000", "netAsset": "0.00000000" }, { "asset": "USDT", "borrowed": "0.00000000", "free": "0.00000000", "interest": "0.00000000", "locked": "0.00000000", "netAsset": "0.00000000" } ] } :raises: BinanceRequestException, BinanceAPIException """ return self._request_margin_api('get', 'margin/account', True, data=params)Thanks @trinhvv. When I used argument "symbol = USDT" or "asset = USDT" does not return only USDT details.
Because this call has no parameters as indicated in https://github.com/binance-exchange/binance-official-api-docs/blob/master/margin-api.md#query-margin-account-details-user_data
@quadricanna @trinhvv Could you share how are you calling client.get_margin_account()? I'm also returning KeyError: 'data'.
You have to download this library, edit the function get_margin_account() in the client.py and put it in your working directory.
@trinhvv v0.7.3 fixes the margin account details call.
@trinhvv Thank you.
@trinhvv is there an easy way to get the USDT balance? See my code below but Biance keeping adding my coins so the index changes from time to time.
usdt_balance = float(client.get_margin_account()['userAssets'][7]['free'])
Thanks
@trinhvv is there an easy way to get the USDT balance? See my code below but Biance keeping adding my coins so the index changes from time to time.
usdt_balance = float(client.get_margin_account()['userAssets'][7]['free'])
Thanks
This is simply Pythonic.
You can do in many way. For me I prefer using pandas because it helps data manipulation.
import pandas as pd
info = client.get_margin_account()
balances = pd.DataFrame(info['userAssets'])
assetBalance = float(balances[balances['asset']=='USDT'].iloc[0]['free'])
info = client.get_margin_account()
balances = pd.DataFrame(info['userAssets'])
assetBalance = float(balances[balances['asset']=='USDT'].iloc[0]['free'])
Thanks @trinhvv
Most helpful comment
Thanks everyone for their input here. Had been on vacation!
I've updated to v0.7.2 which includes all the margin trading endpoints. Some docs here https://python-binance.readthedocs.io/en/latest/margin.html