Sunday, January 24, 2016

Stock screening

Hello, welcome back to my blog about automated trading. Last week I have written article about historical quotes. This week I will write about the stock screening.

Example of the screener is on my trading cloud project page. For the source code example, please download repository and update to branch 3-stock-screener, changeset 8401ea797edc.

Background

I don't want go into theory of what stocks to choose and why. There are already plenty of other articles about that. I am going to the point when you actually know the criteria and you just want to find stocks which matches them. There are about 8000 publicly traded companies just in the US market. One way how to find them and filter them is to use screner. For example there is one offered by finviz. Simply put your filter and get what you want. My system fully automates this. It parses the result returned by finviz and allows it for the further processing. Columns included in the result are Ticker, Company, Sector and Industry. Very simple and copmletely free.

There is one note to be aware. As system merges data from multiple sources, they doesn't necessary have same data (e.g. some company appears only in one of them). Just need to live with that fact, nothing is perfect. Now let's proceed to the concrete code.

Coding

Code start with Instrument class. This is anything which can be traded. In this case this mean stock. There is little bit of mapping, but it is 1-1 (e.g. Ticker in finviz means Symbol in system). Then there is interface FinvizConnector. This has method downloadInstruments which accepts link to the finviz which has encoded filter (simply put filter on screener page and copy paste the link). Finally there is an implementation called DefaultFinvizConnector which does the job. All this is accompanyed with unit test DefaultFinvizConnectorTest and little application to play - DefaultFinvizConnectorTestInternetApp. Have fun!!!

That's it for today. Next time I will write about interface for receiving real time quotes duing trading session. Thank you for reading!

Monday, January 18, 2016

Historical quotes

Hello, welcome back to my blog about automated trading. Last week I have introduces the system for coding. This week I will go through historical quotes.

For start live example is on my trading cloud project page. For the source code example, please download repository and update to branch 2-historical-quotes, changeset 11a633201958. For live example, please go to my project online - Trading Cloud.

Background

Everyone can imagine that historical quotes have something to do with the data in the past. Typically they are collected as a samples of prices over a period of time (tick, second, minute, hour, day...). Per each interval there are 4 prices available called open, high, low and close and also total traded volume over the interval. This together with type and timestamp makes one historical quote. Often historical quotes are displayed as a candle sticks. In my system historical quotes has following parameters.
  • Type - (MINUTE or DAILY) - defines the length of the interval
  • Symbol - company identification
  • Timestamp - beginning of quote interval
  • Open - price at the beginning of interval
  • High - maximal price over interval
  • Low - minimal price over interval
  • Close - price at the end of the interval
  • Volume - volume traded over the whole interval
Now let's look on what you can get. If you are looking for the free source, then the maximum you can get is daily quotes. And there are plenty of providers for that. Intraday quotes are either payed or are accessible as a part of the broker account. For example MbTrading allows access to limited amount of intraday quotes with demo account. That was so far the only one I found.

Coding

For code examples, please updated to the branch 2-historical-quotes. There is defined domain object called HistoricalQuote. This is a data holder object used across multiple layers and therfore it is contrained to be immutble, collection safe and easily usable in unit tests. For that reason the builder pattern is used and methods hashCode and equals are defined to cover all properties.

The second class to look at is HistoricalProvider which is an interface. There are 3 implementations available called CollectionHistoricalProvider, YahoodDailyHistoricalProvider and MbTradingHistoricalProvider. Please look at them and look to their test cases (ended with *Test). Note that there are always compared full collections in assertEquals. So have fun!

There is also a simple application called MbTradingHistoricalProviderTestInternetApp. If you have an account with MbTrading you can enter your credentials and run this against the real broker. If you do this, then please be aware of one problem. There was a bug introduced in some versions of JDK 1.7 (not sure which about exact ones) so it might crash. Solution is to update to the JDK 1.8 or try different version of JDK 1.7.

That's it for today. Next week I will introduce interface for the stock screening. Thank you for reading!

Sunday, January 10, 2016

Coding introduction

Hello, welcome back to my blog about automated trading. Last week I have covered the actions which my system does through the day. Today I will introduce the technologe and system I am using for all my code. At the end of this article everyone will be able to download and compile first utility unit.

