Machine Learning and Automated Trading

Open Source Tools and Techniques For Profitable Trading Strategies

Home Downloads Learning it Hardware Contact

Clone or download the pure R intraday trading framework from repository "INTRADAY-PartAB". Note that it has been designed and tested entirely on linux (ubuntu) and it probably won’t work out-of-the-box on windows.

Also make sure your system has the following:

  • R 2.15 or greater with the following packages: IBrokers, quantmod, blotter, mmap
  • IB gateway or tws running and accepting local API client connections on port 5001, running with a Paper-trading account. API ClientId’s 99 and 96 will be used. (See this page on how to connect to an IB demo TWS)

Configure PartA:
We will configure PartA to produce two feeds. EURUSD 15sec bars and EURGBP 15sec bars. Open “INTRADAY-PartA/datafeed-config.r” and modify IBcontractSpecs <- … so that it reads like this:

IBcontractSpecs <- list( # Currencies - 15sec bars
list(symbol='EUR', type='currency', exchange='', primary='',currency='GBP', outputbarsizeseconds=15L, keepcsvhistory=FALSE, keepoutputhistory=TRUE), #GBP base EUR
list(symbol='EUR', type='currency', exchange='', primary='',currency='USD', outputbarsizeseconds=15L, keepcsvhistory=FALSE, keepoutputhistory=TRUE) #USD base EUR
)

No other changes are needed in PartA

Configure PartB:
We will configure PartB to use the EURUSD 15sec bar feed for trading USD, using EUR as the base currency.

cd ../INTRADAY-PartB/STRATEGY01

Let’s change the amount of cash (EUR) to trade with. Edit “IBcreateExecPortfAcct-intraday.r” and set:

initEq<- 25000

and now the most important part. To make sure that partB actually uses the correct datafeed from partA, set:

ibDatafeedShareFile = '../../INTRADAY-PartA/datafeed-shared/EURUSD--shared.bin'

Now (still in “./INTRADAY-PartB/STRATEGY01″) we are ready to initialize the strategy with :

R --vanilla <IBcreateExecPortfAcct-intraday.r

This will create a bunch of *.rdata files that contain strategy specific information, orderbook history, trade history etc. At this point we are good to go! But wait, we may want to change the stop time to 16:00 or some other value. Edit “intraday.config.local.r” and set:

intraday.core.glTodayCloseoutTime <- as.POSIXct(paste(with(as.POSIXlt(Sys.Date()),paste(1900+year,1+mon,mday,sep='-')) ,'16:00:00'))

Note that the strategy will stop trading at the time specified here or 15 minutes before the market for your contract closes. Whatever comes first!

In case barsizes larger than 15 sec are used, it may be necessary to include the following settings in “intraday.config.local.r” to make sure that the datafeed quality monitoring works properly. (for a description what these settings do, see “intraday.config.r”)

intraday.core.ecounterl1<-list(defcount=2,definterval=120,firstidx=0,n=0,label='level1')
intraday.core.ecounterl2<-list(defcount=1,definterval=1800,firstidx=0,n=0,label='level2')

Running PartA and PartB:
To start the datafeed (PartA), make sure the IB gateway or tws are running, then do.

cd ./INTRADAY-PartA
R
source("01run-paper.r")

Once all the contracts specified in “datafeed-config.r” have been verified and previous data has been appended to the history files by this R process, open a second terminal and start the strategy:

cd ./INTRADAY-PartB/STRATEGY01
R
source("00launch-intraday.r")

The strategy process will print heartbeats and output trade execution info as well as info related to datafeed quality events, calendar events, etc.

Note On Starting And Stopping:
The PartA (datafeed) process has to be started before the PartB(strategy) process!

The PartB process will run until the configured daily closeout time is reached or some other internal stopping condition is met. It will then close all open positions and terminate. It is not a good idea to kill the process manually since it may leave you with open positions. It was designed to be as self-sufficient as possible and can handle a good deal of nasty situations, such as temporary loss of connectivity, big gaps in the datafeed, partial fills, etc and does not require any manual intervention via keyboard. In fact it ignores the keyboard except for ctrl+c.

The PartA process is different in that it has no pre-determined closeout time. However, currently it is configured to handle a maximum of 24 hours of uninterrupted operation. Sometime before that 24h limit it needs to be stopped manually, either with a ctrl+c from within R or with a kill command from a different shell. If needed it can be immediately started again, with the commands described in the previous section. This interruption is necessary to deal with history keeping and other things. Theoretically this 24h limit can be extended to a full week or even more.


Web Analytics