Friday, March 6, 2015

Q uantitative T rading, E RNEST P. C HAN : Chp5

Execution Systems

 

At this point, you should have:

  1. backtested a good strategy
  2. picked a brokerage (e.g., Interactive Brokers),
  3. set up a good operating environment (at first, nothing more than a
    computer, a high-speed Internet connection, and a real-time newsfeed).

Now: to execute the strategy: implement an automated trading system (ATS) to generate
and transmit your orders to your brokerage for execution.

Here we will learn how to:

  1. build an ATS
  2. ways to minimize trading costs
  3. ways to minimize divergence with your expected performance based on your backtests

WHAT AN AUTOMATED TRADING SYSTEM CAN DO FOR YOU

  1. retrieve up-to-date market data from your brokerage or other data vendors
  2. run a trading algorithm to generate orders
  3. submit those orders to your brokerage for execution

Sometimes, all these steps are fully automated. Other times, only part of this process is automated (have to take manual steps)

Fully Automated:  Pros:

  • minimizes human errors and delays.
  • For certain high-frequency - indispensable,

Cons:

  • complicated and costly to build,
  • requiring professional programmers : Java, C#, or C++ for connecting to the API

For lower-frequency strategies: The semiautomated alternative :

  1. generate orders using programs such as Excel or MATLAB
  2. then submit those orders using
    1. built-in tools such as a “basket trader” or “spread trader” offered by your brokerage.
    2. Excel Macro : If your brokerage provides a dynamic data exchange
      (DDE) link to Excel (see below), you can also write a macro attached
      to your Excel spreadsheet that allows you to submit orders
      to the brokerage simply by running the macro. This way, there is
      no need to build an application in a complicated programming language.
      However, it does mean that you would have to perform quite
      a few manual steps in order to submit your orders.

Whether semiautomated or a fully automated: you often need input data beyond  prices (which provided by your brokerage or data vendor). E.g: earnings estimates or dividends data. These nonprice data are typically available free of charge from many web sites – but need retrieval and parsing.

Building a Semiautomated Trading System

semiAutomated

  • generates a list of orders (with, e.g., Excel or MATLAB). Often, same program as the backtest program: [After all, you are implementing the same quantitative strategy that you have backtested.]
    • update the input data file to reflect the most recent data. Done with
      • either a MATLAB program that can directly go to a web site to retrieve the appropriate data, or
      • with a separate program such as HQuote, which can download en masse historical price data for a large number of symbols. Here, MATLAB is used only to organize those data in a suitable format for the trading strategy program that generates today’s orders.
      • The data update step is easy when the most recent data is the previous day’s close, but certainly more difficult when it is the last intraday price. Then, your data
        vendor or brokerage must provide a DDE link that automatically updates an Excel input file.
        • DDE link = an expression to be inserted on an Excel spreadsheet that will automatically load the appropriate data into a cell. generally look like this: (for last price of symbol IBM)
          • =accountid|LAST!IBM
    • To generate the orders, you can run an Excel macro or a MATLAB program, which scans through the information and prices on the spreadsheet, runs the trading algorithm, and writes out the orders to another text file where each line contains the triplet (symbol, side, size). For example, ("IBM", "BUY", "100")  (or more info in each line, like Day only, or Good Till Cancel
  • upload this order file to your brokerage’s basket trader or
    spread trader
    for submission.
    • A basket trader is an application that allows you to upload multiple
      orders for multiple symbols and submit them to the brokerage in
      one keystroke.
    • Spread trader is an application with which you can
      specify the symbols of multiple pairs of stocks or other securities,
      and the conditions when orders for each of these pairs should be
      entered. The spread trader can monitor real-time prices and check
      whether these conditions are satisfied throughout the trading day.
    • If the DDE links of your brokerage allow you to submit orders, you
      can also run an Excel macro to sweep through the order file and
      submit all the orders to your account with one press of the button
      as well.
  • “Here is what I do with a basket trader from Interactive Brokers.
    1. Every day before the market opens I run a MATLAB program that
      retrieves market data, run the trading algorithm, and write out a list
      of orders into an order file that can be over 1,000 lines (corresponding
      to over 1,000 symbols).
    2. I then bring up the basket trader from my trading screen, upload the order file to my account using the basket trader, and in one keystroke submit them all to my account. Some of these orders may get executed at the open; others may get executed later or not at all.
    3. Before the market closes, I cancel all the unexecuted orders by pressing a button.
    4. Finally, if I want to exit all the existing positions, I simply press another button in the basket trader to generate the appropriate exit orders.”
  • “I use REDIPlus’s spread trader for pair-trading strategies…because I can have the spread trader enter orders at all times of the day, not just at the market close
    1. before the market opens I use MATLAB to retrieve market data, run the pairtrading algorithm, and write out limit prices for all the pairs in my
      universe. (Note that the limit prices are limits on the spread, not on
      the individual stocks. If they were on the individual stocks, ordinary
      limit orders would have done the trick and the spread trader would
      have been redundant.)
    2. I then go to the spread trader, which already contains all these pairs that I have previously specified, and manually adjust the limit prices based on the MATLAB output. (Actually, this step can be automated too—all the spread orders information can be written out to an Excel file by MATLAB and uploaded to the spread trader.)
    3. Pressing another button will initiate automatic monitoring
      of prices and entering of orders throughout the trading day.
  • Typically, a semiautomated trading system is suitable if you need to run this step only a few times a day in order to generate one or a few waves of orders.

 

Building a Fully Automated Trading System

loop: constantly scanning the latest prices and generating new waves of orders throughout the trading day. The submission of orders through an API.

FullyAutomated

requires API for data retrieval and order submission. API usually in VB, Java, C# or C++ (no MATLAB). (so – it has to be written in those langs)

Theoretically, Excel, but create a loop in your macro so that it updates the cells
using the DDE links and submit orders when appropriate continuously
throughout the day. Unfortunately, data updates through
DDE links are slow, and generally your brokerage limits the number
of symbols that you can update all at once.

Similarly, order submissions through DDE links are also slow. Hence, for trading strategies that react to real-time market data changes intraday, this setup using a spreadsheet is not
feasible.

TradeStation  - offer a complete backtesting and order submission platform. the drawback of such proprietary systems is that they are seldom as flexible as a general-purpose programming language like MATLAB or Java for the construction of your strategy. For instance, if you want to pursue a rather mathematically complex strategy based on
principal component analysis.

H I R I N G   A    P R O G R A M M I N G       C O N S U L T A N T

Building an ATS generally requires more professional programming skills than
backtesting a strategy. especially true for high-frequency strategies (speed of execution is important).

experienced programmer, the hourly fees may range from $50 to $100. you can negotiate a fixed fee for the entire project ahead of time - $1,000 to $5,000. the brokerage can often refer you to some programmers who have experience with their API. (Interactive Brokers, for example, has a special web page that allows programming consultants to offer their services.) look at elitetrader.com.

one issue: How do you keep your trading strategy confidential? sign nondisclosure agreements (NDAs) but how could you know?!

  1. you-are-not-really-unique : most strategies that you may think are your unique creations are actually quite well known to experienced traders. So, whether you like it or not, other people are already trading very similar strategies and impacting your returns. Adding an extra trader or two, unless the trader works for an institutional money manager, is not likely to cause much more impact.
  2. one-more-trader-is-not-a-big-deal : if you are trading a strategy that has a large capacity (e.g., most futures trading strategies), then the extra market impact from your rogue programmer consultant will be minimal.
  3. divide-the-work : you can choose to compartmentalize your information and implementation—that is, you can hire different programmers to build different parts of the automated trading strategy. Often, one programmer can build an automated trading infrastructure  program that can be used for different strategies, and another one can implement the actual strategy, which will read in input parameters. So in this case, the first programmer does not know your strategy, and the second programmer does not have the infrastructure to execute the strategy.
  4. it’s-the-parameters-stupid : neither programmer knows the actual parameter values to use for your strategy.

 

MINIMIZING TRANSACTION COSTS

  1. refrain from trading low-price stocks:  low-price stocks increase your total commissions costs (since you need to buy or sell more shares for a fixed amount of capital), percentage-wise they also have a wider bid-ask spread and therefore increase your total liquidity costs.
  2. minimize market impact cost, you should:
    • limit the size (number of shares) of your orders based on the liquidity of the stock. One common measure of liquidity is the average daily volume (it is your choice what lookback period you want to average over). As a rule of thumb, each order should not exceed 1 percent of the average daily volume.
    • scale the size of your orders based on the market capitalization of a stock : not linear scale (will result zero-weight for most small- and microcap stocks in your portfolio). we should not allow that ratio to be more than 10 or so, provided that the liquidity (volume) constraint described above is also satisfied. If the capital weight of a stock is proportional to the fourth root of its market cap, it would do the trick.
    • break it down into many smaller orders and execute them over time. BUT: engenders slippage (another kind of transactions costs). Since reducing market impact in this way may increase slippage, it is not really suitable for retail traders whose order size is usually not big enough to require this remedy.

 

TESTING YOUR SYSTEM BY PAPER TRADING

benefits:

  • reveal bugs : practically the only way to see if your ATS software has bugs
    without losing a lot of real money
    • look-ahead bug: Often, the moment you start paper trading you will realize that there is a glaring look-ahead bias in your strategy => “back to the drawing board.
    • compare to the backtest : compare the paper trades and profit and loss (P&L) with the theoretical ones generated by your backtest program using the latest data. If the difference is not due to transaction costs (including an expected delay in execution for the paper trades), then your software likely has bugs
  • strategy intuition: gives you better intuitive understanding of your strategy, including the volatility of its P&L, the typical amount of capital utilized, the number of trades per day, and the various operational difficulties including data issues. Even though you can theoretically check out most of these features of your strategy in a backtest, one will usually gain intuition only if one faces them on a daily, ongoing basis.
  • reveal operational difficulties, such as how fast you can download all the needed data before the market opens each day and how you can optimize your operational procedures in actual execution.
    • DON’T UNDERESTIMATE preparing orders’ time:
      • “It took me some 20 minutes to download and parse all my historical data each morning, and it took another 15 minutes or so to transmit all the orders”
      • “If your trading strategy depends on data or news prior to the market open that cannot be more than 35 minutes old, then you need to either figure out a different execution environment or modify your strategy. It is hard to figure out all these timing issues until paper trading is conducted “
  • discover data-snooping (real-out-of-sample) : if you run a paper trading system for a month or longer, you may even be able to discover data-snooping bias, since paper trading is a true out-of-sample test.
    • bonus thoughts: However, traders usually pay less and less attention to the performance of a paper trading system as time goes on, since there are always more pressing issues (such as the real trading programs that are being run). This inattention causes the paper trading system to perform poorly because of neglect and errors in operation. So data-snooping bias can usually be discovered only when you have actually started trading the system with a small amount of capital.
    •  

WHY DOES ACTUAL PERFORMANCE DIVERGE FROM EXPECTATIONS?

Finally, you have entered your first order and it got executed! Smile

What if after month/2-months/quarter strategy still delivers a meager or maybe even negative returns??? review the list:

  • Do you have bugs in your ATS software?
  • Do the trades generated by your ATS match the ones generated by your backtest program?
  • Are the execution costs much higher than what you expected?
  • Are you trading illiquid stocks that caused a lot of market impact?

Eliminated these ? the two most dreaded causes of divergence: datasnooping bias and regime shifts.

  1. To see if data-snooping bias is causing the underperformance
    of your live trading:
  2. try to eliminate as many rules and as many parameters
    in your strategy as possible. If the backtest performance
    completely fell apart after this exercise, chances are you do have
    this bias and it is time to look for a new strategy. If the backtest
    performance is still reasonable, your poor live trading performance
    may just be due to bad luck.

  3. Regime shifts = financial market structure or the macroeconomic environment undergoes a drastic change so much so that trading strategies that were profitable before may not be profitable now. two noteworthy regime shifts in recent years:
    1. decimalization of stock prices
    2. Prior to 2007, SEC rules state that one cannot short a stock unless it is on a “plus tick” or “zero-plus tick.” (so your backtested short-strategy may look very profitibale)
      • (BTW, even without the plus-tick rule, many stocks, especially
        the small-cap ones or the ones with low liquidity, are “hard
        to borrow.”)

a model for detecting regime shifts automatically – in CH7

 

SUMMARY

ATS is a piece of software that automatically generates and transmits orders to your brokerage account based on your trading strategy. The advantages of having this software
are that:

  • It ensures the faithful adherence to your backtested strategy.
  • It eliminates manual operation so that you can simultaneously
    run multiple strategies.
  • Most importantly, it allows speedy transmissions of orders,
    which is essential to high-frequency trading strategies.

Regarding the difference between a semiautomated trading system and a fully automated trading system:

  • In a semiautomated trading system, the trader still needs to manually upload a text file containing order details to a basket trader or spread trader, and manually press a button to transmit the orders at the appropriate time. However, the order text file can be automatically generated by a program such as MATLAB.
  • In a fully automated trading system, the program will be able to automatically upload data and transmit orders throughout the trading day or even over many days.

After the creation of an ATS: focus on issues important in execution: minimizing transaction costs and paper trading.

Minimizing transaction costs is mainly a matter of not allowing your order size to be too big relative to its average trading volume and relative to its market capitalization. Paper trading allows you to:

  • Discover software bugs in your trading strategy and execution programs.
  • Discover look-ahead or even data-snooping bias.
  • Discover operating difficulties and plan for operating schedules.
  • Estimate transaction costs more realistically.
  • Gain important intuition about P&L volatility, capital usage, portfolio size, and trade frequency.

Finally, what do you do in the situation where your live trading underperforms your backtest? You can start by addressing the usual  problems:

  • Eliminate bugs in the strategy or execution software;
  • reduce transaction costs; and
  • simplify the strategy by eliminating parameters.
  • But, fundamentally, your strategy still may have suffered
    from data-snooping bias or regime shift.

No comments:

Post a Comment