Friday, May 8, 2015

Advanced trade selections in QuantSense

After some hard coding I decided to take a rest for a while and put down a few words on how to do some advanced trade selections in QuantSense. One of the quite common tasks is to compare trading results in a similar time periods of different years. Let us say that we want to compare our own trade results in the first quarter of 2014 vs the first quarter of 2015. Of course, you can do it for every period separately, print the results and compare them on the paper or in multiple windows on a screen.

I will show you a much more convenient way how to do this kind of comparison in QuantSense. We will need one account  for storing your full trade history (it is more like a folder). Let us call this account simply 'Full history'. In QuantSense, go to Accounts screen a create a new empty account with this name. Then import your full trading history from Meta Trader 4 into this account if you have not done it previously.

Next, we will need two another accounts for storing the trades of particular periods. Let us call them '1. quarter 2014' and '1. quarter 2015'. Just create both accounts and leave them empty.

Go to Trades screen and select the 'Full history' account. A full list of trades will appear on the right side. Now activate the date filter and set the date interval to January 1st 2014 - March 31 2014. Click 'Update'. The list of trades will change such that only trades which were closed in this period will appear. Right under the list there is button 'Select/Unselect all'. Click it and all displayed trades will be selected. Now in the bottom menu (right click or swipe from the bottom to open it) there is a 'Copy' button. Click it and QuantSense will offer you the destination accounts. Select the '1.quarter 2014' account and click 'OK'. All the selected trades will be copied to the '1.Quarter 2014' account.
Here is a screenshot of copying trades from 'Full history' to '1. Quarter 2014'.




Got the point now? We repeat the process for the first quarter 2015.

Change the date filter to match the first quarter 2015, i.e. to January 1 2015 - March 31 2015. Click update, select all trades shown and copy them to the '1.Quarter 2015' account. That is all. We have our data prepared. And the best part is coming now.

Go to Reports screen, select both accounts '1.Quarter 2014' and '1.Quarter 2015'. In the bottom menu there is a 'Settings' button, click it. Set the report such that you activate 'Compare' mode. The rest of the settings are self explanatory. If you know that difference between the number of trades in both account is larger you might want to set 'Custom granularity for profit chart' on, let us say, 7 days period in order to 'align' the curves and better see the profits in corresponding time periods. This is how does the Report Settings screen look like with all options selected as described above:



Return back to the Reports screen and click 'Create report'. QuantSense will generate a nice report for you comparing the two selected accounts with statistics computed for every account separately. You get a few charts, a table with statistics and a list of trades for every selected account. Yes, it is that easy.

For a quick view here are two screenshots from the Report screen. The first one showing the chart comparing the profits in both quarters computed in 7 days granularity. Note that my 1. Quarter 2015 (the orange line) finished a bit early - I do not have enough trades in March 2015 but you get the point:



And when you scroll the report a bit lower you can see an easy to read table with all statistics computed for every account separately:



You can save the report to a folder of your choice. It can be opened by any browser. And you can upload it on your website as it is - all files from the report folder to a folder on your website. To see the full report uploaded on my website click the following link.


After you have finished you can delete accounts '1. Quarter 2014' and '1. Quarter 2015'. All your trades are still in 'Full history' account so you will not loose anything.

To complete this example you need to have some upgrades in QuantSense. You can buy them in QuantSense store right in the app. You need Unlimited accounts upgrade to be able to create more than two accounts and to be able to copy trades between accounts. And you need the Reports upgrade to be able to generate the report. Or simply buy a Premium and do not care about upgrades any more. In the time of writing this post it is still for a discounted price and it will stay there until I finish Market Context upgrade allowing you to do interesting analysis and optimizations in the context of market quotes data. Part of it is available for the Premium users right now for beta testing.

Thanks for reading.

Ľubo

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.