Sunday, November 9, 2014

Trade duration analysis with QuantSense

One of the fundamental trading problems are the following two.

1. When to open a position?
2. How big the position should be?
3. When to close it?

According to statistics for the most people (including me) "never" is the answer to the first question. But the most people (including me) ignore the statistics. And the most crazy of us use a statistics to beat the statistics we ignore :).

Even if you have an exact strategy which gives you precise answers on all three questions the strategy itself has to be adjusted to follow the market changing conditions. To help you a bit with the third question QuantSense has a special chart. It shows a relation between the trade duration (x-axis) and its profit (y-axis). It is pretty straightforward to see if your timing of closing positions is right or wrong from this chart. Let us see an example:

Here in this chart you see that as an 'age' of my positions goes higher they get more profitable. On the other hand those positions closed after less than 8-10 hours are roughly speaking equally likely loosing and profitable. See the next picture where I marked the interesting time limit by a yellow line.

For the sample of the trades displayed in this chart we can say that an interesting time limit is around 8-10 hours. All positions which were able to survive in the market longer than 10 hours finished with a profit. This is especially good to know if you define a take profit and a stop loss for your positions even if they are not the same for all your positions. It is better to know what will your profit be in the time of opening your position. If you do so and your chart looks as mine what should you do to optimize your future profit? 

Well, the first idea is to simply buy or sell more if your current position in the market passes the time limit. According to this analysis it is highly probable (if you stick with your current way of trading of course), that this position was a good guess and that it will finish with a profit. So open another one in the same direction, depending on how far from the take profit level is the first one. Or the other, less risky approach, would be to start with a smaller position than usually and open the next one in the same direction as soon as the first one passes the time limit. You can see that most of my positions which I have closed before the time limit are distributed around the zero line with sum of profits somewhere around the zero (just a guess). It simply means that if I will follow my current way of trading and if I will close every position before the time limit I will probably finish with a profit around the zero. Therefore it is better to start with a smaller position and as it gets older and passes the time limit just open the next one in the same direction. Remember, that if for example 80% of your positions passing the time limit will be the winning ones you still have 20% of loosing positions. So if you will increase the volume of your positions which pass the time limit you have to remember that there is still a 20% chance that you will loose. So you need to do some additional computations or estimations on how much you can afford to increase the volume in order to be more profitable that you are now. You should also keep the original stop loss for the new positions so your risk will get higher. If this type of analysis shows you that it will be too risky to buy the next position you can think a bit differently. If you are in a position which is already behind the time limit and it looks good (you are in a trend, or over the support for example) just wait for some technical signal for buying the next one otherwise just keep one position and close it as usually.

Take both approaches just as a rough ideas. You have to figure out the details by yourself as they mostly depend on your way of trading.

Now let us see another example. I guess this would be the common one :).

Here you can see that as the 'age' of the positions goes higher more and more of them finish with a loss. This is a typical example of being stuck in the loosing position and hoping that it will possibly turn to profit some day. Well, here in my case, it obviously did not work. So how can I do better?

Similarly, as in previous example, I should try to find some time limit beyond which the most of the positions are the loosing ones. In the next picture I marked one with a yellow line.

Then I know that if I am in a position, no matter if winning or loosing, after this time limit there is a high chance that it will finish with a loss. So the conclusion is to close such a position or at least decrease its volume. Decreasing the volume of the position is probably a good way to go in case that you can not find some precise time limit. If you kinda 'have a feeling' from the chart that the older positions are more likely to loose you can gradually decrease the volume of the position in a few steps in order to minimize the loss as the risk of loosing goes higher with the age of the position.

Let me remind you that QuantSense, since version 1.2.0, supports one click import of results from Meta Trader 4's strategy tester. So if you are working on a strategy just let it run in strategy tester, save the results as a report, import it into QuantSense and check this chart. If you see similar distribution of trades as in my second (loosing) example you can improve your strategy by implementing a time stop loss. It simply means that your algorithm will close the position after the time limit which you have identified in this chart. This kind of optimization is not that easy to do in strategy tester. You have to code your own time analysis in your algorithm. 

Now you do not have to. I did it in QuantSense. Give it a try. It is free and without any ads. To do this type of analysis you do not need to buy any special upgrades. Thanks for reading and happy gambling!

Tuesday, September 30, 2014

Maximizing your profit with QuantSense LOG

The aim of this post is to show you how to maximize your trading profit in QuantSense LOG. Before we start make sure you have some reasonable number of trades imported in QuantSense LOG. You can import them automatically if you have your trade history from Meta Trader 4 in .htm file (Statement.htm, DetailedStatement.htm or something similar). The other way is to record your trade manually in QuantSense LOG simply by typing in all parameters of the trade. What is the "reasonable" number of trades? Well, four is not enough. We should speak about hundreds, but for start anything more than 10 should show you something interesting. 

