{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Market Orders " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This section covers **market orders**, the most simple yet equally important order type." ] }, { "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": [ "## Placing Orders" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before one gets started, a check of the **open positions** might be helpful." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "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_open_positions().T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Currently, there are no open positions. **To place orders**, one can use the methods `con.create_market_sell_order()` or `con.create_market_buy_order()`, respectively." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "order = con.create_market_sell_order('EUR/USD', 100)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "order = con.create_market_buy_order('USD/JPY', 200)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The new **open positions table** might then look as follows. Two new positions have been created." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "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", "
01
accountId28152912815291
accountName0281529102815291
amountK100200
close1.17701110.152
com00
currencyEUR/USDUSD/JPY
currencyPoint1018.155
grossPL-4-39.9448
isBuyFalseTrue
isDisabledFalseFalse
limit00
open1.17697110.174
ratePrecision53
roll00
stop00
stopMove00
t11
time0606201810412506062018104127
tradeId178067111178067131
usedMargin13002000
valueDate
visiblePL-0.4-2.2
\n", "
" ], "text/plain": [ " 0 1\n", "accountId 2815291 2815291\n", "accountName 02815291 02815291\n", "amountK 100 200\n", "close 1.17701 110.152\n", "com 0 0\n", "currency EUR/USD USD/JPY\n", "currencyPoint 10 18.155\n", "grossPL -4 -39.9448\n", "isBuy False True\n", "isDisabled False False\n", "limit 0 0\n", "open 1.17697 110.174\n", "ratePrecision 5 3\n", "roll 0 0\n", "stop 0 0\n", "stopMove 0 0\n", "t 1 1\n", "time 06062018104125 06062018104127\n", "tradeId 178067111 178067131\n", "usedMargin 1300 2000\n", "valueDate \n", "visiblePL -0.4 -2.2" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.get_open_positions().T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## General Market Orders" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`fxcmpy` allows to place more complex orders, this can be done with the method `con.open_trade()`. A detailed description of the meaning of the parameters is found in the FXCM API Documentation. " ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "order = con.open_trade(symbol='USD/JPY', is_buy=True,\n", " rate=105, is_in_pips=False,\n", " amount='1000', time_in_force='GTC', \n", " order_type='AtMarket', limit=120)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "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", "
012
accountId281529128152912815291
accountName028152910281529102815291
amountK1002001000
close1.17711110.152110.152
com000
currencyEUR/USDUSD/JPYUSD/JPY
currencyPoint1018.155190.7754
grossPL-14-39.9448-190.646
isBuyFalseTrueTrue
isDisabledFalseFalseFalse
limit00120
open1.17697110.174110.173
ratePrecision533
roll000
stop000
stopMove000
t111
time060620181041250606201810412706062018104147
tradeId178067111178067131178067210
usedMargin1300200010000
valueDate
visiblePL-1.4-2.2-2.1
\n", "
" ], "text/plain": [ " 0 1 2\n", "accountId 2815291 2815291 2815291\n", "accountName 02815291 02815291 02815291\n", "amountK 100 200 1000\n", "close 1.17711 110.152 110.152\n", "com 0 0 0\n", "currency EUR/USD USD/JPY USD/JPY\n", "currencyPoint 10 18.1551 90.7754\n", "grossPL -14 -39.9448 -190.646\n", "isBuy False True True\n", "isDisabled False False False\n", "limit 0 0 120\n", "open 1.17697 110.174 110.173\n", "ratePrecision 5 3 3\n", "roll 0 0 0\n", "stop 0 0 0\n", "stopMove 0 0 0\n", "t 1 1 1\n", "time 06062018104125 06062018104127 06062018104147\n", "tradeId 178067111 178067131 178067210\n", "usedMargin 1300 2000 10000\n", "valueDate \n", "visiblePL -1.4 -2.2 -2.1" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.get_open_positions().T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Trade Ids" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Every position has the attribute `tradeId`, here it is the column named `tradeId` of the `DataFrame` object returned by `con.get_open_positions()`." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 178067111\n", "1 178067131\n", "2 178067210\n", "Name: tradeId, dtype: object" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.get_open_positions()['tradeId']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`fxcmpy` provides methods to get the trade `tradeIds` directly." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[178067111, 178067131, 178067210]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.get_open_trade_ids()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[178066615,\n", " 178066381,\n", " 178064934,\n", " 178064922,\n", " 167729381,\n", " 144240844,\n", " 97729921,\n", " 97601524,\n", " 97599708]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.get_closed_trade_ids()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[97599708,\n", " 97601524,\n", " 97729921,\n", " 144240844,\n", " 167729381,\n", " 178064922,\n", " 178064934,\n", " 178066381,\n", " 178066615,\n", " 178067111,\n", " 178067131,\n", " 178067210]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.get_all_trade_ids()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Editing Market Orders" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To modify a trade, you need the `tradeId` value." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "tradeId = con.get_open_trade_ids()[-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To **change the trade's stop or limit rate**, one can use the `change_trade_stop_limit()` method." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "con.change_trade_stop_limit(tradeId, is_in_pips=False,\n", " is_stop=False, rate=115)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As seen below, the limit rate of the last trade has changed to 115. " ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "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", "
012
accountId281529128152912815291
accountName028152910281529102815291
amountK1002001000
close1.17708110.144110.144
com000
currencyEUR/USDUSD/JPYUSD/JPY
currencyPoint1018.156490.782
grossPL-11-54.4741-263.292
isBuyFalseTrueTrue
isDisabledFalseFalseFalse
limit00115
open1.17697110.174110.173
ratePrecision533
roll000
stop000
stopMove000
t111
time060620181041250606201810412706062018104147
tradeId178067111178067131178067210
usedMargin1300200010000
valueDate
visiblePL-1.1-3-2.9
\n", "
" ], "text/plain": [ " 0 1 2\n", "accountId 2815291 2815291 2815291\n", "accountName 02815291 02815291 02815291\n", "amountK 100 200 1000\n", "close 1.17708 110.144 110.144\n", "com 0 0 0\n", "currency EUR/USD USD/JPY USD/JPY\n", "currencyPoint 10 18.1564 90.782\n", "grossPL -11 -54.4741 -263.292\n", "isBuy False True True\n", "isDisabled False False False\n", "limit 0 0 115\n", "open 1.17697 110.174 110.173\n", "ratePrecision 5 3 3\n", "roll 0 0 0\n", "stop 0 0 0\n", "stopMove 0 0 0\n", "t 1 1 1\n", "time 06062018104125 06062018104127 06062018104147\n", "tradeId 178067111 178067131 178067210\n", "usedMargin 1300 2000 10000\n", "valueDate \n", "visiblePL -1.1 -3 -2.9" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.get_open_positions().T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Closing Positions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Positions can be closed individually via the `tradeId` ..." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "con.close_trade(trade_id=tradeId, amount=1000)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "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", "
01
accountId28152912815291
accountName0281529102815291
amountK100200
close1.17718110.145
com00
currencyEUR/USDUSD/JPY
currencyPoint1018.1562
grossPL-21-52.6579
isBuyFalseTrue
isDisabledFalseFalse
limit00
open1.17697110.174
ratePrecision53
roll00
stop00
stopMove00
t11
time0606201810412506062018104127
tradeId178067111178067131
usedMargin13002000
valueDate
visiblePL-2.1-2.9
\n", "
" ], "text/plain": [ " 0 1\n", "accountId 2815291 2815291\n", "accountName 02815291 02815291\n", "amountK 100 200\n", "close 1.17718 110.145\n", "com 0 0\n", "currency EUR/USD USD/JPY\n", "currencyPoint 10 18.1562\n", "grossPL -21 -52.6579\n", "isBuy False True\n", "isDisabled False False\n", "limit 0 0\n", "open 1.17697 110.174\n", "ratePrecision 5 3\n", "roll 0 0\n", "stop 0 0\n", "stopMove 0 0\n", "t 1 1\n", "time 06062018104125 06062018104127\n", "tradeId 178067111 178067131\n", "usedMargin 1300 2000\n", "valueDate \n", "visiblePL -2.1 -2.9" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.get_open_positions().T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... or for all positions in an instrument ... " ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true }, "outputs": [], "source": [ "con.close_all_for_symbol('EUR/USD')" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "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", "
0
accountId2815291
accountName02815291
amountK200
close110.146
com0
currencyUSD/JPY
currencyPoint18.156
grossPL-50.8416
isBuyTrue
isDisabledFalse
limit0
open110.174
ratePrecision3
roll0
stop0
stopMove0
t1
time06062018104127
tradeId178067131
usedMargin2000
valueDate
visiblePL-2.8
\n", "
" ], "text/plain": [ " 0\n", "accountId 2815291\n", "accountName 02815291\n", "amountK 200\n", "close 110.146\n", "com 0\n", "currency USD/JPY\n", "currencyPoint 18.156\n", "grossPL -50.8416\n", "isBuy True\n", "isDisabled False\n", "limit 0\n", "open 110.174\n", "ratePrecision 3\n", "roll 0\n", "stop 0\n", "stopMove 0\n", "t 1\n", "time 06062018104127\n", "tradeId 178067131\n", "usedMargin 2000\n", "valueDate \n", "visiblePL -2.8" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.get_open_positions().T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... or simply for all positions over all instruments." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [], "source": [ "con.close_all()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After this method call, there are naturally no open positions left." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "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": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.get_open_positions()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The **closed positions** are found in the table returned by `con.get_closed_postions()`." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
01234567891011
accountName028152910281529102815291028152910281529102815291028152910281529102815291028152910281529102815291
amountK10010002001001003003003003001000100200
close1.17675110.141110.1421.176631.176631.342731.342731.342731.34273110.141.17726110.151
closeTime060620181038550606201810363306062018103648060620181036380606201810363806062018101450060620181014500606201810145006062018101450060620181042400606201810424706062018104258
com000000000000
currencyEUR/USDUSD/JPYUSD/JPYEUR/USDEUR/USDGBP/USDGBP/USDGBP/USDGBP/USDUSD/JPYEUR/USDUSD/JPY
currencyPoint1090.772518.154510103030303090.77251018.1545
grossPL-22-245.14-23.61-305493-5181-5181-5181-5181-299.62-29-41.76
isBuyFalseTrueTrueFalseFalseTrueTrueTrueTrueTrueFalseTrue
open1.17653110.168110.1551.176331.231561.361.361.361.36110.1731.17697110.174
openTime060620181037580606201810354906062018102959060620181029570409201813304505012018145207050120181452070501201814520705012018145207060620181041470606201810412506062018104127
ratePrecision533555555353
roll0000286.36-821.85-821.85-821.85-821.85000
t222222222222
tradeId178066615178066381178064934178064922167729381144240844977299219760152497599708178067210178067111178067131
valueDate
visiblePL-2.2-2.7-1.3-3549.3-172.7-172.7-172.7-172.7-3.3-2.9-2.3
\n", "
" ], "text/plain": [ " 0 1 2 3 \\\n", "accountName 02815291 02815291 02815291 02815291 \n", "amountK 100 1000 200 100 \n", "close 1.17675 110.141 110.142 1.17663 \n", "closeTime 06062018103855 06062018103633 06062018103648 06062018103638 \n", "com 0 0 0 0 \n", "currency EUR/USD USD/JPY USD/JPY EUR/USD \n", "currencyPoint 10 90.7725 18.1545 10 \n", "grossPL -22 -245.14 -23.61 -30 \n", "isBuy False True True False \n", "open 1.17653 110.168 110.155 1.17633 \n", "openTime 06062018103758 06062018103549 06062018102959 06062018102957 \n", "ratePrecision 5 3 3 5 \n", "roll 0 0 0 0 \n", "t 2 2 2 2 \n", "tradeId 178066615 178066381 178064934 178064922 \n", "valueDate \n", "visiblePL -2.2 -2.7 -1.3 -3 \n", "\n", " 4 5 6 7 \\\n", "accountName 02815291 02815291 02815291 02815291 \n", "amountK 100 300 300 300 \n", "close 1.17663 1.34273 1.34273 1.34273 \n", "closeTime 06062018103638 06062018101450 06062018101450 06062018101450 \n", "com 0 0 0 0 \n", "currency EUR/USD GBP/USD GBP/USD GBP/USD \n", "currencyPoint 10 30 30 30 \n", "grossPL 5493 -5181 -5181 -5181 \n", "isBuy False True True True \n", "open 1.23156 1.36 1.36 1.36 \n", "openTime 04092018133045 05012018145207 05012018145207 05012018145207 \n", "ratePrecision 5 5 5 5 \n", "roll 286.36 -821.85 -821.85 -821.85 \n", "t 2 2 2 2 \n", "tradeId 167729381 144240844 97729921 97601524 \n", "valueDate \n", "visiblePL 549.3 -172.7 -172.7 -172.7 \n", "\n", " 8 9 10 11 \n", "accountName 02815291 02815291 02815291 02815291 \n", "amountK 300 1000 100 200 \n", "close 1.34273 110.14 1.17726 110.151 \n", "closeTime 06062018101450 06062018104240 06062018104247 06062018104258 \n", "com 0 0 0 0 \n", "currency GBP/USD USD/JPY EUR/USD USD/JPY \n", "currencyPoint 30 90.7725 10 18.1545 \n", "grossPL -5181 -299.62 -29 -41.76 \n", "isBuy True True False True \n", "open 1.36 110.173 1.17697 110.174 \n", "openTime 05012018145207 06062018104147 06062018104125 06062018104127 \n", "ratePrecision 5 3 5 3 \n", "roll -821.85 0 0 0 \n", "t 2 2 2 2 \n", "tradeId 97599708 178067210 178067111 178067131 \n", "valueDate \n", "visiblePL -172.7 -3.3 -2.9 -2.3 " ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.get_closed_positions().T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The `fxcmpy_open_position` Class" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For convenience, `fxcmpy` provides a class called `fxcmpy_open_positions`. All open positions are stored in the attribute `fxcmpy.open_pos`, a dictionary with the position's `tradeId` as key and the `fxcmpy_open_position` instance as value. Since there are no open positions left, the `dict` object is empty." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{}" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.open_pos" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Therefore, create an order ..." ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": true }, "outputs": [], "source": [ "order = con.create_market_sell_order('EUR/USD', 100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... to see the `fxcmpy_open_position` objects in the `dict` object." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{178067413: }" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.open_pos" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get a single `fxcmpy_open_position` object, one can use `con.get_open_position()`." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": true }, "outputs": [], "source": [ "tradeId = con.get_open_trade_ids()[0]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "pos = con.get_open_position(tradeId)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pos" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "accountId: 2815291\n", "accountName: 02815291\n", "amountK: 100\n", "close: 1.17731\n", "com: 0\n", "currency: EUR/USD\n", "currencyPoint: 10\n", "grossPL: -26\n", "isBuy: False\n", "isDisabled: False\n", "limit: 0\n", "open: 1.17705\n", "roll: 0\n", "stop: 0\n", "stopMove: 0\n", "time: 2018-06-06 10:43:12\n", "tradeId: 178067413\n", "usedMargin: 1300\n", "valueDate: \n", "visiblePL: -2.6\n", "\n" ] } ], "source": [ "print(pos)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `fxcmpy_open_position` object has `get` methods for all of its attributes, for example:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pos.get_amount()" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'EUR/USD'" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pos.get_currency()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Close the position** with the `pos.close()` method." ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": true }, "outputs": [], "source": [ "pos.close()" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "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": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "con.get_open_positions()" ] }, { "cell_type": "code", "execution_count": 33, "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.4" } }, "nbformat": 4, "nbformat_minor": 2 }