6. Market Orders

This section covers market orders, the most simple yet equally important order type.

In [1]:
import fxcmpy
import pandas as pd
import datetime as dt
con = fxcmpy.fxcmpy(config_file='fxcm.cfg')

6.1. Placing Orders

Before one gets started, a check of the open positions might be helpful.

In [2]:
con.get_open_positions().T
Out[2]:

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.

In [3]:
order = con.create_market_sell_order('EUR/USD', 100)
In [4]:
order = con.create_market_buy_order('USD/JPY', 200)

The new open positions table might then look as follows. Two new positions have been created.

In [5]:
con.get_open_positions().T
Out[5]:
0 1
accountId 2815291 2815291
accountName 02815291 02815291
amountK 100 200
close 1.21278 111.597
com 0 0
currency EUR/USD USD/JPY
currencyPoint 10 17.9199
grossPL -1 -19.7138
isBuy False True
isDisabled False False
limit 0 0
open 1.21277 111.608
ratePrecision 5 3
roll 0 0
stop 0 0
stopMove 0 0
t 1 1
time 01122018135457 01122018135458
tradeId 96429312 96429318
usedMargin 1400 2000
valueDate
visiblePL -0.1 -1.1

6.2. General Market Orders

fxcm.py 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.

In [6]:
order = con.open_trade(symbol='USD/JPY', is_buy=True,
                       rate=105, is_in_pips=False,
                       amount='1000', time_in_force='GTC',
                       order_type='AtMarket', limit=120)
In [7]:
con.get_open_positions().T
Out[7]:
0 1 2
accountId 2815291 2815291 2815291
accountName 02815291 02815291 02815291
amountK 100 200 1000
close 1.21251 111.62 111.62
com 0 0 0
currency EUR/USD USD/JPY USD/JPY
currencyPoint 10 17.9163 89.5812
grossPL 26 21.5015 161.261
isBuy False True True
isDisabled False False False
limit 0 0 120
open 1.21277 111.608 111.602
ratePrecision 5 3 3
roll 0 0 0
stop 0 0 0
stopMove 0 0 0
t 1 1 1
time 01122018135457 01122018135458 01122018135507
tradeId 96429312 96429318 96429362
usedMargin 1400 2000 10000
valueDate
visiblePL 2.6 1.2 1.8

6.3. Trade Ids

Every position has the attribute tradeId, here it is the column named tradeId of the DataFrame object returned by con.get_open_positions().

In [8]:
con.get_open_positions()['tradeId']
Out[8]:
0    96429312
1    96429318
2    96429362
Name: tradeId, dtype: object

fxcm.py provides methods to get the trade tradeIds directly.

In [9]:
con.get_open_trade_ids()
Out[9]:
[96429312, 96429318, 96429362]
In [10]:
con.get_closed_trade_ids()
Out[10]:
[]
In [11]:
con.get_all_trade_ids()
Out[11]:
[96429312, 96429318, 96429362]

6.4. Editing Market Orders

To modify a trade, you need the tradeId value.

In [12]:
tradeId = con.get_open_trade_ids()[-1]

To change the trades stop or limit rate, one can use the change_trade_stop_limit() method.

In [13]:
con.change_trade_stop_limit(tradeId, is_in_pips=False,
                            is_stop=False, rate=115)

As seen below, the limit rate of the last trade has changed to 115.

In [14]:
con.get_open_positions().T
Out[14]:
0 1 2
accountId 2815291 2815291 2815291
accountName 02815291 02815291 02815291
amountK 100 200 1000
close 1.21239 111.617 111.617
com 0 0 0
currency EUR/USD USD/JPY USD/JPY
currencyPoint 10 17.9167 89.5837
grossPL 38 16.1266 134.388
isBuy False True True
isDisabled False False False
limit 0 0 115
open 1.21277 111.608 111.602
ratePrecision 5 3 3
roll 0 0 0
stop 0 0 0
stopMove 0 0 0
t 1 1 1
time 01122018135457 01122018135458 01122018135507
tradeId 96429312 96429318 96429362
usedMargin 1400 2000 10000
valueDate
visiblePL 3.8 0.9 1.5

6.5. Closing Positions

Positions can be closed individually via the tradeId

