TWS Accumulate/Distribute Algo
W E B I N A R N O T E S
The initial intention was to develop an algorithm that would allow the trading of large blocks of stock without being detected in the market. As we went along, the algo evolved into much more, so that in its current state it can even be set up for high frequency trading.
Please keep in mind that the algo can be deployed for futures, options, forex or any product you can trade with us, and it can also be used to trade and allocate the resulting positions among multiple accounts.
Define the Algo
Let’s start with a simple task of say buying 1,000,000 shares of IBKR. (Selling works the same way as buying but I will demonstrate everything from the buying perspective).
We want to issue orders in 500 share increments every 30 seconds. If we can keep to that schedule, we would buy the 1 million shares in about three days. A 500 share buy order every 30 seconds would of course be immediately detected, and front ran --so we need to randomize these orders.
We can check randomize time and size.
- Randomizing time within 20% in case of our 30 seconds means that the next order will be issued at some random time with 24 and 36 seconds after the last fill.
- Randomizing size within 55% means that the size of the next order could be for any number of shares between 225 and 775 shares, rounded to the nearest 100, so the order will be either for 200 or 300 etc. up to 800 shares.
Order Types
The order can be a Limit, Market or Relative order.
Note - Relative orders are not supported for products where cancellation fees are levied by the listing exchange.
Market - There is not much to say about a market order, it is issued at the ask price and you would use it only in case of some extremely liquid stock, where there is usually a penny wide market and large size on both sides.
Limit and Relative orders are much more interesting.
When you put in a relative order you must name what it is relative to, and here you have a lot of choices. Of course, if it is relative to a fixed value than it is really a limit order, but you can make it relative to the prevailing bid or the ask or the last price or VWAP or moving VWAP or moving average or EXP MVG, or your last trade price or the number of shares you have bought so far.
You also must input an offset to the item your order is relative to. Say you want to match the prevailing bid, than you put in BID and an offset of zero.
If you want to be aggressive,
- You could put in the BID +0.01, or 1 cent, and in this case you may also want to make sure that you are not lifting the offer if the market were 1 cent wide, so you would further input that in no case would you bid more than 2 cents under the ask.
- If you want additional restrictions you may say for example that you want your bid to be no higher than the last trade price for the stock and no higher than 1 cent under your last buy price and also no higher than the VWAP, or say the exponential moving average for the last 25 minutes.
We won’t go through all the various combinations of values you can put in here.
The next question is if you want to wait for the current order to be filled before the next order is submitted.
- • If you do not, than additional orders will be transmitted every 24 to 36 seconds and they will accumulate into one or more sizable orders at the exchange.
- If you do check the box to wait for the fill before submitting the next order, which I think is a good idea, then the more difficult the conditions that you put in, the more likely it is that the algo will fall behind schedule of buying 500 shares every 30 seconds.
- So, then the next question is if you have fallen behind, do you want to catch up with the original schedule if the conditions allow?
- Yes, means not waiting the 24 to 36 seconds between every order but that the next order will be submitted immediately after an order is filled until you are caught up.
- Next you need to input if you are you working the order only during regular trading hours or other times too.
- It sometimes happens that somebody comes along and either seriously or just to probe the market or push the stock around, puts in a large offer.
- Since you want to buy a million shares you cannot let such an opportunity go by without taking advantage of it. So you want to jump at a large offer. On the other hand if you do, you are showing your hand so you will do this only if the offer shown is really large, say 200,000 shares.
- If you put in 200,000 that tells the algo to take any large offer that satisfies your conditions as to price, that is at least 200,000 shares but take not more than you need to complete your purchase.
Conditions
Now we come to additional conditions you can pose. Whenever these conditions are not met, the algo can stop permanently or resume when they are satisfied again. Keep in mind that the algo is intended to be able to run unattended as one trader may be running several hundred of them at the same time.

- The first such condition is the price range. You may specify a price range outside of which you do not want to buy the stock.
- If there is news on the stock you may want the algo to stop for some period of time.
- Next you can make the algo being active or not, based upon your position in the stock.
- You can for example run a buy algo and a sell algo at the same time and try to make the two trade a stock back and forth for a profit. In that case you may want to suspend the buy algo if your position becomes longer than some amount.
- Or after having reached a long position in excess of some quantity, you may want to switch to an algo with different parameters. Yes, you can run two or more buy algos for the same stock at the same time with different variables and switch back and forth between the two by making it depend on the position which one is active at any one time.
• The next condition has to do with the path described by the stock price.
-
You may, for example, say that if the 10 minute VWAP is not at least as high as the 50 day moving average, you do not want to buy the stock.
- Go ahead and experiment with putting in various conditions, you will see that as you put in the 10 minute VWAP or the 50 day moving average, the program gives back the actual values to you, so you are not operating in the blind. This feature enables you to set up algorithms to trade chart points even when you are not looking.

- The next condition calls for two symbols and otherwise it is the same as the one we just discussed. Here you can put in any symbol and compare some measure of that to any other, different or same measure of any other or the same symbol, including the very stock you are working with.
Note - The best way to learn is to experiment with putting in various parameters without actually starting the algorithm.
- You could say, for example, that you want to buy this stock only if it has been in a continuous uptrend, i.e. the 10 minute VWAP being higher than the 50 day moving average is not by itself a sufficient condition, you would also like the 10 day moving average to be higher than the 30 day moving average on this stock.
Or you want the same condition to prevail for the IAI, which is a security brokers’ ETF containing IBKR. In that case you would put in the symbol IAI.
You are also free to compare two different things like IAI to SPY and make that into a condition.
Lastly, you can require that two relative measures be at least some fixed amount apart, higher or lower from each other.
You can say that you want IBKR current price vs. its 10 day VWAP to be 2.1% lower than IAI current price vs. its 12 day VWAP.
In addition to moving large blocks of stock through this algo one can implement many different trading strategies by running an algo on the buy side and running one on the sell side at the same time.
To fully appreciate the power of the algorithm and how one trader can in fact do the work of ten or more by using it, you should try to put in hypothetical values for the variables and imagine what it would do. After you got a bit more comfortable with that, you could pick a low priced stock
and do some live experiments with small sizes.
It is important to note that you can stop the algo at any time, or you can change any of the parameters while the algorithm is active. These changes however will not become effective until you click on the {apply} button.
If you become a frequent user of Accumulate / Distribute, you will not want to fill out a new template for each occasion from scratch. For this reason we are developing the ability to name your templates and to copy them for different symbols.
Monitor the Order Progress
The order Summary section for each algo provides real-time data so you can monitor the progress of the order.
The AccumDist Summary tab displays order management lines for all of your Accumulate/Distribute algos -- allowing a single trader to effectively manage multiple orders simultaneously.
Algo in progress: