IBKR Quant Blog


1 2 3 4 5 2 31


Quant

Practical Statistics for Algo Traders - Part 5


The first installments in this series are available here.

 

What’s a Trader to Do?

We’ve seen that with small sample sizes, we can observe wild departures from an expected value – particularly with a Sharpe 1.5 strategy. Probably worryingly to many traders out there is the fact that, as it turns out, even two years of trading might constitute a “small sample”. Depending on your goals and expectations, that’s a long time to be wondering.

So what can be done? Well, there are two main options:

  1. Only trade strategies with super-high Sharpes that enable statistical uncertainty to shrink quickly.
  2. Acknowledge that statistical uncertainty is a part of life as a low frequency trader and find other ways to cope with it.

Option 1 isn’t going to be feasible for the people for whom this article is written. So let’s explore option 2.

While statistical approaches often don’t provide definitive answers to the questions that many traders need answered, market experience can at least partially fill the gaps. Above I touched on the idea that if we had a rational basis for a trade, we’d treat the statistical uncertainty around it’s out of sample performance differently than if we had simply data-mined a chart pattern or used an arbitrary technical analysis rule.

Intuition around what tends to work and what doesn’t, believe it or not, actually starts to come with experience in the markets. Of course, even the most savvy market expert gets it wrong a lot, but market experience can certainly tip the balance in your favour. While you’re acquiring this experience, one of the most sensible things you can do is to focus on trades that can be rationalised in some way. That is, trades that you speculate have an economic, financial, structural, behavioural, or some other reason for existing. Sometimes (quite often in my personal experience!) your hypothesis about the basis of the trade will be false, but at least you give yourself a better chance if the trade had a hypothetical reason for being.

Another good idea is to execute a trade using small positions as widely as possible. Of course, no effect will likely “work” across all markets, but many good ideas can be profitable in more than a single product or financial instrument, or traded using a cross-sectional approach.  If there really is an edge in the trade, executing it widely increases the chances of realising it’s profit expectancy, and you get some diversification benefits as well. This idea of scaling a trade across many markets using small position sizing is one of the great benefits of automated trading.

Finally, it’s important to keep an open mind with any trade. Don’t become overly wedded to a particular idea, as it’s very likely that it won’t work forever. Far more likely is that it will work well sometimes, and not so well at other times. The other side of this is that if you remove a component from a portfolio of strategies, it is often a good idea to “keep an eye on it” to see if it comes back (automation can be useful here too). But once again, deciding on whether to remove or reinstate a component is as much art as science.

So what does this look like in real life? Well here’s an example taken from a prop firm that I know very well. The firm has a research and execution team, who design strategies, validate them and implement the production code to get them to market. Then there’s the operations guys who decide at any given time what goes into the portfolio, and where and how big the various strategies are traded. They use some quantitative tools, but they also use a hefty dose of judgement in making these decisions. That judgement is undoubtedly a significant source of alpha for the firm, and the team has over 50 years of combined experience in the markets from which to make these judgements.

These ideas sound sensible enough, but the elephant in the room is the implied reliance on judgement and discretion, which might feel uncomfortable to systematic traders (to be completely honest, up until a couple of years ago, I’d have felt that same discomfort). The problem is, anyone can learn to do statistics, build time-series models, run tests for cointegration, and all the other things that quants do. But good judgement and intuition is much harder to come by, and is generally only won through experience. And that takes time, and many of the lessons are learned through making mistakes.

Conclusion

Humans tend to make errors of judgement when it comes to drawing conclusions about a sample’s representativeness of the wider population from which it is drawn. In particular, we tend to underestimate the uncertainty of an expected value given a particular sample size. There are times when the implications of these errors of judgement aren’t overly severe, but in a trading context, they can result in disaster. From placing too much faith in a backtest, to tinkering with a strategy before it’s really justified, errors of judgement imply trading losses or missed opportunties.