In [15]:
con.close_trade(trade_id=tradeId, amount=1000)
In [16]:
con.get_open_positions().T
Out[16]:
0 1
accountId 2815291 2815291
accountName 02815291 02815291
amountK 100 200
close 1.21252 111.621
com 0 0
currency EUR/USD USD/JPY
currencyPoint 10 17.9162
grossPL 25 23.2931
isBuy False True
isDisabled False False
limit 0 0
open 1.21277 111.608
ratePrecision 5 3
roll 0 0
stop 0 0
stopMove 0 0
t 1 1
time 01122018135457 01122018135458
tradeId 96429312 96429318
usedMargin 1400 2000
valueDate
visiblePL 2.5 1.3

… or for all positions in an instrument …

In [17]:
con.close_all_for_symbol('EUR/USD')
In [18]:
con.get_open_positions().T
Out[18]:
0
accountId 2815291
accountName 02815291
amountK 200
close 111.633
com 0
currency USD/JPY
currencyPoint 17.9142
grossPL 44.7896
isBuy True
isDisabled False
limit 0
open 111.608
ratePrecision 3
roll 0
stop 0
stopMove 0
t 1
time 01122018135458
tradeId 96429318
usedMargin 2000
valueDate
visiblePL 2.5

… or simply for all positions over all instruments.

In [19]:
con.close_all()

There are no open positions left.

In [20]:
con.get_open_positions()
Out[20]:

The closed positions are found in the table returned by con.get_closed_postions().

In [21]:
con.get_closed_positions().T
Out[21]:
0 1 2 3 4 5 6 7 8
accountName 02815291 02815291 02815291 02815291 02815291 02815291 02815291 02815291 02815291
amountK 100 100 100 1000 200 100 1000 100 200
close 1.21277 1.21377 1.21373 111.136 111.136 1.21351 111.595 1.21306 111.622
closeTime 01122018135435 01122018132307 01122018131730 01122018125855 01122018125856 01122018125856 01122018135528 01122018135544 01122018135549
com 0 0 0 0 0 0 0 0 0
currency EUR/USD EUR/USD EUR/USD USD/JPY USD/JPY EUR/USD USD/JPY EUR/USD USD/JPY
currencyPoint 10 10 10 89.5676 17.9135 10 89.5676 10 17.9135
grossPL 87 -29 -21 -179.96 -35.99 -25 -62.73 -29 25.08
isBuy False False False True True False True False True
open 1.21364 1.21348 1.21352 111.156 111.156 1.21326 111.602 1.21277 111.608
openTime 01122018132410 01122018131348 01122018130602 01122018125852 01122018125851 01122018125850 01122018135507 01122018135457 01122018135458
ratePrecision 5 5 5 3 3 5 3 5 3
roll 0 0 0 0 0 0 0 0 0
t 2 2 2 2 2 2 2 2 2
tradeId 96421953 96421202 96420641 96419876 96419864 96419858 96429362 96429312 96429318
valueDate
visiblePL 8.7 -2.9 -2.1 -2 -2 -2.5 -0.7 -2.9 1.4

6.6. The fxcm_open_position Class

For convenience, fxcm provides a class called fxcm_open_positions. All open positions are stored in the attribute fxcm.open_pos, a dictionary with the position’s tradeId as key and the fxcm_open_position instance as value. Since there are no open positions left, the dict object is empty.

In [22]:
con.open_pos
Out[22]:
{}

Therefore, create an order …

In [23]:
order = con.create_market_sell_order('EUR/USD', 100)

… to see the fxcm_open_position objects in the dict object.

In [24]:
con.open_pos
Out[24]:
{96429674: <fxcm.fxcm_open_position at 0x7fa21c072780>}

To get a single fxcm_open_position object, one can use con.get_open_position().

In [25]:
tradeId = con.get_open_trade_ids()[0]
In [26]:
pos = con.get_open_position(tradeId)
In [27]:
pos
Out[27]:
<fxcm.fxcm_open_position at 0x7fa21c072780>
In [28]:
print(pos)
accountId:        2815291
accountName:      02815291
amountK:          100
close:            1.21223
com:              0
currency:         EUR/USD
currencyPoint:    10
grossPL:          1
isBuy:            False
isDisabled:       False
limit:            0
open:             1.21224
roll:             0
stop:             0
stopMove:         0
time:             2018-01-12 13:56:12
tradeId:          96429674
usedMargin:       1400
valueDate:
visiblePL:        0.1

The fxcm_open_position object has get methods for all of its attributes, for example:

In [29]:
pos.get_amount()
Out[29]:
100
In [30]:
pos.get_currency()
Out[30]:
'EUR/USD'

Close the position with the pos.close() method.

In [31]:
pos.close()
In [32]:
con.get_open_positions()
Out[32]: