Monday, December 26, 2011

Types of Market Orders

Type of order is very important during execution of a trade, as it would lead to unsolicited higher trading cost, if wrong type of order is used. In this text, I will shortly talk about types of market orders.

But foremost, let's shed some light on what is order and it is common properties. An order is an instruction to the market how to execute an order with some criteria. All orders have instrument id, size and if it is a sell or buy. In addition to that, orders have some attached conditions such as limit price, expiry date, market type, market trend and quantity. Lets dive into these types now.

1- Price Related Orders

These orders are executed once some price related criteria are met in order book.

1.1 Market Orders
This is most straightforward order type in which order is executed with available market bid and ask prices. An buy order will be matched with lowest ask price and a sell order will be matched with a highest bid price.

Inpatient traders may give this order to fill their order immediately with some degree of price uncertainty. While it is guaranteed to be executed, the price may move. This order type would have market impact, if big quantity is asked/offered which leads to huge unwanted losses. That order type provides demand and supply liquity to market. Because of spread between bid and ask prices, sequential sell/buy trades cause an immediate loss (ask-bid price). In market orders, sometimes, bid/ask price is improved, if market maker wants to have a priority in order book. Market maker has to improve the ask/bid price if a retail customer is in the order book due to exchange rules.

1.2 Limit Orders
If a trader wants to limit the risk of market move, limit orders can be used. Trader specify a maximum/minimum prices to buy or sell, respectively.In other words, it is the highest buy price and lowest sell price for the trader would be happy to accept. Limit prices are used to minimize cost of market move by making sure execution price is always higher (sell) or lower (buy) than a predefined price.

A limit order can be very aggressive or passive. If limit order is around the market price (marketable limit order), it will be filled quicker than those far away from the bid/ask prices (behind the market). If a limit order is equal the bid/ask prices, then it will be at the market limit order.

Standing limit orders provides option for other traders to trade for free. For example, a sell limit order is an call option for another trader. Similarly, a buy limit order is a put option for other trader. Deepness and structure (symmetric or aysmetric) of a order book in terms of limit orders can be used in trading strategies.

There are two risk in limit order: Execution uncertainty and ex-post regret. If limit order is placed far away from a bid/ask prices, it may not be executed at all. Second risk is that a limit order may be triggered and filled in a short period of time and then move in the same direction. In other words, trader estimates wrong exit point and regret to exit trade earlier.

1.3 Stop Orders
Let say, you want an order get executed once the bid/ask price reach to a a pre-defined price. This order mostly used to stop losses when price moves against their position. For example, a trader may give a sell stop order if the price of a stock drops to a pre-defined lower price (60p) than current price (65p).

Although, stop and limit orders looks like similar, there are different. In limit order, execution price wont be be lower(sell) or higher (buy) than predefined limit. In stop order, once order is triggered, the order is filled with market price. It can be higher or lower than pre-defined stop price.

Stop order is generally used with other order types. For example, with a market order, a trader may put a stop order above of a resistance to buy a stock or below of support point to short a stock.

Stops orders may accelerate price changes. Especially in illiquid markets, if there are lots of asymmetric stop orders, market makers or well informed traders would take advantage of it to trigger sudden huge sell or buy movement to obtain much unavailable stocks (by lowering price) or dumping stocks (by increasing prices). That practice is called tree shaking, and it is applied by market makers time to time to purchase unavailable stocks from public by reducing price of a stock suddenly and triggering stop orders (shake the tree) and then picking up the stocks (apples).

1.4 Limit Stop Orders
If a stop order is used with a limit order, it is called stop-limit order. Trader has to provide two prices in order: Stop price and limit price. Stop price is used for activation and limit price is used to limit cost of market limit. For example, a trader predicts a bull market once a resistance point is passed. But he also wants to limit his exposure to market move. For this one, he would place a stop limit order in which resistance point (maybe slightly higher) as stop price and limit price as 5% of stop price.

1.5 Market-If-Touched Orders
This order type is similar to limit orders with one difference. Once the specified price is touched, it is executed with market price. In that sense, it is similar to stop order but, it is in the opposite direction of current market price. That type of orders are not common, traders mostly use limit orders.

2- Trend Related orders
This type of orders utilise the trend or movement of price. For example, tick sensitive orders uses previous tick price. a previous price is higher than current price then it is a downtick or vice versa uptick. If price do not change between previous and last trade, it is a zerotick. So, an order can be structured in a way that, it is executed only in uptick(or downtick).

Trend related orders are generally used to neutralize market impact. For example, a sell uptick order makes sure, for each sell, another price moves the price up. It wont execute order if price goes up.

Actually, trend related orders are dynamic limit orders. For example, in buy downtick orders, limit price is dynamically adjusted to below difference last price. That type of orders are more effective when the tick size is large. That order type lost its popularity after decimalization of the US stock market in 2000. Tick size was one-sixteenth (6.25cents) before 2000, but it is decreased to 1 cents.

3- Expiry Related Orders
In addition to price related constraints, orders generally includes expiry related conditions, especially for limit and stop orders. These trades generally waits in order book to be matched.

Day orders are valid for the trading day and it is the most common expiry condition. When market is closed, the order expires.

Good-til-cancel (GTC) orders are stays in order book until it is cancelled manually by trader.

Good-until-orders stays in order book until a predefined date or period. Most common periods are week (Good-this-week) and month (Good-this-month).

Fill-or-kill orders are valid only when they are presented to the market. Unfilled part of the order is cancelled immediately.

Good-after-order get activated after a specified time in the order book.

Market-on-open orders are filled only in the opening session of a market which uses market open price.

Market-on-close orders are filled only in the closing session of a market which takes market closing price as basis.

4- Others

4.1 Market-not-Held Orders
Sometimes, a trader would leave trading strategy to the broker or floor specialist, as the broker or specialist is more experienced than trader. Broker comes up with execution plan to minimise trading cost for its client. In this order type, broker does not have legal responsibility to fill order in best prices, unlike market order.

4.2 All-or-None
In this type order, either whole size of requested trade is executed at once or not. A very large trade with a all-or-none order is generally negotiated between traders. As alternative to that, an order can specify minimum accepted quantity.

4.3 Spread Orders
When two different but correlated instrument is traded (sell and buy), a spread order can be used. Since, these instruments are related, there is a spread between them. An order can be constructed buy and sell instruments while the spread between these two instrument do not exceeds (or exceeds) a pre-defined limit.

4.4 Iceberg Orders
A very large order would have very adverse affect on market price if its size displayed on the market. Other traders would take advantage of this condition. To prevent it, display quantity of a large order can be limited to a smaller size, until it is fully filled.

Summary

- Standard order types are used for trading. For example, in FIX protocol, tag 40 (OrdType), following values are expected for an order.
1= Market
2=Limit
3=Stop
4=Stop limit
J=Market If Touched (MIT)
- Market orders executed immediately with current market prices and would cause market impact in large order.
- Limit orders supply liquidity by providing free trading option to other traders.
- While limit and market orders do not stabilize prices, stop orders can be used to destabilize the price.
- Various electronic automated trading strategies (for example iceberg, closing, open, spread) utilise these order types to minimize trading costs.

Below table summarizes types of market order.

Order Type

Usage

Effect on Liquidity

Price Contingencies

Advantages

Disadvantages

Market

Common

Demand immediate liquidity

None

Immediate execution

Uncertain price impact

Standing Limit

Common

Supply liquidity

Hard limit on price, a better or on limit price execution.

Limited price with on market impact

Uncertain execution

Marketable Limit

Common

Supply immediate liquidity

Hard limit on price, a better or on limit price execution.

Limited price impact

Limited price impact

Trend Related

Occasional

Supply liquidity

Must sell on uptrend or buy on downtrend

No price impact, dynamic with market

Uncertain price impact

Stop Market

Occasional

Demands liquidity when it is least available

Triggered when price touches or moves through the stop price;

Used to stop losses

Huge price impact

Stop limit

Rare

Triggered when liquidity is least available; offers liquidity on the side not needed.

Triggered when price touches or moves through the stop price and trade must be at or better than limit price.

Limit price impact upon a trigger

Uncertain execution

Market-if-Touched

Very Rare

Demand immediate liquidity and supplies resiliency

Triggered when price touches or moves through the touch price

Fast execution, upon a trigger

Uncertain price impact


Thursday, December 22, 2011

Reminiscences of a Stock Operator -- Quotes

When i first read the meaning of proclamation carved in a stone in Ephasus from Emperor Valentinian, Valens, and Gratian, somehow, it shocked me. That short text was indicating to me that, not so many things have been changed in terms of politics, power, taxing, corruption since it was carved, in 4th century (text can be found here). Some texts, books are timeless.

Reminiscences of a Stock Operator is one of them, I think. I have come cross that book several times in many reading list, but somehow, I skipped it. Until last month. Finally i decided to give a go to that book. And i regret that i have not read it before. I am amazed with its style and frankness and still contemporary, relevant content, after almost a century. It is a book, it deserves to be read more than once and if not, some parts must be noted down to go through time to time. Therefore, i extracted some quote from the book, for future references.

1

There is nothing new in Wall Street. There can't be because speculation is as old as the hills. Whatever happens in the stock market today has happened before and will happen again.

There is always a reason for fluctuations, but the tape does not concern itself with the why and wherefore. It doesn't go into explanations. I didn't ask the tape why when I was fourteen, and I don't ask it today, at forty. The reason for what a certain stock does today may not be known for two or three days,or weeks, or months. But what the dickens does that matter? Your business with the tape is now -- not tomorrow. The reason can wait.

2

I always made money when I was sure I was right before I began. What beat me was not having brains enough to stick to my own game -- that is, to play the market only when I was satisfied that precedents favored my play. There is a time for all things, but I didn't know it. And that is precisely what beats so many men in Wall Street who are very far from being in the main sucker class. There is the plain fool, who does the wrong thing at all times everywhere, but there is the Wall Street fool, who thinks he must trade all the time. No man can always have adequate reasons for buying or selling stocks daily or sufficient knowledge to make his. play an intelligent play.

The desire for constant action irrespective of underlying conditions is responsible for many losses in Wall Street even among the professionals, who feel that they must take home some money every day, as though they were working for regular wages. I was only a kid, remember. I did not know then what I learned later, what made me fifteen years later, wait two long weeks and see a stock on which I was very bullish go up thirty points before I felt that it was safe to buy it.

A stock operator has to fight a lot of expensive enemies within himself.

I don't know whether I make myself plain, but I never lose my temper over the stock market. I never argue with the tape. Getting sore at the market doesn't get you anywhere.

3

It takes a man a long time to learn all the lessons of all his mistakes. They say there are two sides to everything. But there is only one side to the stock market; and it is not the bull side or the bear side, but the right side.

I have been flat broke several times, but my loss has never been a total loss. Otherwise, I wouldn't be here now. I always knew I would have another chance and that I would not make the same mistake a second time. I believed in myself.

Speculation is a hard and trying business, and a speculator must be on the job all the time or he'll soon have no job to be on.

I owe my early success as a trader to them and not to brains or knowledge, because my mind was untrained and my ignorance was colossal. The game taught me the game. And it didn't spare the rod while teaching.

If somebody had told me my method would not work I nevertheless would have tried it out to make sure for myself, for when I am wrong only one thing convinces me of it, and that is, to lose money. And I am only right when I make money. That is speculating.

Ignorance at twenty-two isn't a structural defect.

If the unusual never happened there would be no difference in people and then there wouldn't be any fun in life. The game would become merely a matter of addition and subtraction. It would make of us a race of bookkeepers with plodding minds. It's the guessing that develops a man's brainpower. Just consider what you have to do to guess right.

4

Brokers always had the hope of getting away from me what I had taken from them. They regarded my winnings as temporary loans.

5

The first change I made in my play was in the matter of time. I couldn't wait for the sure thing to come along and then take a point or two out of it as I could in the bucket shops. I had to start much earlier if I wanted to catch the move in Fullerton's office. In other words, I had to study what was going to happen to anticipate stock movements. That sounds asininely commonplace, but you know what I mean. It was the change in my own attitude toward the game that was of supreme importance to me. It taught me, little by little, the essential difference between betting on fluctuations and anticipating inevitable advances and declines, between gambling and speculating.

I discovered that although I often was 100 per cent right on the market that is, in my diagnosis of conditions and general trend -- I was not making as much money as my market "rightness" entitled me to. Why wasn't I? There was as much to learn from partial victory as from defeat.

They say you never grow poor taking profits. No, you don't. But neither do you grow rich taking a four-point profit in a bull market. Where I should have made twenty thousand dollars I made two thousand. That was what my conservatism did for me.

Suckers differ among themselves according to the degree of experience. The tyro knows nothing, and everybody, including himself, knows it. But the next, or second, grade thinks he knows a great deal and makes others feel that way too. He is the experienced sucker, who has studied not the market itself but a few remarks about the market made by a still higher grade of suckers. The second-grade sucker knows how to keep from losing his money in some of the ways that get the raw beginner. It is this semisucker rather than the 100 per cent article who is the real all-the-year-round support of the commission houses. He lasts about three and a half years on an average, as compared with a single season of from three to thirty weeks, which is the usual Wall Street life of a first offender. It is naturally the semisucker who is always quoting the famous trading aphorisms and the various rules of the game. He knows all the don'ts that ever fell from the oracular lips of the old stagers excepting the principal one, which is: Don't be a sucker!

The customer would finish the tale of his perplexity and then ask: "What do you think I ought to do?" Old Turkey would cock his head to one side, contemplate his fellow customer with a fatherly smile, and finally he would say very impressively, "You know, it's a bull market!" Time and again I heard him say, "Well, this is a bull market, you know!" as though he were giving to you a priceless talisman wrapped up in a million-dollar accident-insurance policy. And of course I did not get his meaning.

What old Mr. Partridge said did not mean much to me until I began to think about my own numerous failures to make as much money as I ought to when I was so right on the general market. The more I studied the more I realized how wise that old chap was. He had evidently suffered from the same defect in his young days and knew his own human weaknesses. He would not lay himself open to a temptation that experience had taught him was hard to resist and had always proved expensive to him, as it was to me. I think it was a long step forward in my trading education when I realized at last that when old Mr. Partridge kept on telling the other customers, "Well, you know this is a bull market!" he really meant to tell them that the big money was not in the individual fluctuations but in the main movements that is, not in reading the tape but in sizing up the entire market and its trend.

After spending many years in Wall Street and after making and losing millions of dollars I want to tell you this: It never was my thinking that made the big money for me. It always was my sitting. Got that? My sitting tight! It is no trick at all to be right on the market. You always find lots of early bulls in bull markets and early bears in bear markets. I've known many men who were right at exactly the right time, and began buying or selling stocks when prices were at the very level, which should show the greatest profit. And their experience invariably matched mine -- that is, they made no real money out of it. Men who can both be right and sit tight are uncommon. I found it one of the hardest things to learn. But it is only after a stock operator has firmly grasped this that he can make big money. It is literally true that millions come easier to a trader after he knows how to trade than hundreds did in the days of his ignorance.

A man may see straight and clearly and yet become impatient or doubtful when the market takes its time about doing as he figured it must do. That is why so many men in Wall Street, who are not at all in the sucker class, not even in the third grade, nevertheless lose money. The market does not beat them. They beat themselves, because though they have brains they cannot sit tight.

Disregarding the big swing and trying to jump in and out was fatal to me. Nobody can catch all the fluctuations. In a bull market your game is to buy and hold until you believe that the bull market is near its end. To do this you must study general conditions and not tips or special factors affecting individual stocks.

One of the most helpful things that anybody can learn is to give up trying to catch the last eighth or the first. These two are the most expensive eighths in the world. They have cost stock traders, in the aggregate, enough millions of dollars to build a concrete highway across the continent.

Without faith in his own judgment no man can go very far in this game. That is about all I have learned to study general conditions, to take a position and stick to it. I can wait without a twinge of impatience. I can see a setback without being shaken, knowing that it is only temporary.

If I learned all this so slowly it was because I learned by my mistakes, and some time always elapses between making a mistake and realizing it, and more time between realizing it and exactly determining it.

6

It isn't a hunch but the subconscious mind, which is the creative mind, at work. That is the mind which makes artists do things without their knowing how they came to do them. Perhaps with me it was the cumulative effect of a lot of little things individually insignificant but collectively powerful.

I began to think of basic conditions instead of individual stocks. I promoted myself to a higher grade in the hard school of speculation. It was a long and difficult step to take.

7

People don't seem to grasp easily the fundamentals of stock trading. I have often said that to buy on a rising market is the most comfortable way of buying stocks. Now, the point is not so much to buy as cheap as possible or go short at top prices, but to buy or sell at the right time. When I am bearish and I sell a stock, each sale must be at a lower level than the previous sale. When I am buying, the reverse is true. I must buy on a rising scale. I don't buy long stock on a scale down, I buy on a scale up.

Remember that stocks are never too high for you to begin buying or too low to begin selling. But after the initial transaction, don't make a second unless the first shows you a profit. Wait and watch.

8

I was utterly free from speculative prejudices. The bear side doesn't appeal to me any more than the bull side, or vice versa. My one steadfast prejudice is against being wrong.

That is why I repeat that I never argue with the tape. To be angry at the market because it unexpectedly or even illogically goes against you is like getting mad at your lungs because you have pneumonia.

9

I have always played a lone hand. I began that way in the bucket shops and have kept it up. It is the way my mind works. I have to do my own seeing and my own thinking. But I can tell you after the market began to go my way I felt for the first time in my life that I had allies -- the strongest and truest in the world: underlying conditions. They were helping me with all their might.

It was an old trading theory of mine that when a stock crosses 100 or 200 or 300 for the first time the price does not stop at the even figure but goes a good deal higher, so that if you buy it as soon as it crosses the line it is almost certain to show you a profit. Timid people don't like to buy a stock at a new high record. But I had the history of such movements to guide me.

The only thing to do when a man is wrong is to be right by ceasing to be wrong.

My greatest discovery was that a man must study general conditions, to size them so as to be able to anticipate probabilities. In short, I had learned that I had to work for my money. I was no longer betting blindly or concerned with mastering the technic of the game, but with earning my successes by hard study and clear thinking.

My biggest winnings were not in dollars but in the intangibles: I had been right, I had looked ahead and followed a clear-cut plan. I had learned what a man must do in order to make big money; I was permanently out of the gambler class; I had at last learned to trade intelligently in a big way. It was a day of days for me.

10

If a man is both wise and lucky, he will not make the same mistake twice. But he will make any one of the ten thousand brothers or cousins of the original. The Mistake family is so large that there is always one of them around when you want to see what you can do in the fool-play line.

Losing money is the least of my trcubles. A loss never bothers me after I take it. I forget it overnight. But being wrong -- not taking the loss that is what does the damage to the pocketbook and to the soul.

Money creates needs or encourages their multiplication. I mean that after a man makes money in the stock market he very quickly loses the habit of not spending. But after he loses his money it takes him a long time to lose the habit of spending.

I would rather play commodities than stocks. There is no question about their greater legitimacy, as it were. It partakes more of the nature of a commercial venture than trading in stocks does. A man can approach it as he might any mercantile problem. It may be possible to use fictitious arguments for or against a certain trend in a commodity market; but success will be only temporary, for in the end the facts are bound to prevail, so that a trader gets dividends on study and observation, as he does in a regular business. He can watch and weigh conditions and he knows as much about it as anyone else. He need not guard against inside cliques. Dividends are not unexpectedly passed or increased overnight in the cotton market or in wheat or corn. In the long run commodity prices are governed but by one law -- the economic law of demand and supply. The business of the trader in commodities is simply to get facts about the demand and the supply, present and prospective. He does not indulge in guesses about a dozen things as he does in stocks. It always appealed to me trading in commodities.

Prices, like everything else, move along the line of least resistance. They will do whatever comes easiest, therefore they will go up if there is less resistance to an advance than to a decline; and vice versa.

The trend has been established before the news is published, and in bull markets bear items are ignored and bull news exaggerated, and vice versa.

A man has to guard against many things, and most of all against himself -- that is, against human nature. That is the reason why I say that the man who is right always has two forces working in his favor basic conditions and the men who are wrong.

In a narrow market, when prices are not getting anywhere to speak of but move within a narrow range, there is no sense in trying to anticipate what the next big movement is going to be up or down. The thing to do is to watch the market, read the tape to determine the limits of the get-nowhere prices, and make up your mind that you will not take an interest until the price breaks through the limit in either direction. A speculator must concern himself with making money out of the market and not with insisting that the tape must agree with him. Never argue with it or ask it for reasons or explanations. Stock-market post-mortems don't pay dividends.

It is surprising how many experienced traders there are who look incredulous when I tell them that when I buy stocks for a rise I like to pay top prices and when I sell I must sell low or not at all. It would not be so difficult to make money if a trader always stuck to his speculative guns -- that is, waited for the line of least resistance to define itself and began buying only when the tape said up or selling only when it said down. He should accumulate his line on the way up. Let him buy one-fifth of his full line. If that does not show him a profit he must not increase his holdings because he has obviously begun wrong; he is wrong temporarily and there is no profit in being wrong at any time. The same tape that said up did not necessarily lie merely because it is now saying NOT YET.

