{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# OCO Orders" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This section is about **OCO (\"One Cancels Other\") orders**." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import fxcmpy\n", "import pandas as pd\n", "import datetime as dt\n", "con = fxcmpy.fxcmpy(config_file='fxcm.cfg')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating an OCO Order" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To begin with, check **the open positons**." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: []\n", "Index: []" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.get_orders()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "OCO orders are created by the method `con.create_oco_order()`. A detailed description of the method's parameters is found in section _API Documentation_. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "oco_order = con.create_oco_order(account_id='2815291', \n", " symbol='EUR/USD', \n", " is_buy=True, is_buy2=False, \n", " amount=30, is_in_pips= False, \n", " time_in_force='GTC',\n", " at_market=1, \n", " order_type='MarketRange', \n", " expiration='01122017',\n", " limit=1.13, limit2=1.12, \n", " rate=1.11, rate2=1.13,\n", " stop=1.1, stop2=1.15, \n", " trailing_step=0, trailing_step2=0, \n", " trailing_stop_step=0, \n", " trailing_stop_step2=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `con.oco_orders` attribute is a `dict` object, containing all existing OCO orders as values and their `ids` as keys. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{404812350: }" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.oco_orders " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`con.get_oco_order_ids()` returns the OCO order `ids`." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[404812350]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.get_oco_order_ids()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "After the OCO order placement, two orders a active. The `ocoBulkId` value connects the two orders to the OCO order." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
01
accountId28152912815291
accountName0281529102815291
amountK3030
buy1.110
currencyEUR/USDEUR/USD
currencyPoint33
expireDate
isBuyTrueFalse
isELSOrderFalseFalse
isEntryOrderTrueTrue
isLimitOrderTrueFalse
isNetQuantityFalseFalse
isStopOrderFalseTrue
limit1.131.12
limitPegBaseType-1-1
limitRate1.131.12
ocoBulkId404812350404812350
orderId404812349404812351
range00
ratePrecision55
sell01.13
status11
stop1.11.15
stopMove00
stopPegBaseType-1-1
stopRate1.11.15
t33
time0607201813595293106072018135952932
timeInForceGTCGTC
tradeId178168188178168189
typeLESE
\n", "
" ], "text/plain": [ " 0 1\n", "accountId 2815291 2815291\n", "accountName 02815291 02815291\n", "amountK 30 30\n", "buy 1.11 0\n", "currency EUR/USD EUR/USD\n", "currencyPoint 3 3\n", "expireDate \n", "isBuy True False\n", "isELSOrder False False\n", "isEntryOrder True True\n", "isLimitOrder True False\n", "isNetQuantity False False\n", "isStopOrder False True\n", "limit 1.13 1.12\n", "limitPegBaseType -1 -1\n", "limitRate 1.13 1.12\n", "ocoBulkId 404812350 404812350\n", "orderId 404812349 404812351\n", "range 0 0\n", "ratePrecision 5 5\n", "sell 0 1.13\n", "status 1 1\n", "stop 1.1 1.15\n", "stopMove 0 0\n", "stopPegBaseType -1 -1\n", "stopRate 1.1 1.15\n", "t 3 3\n", "time 06072018135952931 06072018135952932\n", "timeInForce GTC GTC\n", "tradeId 178168188 178168189\n", "type LE SE" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.get_orders().T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Changing OCO Orders" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To add an existing order to an OCO order, the method `con.add_to_oco()` is used.\n", "\n", "First, an entry **order is created**." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "order = con.create_entry_order(symbol='USD/JPY', is_buy=True, rate = 110,\n", " amount=50, is_in_pips = False, \n", " time_in_force='GTC', stop=None,\n", " limit=112, trailing_step=None)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The new order's `id` is:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "404812374" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "order_id = order.get_orderId()\n", "order_id" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The order's `ocoBulkId` should be 0:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "order.get_ocoBulkId()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Second, the **order is added** to the OCO order via the `ocoBulkId` value." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "bulk_id = con.get_oco_order_ids()[0]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "con.add_to_oco(oco_bulk_id=bulk_id, order_ids=[order_id])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now the `ocoBulkId` value is the same as for the OCO order from above." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "404812350" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "order.get_ocoBulkId()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Removing an order** is accomplished via the `con.remove_from_oco()` method." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "con.remove_from_oco(order_ids=[order_id])" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "order.get_ocoBulkId()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once can also **add and remove orders** in one step, just use `edit_oco_order()`" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "order_id_2 = con.get_order_ids()[0]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "con.edit_oco(oco_bulk_id=bulk_id, \n", " remove_order_ids=[order_id_2],\n", " add_order_ids=[order_id])" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012
accountId281529128152912815291
accountName028152910281529102815291
amountK303050
buy1.110110
currencyEUR/USDEUR/USDUSD/JPY
currencyPoint334.54496
expireDate
isBuyTrueFalseTrue
isELSOrderFalseFalseFalse
isEntryOrderTrueTrueTrue
isLimitOrderTrueFalseTrue
isNetQuantityFalseFalseFalse
isStopOrderFalseTrueFalse
limit1.131.12112
limitPegBaseType-1-1-1
limitRate1.131.12112
ocoBulkId0404812350404812350
orderId404812349404812351404812374
range000
ratePrecision553
sell01.130
status111
stop1.11.150
stopMove000
stopPegBaseType-1-1-1
stopRate1.11.150
t333
time060720181401396760607201813595293206072018140139677
timeInForceGTCGTCGTC
tradeId178168188178168189178168201
typeLESELE
\n", "
" ], "text/plain": [ " 0 1 2\n", "accountId 2815291 2815291 2815291\n", "accountName 02815291 02815291 02815291\n", "amountK 30 30 50\n", "buy 1.11 0 110\n", "currency EUR/USD EUR/USD USD/JPY\n", "currencyPoint 3 3 4.54496\n", "expireDate \n", "isBuy True False True\n", "isELSOrder False False False\n", "isEntryOrder True True True\n", "isLimitOrder True False True\n", "isNetQuantity False False False\n", "isStopOrder False True False\n", "limit 1.13 1.12 112\n", "limitPegBaseType -1 -1 -1\n", "limitRate 1.13 1.12 112\n", "ocoBulkId 0 404812350 404812350\n", "orderId 404812349 404812351 404812374\n", "range 0 0 0\n", "ratePrecision 5 5 3\n", "sell 0 1.13 0\n", "status 1 1 1\n", "stop 1.1 1.15 0\n", "stopMove 0 0 0\n", "stopPegBaseType -1 -1 -1\n", "stopRate 1.1 1.15 0\n", "t 3 3 3\n", "time 06072018140139676 06072018135952932 06072018140139677\n", "timeInForce GTC GTC GTC\n", "tradeId 178168188 178168189 178168201\n", "type LE SE LE" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.get_orders().T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The `fxcmpy_oco_order` Class" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The methods `con.get_oco_order()` and `con.create_oco_order()` both return an instance of the `fxcmpy_oco_order` class. " ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": true }, "outputs": [], "source": [ "oco_order = con.get_oco_order(bulk_id)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "oco_order" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This class provides methods to get the instance's `ocoBulkId` ..." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "404812350" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "oco_order.get_ocoBulkId()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... the `ids` of the contained orders ..." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[404812351, 404812374]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "oco_order.get_order_ids()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... and the order objects itself." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[,\n", " ]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "oco_order.get_orders()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The class has also methods to **add or remove existing orders**." ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": true }, "outputs": [], "source": [ "new_order = con.create_entry_order(symbol='GBP/USD', is_buy=True,\n", " amount=300, rate= 1.36,\n", " limit=1.37, is_in_pips = False,\n", " time_in_force='GTC')" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": true }, "outputs": [], "source": [ "oco_order.add_order([new_order])" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[,\n", " ,\n", " ]" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "oco_order.get_orders()" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "oco_order.remove_order([new_order])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is also possible to add orders to or remove from the OCO order **in a single step**." ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": true }, "outputs": [], "source": [ "order = oco_order.get_orders()[0]" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": true }, "outputs": [], "source": [ "oco_order.edit_order(add_orders=[new_order], remove_orders=[order])" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[404812374, 404812521]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "oco_order.get_order_ids()" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": true }, "outputs": [], "source": [ "con.close()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }