Sunday, 23 December 2012

Magic of Floating Point Representation in the Digital World

Many great engineering and scientific advances of recent decades would not have been possible without the floating-point capabilities of digital computers. Still, some results of floating-point calculations look pretty strange, even to people with years of mathematical experience. I will attempt to explain the causes of some of these strange results and give some suggestions where appropriate.
Lets take a look at the following example :



groovy> System.out.println(100.87d * 0.01d)
1.0087000000000002    --> Oops ! What is happening here ?
groovy>

We all know 100.87 multiplied by 0.01 is 1.0087. How is this junk introduced. Lets look at the following statements to add more confusion:

groovy> System.out.println(100.87f * 0.01f)
1.0087000049196178
groovy> 
groovy> System.out.println(100.87 * 0.01)
1.0087
groovy>

To answers these questions lets go to the basics.
How are floating point numbers represented in the modern computer systems and how is arithmetic done. As we all know, floating point numbers are represented in binary format which follows IEEE standard for floating point arithmetic. 
The format for single precision numbers uses 32 bits divided in the following way,
     seeeeeeeefffffffffffffffffffffff
     
     s = sign bit, 1 bit
     e = exponent, 8 bits  (E_min=-126, E_max=127, bias=127)
     f = fraction, 23 bits
The format for double precision numbers uses 64 bits divided in the following way,
     seeeeeeeeeeeffffffffffffffffffffffffffffffffffffffffffffffffffff
     
     s = sign bit, 1 bit
     e = exponent, 11 bits  (E_min=-1022, E_max=1023, bias=1023)
     f = fraction, 52 bits

The above problem is because some numbers can't be represented exactly in this format.

(100.87)10 = (1100100.11011110101110000101000111101011100001010001111010111000010100.........)2

When we say 100.87d, it means 100.87 is stored in a double variable which has 64 bit precision and when we say 100.87f, it means 100.87 is stored in a float variable, which has 32 bit precision. So even though 100.87 is a real number, it can't be exactly represented in binary format. This poses a big challenge to programmers in financial domain where accuracy is very important. So if you normalizing a price of 100.87 with a scale of 0.01, you would introduce noise as seen above. Mostly what programmers do is rounding to some significant digits based on the requirement. 


A key feature of the IEEE standard is that it requires correctly rounded arithmetic operations. Very often, the result of an arithmetic operation on two floating point numbers is not a floating point number. This is most obviously the case for multiplication and division; for example, 1 and 10 are both floating point numbers but 1/10 is not, regardless of where the single or double format is in use. It is also true of addition and subtraction.

Let x and y be floating point numbers, let +,−,* ,/ denote the four standard arithmetic operations, and let (+),(-), (*), (/) denote the corresponding operations as they are actually implemented on the computer. Thus, x + y may not be a floating point number, but x (+) y is the floating point number which is the computed approximation of x + y. When the result of a floating point operation is not a floating point number, the IEEE standard requires
that the computed result is the rounded value of the exact result. It is worth stating this requirement carefully. The rule is as follows: if x and y are floating point numbers, then
x (+) y = round(x + y);
x (-) y = round(x − y);
x (*) y = round(x * y);
and
x (/) y = round(x / y)
where round is the operation of rounding to the nearest floating point number in the single or double format, whichever is in use.

Now lets explain the behaviors seen in my cases presented at the beginning.
groovy> System.out.println(100.87d * 0.01d)
1.0087000000000002
groovy> System.out.println(100.87f * 0.01f)
1.0087000049196178
groovy> 

100.87 is not exactly representable in binary, same is the case with 0.01. We represent an approximation of these numbers in binary. The rule is if a number is not exactly representable, then it must be approximated by one of the nearest representable values. So, when we are multiplying 100.87 with 0.01 in the above case, we actually are multiplying some approximation of these numbers and that's why we the junk. This is expected. But what about the below case. What is happening here ?
groovy> System.out.println(100.87 * 0.01)
1.0087
groovy>

In the above case we are not storing the intermediate values in any variable and hence no rounding is done. They are stored in registers and hence we are getting better precision. To prove my point analyze the following cases:
groovy> System.out.println((float)(100.87f * 0.01f))
1.0087
groovy> System.out.println(100.87f * 0.01f)
1.0087000049196178
groovy>

In the 1st case you are putting the value in a float variable and then printing. In the 2nd case, you are directly printing the value in the registers.

Saturday, 8 December 2012

HipHop for PHP

HipHop for PHP is a source code transformer for PHP script code. Hiphop for PHP is a set of PHP execution engines. HipHop started as project at Facebook Inc. and was later made open source. Till date, facebook has achieved 6X reduction in CPU utilization for its site using HipHop as compared to Apache.

One of the design of HipHop was to write complex logic with PHP. Since PHP is an interpreted language, it is bound to be slow. Companies like Facebook which have large PHP codebases would have to write their complex functionality with extensions in C or C++. This would lead to lesser amount of resources which could work on the complete codebase. By continuing to use PHP with HipHop Facebook is able to maintain a high number of engineers who can work on the whole codebase.

HipHop has evolved over the years. Initially it started as 'HPHPc' which translates the PHP code to C++ code and  then passes it to gcc which compiles it into one monolithic binary representing the site's entire code tree. This gave significant performance gains, but it was horrible for development. The most important reason to use PHP is to avoid having to recompile for every small change, since PHP is a interpreted language.

'HPHPi' came hereafter as an interactive version of HPHP. It had the feature to automatically recompile on changing the behavior, but it led to different behavior in development and production machine which is risky.

HipHop Virtual Machine (HHVM)was created with an intent to replace both HPHPc and HPHPi. HHVM does not call gcc unlike its predecessors. HHVM transforms PHP code to byte code just as regular PHP does. It differs from the regular PHP due to its optimizer and JIT(Just in Time Compilation).

The following article gives a complete picture of HipHop VM :
https://www.facebook.com/note.php?note_id=10150415177928920

Sunday, 2 December 2012

Algorithmic Trading - Part 1

Algorithmic Trading is the use of electronic platform for placing trading orders with an algorithm deciding the timing, price and size of orders. High Frequency Trading (HFT) is a special class of algorithmic trading in which trading systems make trading decisions based on the information they receive electronically before human traders are capable of processing the information they observe. 

Who is suited for Algorithmic Trading ?

The most important reason for going for algorithmic trading is to control the market impact while placing big orders and thus seek favorable prices. Anyone who places big orders needs to worry about the market impact. As a thumb rule, if the size of order exceeds the sum of best 5 levels you need to worry about the market impact.  Big institutions like pension fund managers, investment banks, hedge funds, etc. are the ones who place Algo orders.

 Lets see how algorithmic trading minimizes market impact. All the orders placed can be classified either as Market Orders or Limit Orders. Markets orders want the exchange to execute the order in the best possible price. So, if you want to buy, the exchange will find the seller who is willing to sell at the lowest possible price and execute the order. Limit Ordrs on th other hand specify the limit on the negative side. These limit orders are not executed right away and they stay in the book for some time. These live but non executed orders form what is called the order book. Lets say I place a Market Order to buy 1 million Yahoo shares. The exchange will try to find the best seller which is selling at the least price and then the next best and so on, till the I get 1 million shares. This means that the execution price is going to be poor. On the other hand if I place a limit order. Now, the exchange will execute whatever it can but it will stop as soon as there are no more sell side orders with favorable price. Once this is done, your order becomes part of order book.  Once everyone, including other algorithms, sees such a massive order sitting on the buy side, price is bound to rise sharply.

However, algorithmic trading has generally been a sell-side product. This means that institutional brokers are the ones who actually have the systems for placing and managing algorithmic orders. Institutional traders place Algo orders with brokers who submit the orders either programatically from automated systems or manually through user interfaces. Institutional brokers and investors have invested a lot of time and money on Algo trading systems over the years.

