From 7311fd3aeeec79b2f8be34b83dcee3d83c432098 Mon Sep 17 00:00:00 2001 From: "skylerjy.feng" Date: Mon, 7 Nov 2022 16:29:17 +0800 Subject: [PATCH] set leverage revisions --- example/get_started_en.ipynb | 64 +++++++++++++------- example/trade_derivatives_en.ipynb | 94 ++++++++++++++++++++---------- 2 files changed, 106 insertions(+), 52 deletions(-) diff --git a/example/get_started_en.ipynb b/example/get_started_en.ipynb index fee4020..de8db08 100644 --- a/example/get_started_en.ipynb +++ b/example/get_started_en.ipynb @@ -15,13 +15,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "pycharm": { "name": "#%%\n" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: python-okx in /Users/skylerfeng/opt/anaconda3/lib/python3.9/site-packages (0.0.12)\n", + "Requirement already satisfied: h2 in /Users/skylerfeng/opt/anaconda3/lib/python3.9/site-packages (from python-okx) (4.1.0)\n", + "Requirement already satisfied: httpx in /Users/skylerfeng/opt/anaconda3/lib/python3.9/site-packages (from python-okx) (0.23.0)\n", + "Requirement already satisfied: hyperframe<7,>=6.0 in /Users/skylerfeng/opt/anaconda3/lib/python3.9/site-packages (from h2->python-okx) (6.0.1)\n", + "Requirement already satisfied: hpack<5,>=4.0 in /Users/skylerfeng/opt/anaconda3/lib/python3.9/site-packages (from h2->python-okx) (4.0.0)\n", + "Requirement already satisfied: rfc3986[idna2008]<2,>=1.3 in /Users/skylerfeng/opt/anaconda3/lib/python3.9/site-packages (from httpx->python-okx) (1.5.0)\n", + "Requirement already satisfied: certifi in /Users/skylerfeng/opt/anaconda3/lib/python3.9/site-packages (from httpx->python-okx) (2021.10.8)\n", + "Requirement already satisfied: httpcore<0.16.0,>=0.15.0 in /Users/skylerfeng/opt/anaconda3/lib/python3.9/site-packages (from httpx->python-okx) (0.15.0)\n", + "Requirement already satisfied: sniffio in /Users/skylerfeng/opt/anaconda3/lib/python3.9/site-packages (from httpx->python-okx) (1.2.0)\n", + "Requirement already satisfied: h11<0.13,>=0.11 in /Users/skylerfeng/opt/anaconda3/lib/python3.9/site-packages (from httpcore<0.16.0,>=0.15.0->httpx->python-okx) (0.12.0)\n", + "Requirement already satisfied: anyio==3.* in /Users/skylerfeng/opt/anaconda3/lib/python3.9/site-packages (from httpcore<0.16.0,>=0.15.0->httpx->python-okx) (3.5.0)\n", + "Requirement already satisfied: idna>=2.8 in /Users/skylerfeng/opt/anaconda3/lib/python3.9/site-packages (from anyio==3.*->httpcore<0.16.0,>=0.15.0->httpx->python-okx) (3.3)\n" + ] + } + ], "source": [ "! pip install python-okx --upgrade" ] @@ -77,13 +96,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "pycharm": { "name": "#%%\n" } }, - "outputs": [], + "outputs": [ + { + "ename": "ModuleNotFoundError", + "evalue": "No module named 'okx'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn [2], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mokx\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mTrade\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mTrade\u001b[39;00m\n", + "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'okx'" + ] + } + ], "source": [ "import okx.Trade as Trade" ] @@ -277,26 +308,14 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "pycharm": { "name": "#%%\n" }, "scrolled": true }, - "outputs": [ - { - "ename": "AttributeError", - "evalue": "'MarketAPI' object has no attribute 'get_instruments'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "Input \u001b[0;32mIn [1]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m flag \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m1\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;66;03m# live trading:0 , demo trading:1\u001b[39;00m\n\u001b[1;32m 5\u001b[0m marketDataAPI \u001b[38;5;241m=\u001b[39m MarketData\u001b[38;5;241m.\u001b[39mMarketAPI(flag\u001b[38;5;241m=\u001b[39mflag)\n\u001b[0;32m----> 7\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[43mmarketDataAPI\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_instruments\u001b[49m(\n\u001b[1;32m 8\u001b[0m instType\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSPOT\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 9\u001b[0m )\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28mprint\u001b[39m(result)\n", - "\u001b[0;31mAttributeError\u001b[0m: 'MarketAPI' object has no attribute 'get_instruments'" - ] - } - ], + "outputs": [], "source": [ "import okx.MarketData as MarketData\n", "\n", @@ -1094,7 +1113,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.8.9 64-bit", "language": "python", "name": "python3" }, @@ -1108,7 +1127,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.12" + "version": "3.8.9" + }, + "vscode": { + "interpreter": { + "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" + } } }, "nbformat": 4, diff --git a/example/trade_derivatives_en.ipynb b/example/trade_derivatives_en.ipynb index 619f22b..bb43a24 100644 --- a/example/trade_derivatives_en.ipynb +++ b/example/trade_derivatives_en.ipynb @@ -16,23 +16,7 @@ "- Perpetual swaps\n", "- Options\n", "You can refer to [Derivatives Explained](https://www.okx.com/academy/en/bitcoin-derivatives-explained-futures-perpetual-swaps-and-options) for explanations and more information. \n", - "We will use Perpetual swaps as examples in this tutorial.\n", - "\n", - "## Install python package\n", - "You can install and upgrade `python-okx` from PyPi server." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "pycharm": { - "name": "#%%\n" - } - }, - "outputs": [], - "source": [ - "! pip install python-okx --upgrade" + "We will use Perpetual swaps as examples in this tutorial.\n" ] }, { @@ -62,13 +46,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "pycharm": { "name": "#%%\n" } }, - "outputs": [], + "outputs": [ + { + "ename": "ModuleNotFoundError", + "evalue": "No module named 'okx'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn [1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mokx\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mTrade\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mTrade\u001b[39;00m\n", + "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'okx'" + ] + } + ], "source": [ "import okx.Trade as Trade" ] @@ -305,20 +301,32 @@ "metadata": {}, "outputs": [], "source": [ - "# Set leverage on long BTC-USDT-200802 for isolated `FUTURES`\n", + "# Set leverage to be 5x for all cross-margin BTC-USDT SWAP positions,\n", + "# by providing any SWAP instId with BTC-USDT as the underlying\n", "result = accountAPI.set_leverage(\n", - " instId = \"BTC-USDT-200802\",\n", + " instId = \"BTC-USDT-SWAP\",\n", + " lever = \"5\",\n", + " mgnMode = \"cross\"\n", + ")\n", + "print(result)\n", + "\n", + "# Set leverage to be 5x for all isolated-margin BTC-USDT SWAP positions,\n", + "# by providing any SWAP instId with BTC-USDT as the underlying\n", + "result = accountAPI.set_leverage(\n", + " instId = \"BTC-USDT-SWAP\",\n", " lever = \"5\",\n", - " posSide = \"long\",\n", " mgnMode = \"isolated\"\n", ")\n", "print(result)\n", "\n", - "# Set leverage for cross `FUTURES/SWAP` at underlying level\n", + "# Set leverage to be 5x for an isolated-margin \n", + "# BTC-USDT-SWAP long position;\n", + "# This does NOT affect the leverage of any other BTC-USDT SWAP positions with different maturities or posSide\n", "result = accountAPI.set_leverage(\n", - " instId = \"BTC-USDT-200802\",\n", + " instId = \"BTC-USDT-SWAP\",\n", " lever = \"5\",\n", - " mgnMode = \"cross\"\n", + " posSide = \"long\",\n", + " mgnMode = \"isolated\"\n", ")\n", "print(result)" ] @@ -348,11 +356,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "pycharm": { - "name": "#%%\n" - } - }, + "metadata": {}, "outputs": [], "source": [ "import okx.Trade as Trade\n", @@ -362,6 +366,27 @@ "tradeAPI = Trade.TradeAPI(api_key, secret_key, passphrase, False, flag)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Place orders under different position (order placement) modes: long/short and buy/sell\n", + "There are two position (order placement) modes when trading FUTURES and perpetual SWAP: long/short and buy/sell (net).\n", + "You can change the position (order placement) mode between long/short and buy/sell (net), via the API [Set position mode](https://www.okx.com/docs-v5/en/#rest-api-account-set-position-mode):" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "result = accountAPI.set_position_mode(\n", + " posMode=\"long_short_mode\"\n", + ")\n", + "print(result)" + ] + }, { "cell_type": "markdown", "metadata": { @@ -637,7 +662,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.8.9 64-bit", "language": "python", "name": "python3" }, @@ -651,7 +676,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.12" + "version": "3.8.9" + }, + "vscode": { + "interpreter": { + "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" + } } }, "nbformat": 4,