Technology stack

At first let's look what technology stack I am using. I prefer to be independent. Therefore my stack is very small and every item brings me the real big benefit. Please see the list below.
  • Java
  • Maven
  • Mercurial (TortoiseHg)
  • Bitbucket
  • NetBeans IDE
  • OpenShift
  • Tomcat
  • PostgreSQL
All these technologies are well documented so if you want know more, then just DuckDuckGo them. From now on I am expecting user knowledge of these.

One important item is Bitbucket, the online version control system. I have created open repository there to allow anyone clone and run accompanying source without need of any registration. Link is below.

https://bitbucket.org/radekhecl/trading-cloud-blog

Every article which has accompanying source code will have separate branch and I will will say the branch name and the changeset. The only required action is to update repository to that revision and build it by Maven (mvn clean package). There are no additional dependencies required (except Java and Maven). If you like IDE, then you can use whatever you want as far as it supports Maven projects (I like NetBeans).

Coding style

Now let's go through the coding style. I love to write small units which are consistent, stable, thread safe, reusable, testable and well documented. This is proven for me to be very efficient. Cost for this is that initial set up takes more time. So let's just live with that. I have 30 page document explaining everything in detail with examples. If you are interested, then let me know and I will share it. For this blog let me just point 6 most important rules in the simple form.
  • Separate objects into data holders and functional objects
  • Data holders used across multiple layers are unmodifiable
  • Data holders used across multiple layers have equals method comparing all fields
  • Functional objects are derived from interface
  • Functional object with not trivial logic has to be tested
  • Full objects has to be compared during tests
Later in the blog I will point to these rules by examples. Believe that should make it clear. If not, please ask question and I will make it clear. Now let's look on the repository.

First example

There is commit ready for you. Branch is called 1-coding-style, changeset is aa83170744cd. At this point project includes class DefaultHttpConnector together with test DefaultHttpConnectorTest. This is utility unit which performs http requests. It will be used later as a connectivity provider. So use Maven and compile it.

Rules for usage are very simple. Do whatever you want, but don't make me responsible if something goes wrong. There is no guarantee from me. Would be nice if anyone can write me a 'thank you' message.

That's it for today. Next week I am going to publish article about historical quotes. Thank you for reading!

Sunday, January 3, 2016

Daily cycle

Hello, welcome back to my blog about automated trading. Last week I have desrcibed infrastructure and stated that smallest considerable time interval will be 1 minute. Today I go through processes which run through day and night. To start lt's look at the image below.



Daily cycle

For simplicity I will be considering that market works as any regular shop. That is open during day and closed during night, weekends and holidays. There gives 2 types of processes system needs to handle - Overnight and Trading session. Let's go through them.

Overnight processes

There is nothing possible to buy or sell during overnight period, because market is closed. This time can span from hours (overnight) to days (weekends). Because trading system runs on the cloud which is 24/7 then night time is used for a scheduled processes. Please look to the image below to see examples of them.

Overnight processes
After market is closed, then system cleans up. Then daily results are calculated (account value, overnight exposure, statistics...). This is the very first part of the processing. Now the next step is something I call Training. This includes stock screening, machine learning and optimization. This part usually takes hours as each trading strategy can try millions of combinations. Result of this process is used as cache during trading session. And finally third and last phase is preparation for market open. This is when cache is loaded, connection to the real time service is established and all sources are subscribed. That's it about overnight processes for the moment. Each of these will be a separate chapter later.

Trading session processes

Trading session processes are different. They take place while market is open and they are invoked by events. Most common event is that new quote is received (although this is not the only one). So let's take it as an example.
Trading session processes

When new quote is send by broke, then it is received by trading system. Trading system maps each quote to the strategies which are interested in it. Then each strategy have a chance to react. This is the end of the event processing. Reacting to the different types of events works in the same way (e.g. news, order fill...). One note here is that there might be hundred of events per second so reaction has to be very quick. Therefore utilizing the cached data calculated during overnight time is absolutely necessary. That's about trading session processes.

That's it for today. Next week I will establish the standards for coding and let you know where you will be able to find open source code. Thank you for reading!