Hi! Here is an application which would load required price histories with predefined number of bars, and then continue to build price histories based on incoming offers. It would periodically write the results to the output files.
You have to create a file
Configuration.xml
to provide arguments (password is provided via command line).
This is a sample of configuration file.
- Code: Select all
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<Settings>
<UserID>GBD76837005</UserID>
<URL>www.fxcorporate.com/Hosts.jsp</URL>
<Connection>demo</Connection>
<SessionID></SessionID>
<Pin></Pin>
<TradingDayOffset>-7</TradingDayOffset>
<TradingWeekOffset>0</TradingWeekOffset>
<Delimiter>;</Delimiter>
<Timeout>60</Timeout>
<OutputDir>c:\fxcmdata</OutputDir>
<SeparateDateAndTime>N</SeparateDateAndTime>
<FormatDecimalPlaces>Y</FormatDecimalPlaces>
<NeedLastCompletedCandle>N</NeedLastCompletedCandle>
<TimeZone>EST</TimeZone>
<History>
<Instrument>USD/JPY</Instrument>
<TimeFrame>m5</TimeFrame>
<File>USD_JPY_min5</File>
<NumBars>1000</NumBars>
</History>
<History>
<Instrument>EUR/USD</Instrument>
<TimeFrame>m1</TimeFrame>
<File>EUR_USD_min1</File>
<NumBars>1000</NumBars>
</History>
<History>
<Instrument>USD/JPY</Instrument>
<TimeFrame>m1</TimeFrame>
<File>USD_JPY_min1</File>
<NumBars>1000</NumBars>
</History>
</Settings>
</configuration>
You always have to provide
UserID
,
URL
,
Connection
, and
Password
. You may need to provide
SessionID
and
Pin
.
You can provide
TradingDayOffset
(default=-7) and
TradingWeekOffset
(default=0).
You have to provide
Delimeter
for you .csv file (usually ',' or ';').
You have to provide
Timeout
(in seconds) for writing to .csv file.
You can provide
OutputDir
(deafult is current directory).
You can provide
SeparateDateAndTime
(if the value is 'y' or 'Y', separate fields for date and time would be created;
default is one field for DateTime).
You can provide
FormatDecimalPlaces
(if the value is 'y' or 'Y', all prices will have the same number of decimal places
(trailing zeroes will be added if needed), default is 'N' ('Do not format decimal places').
You can provide TimeZone (valid values are "UTC", "EST", and "Local"). The default value is "EST".
You can provide
NeedLastCompletedCandle
(if the value is 'y' or 'Y', the last completed candle will be shown on the screen, and offers would not be shown),
default is 'N' ('User needs forming candles as well').
For each price history you would like to get, you have to provide
Instrument
,
TimeFrame
,
File
(name of the output file),
NumBars
(number of bars loaded from the start).
If you would like just get the last candle use
<NumBars>2</NumBars>
.
This application uses
ForexConnect API.
These are main classes used by application:
The main class
Program
reads configuration XML file and validates input.
Program
creates a session, creates an instance of session status listener, and subscribes it to the session. Then it creates
ResponseQueue
(for storing a queue of
O2GResponses
) and an instance of response listener. The instance of response listener is subscribed to the session. Then application logs in.
EventListener
catches asynchronous events (“connected”, "reconnecting", “status changed”, “got a response”) and calls
HistoryMgr
for action.
Please see the class
HistoryMgr
and supporting classes:
Class
HistoryMgr
is a heart of the application. It keeps a list of
CandleHistories
.
CandleHistory
keeps a price history required by configuration XML file. It contains input parameters (instrument, timeframe, initial number of candles, name of the output file) as well as a list of
Candles
ready to be written to output file and new
Ticks
to be added to the candles. It also contains last request ID for getting
MarketDataSnapshot
. Each
CandleHistory
contains
BinaryWriter
for writing output file and
Timer
for firing writing event (initially timer is disabled). It also contains the start position of the last line of the output file.
Candle
contains information about one candle (time, OHLC bid and ask prices, and volume) while
Tick
contains information about one tick (time, bid and ask prices, and volume of current minute). Helper class
Timeframe
is used to find a candle incoming tick belongs to.
When
onConnected
event occurs, function
StartGettingData
of
HistoryMgr
is called.
HistoryMgr
gets all available instruments and subscriptions. Price histories would be gathered for valid and subscribed instruments only. Function
SendMarketDataSnapshotForHistory
fills and sends request for
MarketdataSnapshot
. This function can be called several times until the required number of initial candles would be received. So when using using
createMarketDataSnapshotRequestInstrument O2GRequest createMarketDataSnapshotRequestInstrument (string instrument, O2GTimeframe timeframe, int maxBars)
initially
maxBars
equals to the required number of bars, and is reduced for the amount of candles in the history for subsequent calls.
When using
fillMarketDataSnapshotRequestTimevoid fillMarketDataSnapshotRequestTime (O2GRequest request, DateTime timeFrom, DateTime timeTo)
timeFrom
equals 01/01/1900 while
timeTo
initially equals to
ZERODATE (which means “up to now”), and during subsequent calls
timeTo
equals the date of the earliest candle.
The request ID for
MarketDataSnapshot
would be kept with a history.
Function
ProcessMarketDataSnapshot
is called when
onResponse
event occurs, and the type of response is
MarketDataSnapshot.
It adds candles to the history with corresponding request ID and makes another call to
SendMarketDataSnapshotForHistory
if there not enough candles.
Function
ProcessOffer
is called when
onResponse
event occurs and the type of response is either
GetOffers or
TablesUpdates.
If candles are ready, the new ticks would be added to the candles’ collection, otherwise they would be added to the ticks’ collection.
When candles are ready (application got required number of initial candles), timer is launched. On event “Timer elapsed” output file is updated. The last candle written to output file is exchanged for the candle from the
CandleHistory
(which could be updated with new ticks), and new candles are added.
Should "reconnect" event happen
CandleHistories
are reset (ticks' and candles' collections are cleared, application sends new requests for price histories, and output files are rewritten.
You can download project exporting live chart data to .csv file here:
The attachment ProgramMain.png is no longer available