The next assumption for this kind of output from the QuantSense LOG is that all your trades have minimal and maximal values defined. This is a bit complicated since you have to enter them manually even for the imported trades. This is because your trade history do not contain this kind of data. Since I wrote QuantSense primarily for myself and since I am a lazy man the PRO version is able to approximate these values from market quotes. But PRO version is not done yet so we have to do this work manually (ok, not me I got unfinished PRO version where the approximation already works). If you do not have maximal and minimal values of the trades in your records the histogram chart will not display anything and you get some message about the reason why this is not happening.

If we have all necessary data in the database lets display the histogram chart. Probably the best way is to do this at one particular instrument. So switch your trade navigator (the left panel in the Dashboard screen) to the "Instruments" view. Pick an instrument (EURUSD for example) and if you want you can filter the resulting set of trades by date interval (to make the computation on the trades from the last year only for example). Now select the "Histogram" chart from the scrolling chart menu in the top part of the screen. You should see something like this:

Full size image here

Let me describe the displayed data a little bit. The x-axis in the bottom is divided into 20 so called buckets. For simplicity just assume that the number under the green column is a representative of all profits which belongs to this bucket. The height of the column (the y-axis) shows the number of trades from this bucket. These are the trades whose maximal(not realized) profit was approximately the value under the column. From the picture here we can see for example that I have made 10 trades whose maximal position value was around 242 pips (6th column from the left). Note that all of these trades could end with a loss. What we are trying to find here is how good are you in opening positions. It means how high can your position typically go while it is open. If you then wait too long and your position hits stop loss that is the other story and we want this story to finish in green numbers.

We should know now what is the histogram displaying and we can start optimizing our future profits. Lets say that I will change my trading from tomorrow such that I will close the position after it hits the profit of 800 pips. I will make no other changes in my way of trading only this one thing. Just close it after it reaches +800 pips. Will be my profit positive? Of course, nobody knows the future but we have a sample of our behavior and all our reactions to the market actions in our sample of trades we made in the past. So we can guess with some decent probability what will happen after some number of trades. Because we wait until the profit hits 800 pips all the trades whose maximal profit is less will turn against us and will finish with loss by hitting your stop loss. Take a look at the histogram. Those loosing trades will be all the trades to the left from the column with value 854 (15th column from the left). On the other hand some of the trades from column 854 and all the trades to the right of the column 854 will satisfy our condition (their maximal profit was over 800 so we would close them when they hit precisely 800 pips). All we have to do is to count the loosing and winning trades from the histogram (summing up the heights of particular columns). In my case there is something around 136 loosing trades. The number of winning trades is 6. The resulting mathematics now depends on what was your typical stop loss in the displayed sample of trades. Let say it was 50 pips. That means if we trade such that we close the trade when it hits exactly 800 pips of profit we will have 6 winning trades with  4800 pips of profit and 136 loosing trades with loss of 6800 pips. So the clean loss would be -2000 pips. 

Now you should see the point. 800 pips was not a good idea. So try to decrease our take profit value to 50 pips. Then all trades to the right of the column 38 (3rd from the left) will be the winning ones and all the trades to left from the column 38 including the trades from it will be the loosing ones. Again, we sum the counts up and we get 77 winning trades realizing profit 50 pips each and 55 loosing trades realizing loss of 50 pips each. This seems much better with a profit of 1100 pips.

So this is what the histogram in QuantSense LOG is for. Some important remarks at the end.

  1. You should not change your trading strategy in any other point except for the take profit parameter. Remember, all your trades used for the optimization were made in some way - it does not have to be a precise strategy. You can open your trades using your intuition as well. So for example (my case) if you trying to open positions for a longer term awaiting profits in thousands of pips just do the same. Open the position any time you think the price will go very high (or low) in the next few days. With one exception. Close that position after it gets to the computed value of optimized profit. I know its hard especially if you think the position will be in the marked whole week and suddenly you have to close it after one hour because of the optimization. But that is how it works.
  2. Do not start scalping or some kind of very slow high frequency trading :) if your new profit target is very small. If you did 20 trades per month before keep that frequency now as well.
  3. If you find the optimal value of your profit target and you will keep the points 1 and 2, your results should improve.
  4. The optimal value of your profit target does not have to exist. In this case you can only minimize your future loss and you have to think about changing your strategy (or your intuition :)). I hope this is not your case.
One of my goals is to make this process automatic in QuantSense PRO. I know to do this manually with your finger on a chart is a pain. But it is enough to do this once per month to see how you should adjust your trading according to last sample of trades.

Thanks for reading and for using QuantSense LOG.