We also saw that a “significant sample size” (where significant implies large enough that the sample is likely representative of the population) for typical retail level, low-frequency trading strategies can take so much time to acquire that it becomes almost useless in a practical sense. Here at Robot Wealth, we believe that systematic trading is one of those endeavours that requires a breadth of skills and experience, and that success is found where practical statistics and data science skills intersect with market experience.

The need for experience and judgement to compliment good analysis skills is one of the most important realisations I had when I moved from amateur trading into the professional space. That experience doesn’t come easily or quickly, but we believe that by demonstrating exactly what we do to build and trade a portfolio, we can help you acquire it as quickly as possible.

 

 

---------------------------------------------------------------------------

To download the code used in this article, and to learn more about Robot Wealth click here: https://robotwealth.com/

This article is from Robot Wealth and is being posted with Robot Wealth’s permission. The views expressed in this article are solely those of the author and/or Robot Wealth and IB is not endorsing or recommending any investment or trading discussed in the article. This material is for information only and is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad-based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation by IB to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.


20108




Quant

Join IBKR for a Free Webinar with Kristal AI on Intrinsic Value Investing


Join IBKR for a Free Webinar with Kristal AI on Intrinsic Value Investing

Thursday, September 27, 2018 at 04:30 EDT

 

Register

 

In this session we will delve into the meaning of  the fundamental analysis of stocks by providing an overview of the four financial statements and some commonly used valuation models. Then, we will further describe in detail the investing methods of some of the gurus of Value Investing, namely Benjamin Graham, Warren Buffett and Walter Schloss before ending the session by highlighting different investment styles.

Speaker:    Arun Pai, Chief Investment Officer Kristal.AI

 

Sponsored by:   Kristal.AI

 

Information posted on IBKR Quant that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Quant are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results


20039




Quant

How To Install TensorFlow GPU (With Detailed Steps)


By Varun Divakar

When I started working on Deep Learning (DL) models, I found that the amount of time needed to train these models on a CPU was too high and it hinders your research work if you are creating multiple models in a day. Later I heard about the superior performance of the GPUs, so I decided to get one for myself. One of the basic problems that I initially faced was the installation of TensorFlow GPU.

After a lot of trouble and a burnt motherboard (not due to TensorFlow), I learnt how to do it. A few days earlier I spoke to someone who was facing a similar issue, so I thought I might help people who are stuck in a similar situation, by writing down the steps that I followed to get it working.

In this blog, we will understand how to install tensorflow on a Nvidia GPU system. Before we do that, let us look at the various steps involved in the process of installation:

  1. Uninstall Nvidia
  2. Install Visual Studio
  3. Install CUDA
  4. Install cuDNN
  5. Install Anaconda
  6. Install TensorFlow-GPU
  7. Install Keras

 

1. Uninstall Nvidia

This may not look like a necessary step, but believe me, it will save you a lot of trouble if there are compatibility issues between your current driver and the CUDA. Once you login to your system, go to the control panel, and then to the ‘Uninstall a program’ link. Then scroll below to the section with programs that have been published by the NVIDIA corporation.

Quant

Here, you uninstall all the NVIDIA programs. Do not worry if you have some drivers, they can be updated later once you finish the setup.

Once you have removed all the programs, go to the C drive and check all the program files folders and delete any NVIDIA folders in them.

Quant

 

2. Install Visual Studio

In the next step, we will install the visual studio community from here

Qaunt

 

Here, make sure that you select the community option.

 

Quant

 

 

Once you have downloaded the Visual Studio, follow the setup process and complete the installation.

 

3. Install CUDA

This is a tricky step, and before you go ahead and install the latest version of CUDA (which is what I initially did), check the version of CUDA that is supported by the latest TensorFlow, by using this link.

I have a windows based system, so the corresponding link shows me that the latest supported version of CUDA is 9.0 and its corresponding cuDNN version is 7.

Quant

