Monday, March 28, 2016

Containers and technologies

Hello, welcome back to my blog about automated trading. Last week I have covered the task scheduling. This week I will go through and various technologies (last week I told containers, but I really mean technologies (; ).

Application set up 

When I am developing my application I like to have nice dashboard and controls. Typically I do this through the web browser. Reason is, because it is easy and pretty independent. This gives me a lot of chices in terms of cloud providers, devices and browsers. I really like that.

For this I am using standard boing cmbination Tomcat & Spring MVC. Tomcat is a container which holds the environment. Spring gives life to the application. My ideology with spring is following - use it, don't over use it. Therefore I am using only these parts:
  • Annotation configuration - so far I found this the simples
  • Spring profiles - to be able change implementation of units based on value in property files
  • Xml bean configuration - for beans coming from library
  • Transactions - annotaion driven
  • Scheduling
  • Web security
  • Jsp view resolver
  • Integration with my batis - more below
As you could see in the repository most of my code is framework independent. This is, because I like to have a choice and I want to make sure that all my logic can be covered nicely by unit tests and can be performance tuned or parallerized on the unit level (without running the whole app). This is where I found frameworks are struggling.

Data access layer

During the application life cycle I need to persist the data. So I can stop application without worrying about the data loss. For this I am using PostgreSQL. Giving the amount of data I need to store and good functionality this is a right way.

Now more concrete I use MyBatis framework to access the database. I prefer this, because it gives me good control while keeping everything relatively simple. I like to create database schema manually. Now to connect MyBatis with application layer I use framework called em-dao. This framework gives me ability to write application code for Postgre database and in the same time having unit tests connected to in memory HyperSQL database.

Page rendering, css and javascript

To render web pages I am using Jsp. Pages itself are based on jQuery and Twitter Bootstrap. With control pages I am focusing on useability rather than on prettiness. Now to inject these components into my project I am using WebJars. WebJars is project which packages many of these components into a maven dependencies. Then in order to use some css or jsp I only need to include dependency in pom file and set up correct path in the jsp file. There is one catch tho. Some versions of Tomcat7 have a bug which makes this impossible. This has been fixed. So if you are doing this, then use the latest version.

For standardized processes and components like error handling, busy state, data loading and tables I am using framework called em-eac. This makes development much more simple.

That's it for today. Next week I publish code for one trading algorithm. Thank you for reading!

Monday, March 21, 2016

Task scheduling

Hello, welcome back to my blog about automated trading. Last week I have summarized the interfaces with third parties. This week I will cover the task scheduling.

For the latest source code, please download repository and update to branch 8-scheduling, changeset dc70abec76f0.

Background

Task scheduling is very important thing. During the day there are 3 possible types of tasks. The first type is invoked at the specified time (e.g. after market closes). The second type is invoked when some event happens (e.g. new message is delivered during trading session). And third type is invoked per user request.

I put the same core requirement for all of them. If there is an error, I would like to know about it.

In addition by the experience I discovered that it is useful to have the following:
  • Stop on the first error when chain of tasks is invoked
  • Log some of the tasks when they finish successfully (not all of them)
  • Run tasks in synchronous and asynchronous mode
  • Discover current status of any task
  • Run scheduled taks with respect to the different time zone 
These requirements satisfies what I want from my trading platform. So let's how it is coded.

Coding

The first interface to look at is Engine. This is just a container which allows to invoke tasks synchronously and asynchronously and allows to query the status of each of them. Then there is an implementation called SectionEngine. And this implementation contains property listener which allows to attach the different listeners. Listener implementations are ListTaskRunListener, ExceptionTaskRunListener, LogTaskRunListener and MessageExceptionTaskRunListener. Combination of these allows to satisfy the behavior (and actually gives me flexibility to reuse same code for other projects (; ).

Now I have put one class called TaskScheduler. This is implementation which automatically invokes the tasks. Please note that there are annotations there. These will be useful once the whole code runs within a container. TaskScheduler uses CronSequenceGenerator from spring to manage time zones. This works pretty well, see the accompanying test case.

Last there is an enumeration called TradingCloudTask. This simply enumerates the tasks I have in my cloud. In the real implementation I create instance of engine which runs TradingCloudTask. So have fun with it.

That's it for today. Next week I make an article about containers which allows to deploy the whole solution to the cloud. Thank you for reading!

Tuesday, March 15, 2016

API summary

Hello, welcome back to my blog about automated trading. Last week I have covered the fix protocol. This week I will make a short summary of APIs we have.

For the latest source code, please download repository and update to branch 7-fix-protocol, changeset b386cea63bc9. Now I will be talking about 4 APIs - historical, real time, fix protocol and finviz.

Historical

Historical API is the first connection type to the broker. This is typically used to be able study price and volume patterns and create future predictions. Broker typically allows to make pull requests for the data. Example implementations are MbTradingHistoricalProvider and YahooDailyHistoricalProvider.

Real time

Real time API is connection to the broker which allows to quickly transfer data during trading session. Typical usage is to wait for certain event and then make the reaction. Most often the price information is transferred. Additional information might be included. This API is typically implemented throgh the TCP protocol so messages are pushed to the client. Example implementation is MbTradingQuoteConnector.

Fix protocol

Fix protocol is typically the last endpoint of the connection to the broker. Although some brokers are using proprietary protocols. This end point allows for client to perform orders, receive reports and manage account. Example implementation is QuickFixJInitiatorFixEngine.

Finviz

Finviz is the exmaple of adding the third party data into the system. This page allows to screen for the stocks which matches the specified criteria. Example implementation is DefaultFinvizConnector.

That's it for today. Next week I will introduce the components for task scheduling. Thank you for reading!

Monday, March 7, 2016

Fix protocol

Hello, welcome back to my blog about automated trading. Two weeks ago I have finished the chapter about back testing and presented stand alone program for it. This week I am going to talk about fix protocol.

For the source code, please download repository and update to branch 7-fix-protocol, changeset b386cea63bc9.

Background

Fix protocol is standard for financial information exchange. Broker typically uses fix for sending an orders to buy or sell and for account monitoring (although some brokers have proprietary protocol). Comparing to the messages related to historical quotes or real time quotes fix protocol is pretty complex. It has well defined method for session management (including recovery of the lost messages) and messages are guaranteed to be processed in order. On top of this some institutions requires encryption. All this complexity brings me up to the point. It is better to use a library than trying to implement whole fix on my own (although I would be capable to do it, but this is not where I want to spend time). As I made research the freely available implementation is called QuickFix/J. Actually was not able to find any other good and free implementation. Do you know some?

Coding

My implementation of fix protocol is very simple. It is build up as a wrapper for QuickFix/J implementation. Purpose is to decouple third party library from my own standardized code. In this way I keep my independence and ability to use different implementation in the future. So now let's go through points to look at.

The first point is the application pom file. I have introduced new repository - Marketcetera. This allows me to include QuickFix as a dependency so there is no need to do any special steps in the build system.

And second point is the code itself. The main interface is calle FixEngine and implementation is  QuickFixJInitiatorFixEngine. From there you can track to the FixReceiver and FixMessage. To see everything in action, please look to the unit test. Have fun!

That's it for today. Next week I will summarize the APIs we have so far. Thank you for reading!