It is simple arithmetic to prove that it is a wise thing to have the big bet down only when you win, and when you lose to lose only a small exploratory bet, as it were. If a man trades in the way I have described, he will always be in the profitable position of being able to cash in on the big bet.

The average speculator has arrayed against him his own nature. The weaknesses that all men are prone to are fatal to success in speculation -- usually those very weaknesses that make him likable to his fellows or that he himself particularly guards against in those other ventures of his where they are not nearly so dangerous as when he is trading in stocks or commodities.

The speculator's chief enemies are always boring from within. It is inseparable from human nature to hope and to fear. In speculation when the market goes against you -- you hope that every day will be the last day and you lose more than you should had you not listened to hope -- to the same ally that is so potent a success-bringer to empire builders and pioneers, big and little. And when the market goes your way you become fearful that the next day will take away your profit, and you get out too soon. Fear keeps you from making as much money as you ought to. The successful trader has to fight these two deep-seated instincts. He has to reverse what you might call his natural impulses. Instead of hoping he must fear; instead of fearing he must hope. He must fear that his loss may develop into a much bigger loss, and hope that his profit may become a big profit. It is absolutely wrong to gamble in stocks the way the average man does.

I have been in the speculative game ever since I was fourteen. It is all I have ever done. I think I know what I am talking about. And the conclusion that I have reached after nearly thirty years of constant trading, both on a shoestring and with millions of dollars back of me, is this: A man may beat a stock or a group at a certain time, but no man living can beat the stock market!

11

A trader gets to play the game as the professional billiard player does -- that is, he looks far ahead instead of considering the particular shot before him. It gets to be an instinct to play for position.

That is one trouble about trading on a large scale. You cannot sneak out as you can when you pike along. You cannot always sell out when you wish or when you think it wise. You have to get out when you can; when you have a market that will absorb your entire line. Failure to grasp the opportunity to get out may cost you millions. You cannot hesitate. I f you do you are lost.

12

A man cannot be convinced against his own convictions, but he can be talked into a state of uncertainty and indecision, which is even worse, for that means that he cannot trade with confidence and comfort.

I have done nothing in my life but trade in stocks and commodities. I naturally think that if it is wrong to be bearish it must be right to be a bull. And if it is right to be a bull it is imperative to buy. As my old Palm Beach friend said Pat Hearne used to say, "You can't tell till you bet!" I must prove whether I am right on the market or not; and the proofs are to be read only in my brokers' statements at the end of the month.

I did precisely the wrong thing. The cotton showed me a loss and I kept it. The wheat showed me a profit and I sold it out. It was an utterly foolish play, but all I can say in extenuation is that it wasn't really my deal, but Thomas'. Of all speculative blunders there are few greater than trying to average a losing game. My cotton deal proved it to the hilt a little later. Always sell what shows you a loss and keep what shows you a profit.

It cost me millions to learn that another dangerous enemy to a trader is his susceptibility to the urgings of a magnetic personality when plausibly expressed by a brilliant mind. It has always seemed to me, however, that I might have learned my lesson quite as well if the cost had been only one million. But Fate does not always let you fix the tuition fee. She delivers the educational wallop and presents her own bill, knowing you have to pay it, no matter what the amount may be. Having learned what folly I was capable of I closed that particular incident. Percy Thomas went out of my life.

The hope of making the stock market pay your bill is one of the most prolific sources of loss in Wall Street. You will chip out all you have if you adhere to your determination. There isn't a man in Wall Street who has not lost money trying to make the market pay for an automobile or a bracelet or a motor boat or a painting. I could build a huge hospital with the birthday presents that the tight-fisted stock market has refused to pay for. In fact, of all hoodoos in Wall Street I think the resolve to induce the stock market to act as a fairy godmother is the busiest and most persistent.

13

It was improper and unwise for me as a speculator to allow myself to be influenced by any consideration to act against my own judgment. Noblesse oblige, but not in the stock market, because the tape is not chivalrous and moreover does not reward loyalty.

I was very lucky. I was rampantly bullish in a wild bull market. Things were certainly coming my way so that there wasn't anything to do but to make money. It made me remember a saying of the late H. H. Rogers, of the Standard Oil Company, to the effect that there were times when a man could no more help making money than he could help getting wet if he went out in a rainstorm without an umbrella. It was the most clearly defined bull market we ever had.

Nowhere does history indulge in repetitions so often or so uniformly as in Wall Street. When you read contemporary accounts of booms or panics the one thing that strikes you most forcibly is how little either stock speculation or stock speculators today differ from yesterday. The game does not change and neither does human nature.

A market does not culminate in one grand blaze of glory. Neither does it end with a sudden reversal of form. A market can and does often cease to be a bull market long before prices generally begin to break. My long expected warning came to me when I noticed that, one after another, those stocks which had been the leaders of the market reacted several points from the top and for the first time in many months -- did not come back. Their race evidently was run, and that clearly necessitated a change in my trading tactics.

It is enough for the experienced trader to perceive that something is wrong. He must not expect the tape to become a lecturer. His job is to listen for it to say "Get out!" and not wait for it to submit a legal brief for approval.

Then one day the entire market became quite weak and prices of all stocks began to fall. When I had a profit of at least four points in each and every one of the twelve stocks that I was short of, I knew that I was right. The tape told me it was now safe to be bearish, so I promptly doubled up... After I doubled up, I did not make another trade for a long time.

Never try to sell at the top. It isn't wise. Sell after a reaction if there is no rally.

In a bear market it is always wise to cover if complete demoralisation suddenly develops. That is the only way, if you swing a good-sized line, of turning a big paper profit into real money both quickly and without regrettable reductions. For instance, I was short fifty thousand shares of United States Steel alone. Of course I was short of other stocks, and when I saw I had the market to cover in, I lid. My profits amounted to about one and a half million dollars. It was not a chance to disregard.

14

We ran smack into a long moneyless period; four mighty lean years. There was not a penny to be made. As Billy Henriquez once said, "It was the kind of market in which not even a skunk could make a scent."

I have come to feel that it is as necessary to know how to read myself as to know how to read the tape. I have studied and reckoned on my own reactions to given impulses or to the inevitable temptations of an active market, quite in the same mood and spirit as I have considered crop conditions or analysed reports of earnings.

16

It has always seemed to me the height of dam foolishness to trade on tips. I suppose I am not built the way a tip-taker is. I sometimes think that tip-takers are like drunkards. There are some who can't resist the craving and always look forward to those jags which they consider indispensable to their happiness. It is so easy to open your ears and let the tip in. To be told precisely what to do to be happy in such a manner that you can eagily obey is the next nicest thing to being happy which is a mighty long first step toward the fulfilment of your heart's desire. It is not so much greed made blind by eagerness as it is hope bandaged by the unwillingness to do any thinking.

Old Baron Rothschild's recipe for wealth winning applies with greater force than ever to speculation. Somebody asked him if making money in the Bourse was not a very difficult matter, and he replied that, on the contrary, he thought it was very easy. I will tell you my secret if you wish. It is this: I never buy at the bottom and I always sell too soon."

17

