Skip to content

Instantly share code, notes, and snippets.

@muyexi
Last active October 18, 2021 06:19
Show Gist options
  • Save muyexi/fc3ba2e88755aa3823365ee4e25e808f to your computer and use it in GitHub Desktop.
Save muyexi/fc3ba2e88755aa3823365ee4e25e808f to your computer and use it in GitHub Desktop.
Binance.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Binance.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyMVdh1B0ThE7tpCri9lxgsr",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/muyexi/fc3ba2e88755aa3823365ee4e25e808f/binance.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-f-IBUDUEunC"
},
"source": [
"Change default margin mode and leverage for all Binance future contracts.\n",
"\n",
"For your API key, remmeber to [enable futures](https://www.binance.com/en/my/settings/api-management)."
]
},
{
"cell_type": "code",
"metadata": {
"id": "JARWhZAPFI-S"
},
"source": [
"!pip install ccxt"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "IGL01UDqC5Bx"
},
"source": [
"import ccxt\n",
"import json\n",
"\n",
"binance = ccxt.binance({\n",
" \"options\": {\"defaultType\": \"future\"},\n",
" \"timeout\": 30000,\n",
" \"apiKey\":\"YOUR_API_KEY\",\n",
" \"secret\": \"YOUR_API_SECRET\",\n",
" \"enableRateLimit\": True,\n",
"})\n",
"\n",
"binance.load_markets() # load markets to get the market id from a unified symbol\n",
"\n",
"for k,v in binance.markets.items():\n",
" print(v['id'])\n",
" \n",
" try:\n",
" res = binance.set_leverage(10, v['id'])\n",
" print (json.dumps(res, indent=2))\n",
"\n",
" res = binance.set_margin_mode(v['id'], 'ISOLATED') # ISOLATED or CROSSED\n",
" print (json.dumps(res, indent=2)) \n",
" except Exception as e:\n",
" print (e)\n",
" pass\n",
" "
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment