Monday, May 30, 2016

Intraday trading algorithm

Hello, welcome back to my blog about automated trading. Last week I made research about he Microsoft stock, made visualizations of technical indicators and discovered that for daily trading (means no overnight holds) the day based technical indicators don't help. Then I discovered that for the growing days (close > open) the day low will happen most likely during morning. With this information I am going to build the intraday algorithm.

For the source code example, please download repository and update to branch 15-algo_implementation-3, changeset 36d12fbc5e16.

Algo code

There is prepared class Algo3 and test applciation Algo3TestApp in the repository. This algorithm needs the intraday quotes which I am not allowed to publish. Therefore to run the app you will need to set up the your own historical provider or provide your own credentials for MbTrading account.

Algorithm works in the following way. In the training algo takes all daily historical data for the past year. From quote list only the days for which close > open (growing days) are considered. For every growing day algo uses open - low and high - open to calculate averages and standard deviations. This is the preparation for trading session.
Now during the trading session algo searches for the pattern which shows minimum within the morning and is within the expected range (average of open - low +- multiplier of standard deviation calculated in the training) for the growing day. Note that algo searches for some bounce from the minimum. If there is, then position is opened. Stop loss is set up to the identified minimum. Target price is set up as open plus average of high - open calculated during training. And finally every position has a time stop before end of the day as well. The time is chosen to be in as the avagere time for highs.

Results

With this algo I made 2 tests and then for comparism one test of algo from this post. Follows the set up for all tests and then separate inputs and results for each test.
  • Transaction cost is 10$ (means 20$ if you one time buy and sell within the day)
  • Start date - 2015/11/30
  • End date - 2016/05/30
1) With algo as presented

Setup:
  • Initical capital - 30.000$
  • Risk - 200$ (this is max risk per trade)
  • Max quantity - 400 (buy 400 stocks at max)
Results:
  • End capital: 30.527$
  • Percent change: 1.75
2) Increased capital and size to make comission less relevant

Setup:
  • Initical capital - 50.000$
  • Risk - 400$
  • Max quantity - 800
Results:
  • End capital: 52.276$
  • Percent change: 4.55
3) To compare result with algorithm from this post

Setup:
  • Initial capital - 30.000$
  • Other parameters are different, because it is fundamentally different algorithm I just let them as they are
 Results:
  • End capital: 39.033$
  • Percent change: 30.11

Conclusion

Definitely both strategies have potential to improve. I already have possible ideas and will try them somewhen later. For the moment the long term strategy seems to be the better choice although there is the overnight risk and it is not that sexy. The return for long term strategy is multiple times higher. In addition it is scalable to the size which I can (as a retail guy) afford to pay in cash and 100% loose without going broke (for sure I will be pissed of). This gives me easy sleep and therefore I will go with it.


And what about you? Can you think about any profitable trading strategy?

That's it for today. Next week I will discuss the contract between me, broker and another financial service providers. Thank you for reading!

Sunday, May 22, 2016

Visualization of technical indicators

Hello, welcome back to my blog about automated trading. Last time I was connecting building the neural network based up on the technical indicators. This produced a dissapointing result. Therefore I have decided to take the step back and look in more detail to some of the technical indicators.

For the source code example, please download repository and update to branch 14-visualization, changeset 694ec142c4d2. There is a test application ObservationGraphDataGeneratorApp which downloads quotes and generates csv data files (for intraday part you will need to change historical provider). Now in /data directory there are 2 files - MSFT-daily.csv and MSFT-intraday.csv with observations extracted from Microsoft historical data.

Together with this downloader I have developerd also application for visualization which can be found on enterprisemath.com.

What I was searching

I was serching for daily based technical indicators which would increase my chance for success during the next trading day. I want to open and close position within that day to avoid the overnight risk. Reality is that none of the daily based technical incators seems to be usefull for this type of trading. So let's look at the reasons.

First I have analyzed past 3 years of Microsoft stocks. During that period stock price went from about 35 to 50. First I have discovered that there are nearly same number of days when price was rising or falling. This can be shown in the next picture.
What does this means? This means if I open position early in the morning and close it before the market closes, then my chances for success are 50/50 (without consideration the size of P&L). Now let's look at it even more. I put this into the relation with popular technical indicators. All the situations looked pretty much as the following graph.
To increase success rate I would need to find areas where blue is higher than the black one. I haven't find any single case. This essentially means that technical indicators (daily based) will not help me at all with this type of trading no matter how good prediction framework I use. So let's forgot about them and see look at this from the different point.

Possible helpers for trading

Daily technical indicators seems not be very helpful. But with intraday data i found 2 cases which migh be useful. First one is the minute which contains the daily low.
Looking to the blue line, it suggest that for successful trades the most of time minumum is happening before the lunch time. And definitelly it is better to close trade if new low is reached after 2 pm. Then the second is the difference between open and low price for the day. Look to the picture.
Again the blue line suggest to set up stop loss somewhere around open - 0.56 (I haven't calculated it exactly). If this price goes below this low it is better to accept loss and close the trade. In the similar way I can make it with daily highs and discover the good closing time and target prices.

That's it for today. For the next week I am going to build trading algorithm using this knowledge. Thank you for reading!!!

Sunday, May 15, 2016

Technical indicators with neural nets

Hello, welcome back to my blog about automated trading. Two weeks ago I have prepared research about the technical indicators. This time I will connect them with the feed forward neural network.

For the source code example, please download repository and update to branch 13-nn_predictions, changeset de4b7457a020.

Background

Theory says that Neural Network is very good for discovering dependencies. So I took it in the naive way. For notation, please refer to the previous post. I took the following indicators:
  • Today's %K(5)
  • Percent change between today.close and today SMA(5)
  • Percent change between today.close and today SMA(10)
  • Percent change between today.close and today.bollinger lower band with (14,2)
  • Percent change between today.close and tmorrow.open
  • Tomorrow's day of the week 
Now during the research I discovered few things (will be talking about them below). One of them was that it doesn't make sense to make complicated questions at once. So category I choose for this time is whether tomorrow.close > tomorrow.open or not.

Coding and results

There is a bit of reorganization within a code. I put a lot of models into the librares. And I have grouped the test application together into com.enterprisemath.tradingcloudblog.testapps package. The one for today is called PricePredictionTestApp2. The whole workflow is same as in this article. The only what changes is group of stocks, question and prediction method.

This method gave me following results.
  • Number of not sure: 1296
  • Number of true when true: 15
  • Number of true when false: 34
  • Number of false when true: 8
  • Number of false when false: 11
  • Total success rate: 0.019
Honestly, this doesn't look good enough to bet money on it. However walking through the whole process and studying for it actually gave me good insights. Let's summarize the learning points.
  • Use simple questions for predictions. It's better to combine multiple models rather than trying to build the super model.
  • Data preparation
  • Confusion matrix
  • Probabilistic and recurrent neural networks
All these are good startups for the whole research and give enough space to improve. So let's do that.

That's it for today. Next week I will talk about the data preparation. Thank you for reading!!!

Monday, May 2, 2016

Technical indicators

Hello, welcome back to my blog about automated trading. Last week I have presented trading algorithm using neural networks. This algorithm ended up with 3000USD loss after 6 month benchmark. Therefore for this week I have prepared little research of technical indicators with a goal to find better observations for the neural net.

Original prediction question

For the neural net I have defined the following terms and criteria. Let's say today is the last trading day where open, high, low and close prices are know. Let's say tomorrow is the next trading day (it might be really tomorrow or for example next Monday if today is a Friday). Let's set the convention that today.open, tomorrow.high... are shortcusts for today open price, tomorrow high price and so on.
Now for the prediction. Task is to buy stocks tomorrow after market opens. So tomorrow.open is known at the time of buying. Then the prediction model should find stocks which will satisfy the following criteria.
  1. tomorrow.low > today.low (for stop loss)
  2. tomorrow.high - tomorrow.open > 2 * (tomorrow.open - today.low) (for risk/reward ratio and target price)
Now the task is to find technical indicators which will allow me to create neural network which gives the more accurate answers.

Method & results

For the whole research I have use stocks according this filter.
There are 318 matched stocks as a time of writing this article. For each of them I have used data passed 3 years. After every trading day indicator told me whether the next day will match the conditions or not. I am interested only in the cases when indicator says it will match as this will be the buy signal. From this I measured the success rate in percent. I skipped the observations when tomorrow.open is lower than today.low as in shuch case algorithm would not buy anyway.

Method gave me the following results for various indicators.
  1. Indicator according today'sprice.
    • Signal is trigerred when today.close > today.open
    • Number of signals - 111316
    • Success rate - 7%
  2. Indicator of close price over simple moving average (SMA)
    • Singnal is trigerred when today.close > today's SMA(5) 
    • Number of signals - 113846
    • Success rate - 8%
  3. Indicator of close price over SMA
    • Singnal is trigerred when today.close > today's SMA(25) 
    • Number of signals - 109487
    • Success rate - 8%
  4. Indicator for trend based on SMA
    • Signal is trigerred when todays' today SMA(10) < today SMA(5)
    • Number of signals - 110890
    • Success rate - 9%
  5. Indicator for trend based on SMA
    • Signal is trigerred when todays' today SMA(20) < today SMA(10) < today SMA(5)
    • Number of signals - 75086
    • Success rate - 9%
  6. Volume based indicator
    •  Signal is trigerred when today.volume > today.SMA over volume (5)
    •  Number of signals - 91169
    • Success rate - 9%
  7. Volume based indicator
    •  Signal is trigerred when today.volume > today.SMA over volume (25)
    •  Number of signals - 76774
    • Success rate - 8%
  8. Lower bollinger band indicator
    • Signal is trigered when today.close < today.bollinger lower band with (14,2)
    • Number of signals - 10332
    • Success rate - 12%
  9. Stochastic K incidator
    • Signal is trigerred when today's %K(5) is under 20
    • Number of signals - 40223
    • Success rate - 13%
  10. Stochastic K incidator
    • Signal is trigerred when today's %K(14) is under 20
    • Number of signals - 41796
    • Success rate - 12%
  11. Stochastic D incidator
    • Signal is trigerred when today's %D(14,3) is under 20
    • Number of signals - 39123
    • Success rate - 10%
That were the individual indicators. The best would work only in 13% of cases. That's not enough. Therefore I tried various combinations of them. Signal is trigerred in 'AND' manner. Meaning all of theme must say yes. The most interesting results are following.
  • 4. and 6. and 8. and 9.
    • Number of signals - 487
    • Success rate - 16%
  • 4. and 6. and 8.
    •  Number of signals - 766
    •  Success rate - 15%
  • 4. and 8. and 9.
    • Number of signals - 2356
    • Success rate 14%
That's it. For the next week I will make little bit more tests, take the most indicators and apply them in the previously created neural network interface. This allows me to add additional data into consideration (e.g. day of the week). With that I am really looking forward for the result.

That's it for today. Next week I will show how indicators works with the neural networks. Thank you for reading!!!