The training of a stock trader is like a medical education. The physician has to spend long years learning anatomy, physiology, materia medica and collateral subjects by the dozen. He learns the theory and then proceeds to devote his life to the practice. He observes and classifies all sorts of pathological phenomena. He learns to diagnose. If his diagnosis is correct, and that depends upon the accuracy of his observation -- he ought to do pretty well in his prognosis, always keeping in mind, of course, that human fallibility and the utterly unforeseen will keep him from scoring 100 per cent of bull's eyes. And then, as he gains in experience, he. learns not only to do the right thing but to do it instantly, so that many people will think he does it instinctively. It really isn't automatism. It is that he has diagnosed the case according to his observations of such cases during a period of many years; and, naturally, after he has diagnosed it, he can only treat it in the way that experience has taught him is the proper treatment. You can transmit knowledge -- that is, your particular collection of card indexed facts, but not your experience. A man may know what to do and lose money -- if he doesn't do it quickly enough.

Observation, experience, memory and mathematics -- these are what the successful trader must depend on. He must bet always on probabilities -- that is, try to anticipate them. Years of practice at the game, of constant study, of always remembering, enable the trader to act on the instant when the unexpected happens as well as when the expected comes to pass.

I have found that experience is apt to be steady dividend payer in this game and that observation gives you the best tips of all. The behaviour of a certain stock is all you need at times. You observe it.

I never buy a stock even in a bull market, if it doesn't act as it ought to act in that kind of market.

An old broker once said to me: "If I am walking along a railroad track and I see a train coming toward me at sixty miles an hour, do I keep on walking on the ties? Friend, I sidestep. And I do not even pat myself on the back for being so wise and prudent."

Experiences had taught me to beware of buying a stock that refuses to follow the group-leader.

18

It was not difficult to be both fearless and patient. A speculator must have faith in himself and in his judgment. The late Dickson G. Watts, ex-President of the New York Cotton Exchange and famous author of "Speculation as a Fine Art," says that courage in a speculator is merely confidence to act on the decision of his mind. With me, I cannot fear to be wrong because I never think I am wrong until I am proven wrong. In fact, I am uncomfortable unless I am capitalising my experience. The course of the market at a given time does not necessarily prove me wrong. It is the character of the advance or of the decline that determines for me the correctness or the fallacy of my market position. I can only rise by knowledge. If I fall it must be by my own blunders.

19

The study of the psychology of speculators is as valuable as it ever was. Weapons change, but strategy remains strategy, on the New York Stock Exchange as on the battlefield. I think the clearest summing up of the whole thing was expressed by Thomas F. Woodlock when he declared: "The principles of successful stock speculation are based on the supposition that people will continue in the future to make the mistakes that they have made in the past."

In booms, which is when the public is in the market in the greatest numbers, there is never any need of subtlety, so there is no sense of. wasting time: discussing either manipulation or speculation during such times; it would be like trying to find the difference in raindrops that are falling synchronously on the same roof across the street. The sucker has always tried to get something for nothing, and the appeal in all booms is always frankly to the gambling instinct aroused by cupidity and spurred by a pervasive prosperity. People who look for easy money invariably pay for the privilege of proving conclusively that it cannot be found on this sordid earth. At first, when I listened to the accounts of old-time deals and devices I used to think that people were more gullible in the i86o's and '7o's than in the igoo's. But I was sure to read in the newspapers that very day or the next something about the latest Ponzi or the bust-up of some bucketing broker and about the millions of sucker money gone to join th,silent majority of vanished savings.

20

He did not argue with the tape is plain. He was utterly fearless but never reckless. He could and did turn in a twinkling, if he found he was wrong.

There is no question that advertising is an art, and manipulation is the art of advertising through the medium of the tape. The tape should tell the story the manipulator wishes its readers to see. The truer the story the more convincing it is bound to be, and the more convincing it is the better the advertising is. A manipulator today, for instance, has not only to make a stock look strong but also to make it be strong. Manipulation therefore must be based on sound trading principles. That is what made Keene such a marvellous manipulator; he was a consummate trader to begin with.

It is well to remember a rule of manipulation, a rule that Keene and his able predecessors well knew. It is this: Stocks are manipulated to the highest point possible and then sold to the public on the way down.

The first step in a bull movement in a stock is to advertise the fact that there is a bull movement on. Sounds silly, doesn't it? Well, think a moment. It isn't as silly as it sounded, is it? The most effective way to advertise what, in effect, are your honourable intentions is to make the stock active and strong. After all is said and done, the greatest publicity agent in the wide world is the ticker, and by far the best advertising medium is the tape.

It is always well to make it plain to the traders and to the public, also that there is a demand for the stock on the way down. That tends to check both reckless short selling by the professionals and liquidation by frightened holders which is the selling you usually see when a stock gets weaker and weaker, which in turn is what a stock does when it is not supported. These covering purchases of mine constitute what I call the stabilising process. As the market broadens I of course sell stock on the way up, but never enough to check the rise. This is in strict accordance with my stabilising plans. It is obvious that the more stock I sell on a reasonable and orderly advance the more I encourage the conservative speculators, who are more numerous than the reckless room traders; and in addition the more support I shall be able to give to the stock on the inevitable weak days. By always being short 'I always am in a position to support the stock without danger to myself. As a rule I begin my selling at a price that will show me a profit. But I often sell without having a profit, simply to create or to increase what I may call my riskless buying power. My business is not alone to put up the price or to sell a big block of stock for a client but to make money for myself. That is why I do not ask any clients to finance my operations. My fee is contingent upon my success.

21

The big money in booms is always made first by the public-on paper. And it remains on paper.

22

[In the boom of 1915] It got so that all a man had to do was to say that he had a friend who was a friend of a member of one of the Allied commissions and he would be offered all the capital needed to carry out the contracts he had not yet secured. I used to hear incredible stories of clerks becoming presidents of companies doing a business of millions of dollars on money borrowed from trusting trust companies, and of contracts that left a trail of profits as they passed from man to man. A flood of gold was pouring into this country from Europe and the banks had to find ways of impounding it.

The way business was done might have been regarded with misgivings by the old, but there didn't seem to be so many of them about. The fashion for gray-haired presidents of banks was all very well in tranquil times, but youth was the chief qualification in these strenuous times. The banks certainly did make enormous profits.

23

Manipulation of some sort enters into practically all advances in individual stocks and that such advances are engineered by insiders with one object in view and one only and that is to sell at the best profit possible.

24

The public ought always to keep in mind the elementals of stock trading. When a stock is going up no elaborate explanation is needed as to why it is going up. It takes continuous to buying to make a stock keep on going up. As long as it does so, with only small and natural reactions from time to time, it is a pretty safe proposition to trail along with it. But if after a long steady rise a stock turns and gradually begins to go down, with only occasional small rallies, it is obvious that the line of least resistance has changed from upward to downward. Such being the case why should any one ask for explanations? There are probably very good reasons why it should go down, but these reasons are known only to a few people who either keep those reasons to themselves, or else actually tell the public that the stock is cheap. The nature of the game as it is played is such that the public should realise that the truth cannot be told by the few who know.

Sunday, January 09, 2011

Java Concurrency In Practice