As it goes without saying, to install TensorFlow GPU you need to have an actual GPU in your system. So please check if you have a GPU on your system and if you do have it, check if it is a compatible version using the third link in the above screenshot.

Once you are certain that your GPU is compatible, download the CUDA Toolkit 9.0 from this link.

Please choose your OS, architecture (CPU type of the platform) and version of the OS correctly. Then click on the exe(local) button,

Now download the base installer and all the available patches along with it.

Quant

Once the download is complete, install the base installer first followed by the patches starting from Patch 1 to Patch 4.

 

4. Install cuDNN

Once your installation is completed, you can download the cuDNN files. To do this, go to this link.

Here to download the required files, you need to have a developer’s login. So, please go ahead and create your login if you do not have one.

Once you create your login and agree to the terms and conditions, visit the archived cuDNN files using this link.

And click on the cuDNN version 7.0 for CUDA 9.0

Quant

Then choose the appropriate OS option for your system.

Quant

This will download a zip file on to your system. Once you unzip the file, you will see three folders in it: bin, include and lib. Extract these three files onto your desktop.

Quant

Once you have extracted them. Go to the C drive, there you will find a folder named NVIDIA GPU Computing Toolkit. Inside this, you will find a folder named CUDA which has a folder named v9.0. In this folder, you can see that you have the same three folders: bin, include and lib. Copy the contents of the bin folder on your desktop to the bin folder in the v9.0 folder. Similarly, transfer the contents of the include and lib folders.

Once you are done with the transfer of the contents, go to the start menu and search for ”edit the environment variables”. Click on the search result and open the System Properties window and within it open the Advanced tab.

Quant

Now click on the ‘Environment Variables’,

Quant

and under System Variables look for PATH, and select it and then click edit.

Add the following two paths to the path variable:

  • C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin
  • C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\libnvvp

Quant

Once you are done with this, you can download Anaconda, and if you already have it, then create a Python 3.5 environment in it.

 

5. Install Anaconda

To install Anaconda on your system, visit this link.

Here choose your OS and the Python 3.6 version, then click on download. Follow the instructions in the setup manager and complete the installation process.

Once you have completed the installation of Anaconda. Create a python 3.5 environment using the following command in the terminal or anaconda prompt.

conda create -n tensorflow python=3.5

Once the environment is created, activate it using the following command in the terminal or anaconda prompt:

activate tensorflow

 

6. Install TensorFlow- GPU

Once you have the environment ready, you can install the tensorflow GPU using the following command in the terminal or anaconda prompt:

pip install --ignore-installed --upgrade tensorflow-gpu

You will need to specify the version of tensorflow-gpu, if you are using a different version of CUDA and cuDNN than what is shown in this blog. The above line installs the latest version of tensorflow by default. If you have any issues while installing tensorflow, please check this link.

 

7. Install Keras

Once the tensorflow is installed, you can install Keras. Using the following command:

pip install keras

Once the installation of keras is successfully completed, you can verify it by running the following command on Spyder IDE or Jupyter notebook:

import keras

Some people might face an issue with the msg package. In case you do, you can install it using the following command

conda install -c anaconda msgpack-python

 

I hope you have successfully installed the tensorflow- gpu on your system.

In this article, we have covered many important aspects like how to install Anaconda, how to install tensorflow, how to install keras, by installing tensorflow gpu on windows. We started by uninstalling the Nvidia GPU system and progressed to learning how to install tensorflow gpu.

 

If you want to learn more about QuantInsti, or to download sample code, visit QuantInsti website and the educational offerings at their Executive Programme in Algorithmic Trading (EPAT™).

 

This article is from QuantInsti and is being posted with QuantInsti’s permission. The views expressed in this article are solely those of the author and/or QuantInsti and IB is not endorsing or recommending any investment or trading discussed in the article. This material is for information only and is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad-based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation by IB to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.


20049




Quant

Introduction to the IBKR TWS C++ API - a webinar recording with IBKR API Team


View the recording

The Interactive Brokers API Team demonstrated:
 

  1. Overview of TWS API capabilities and setup
  2. Availability of C++ API on various platforms (Windows, Linux, OSX)
  3. API architecture from a programming perspective
  4. Example of the flow of an API program execution using the 'Testbed' sample from version 9.72
  5. Examples of API functions, such as to request market data and place orders, and expected results

 

IBKR

 

Start your FREE Trial Simulated account and test your trading strategies with the IBKR C++ API!

FREE TRIAL

 

Information posted on IBKR Quant that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Quant are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.


20197




Quant

Practical Statistics for Algo Traders - Part 4


Why the Law of Large Numbers Matters to Traders


The first installments in this series are available here.

As much as I’m sure you enjoy thinking about the statistics of burger review systems, let’s turn our attention to trading. In particular, I want to show you how our intuition around the law of large numbers can lead us to make bad decisions in our trading, and what to do about it.

High-frequency trading strategies typically have a much higher Sharpe ratio than low frequency strategies, since the variability of returns is generally much higher in the latter. If you had a high-frequency strategy with a Sharpe ratio in the high single digits, you’d only need to see a week or two of negative returns – perhaps less – to be quite sure that your strategy was broken.

But most of us don’t have the capital or infrastructure to realise a high-frequency strategy. Instead, we trade lower frequency strategies and accept that our Sharpe ratios are going to be lower as well. In my experience, a typical non-professional might consider trading a strategy with a Sharpe between about 1.0 and 2.0.

How long does it take to realise such a strategy’s true Sharpe? And how much could that Sharpe vary when measured on samples of various sizes? The answer, which we’ll get too shortly, might surprise you, or even scare you! Because it turns out that a “large” number may or may not be so large, depending on the context. And that lack of context awareness is precisely where we tend to make our most severe errors in the application of the Law of Large Numbers.

First of all, let’s simulate various realisations of 40 days of trading a strategy with a true Sharpe ratio of 1.5. This is equivalent to around two months of trading.

If we set the strategy’s mean daily return, mu , to 0.1%, we can calculate the standard deviation of returns, sigma , that results in a true Sharpe of 1.5:

Quant

And here’s 5,000 realisations of 40 days of trading a strategy with this performance (under the assumption that daily returns are normally distributed, an inaccurate but convenient simplification that won’t detract too much from the point):

Quant

Quant

Whoa! The histogram shows that it isn’t inconceivable (in fact it’s quite likely) that our Sharpe 1.5 strategy could give us an annualised Sharpe of -2 or less over a 40-day period!

What would you do if the strategy you’d backtested to a Sharpe of 1.5 had delivered an annualised Sharpe of -2 over the first two months of trading? Would you turn it off? Tinker with it? Maybe adjust a parameter or two?

You should probably do nothing! At least until you’ve assessed the probability of your strategy delivering the actual results, assuming it’s performance was indeed what you’d backtested it to be. To do that, you can just sum up the number of simulated 40-day Sharpes that were less than or equal to -2, and then divide by the number of Sharpes we simulated:

Quant

which works out to about 8.5%.

Let’s now look at the convergence of our Sharpe ratio to the expected Sharpe as we increase the sample size, just as we did in the burger review example above. Here’s the code:

Quant

 

And the output:

Quant

 

Once again we see the sample uncertainty shrink as we increase the sample size, but this time it’s magnitude looks much more frightening. Note the uncertainty even after 500 trading days! This implies that our strategy with a long-term Sharpe of 1.5 could conceivably deliver very small or even negative returns in a two-year period.

If you’ve done a lot of backtesting, you probably understand from experience that a strategy with a Sharpe of 1.5 can indeed have drawdowns that last one or two years. So maybe this result doesn’t surprise you that much. But consider how you’d feel and act in real time if you suffered through such a drawdown after going live with this strategy that you’d painstakingly developed. Would you factor the uncertainty of the sample size into your decision making?

The point is that this time the uncertainty really matters. Maybe you don’t care that much if you thought you were getting a 5-star burger, but ended up eating a 4-star offering. You could probably live with that. But what if you were expecting to realise your Sharpe 1.5 strategy, but after 2 years you’d barely broken even?

Returning to our 40-days of unprofitable trading of our allegedly profitable strategy. As mentioned above, there’s an 8.5% chance of getting an annualised Sharpe of -2 from this scenario. Maybe that’s enough to convince you that your strategy is not actually going to deliver a Sharpe of 1.5. Maybe you’d be willing to stick it out until the probability dropped below 5%. It’s up to you, and in my opinion should depend at least to some extent on your prior beliefs about your strategy.1  For instance, if you had a strong conviction that your strategy was based on a real market anomaly, maybe you’d stick to your guns longer than if you had simply data-mined a pattern in a price chart with no real rationalisation for it’s profitability. This is an important point, and I’ll touch on it again towards the end of the article.

No doubt you’ve already realised that the backtest itself is unlikely to be a true representation of the strategy’s real performance. Due to it’s finite history, the backtest itself is just a “sample” from the true “population”! So how much confidence can you have in your backtest anyway?

In the next article, I’ll show you a method for incorporating both our prior beliefs about our strategy’s backtest and the new information from the 40 trading days to construct credible limits on our strategy’s likely true performance. As you might imagine from the scatterplot above, that interval will likely be quite wide, so there’s really no way around acknowledging the inherent uncertainty in the problem of whether or not to continue trading our strategy.

 

  1. I deliberately used the term “prior” here as a reference to Bayesian reasoning, which in my opinion is a great way to think about all kinds of problems like these. I’d like to come back to this topic in the future. Let me know in the comments if that interests you.

---------------------------------------------------------------------------

To download the code used in this article, and to learn more about Robot Wealth click here: https://robotwealth.com/

This article is from Robot Wealth and is being posted with Robot Wealth’s permission. The views expressed in this article are solely those of the author and/or Robot Wealth and IB is not endorsing or recommending any investment or trading discussed in the article. This material is for information only and is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad-based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation by IB to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.


20107




1 2 3 4 5 2 31

Disclosures

We appreciate your feedback. If you have any questions or comments about IBKR Quant Blog please contact ibkrquant@ibkr.com.

The material (including articles and commentary) provided on IBKR Quant Blog is offered for informational purposes only. The posted material is NOT a recommendation by Interactive Brokers (IB) that you or your clients should contract for the services of or invest with any of the independent advisors or hedge funds or others who may post on IBKR Quant Blog or invest with any advisors or hedge funds. The advisors, hedge funds and other analysts who may post on IBKR Quant Blog are independent of IB and IB does not make any representations or warranties concerning the past or future performance of these advisors, hedge funds and others or the accuracy of the information they provide. Interactive Brokers does not conduct a "suitability review" to make sure the trading of any advisor or hedge fund or other party is suitable for you.

Securities or other financial instruments mentioned in the material posted are not suitable for all investors. The material posted does not take into account your particular investment objectives, financial situations or needs and is not intended as a recommendation to you of any particular securities, financial instruments or strategies. Before making any investment or trade, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice. Past performance is no guarantee of future results.

Any information provided by third parties has been obtained from sources believed to be reliable and accurate; however, IB does not warrant its accuracy and assumes no responsibility for any errors or omissions.

Any information posted by employees of IB or an affiliated company is based upon information that is believed to be reliable. However, neither IB nor its affiliates warrant its completeness, accuracy or adequacy. IB does not make any representations or warranties concerning the past or future performance of any financial instrument. By posting material on IB Quant Blog, IB is not representing that any particular financial instrument or trading strategy is appropriate for you.