2. Server Connection¶
This section is about the server and API connection itself. It needs to be established before any other tasks can be accomplished.
2.1. Basic Connection¶
The following assumes that you have created a configuration file with your credentials as described in the Quick Start section.
In [1]:
import fxcmpy
In [2]:
con = fxcmpy.fxcmpy(config_file='fxcm.cfg')
2.2. Status Information¶
To check if the connection is established, you can use the method
con.is_connected()
.
In [3]:
con.is_connected()
Out[3]:
True
After the connection is established, fxcmpy
gathers information
about your FXCM account.
For example, the ids
of your accounts within your FXCM account are
retrieved.
In [4]:
con.get_account_ids()
Out[4]:
[2815291]
The first account id
is used as default:
In [5]:
con.get_default_account()
Out[5]:
2815291
To set another account as default, use con.set_default_account()
.
In [6]:
con.set_default_account(2815291)
The ids
of your existing orders from previous sessions are also
available …
In [7]:
con.get_order_ids() # regular orders
Out[7]:
[422719537,
423235903,
423235901,
422724771,
404812349,
404812351,
422696238,
422722769,
424130850,
422912217,
422912219,
422725725,
423269441,
423269701]
In [8]:
con.get_oco_order_ids() # OCO orders
Out[8]:
[423235902, 422912218]
… as well as the available instruments.
In [9]:
instruments = con.get_instruments()
print(instruments)
['EUR/USD', 'USD/JPY', 'GBP/USD', 'USD/CHF', 'EUR/CHF', 'AUD/USD', 'USD/CAD', 'NZD/USD', 'EUR/GBP', 'EUR/JPY', 'GBP/JPY', 'CHF/JPY', 'GBP/CHF', 'EUR/AUD', 'EUR/CAD', 'AUD/CAD', 'AUD/JPY', 'CAD/JPY', 'NZD/JPY', 'GBP/CAD', 'GBP/NZD', 'GBP/AUD', 'AUD/NZD', 'USD/SEK', 'EUR/SEK', 'EUR/NOK', 'USD/NOK', 'USD/MXN', 'AUD/CHF', 'EUR/NZD', 'USD/ZAR', 'USD/HKD', 'ZAR/JPY', 'USD/TRY', 'EUR/TRY', 'NZD/CHF', 'CAD/CHF', 'NZD/CAD', 'TRY/JPY', 'USD/CNH', 'AUS200', 'ESP35', 'FRA40', 'GER30', 'HKG33', 'JPN225', 'NAS100', 'SPX500', 'UK100', 'US30', 'Copper', 'CHN50', 'EUSTX50', 'USDOLLAR', 'USOil', 'UKOil', 'SOYF', 'NGAS', 'Bund', 'XAU/USD', 'XAG/USD']
To free resources, it is recommended to close the connection at the end of a session.
In [10]:
con.close()
2.3. Using a proxy¶
The constructor of the fxcmpy class accepts 3 parameter to establish a connection via proxy, namely:
proxy_url
, the URL or IP address of the proxy,proxy_port
, the port number of the proxy andproxy_type
, one ofhttp
,socks4
orsocks5
.
In [11]:
con = fxcmpy.fxcmpy(config_file='fxcm.cfg', proxy_url='192.241.154.247',
proxy_port=8080, proxy_type='http')
In [12]:
con.is_connected()
Out[12]:
True
In [13]:
con.close()