Although with new cutting edge devices, computing performance is increasing continuously, still high performance and low latency programming plays an important role to utilise these devices. Unlike other programming languages such as C/C++, Java provides compact, portable, optimum, intuitive, built-in components to take full advantage resources to provide high performance computing. In this text, i wont claim that Java provides better performance amongst other available languages (there are enough blogs about these benchmarks). I will focus on how to use Java to develop high performance application. Similar to some of my other articles, i will use a book, Java Concurrency in Practice, to explain concurrency in java.

Without a doubt, Java Concurrency in Practice is an unique book in this field. It is a must reading for every Java concurrency developer. The authors have first hand experience in developing concurrency and threading package at Sun and have enormous experience in practice.
Concurrency is not a new subject in software development. Similar to other engineering problems, utilising available resources with fairness and convenience is always being one of the main objective in software development. To utilise finite and costly resources with fairness and convenience, java provides a highly optimised, compact concurrency framework which includes threads, thread execution services, memory sharing facilities, concurrent or synchronized data structures, explicit and implicit locks , and atomic variable.

Threads

To take full advantage of available resources (CPU, memory, disk, network), multiple threads can be started in an application. A thread is similar to process but its life cycle is bounded with main thread (process) and managed by main threads. Each thread has its own stack for local variable and execution path but uses shared heap memory for global objects.

Threads provide parallel processing in application, simplicity of modelling, handling of asynchronous events, and more responsive feedback. Parallel processing is obtained by exploiting available (or by simulating) multiple processors. Dividing whole problem space or task into sub problem space or sub tasks and then assigning each part to a thread, naturally, is a simple modelling to solve complex problem. Since many resources are asynchronous, such as I/O resources, non-blocking threads would also provide high utilization of resources. One of other advantages of thread is responsiveness. By assigning specific tasks (UI interaction) to specific threads (GUI thread), a better response and throughput would be obtained.

Java uses different platform specific frameworks to implement threads (for example, PThreads for Linux) and therefore some limitation is platform specific. For example, there is a theoretical maximum number of threads in each platform. But in practice for high performance computing, number of thread is strictly correlated with number of available processor and idle time between subtasks executions.
Threads are very common in software development. Even an application does not use explicitly thread itself, libraries or frameworks used in this application use thread.

Risk of Threads

As explained above threads provides many benefits. But these benefits comes with cost of safety, liveness and performance.

Since many threads would access mutable shared objects and resources at same time, they would cause inconsistent object and resource states. Therefore, when accessing shared objects, a thread safe mechanism must be deployed to protect consistency of mutable object's states.

In order to provide thread safety, several mechanism, such as lock or synchronization are used. While these mechanism provides safety, they would also block or limit accessing resources therefore causes liveness issues. If threads are not managed very well, it would cause extra overhead, performance issues during context switching between threads and synchronization.

Thread Safety

If a shared and mutable object is shared by many thread at same time, state of this object can become inconsistent if proper synchronization is not applied over shared object, as many thread can access and modify the object at same time. In order to protect a consistent and deterministic state of object , one of the following has to be done:

  • Don't share the object across threads
  • Make the object is immutable
  • Use synchronization mechanisms to control access and modification of shared object by multiple objects.

In the book, thread safe is defined as follows:

"A class is thread-safe if it behaves correctly when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of those threads by the runtime environment, and with no additional synchronization or other coordination on the part of the calling code."

Good object oriented techniques such as encapsulation, immutability would improve thread safety. With encapsulation inner states of objects and synchronized blocks would be hidden from clients.

Thread safety can be obtained by implementing good synchronization mechanism with:

Implicit locking: Accessing a resource is blocked with implicit lock of an object. In java, each object holds an implicit lock. synchronized keyword is used to acquire implicit lock of an object. And with volatile keyword, memory visibility can be guaranteed.

void synchronized myMethod() {
// Access or modify shared state guarded by "this"
}

void static synchronized myMethod() {
// Access or modify shared state guarded by class itself
}

void synchronized (lock) {
// Access or modify shared state guarded by lock object
}

Explicit locking: In addition to implicit locks, java provides more flexible locking classes for various situations. Some of these classes are: ReentrantLock, Semaphore, ReentrantReadWriteLock, CountDownLatch, SynchronousQueue, and FutureTask

Atomic variables: Some operations, such as increasing a shared counter variable in a multithreaded environment must be atomic. By atomic, we mean, access and modify operation has to be done automatic. Java uses Compare and Swap (CAS) mechanism with underlying platform specific functions to implement atomicity. Some of these atomic classes are: AtomicInteger, AtomicLong, AtomicBoolean, AtomicReference

Built-in concurrent or synchronized data structures: Java provides various built-in thread safe data structures such as Vector, Hashtable, java.util.concurrent.*, ...

When a thread enters a synchronized or locked block, it must hold or acquire lock to enter the synchronized or locked block. Otherwise, threads are blocked on the lock. In Java locks are reentrant, in another words, if a thread holds a lock in a synchronized block and if reenters the block with same lock, the thread wont be blocked.

Sharing Objects

If mutable objects are not shared between threads, there wont be much headache in developing multi thread applications. In practice, there are not many multi-threaded applications which does not share mutable objects across threads.

When a mutable object is shared, it must be guarded to restrict modification at same time by multiple threads. Once modification is done, latest state of the shared object must be visible to other threads immediately. Visibility of a shared object is guaranteed with locks (implicit and explicit locks such as synchronized) and volatility keyword. In other words, for example, synchronized keywords does not only prevent multiple thread to access an object, but it also publish state of the object immediately to other threads once synchronized block is completed.

Without visibility and publication feature of synchronization, object can be in stale state where data may not be up to date for other threads. Nonetheless, when a thread reads a variable, it reads a value which is stored there by a thread (maybe be not latest data but it is not random data). This safety guarantee is called out-of-thin-air safety. This is guaranteed for all variable apart from 64 bit variable such as double and long. This variable has to be declared as volatile otherwise, JVM would fetch these variables in two 32 bit operations.

An object must be encapsulated well to not expose inner state. Otherwise, exposed or escaped inner state of an object would cause inconsistent state and invisibility issue across threads.

The most useful policies for using and sharing objects in a concurrent program are: Thread-confinement, immutability, (shared read-only),using built-in thread-safe objects, guarded by specific lock.

Thread Confinement and ThreadLocal

As mentioned before, If an object is not shared by multiple thread and accessed only by one thread, thread safety will be guaranteed automatically. This technique is called thread confinement. Thread confinement is extensively used in some frameworks such as Swing and JDBC.

Thread confinement can be implemented in various ways, two of which are very popular: Stack confinement and ThreadLocal. In the first method, shared objects are locally copied/cloned (therefore an instance of them is created in stack) before they are modified or accessed by a thread. But more formal and built-in confinement method is ThreadLocal which holds a copy of an object for a thread. In other words, it is a pair-value, container, which provides initialValue, set and get methods.

Immutability

Immutable objects can be used safely by any thread without additional synchronization, even when synchronization is not used to publish them. An object can be immutable either:
  • All its fields are final
  • Its state can not be changed after construction

.... To be Continued

Monday, December 06, 2010

Effective C++ (55 Specific Ways)

1: View C++ as a federation of languages
  • Rules for effective C++ programming vary, depending on the part of C++ ( (C, Object Oriented C++, Template C++, STL) you are using.
2: Prefer consts, enums, and inlines to #defines
  • For simple constants, prefer const objects or enums to #defines.
  • For function-like macros, prefer inline functions to #defines.
3: Use const whenever possible
  • Declaring something const helps compilers detect usage errors. const can be applied to objects at any scope, to function parameters and return types, and to member functions as a whole.
  • Compilers enforce bitwise constness, but you should program using conceptual constness.
  • When const and non-const member functions have essentially identical implementations, code duplication can be avoided by having the non-const version call the const version.
4: Make sure that objects are initialized before they're used
  • Manually initialize objects of built-in type, because C++ only sometimes initializes them itself.
  • In a constructor, prefer use of the member initialization list to assignment inside the body of the constructor. List data members in the initialization list in the same order they're declared in the class.
  • Avoid initialization order problems across translation units by replacing non-local static objects with local static objects.
5: Know what functions C++ silently writes and calls
  • Compilers may implicitly generate a class's default constructor, copy constructor, copy assignment operator, and destructor.
6: Explicitly disallow the use of compiler-generated functions you do not want
  • To disallow functionality automatically provided by compilers, declare the corresponding member functions private and give no implementations. Using a base class like Uncopyable is one way to do this.
7: Declare destructors virtual in polymorphic base classes
  • Polymorphic base classes should declare virtual destructors. If a class has any virtual functions, it should have a virtual destructor.
  • Classes not designed to be base classes or not designed to be used polymorphically should not declare virtual destructors.
8: Prevent exceptions from leaving destructors
  • Destructors should never emit exceptions. If functions called in a destructor may throw, the destructor should catch any exceptions, then swallow them or terminate the program.
  • If class clients need to be able to react to exceptions thrown during an operation, the class should provide a regular (i.e., non-destructor) function that performs the operation.
9: Never call virtual functions during construction or destruction
  • Don't call virtual functions during construction or destruction, because such calls will never go to a more derived class than that of the currently executing constructor or destructor.
10: Have assignment operators return a reference to *this
  • Have assignment operators return a reference to *this.
11: Handle assignment to self in operator=
  • Make sure operator= is well-behaved when an object is assigned to itself. Techniques include comparing addresses of source and target objects, careful statement ordering, and copy-and-swap.
  • Make sure that any function operating on more than one object behaves correctly if two or more of the objects are the same.
12: Copy all parts of an object
  • Copying functions should be sure to copy all of an object's data members and all of its base class parts.
  • Don't try to implement one of the copying functions in terms of the other. Instead, put common functionality in a third function that both call.
Item 13: Use objects to manage resources.
  • To prevent resource leaks, use RAII objects that acquire resources in their constructors and release them in their destructors.
  • Two commonly useful RAII classes are TR1::shared_ptr and auto_ptr. tr1::shared_ptr is usually the better choice, because its behavior when copied is intuitive. Copying an auto_ptr sets it to null.
Item 14: Think carefully about copying behavior in resource-managing classes.
  • Copying an RAII object entails copying the resource it manages, so the copying behavior of the resource determines the copying behavior of the RAII object.
  • Common RAII class copying behaviors are disallowing copying and performing reference counting, but other behaviors are possible.
Item 15: Provide access to raw resources in resource-managing classes.
  • APIs often require access to raw resources, so each RAII class should offer a way to get at the resource it manages.
  • Access may be via explicit conversion or implicit conversion. In general, explicit conversion is safer, but implicit conversion is more convenient for clients.
Item 16: Use the same form in corresponding uses of new and delete.
  • If you use [] in a new expression, you must use [] in the corresponding delete expression. If you don't use [] in a new expression, you mustn't use [] in the corresponding delete expression.
Item 17: Store newed objects in smart pointers in standalone statements.
  • Store newed objects in smart pointers in standalone statements. Failure to do this can lead to subtle resource leaks when exceptions are thrown.
Item 18: Make interfaces easy to use correctly and hard to use incorrectly
  • Good interfaces are easy to use correctly and hard to use incorrectly. Your should strive for these characteristics in all your interfaces.
  • Ways to facilitate correct use include consistency in interfaces and behavioral compatibility with built-in types.
  • Ways to prevent errors include creating new types, restricting operations on types, constraining object values, and eliminating client resource management responsibilities.
  • TR1::shared_ptr supports custom deleters. This prevents the cross-DLL problem, can be used to automatically unlock mutexes (see Item 14), etc.
Item 19: Treat class design as type design
  • Class design is type design. Before defining a new type, be sure to consider all the issues discussed in this Item.
Item 20: Prefer pass-by-reference-to-const to pass-by-value
  • Prefer pass-by-reference-to-const over pass-by-value. It's typically more efficient and it avoids the slicing problem.
  • The rule doesn't apply to built-in types and STL iterator and function object types. For them, pass-by-value is usually appropriate.
Item 21: Don't try to return a reference when you must return an object
  • Never return a pointer or reference to a local stack object, a reference to a heap-allocated object, or a pointer or reference to a local static object if there is a chance that more than one such object will be needed. (Item 4 provides an example of a design where returning a reference to a local static is reasonable, at least in single-threaded environments.)
Item 22: Declare data members private
  • Declare data members private. It gives clients syntactically uniform access to data, affords fine-grained access control, allows invariants to be enforced, and offers class authors implementation flexibility.
  • protected is no more encapsulated than public.
Item 23: Prefer non-member non-friend functions to member functions
  • Prefer non-member non-friend functions to member functions. Doing so increases encapsulation, packaging flexibility, and functional extensibility.
Item 24: Declare non-member functions when type conversions should apply to all parameters
  • If you need type conversions on all parameters to a function (including the one pointed to by the this pointer), the function must be a non-member.
Item 25: Consider support for a non-throwing swap
  • Provide a swap member function when std::swap would be inefficient for your type. Make sure your swap doesn't throw exceptions.
  • If you offer a member swap, also offer a non-member swap that calls the member. For classes (not templates), specialize std::swap, too.
  • When calling swap, employ a using declaration for std::swap, then call swap without namespace qualification.
  • It's fine to totally specialize std templates for user-defined types, but never try to add something completely new to std.
Item 26: Postpone variable definitions as long as possible.
  • Postpone variable definitions as long as possible. It increases program clarity and improves program efficiency.
Item 27: Minimize casting.
  • Avoid casts whenever practical, especially dynamic_casts in performance-sensitive code. If a design requires casting, try to develop a cast-free alternative.
  • When casting is necessary, try to hide it inside a function. Clients can then call the function instead of putting casts in their own code.
  • Prefer C++-style casts to old-style casts. They are easier to see, and they are more specific about what they do.
Item 28: Avoid returning "handles" to object internals.
  • Avoid returning handles (references, pointers, or iterators) to object internals. It increases encapsulation, helps const member functions act const, and minimizes the creation of dangling handles.
Item29: Strive for exception-safe code.
  • Exception-safe functions leak no resources and allow no data structures to become corrupted, even when exceptions are thrown. Such functions offer the basic, strong, or nothrow guarantees.
  • The strong guarantee can often be implemented via copy-and-swap, but the strong guarantee is not practical for all functions.
  • A function can usually offer a guarantee no stronger than the weakest guarantee of the functions it calls.
Item 30: Understand the ins and outs of inlining.
  • Limit most inlining to small, frequently called functions. This facilitates debugging and binary upgradability, minimizes potential code bloat, and maximizes the chances of greater program speed.
  • Don't declare function templates inline just because they appear in header files.
Item31: Minimize compilation dependencies between files.
  • The general idea behind minimizing compilation dependencies is to depend on declarations instead of definitions. Two approaches based on this idea are Handle classes and Interface classes.
  • Library header files should exist in full and declaration-only forms. This applies regardless of whether templates are involved.
Item 32: Make sure public inheritance models "is-a."
  • Public inheritance means "is-a." Everything that applies to base classes must also apply to derived classes, because every derived class object is a base class object.
Item 33: Avoid hiding inherited names
  • Names in derived classes hide names in base classes. Under public inheritance, this is never desirable.
  • To make hidden names visible again, employ using declarations or forwarding functions.
Item 34: Differentiate between inheritance of interface and inheritance of implementation
  • Inheritance of interface is different from inheritance of implementation. Under public inheritance, derived classes always inherit base class interfaces.
  • Pure virtual functions specify inheritance of interface only.
  • Simple (impure) virtual functions specify inheritance of interface plus inheritance of a default implementation.
  • Non-virtual functions specify inheritance of interface plus inheritance of a mandatory implementation.
Item 35: Consider alternatives to virtual functions
  • Alternatives to virtual functions include the NVI idiom and various forms of the Strategy design pattern. The NVI idiom is itself an example of the Template Method design pattern.
  • A disadvantage of moving functionality from a member function to a function outside the class is that the non-member function lacks access to the class's non-public members.
  • tr1::function objects act like generalized function pointers. Such objects support all callable entities compatible with a given target signature.
Item 36: Never redefine an inherited non-virtual function
  • Never redefine an inherited non-virtual function.
Item 37: Never redefine a function's inherited default parameter value
  • Never redefine an inherited default parameter value, because default parameter values are statically bound, while virtual functions — the only functions you should be overriding — are dynamically bound.
Item 38: Model "has-a" or "is-implemented-in-terms-of" through composition
  • Composition has meanings completely different from that of public inheritance.
  • In the application domain, composition means has-a. In the implementation domain, it means is-implemented-in-terms-of.
Item 39: Use private inheritance judiciously
  • Private inheritance means is-implemented-in-terms of. It's usually inferior to composition, but it makes sense when a derived class needs access to protected base class members or needs to redefine inherited virtual functions.
  • Unlike composition, private inheritance can enable the empty base optimization. This can be important for library developers who strive to minimize object sizes.
Item 40: Use multiple inheritance judiciously
  • Multiple inheritance is more complex than single inheritance. It can lead to new ambiguity issues and to the need for virtual inheritance.
  • Virtual inheritance imposes costs in size, speed, and complexity of initialization and assignment. It's most practical when virtual base classes have no data.
  • Multiple inheritance does have legitimate uses. One scenario involves combining public inheritance from an Interface class with private inheritance from a class that helps with implementation.
Item 41: Understand implicit interfaces and compile-time polymorphism
  • Both classes and templates support interfaces and polymorphism.
  • For classes, interfaces are explicit and centered on function signatures. Polymorphism occurs at runtime through virtual functions.
  • For template parameters, interfaces are implicit and based on valid expressions. Polymorphism occurs during compilation through template instantiation and function overloading resolution.
Item 42: Understand the two meanings of typename
  • When declaring template parameters, class and typename are interchangeable.
  • Use typename to identify nested dependent type names, except in base class lists or as a base class identifier in a member initialization list.
Item 43: Know how to access names in templatized base classes
  • In derived class templates, refer to names in base class templates via a "this->" prefix, via using declarations, or via an explicit base class qualification.
Item 44: Factor parameter-independent code out of templates
  • Templates generate multiple classes and multiple functions, so any template code not dependent on a template parameter causes bloat.
  • Bloat due to non-type template parameters can often be eliminated by replacing template parameters with function parameters or class data members.
  • Bloat due to type parameters can be reduced by sharing implementations for instantiation types with identical binary representations.
Item 45: Use member function templates to accept "all compatible types."
  • Use member function templates to generate functions that accept all compatible types.
  • If you declare member templates for generalized copy construction or generalized assignment, you'll still need to declare the normal copy constructor and copy assignment operator, too.
Item 46: Define non-member functions inside templates when type conversions are desired
  • When writing a class template that offers functions related to the template that support implicit type conversions on all parameters, define those functions as friends inside the class template.
Item 47: Use traits classes for information about types
  • Traits classes make information about types available during compilation. They're implemented using templates and template specializations.
  • In conjunction with overloading, traits classes make it possible to perform compile-time if...else tests on types.
48: Be aware of template metaprogramming
  • Template metaprogramming can shift work from runtime to compile-time, thus enabling earlier error detection and higher runtime performance.
  • TMP can be used to generate custom code based on combinations of policy choices, and it can also be used to avoid generating code inappropriate for particular types.
49: Understand the behavior of the new-handler
  • set_new_handler allows you to specify a function to be called when memory allocation requests cannot be satisfied.
  • Nothrow new is of limited utility, because it applies only to memory allocation; subsequent constructor calls may still throw exceptions.
50: Understand when it makes sense to replace new and delete
  • There are many valid reasons for writing custom versions of new and delete, including improving performance, debugging heap usage errors, and collecting heap usage information.
51: Adhere to convention when writing new and delete
  • operator new should contain an infinite loop trying to allocate memory, should call the new-handler if it can't satisfy a memory request, and should handle requests for zero bytes. Class-specific versions should handle requests for larger blocks than expected.
  • operator delete should do nothing if passed a pointer that is null. Class-specific versions should handle blocks that are larger than expected.
52: Write placement delete if you write placement new
  • When you write a placement version of operator new, be sure to write the corresponding placement version of operator delete. If you don't, your program may experience subtle, intermittent memory leaks.
  • When you declare placement versions of new and delete, be sure not to unintentionally hide the normal versions of those functions.
53: Pay attention to compiler warnings.
  • Take compiler warnings seriously, and strive to compile warning-free at the maximum warning level supported by your compilers.
  • Don't become dependent on compiler warnings, because different compilers warn about different things. Porting to a new compiler may eliminate warning messages you've come to rely on.
54: Familiarize yourself with the standard library, including TR1
  • The primary standard C++ library functionality consists of the STL, iostreams, and locales. The C99 standard library is also included.
  • TR1 adds support for smart pointers (e.g., tr1::shared_ptr), generalized function pointers (tr1::function), hash-based containers, regular expressions, and 10 other components.
  • TR1 itself is only a specification. To take advantage of TR1, you need an implementation. One source for implementations of TR1 components is Boost.
55: Familiarize yourself with Boost.
  • Boost is a community and web site for the development of free, open source, peer-reviewed C++ libraries. Boost plays an influential role in C++ standardization.
  • Boost offers implementations of many TR1 components, but it also offers many other libraries, too.