Lets look at one of the simplest algorithmic strategies :

TWAP (Time Weighted Average Price) trading strategy :

TWAP breaks a big orders into smaller chucks of orders which are executed after a fixed interval. But a simple algorithm or even a smart trader can figure out this pattern by looking at the order book. So, nearly all production TWAP algorithms involve some type of randomization. This randomization can be done in terms of the sizes of orders or in terms of the intervals between the orders or both. 

The basic rule is to start with an initial order size and keep increasing or decreasing the size of order. The first question that comes to mind is when to increase and when to decrease the size of order. The answer depends on whether the market is in mean reversion mode or is trending towards one direction. If you a buyer and the price continues to drop, then it makes sense to wait for some time as the price is going to be more lucrative. On the other hand, if the market is in mean reversion mode, the trend is not going to last for ever and so we need to increase the size whenever price becomes favorable.

The next question is how to predict the market. Well, truly speaking, nobody can predict the market. We just try to be correct more often than we are wrong. This is where high frequency trading comes into picture which helps us to predict the market by analyzing the market trends quickly.

Saturday, 3 November 2012

The Mystery about Sensex

I really wonder why people in India always talk about Sensex without knowing what it is and what is indicates ? Forget about common people, even news channels and financial newspaper always talk about Sensex. Most of the time if you switch to a news channel, they would be talking about Sensex going up or down and predicting India's economic condition by analyzing the Sensex. Sensex is the most tracked and the most talked about index in India. But what is surprising is that it is also least understood index.

What is Sensex ?

Sensex stands for Sensitive Index which is used for Bombay Stock Exchange (BSE). Sensex which is also popularly known as BSE 30 in the financial world is a free-float market-capitalization of 30 well established companies listed on Bombay Stock Exchange. In other words, Sensex tracks price movements of 30 stocks on BSE. During market hours, prices of the index scrips, at which latest trades are executed, are used by the trading system to calculate Sensex  every 15 seconds and disseminate in real time. Sensex makes sense only for Index Fund managers and I don't understand why common man worries about Sensex. Sensex is just a virtual show window of Bombay Stock Market.

Sensex Calculation :

Foremost question which arises is how are these 30 companies selected ?
The top 30 companies with highest free market capitalization are selected. Free market capitalization considers only the shares which are not locked in or the shares which are available for trading. It excludes the shares which are locked by promoters or other strategic investors which can not be traded.
So,
 Free Market Capitalization = Total shares available for trading X Current Market Price

Its clear from the above formula that free market capitalization is dynamic due to the changing market price.
BSE reviews the list of stocks periodically. This essentially means that some stocks are moved out of the Sensex calculation and some are moved in after every review. The 30 stocks selected by the above process are given some weightage  as per its free market capitalization. The base value of Sensex is 100 as of April 1, 1979. The level of the Sensex at any point of time thus reflects the collective free float value of 30 component stocks, relative to the base period. So, a Sensex level of 30000 indicates that the Capitalization of current top 30 stocks is 300 times the Capitalization of top 30 stocks as of the base year.


Myths about Sensex :

1. Sensex mirrors the performance of all the sectors in the Indian ecomony.

The top 30 companies used in the calculation of Sensex do not have nay sectoral quota. These 30 companies mostly include IT companies and Oil & Natural gas companies. Sectors such aviation, gem processing do not have any representation in the Sensex. Hence Sensex can't indicate the performance of all the sectors in the Indian economy.

2. Sensex is an indicator of India's economic growth.

Often Sensex is taken an indicator of India's economic growth. It should be noted that Sensex tracks only the companies which are listed on Bombay Stock Exchange (BSE). Many Indian companies are not listed on BSE. Hence it foolish to consider Sensex as a true indicator of India's economic growth.

3. Sensex tracks performance of all companies listed on BSE.

Sensex tracks only a small set  of 30 companies with the highest Market Capitalization, but there are around 3600 companies listed on BSE. So, even if Sensex rises, many listed individual stocks may have lost value and this scenario has happened many times. In fact on October 29, 2007, The day Sensex touched 20000, 2123 out of 3113 companies declined in value. 

4. Sensex captures price movement of all these 30 companies.

Again wrong. The 30 companies used in the calculation are not given equal weightage. In fact, top 30 companies used is the calculation of Sensex have more than 65% weightage. Thus the rise and fall of these 10 stocks considerably impact the rise and fall of Sensex than the last 20 stocks.

Conclusion :

Sensex is not an unbiased true indicator of top 30 stocks listed on BSE let alone entire stock market and Indian economy. Retail investors should not look at the Sensex and rush into investing in the stock market. A little known fact about Sensex is that its base year has been changed (it was decreased). So, just by decreasing the base year, you can raise the Sensex and this was exactly the motive behind decreasing the base year. The top 30 companies which are used in the Sensex calculation today, were not even present at the time of the base year. So, whats the purpose of this index. In my opinion this base year should be raised at least to the nineties to make some sense.

Saturday, 27 October 2012

Trading Systems - Part 1

Trading Systems are simply set of rules that traders use to determine their entry and exit from a position. In an ideal condition, traders should be like machines which have no emotions. But this is an ideal case and there is practically no emotionless human being. Now the question is what stops us from building a machine which trades.

What is a Trading System ?

A trading system is just a strategy which you develop to trade. You might need different trading systems for different markets because the strategy you use to trade in one market may not be applicable to another market. A trading system consists of 8 parts :
1. A Market Filter
2. Set up conditions
3. Time Frame
4. Entry signal
5. Protective Stop
6. Re-entry Strategy
7. Exit Strategy
8. A position sizing algorithm

A Market Filter : 

A market filter is a way of looking at your market to determine whether this market is appropriate for your system. We can have bullish markets, we can bearish markets, we can have flat markets etc. Your trading system might work well only in one of these market conditions. As a result you need a filter to determine whether your trading system should trade in such market conditions.

Set Up Conditions :

This is your screening criteria. For example, if you trade stocks, there would be 10000+ stocks available that you might decide to invest in at a time.As a result, trading systems employ a series of screening criteria to bring this number down to around 100 stocks. You might have a component in trading system to watch the stock to go down for 7 consecutive days before entry. 

Time Frame :

The first thing when you are developing a trading system is to decide what kind of trader you are. Are you a day trader or a swing trader ? Do you look at charts everyday, every week, every month or every year. How long do you want to hold on to your positions ? This will help determine which time frame you will use to trade.Though you still will be looking at multiple time frames, this will be the main time frame you will use when looking for a trade signal.

Entry Signal :

It is signal that tell you when you might enter a position - either long or short. Entry signal depends on entry rules. The simplest way to generate an entry signal is to employ moving average cross over system. The dual moving average crossover (DMAC) is a simple entry rule. If you employ this rule, a 'buy' entry signal is generated when the shorter term moving average crosses the shorter term moving average. Many traders seem to despise such simple procedures and prefer to use more sophisticated rules.

Protective Stop : 

This is one of the most important components of a trading system. This defines the worst case loss that you would want to experience. Your stop might be some value that would keep you in the stock for a long time(e.g. a 25% drop in the price of the stock) or something that will get you out quickly if the market turns against you(e.g. a 25% drop). Without having proper protective stop, your trading system can incur heavy losses. 

Re-entry Strategy :

Quite Often when you get stopped out of a position, the stock will turn around in the direction that favors your old position. When this happens you might have a perfect chance for profits that is not covered by your original set-up and entry conditions. As a result, you need to think about re-entry criteria. When might you want to get back into a closed out position ? Under what conditions would this be feasible and what criteria would trigger your re-entry ?

Exit Strategy : 

Just as we have a rule which generates a signal which tells us when to enter a market, there is a rule which generates a signal which tells us when to exit a market. Sometimes you might have to exit from a winning position and sometimes you might have to exit from a loosing position. In fact in most of the profitable trading systems, only 25 - 30 % of the trades are profitable. This is the most critical part of the trading system. You must spend a great deal of time on exit strategies. For exits, you have different options. You can either trail your stop, or have a set target and exit  when the price hits that target.


Position Sizing Algorithm : 

A position sizing algorithm just tell you 'how much' and 'how big' of a position to take. Position Sizing algorithm is a key factor in whether or not you stay in the game, or whether your gains are huge or minimal. In its simplest form, it boils down to : how many shares to trade, when to increase, when to decrease, when to take profits and so on. 

Advantages of Trading System :

Emotionless :  A trading system takes all emotions out of trading which is often cited as one of the biggest flaws in individual traders. 

Save Time : A trading system saves a lot of time which a trader would spend on analysis. A trading system can even directly place trading orders without the intervention of a trader.

Disadvantages of a Trading System : 

If a trading system would be without any disadvantages, it would be like a money making machine. But this is not the case. Lets look at the other side of the coin.

Complexity : Trading systems are very complex. You require solid technical expertise in the development phase as well as afterwards to make any changes in strategies. 

Transaction Costs : These are the costs incurred when do a transaction through a trading system. These are more than commission costs.

Time consuming in the development phase : It takes a lot of time to develop a trading system. You have to come up with strategies and then code your strategies. While coding you would have to come up with very efficient code since the data that you use to trade can be huge and you have to process the data efficiently. Also you are directly dealing with money, so it needs rigorous testing. 

Saturday, 20 October 2012

A View on the Transition from Academia To Finance

The other day I was going through an article by Catherine O'Neil from D.E. Shaw group about her transition from academia to finance which I found very interesting. I am sharing that edited article here. First I would like to introduce Catherine.After earning a degree in mathematics from UC Berkeley in 1994, Catherine went to Harvard as a graduate student where she studied number theory and graduated with a Ph.D. in 1999. Then she went to the Massachusetts Institute of Technology as a Moore instructor and then a second postdoc. She started working for DE Shaw group in June of 2007.
Now on to the answers from Catherine:

Why leave Academia ? Why finance ? Why the D.E. Shaw group ?

I found the time scale of academic life frustrating. What started as a moment of insight would take years to get published and disseminated, and that's if you are lucky.
     Finance is a huge and rapidly growing, sexy new field which combines the newest technology with the invention of mathematics to deal with ever growing abundant data. It is the essence of modernity,
and paired with New York City’s infinite energy, I found it extremely attractive. It was really as
simple as that—I didn’t actually know any finance when I decided to apply.
     I first heard of the D. E. Shaw group when Eric Wepsic, my high school math friend, chose to leave Harvard math graduate school to work at this company way back in 1994. Eric would send me emails every year or so, asking if I knew anyone interested in working there, and one day I wrote back and said, “How about me?” The D. E. Shaw group was particularly appealing because it is known for being a well-run company, and since I had decided to try my hand at business, I wanted to start at a good one. Although some people apply to the D. E. Shaw group because it’s known as being pretty academic, I think this is not an appropriate line of reasoning: get a job here if you want to be in business and not academics.

What is D.E. Shaw group like ?


There are a number of groups here and some of them, including my group, focus on the systematic quantitative investment strategies that made the D. E. Shaw group famous, while others
work on more fundamental strategies. I work as a quantitative analyst in a group consisting of about twenty traders, quants, and programmers. A group can be thought of as being similar to an academic department at a university. Groups differ by the type of financial instrument they trade or the means used to approach the trading of a common instrument. Frequently, groups overlap in the type of instrument traded but each group
has its own way of looking at the market.
     In the world of finance, the D. E. Shaw group is special. For example, we have no dress code. Personally I don’t really care about that, but this flexibility has allowed us to attract a number of really exceptional people for whom this is important. More importantly, we are not expected to work insane hours, which is great for me and my young family. When I say not insane, I should mention I work about 9.5 hours a day, five days a week, which is definitely more time than I spent in my office as an academic.

What do you do there ?


On a daily scale, my time in largest to smallest allotments is spent writing code to test models, writing up projects, talking to my manager, talking to other quants in my group, attending or giving a weekly seminar, learning techniques and thinking of new models, and reading business news. As a quant my job is to understand how financial markets work, which is neither purely mathematical nor purely social but which has elements of both. I might come up with an idea using broad economic themes but it is not a model until it is in a testable form involving concrete data. Also, in my group we rotate the responsibility of maintaining the
automatic computer trading system. This is really just a huge program that decides what and when to trade, and it needs constant attention. So for one week in about thirteen, I am on-call basically all the time. As a recent academic, I find this to be the part of my job that is probably the most alien and intimidating, but it is also extremely satisfying to be involved with the nuts and bolts of the operation.
     The transition from academia to finance has meant a shift in my priorities. Unlike when I was in academics, I no longer have to worry about grants, getting papers published and waiting a long time from beginning to end on my projects.  Now working in finance I do worry about the relevance and testability of my ideas, the minute correctness of my code, and of course profit. Leaving an academic career has meant giving up teaching, the students, and the absolute freedom to work on any project I want. On the other hand, finance has provided me with the opportunity to come up with good, new ideas that will be put into effect, be profitable, and for which I will be directly rewarded.

Would I like your job ?


To that question, I would counter with these:
Are you efficient-minded?
Can you sustain focus?
Are you flexible about the field to which you apply your quantitative talents?
Do you like to understand how systems work as well as the theory behind them?
Do you enjoy mastering new skills?
Do you appreciate the existence of a “bottom line”, a way to quantitatively measure the success of your projects and your ideas?
Are you articulate?
Are you good at following through and finishing projects?

     Notice I didn’t ask if you are particularly informed about finance or money per se, because honestly I wasn’t when I decided to enter finance. I don’t think it was a disadvantage, and now I really enjoy finance and find myself reading finance books instead of fiction. I had also never coded, but now I really enjoy coding. Both of those are skills that anyone with focus, intelligence, and flexibility can master and enjoy. For me and for many of my colleagues it is intrinsically satisfying to be in a collaborative atmosphere as part of
a functional, productive, and hard-working team with clear goals.

What does the D.E. Shaw group look for ?


The D. E. Shaw group hires people of extraordinary ability. Quant candidates typically come from math, physics, or computer science backgrounds and often have Ph.D.s. This is not to say having a Ph. D. is a requirement, but certainly being capable and smart enough to have a Ph.D. is. What we are really looking for is new ideas, and so our target is the creative, careful thinker. We look for evidence of such talents in the forms of published original papers as well as original personal projects or specialized hobbies. We typically do give brainteasers in interviews, but it is not true that only people who are insanely quick at brainteasers are seriously considered. I do not consider myself all that quick, for example, but I am methodical, articulate, and I don’t make huge mistakes.

Is Finance a good place for women ?


Working in finance is different from working in academics in that there is no tenure. However, I would recommend thinking about the concept of employability over job security. Although a given company may not last forever, the finance industry is here to stay, and there is always a need for quantitatively strong people. If you find yourself out of a job, but you have real skills and knowledge, chances are you will find another job quickly.
     Partly because of this consideration, the D. E. Shaw group tries very hard to get great people and keep them. The turnover is low, partly due to our selectiveness in hiring only the very best people, and partly because people feel valued and don’t want to leave. In fact some people have been known to retire early, but soon change their minds and return. There is little burn-out because the hours and conditions are reasonable.

What is corporate culture like ?


It is really different. People are both more competitive and more collaborative. They are more competitive in the sense that there’s lots of money involved, and therefore getting credit for an idea that makes money is a direct channel to getting paid better. At the same time, everyone relies on their colleagues to keep the whole thing running and so it is imperative that we work as a team. It’s an intense, challenging, and exciting environment to work in.
     About the money: many mathematicians who talk to me about moving to finance are genuinely worried about the potentially corruptive power of money. I take that fear very seriously, and I think I probably would have applied to the D. E. Shaw group earlier if I hadn’t experienced it myself.
     Several factors have helped me come to terms with this concern. First, it is really expensive to live in New York, especially with kids. So actually as a new quant, you are not all that rich, even though you are making more than almost all academics. However, it is clear that if you stay in finance for long enough, and are successful, you do become rich. Even so, I do not find my colleagues to be particularly acquisitive, and indeed some of them are known to support progressive causes and charities such as the Robin Hood Foundation, and I’m sure many of them quietly do so as well. In fact it is a stated goal of the D. E. Shaw group to foster an ethical work environment and to do what’s right.
     I think one can resist being corrupted by money by keeping a perspective and maintaining personal boundaries. I personally give a certain amount of my paycheck to my favorite grass-roots charity. I thereby see working here as a fantastic and rare opportunity to have a great job and to improve the world in some small way simultaneously.





Saturday, 6 October 2012

Financial Information Exchange (FIX)

What is FIX ?

FIX was originally called Fidelity Information Exchange, but now FIX stands for Financial Information Exchange. FIX is an industry driven standard to communicate trading information electronically between brokers, buyer institutions, seller institutions and markets. FIX is platform independent, so it works with various types of computers and communication systems. FIX has been developed through the collaboration of banks, institutional investors, brokers and exchanges. These market participants wanted a common language for automated trading of financial instruments.

Brief History

FIX was first developed in 1992 by Robert Bob for equity trading between Fidelity investments and Salomon Brothers. Hence it was first called Fidelity Information Exchange. FIX has now become the de facto messaging standard for pre-trade and trade communications in the global equity market. FIX has now entered post trade space as well as foreign exchange, fixed income and derivative markets. FIX Protocol Ltd. is the company established for the purpose of ownership and maintenance of the specification. FIX is gaining increased attention within the exchange community as over three quarters of all exchanges surveyed supported a FIX interface, with the majority handling over 25% of their total tarding volume via FIX.

FIX Connectivity Diagram

Financial Information eXchange System Connectivity Diagram.svg
source : http://en.wikipedia.org/w/index.php?title=File:Financial_Information_eXchange_System_Connectivity_Diagram.svg&page=1

FIX Messages

FIX session is layered on TCP. FIX messages are formed from a number of fields, each field is a tag value pairing that is separated from the next field by a delimiter ASCII 01. The tag is a string representation of a integer that indicates the meaning of the field. The value is an array of bytes that hold a specific meaning for the particular tag. FIX protocol defines a set of fields that make up a message. Some of these fields are mandatory and some are optional. The ordering of the fields is unimportant. A FIX message is composed of a header, a body and a trailer. Following is an example of FIX message:

Sending: [
 BeginString 'FIX.4.2'
BodyLength '200'
MsgType 'D' "Order - Single"
MsgSeqNum '81'
SenderCompID 'DSCWE10'
SenderSubID 's1'
SendingTime '20100914-13:34:27.643'
TargetCompID 'BCAPSUBM'
PossResend 'N'
Account 'DESHVALI'
ClOrdID 's1bs6940_20100914'
Currency 'USD‘
OrderQty '100'
OrdType '2' "Limit"
Price '129.45'
Side '1' "Buy"
Symbol 'IBM'
TimeInForce '0' "Day"
TransactTime '20100914-13:34:27'
ExDestination 'N'
MaxFloor '100'
SecurityType 'CS' "Common Stock"
CheckSum '161'
]

Recovery Handling : 

FIX has an optimistic model for recovery. It does not has per message acknowledgement. Instead the receiver monitors sequence number and detects gaps. Sequence number increases monotonically and is generally reset per day. There are serious errors is sequence number is decreased.