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.

Saturday 25 August 2012

Thread Locals in Java

Thread Locals

Thread local  is a scope just as we have static scoped variables which belong to a class or instance scoped variables which belong to an Object. Thread local variables belong to a thread. Each thread would have its own thread local variable. So, threads can't modify each other's thread local variables. Thread variables are a sort of global variables which are restricted to a thread.

When do we use Thread Local variables ?

By default, data is shared between threads. You can refer to my previous post to get an idea about what is shared among threads. You can use Thread Local variables when you want each thread to have its own copy of something. One very important use cases of thread locals is when we have an object that is not thread safe, but we want to avoid synchronizing access to that object. It would be more clear from an example.

Suppose I have a requirement to use use Java Calendar object in my code. Since Java Calendar is not thread safe, we can either have Calendar object as an instance variable or have it as a class variable and provide synchronization to access it. The first method can't be used in most of the production codes because Calendar object creation is an expensive operation. And still if we have 2 threads of the same process accessing the variable, we need synchronization. The second method looks good since we would have only one object of Calendar class, but we would have to take care of synchronization. 

If we don't want to bother about synchronization, we can go for thread local variables in which case, each thread would be given its own local copy of the thread local variable. Another alternative to thread locals or synchronization is to make the variable a local variable. Local variables are always thread safe. But in our case, since Calendar object creation is an expensive operation, it is not recommended to use local variable for Calendar. Since each time the method is called, a Calendar object would be created which is an expensive operation, it would slow down the.

Another very important use case of Thread Local variables is when we want to associate state with a thread. Many frameworks use ThreadLocals to maintain some context related to the current thread. Web applications might store information about the current request and current session in thread local variables so that the application has easy access to them without passing them as parameters every time. Let me explain this with a scenario.

Lets say, we have a Servlet which calls  some methods. You have a requirement to generate a unique transaction ID for each request you receive and pass this transaction ID to the business methods for processing. One way is to generate a unique transaction ID each time the servlet receive a request and pass this trasaction ID to the methods which require it. But this doesn't look good, since passing of transaction ID to all methods which require it is redundant and unnecessary. Instead, we can use thread local variable to store the transaction ID. Every method which requires transaction ID can access it through the thread local variable. The servlet might be receiving many requests, but each request is processed in a separate thread. So, each transaction ID would be local to a thread and would be accessible all through the thread's execution which is what I mean when I say that Thread Local variables are global.

Usage of Thread Local variables in Java

Java provide a class named ThreadLocal by which you can set and get Thread Local variables.
Typically Thread Local variables are static fields in classes. The code below shows you how to create a Thread Local variable.

Problems with Thread Locals

Thread Locals also comes up with many problems and you have to be careful while using thread locals. Thread Locals can lead to classloading leaks. Thread Locals are very dangerous when it comes to long running applications and garbage collection. Let me explain this point a little bit.

If you use thread locals to store some object instance, there is a high risk that the object stored in thread local is never collected by garbage collector when your application runs inside WebLogic Server. This is because WebLogic server maintains a pool of working threads even when the class that created it is garbage collected. So, if you do not clean up when you are done, any references that it holds as part of the webapp deployment will remain in the heap and would never be garbage collected. This problem can be solved through the proper use of Weak References with Thread Locals. 

Sunday 19 August 2012

Processes andThreads

In this post I would be discussing about processes and threads.

Process

A process is an instance of a program that is being executed. A process consumes the resources of an operating system. Since there are many processes running at a time, how does the OS manages its resources ? To manage processes, an operating system has a process table. A process table is a data structure which includes the following information:
  • Process ID
  • Process Owner
  • Process priority
  • Pointer to the executable code of the process
  • Parent Process
  • Environment variables
  • Process state
A process can have many threads of execution. By default, any running program has a single thread of execution. A process has a unique address space which is generally not shared with any other process, except during inter process communication, the operating system can relax this condition.

Threads

 A thread is a smallest unit of execution that can be scheduled by an OS. A thread is called the light weight process because thread creation can be 10-100 times faster than a process creation. This is because threads share address space unlike processes which do not share address space. Here I am talking about the threads of the same process. Threads of different processes, of course do not share address space. The main reason for having threads is that in many applications, many activities are going on at the same time. Some of these activities may block from time to time. By decomposing such an application into multiple threads, we increase performance. Threads yield no performance gain when all of them are CPU bound, but when there is substantial amount of I/O as well as computing. Having threads, allows the activities to overlap, this speeding up the application. Threads also allow parallel execution on a multiprocessor system. In this case, the programmer needs to be careful to avoid race condition.

A thread has the following information:
  • Thread ID
  • Program Counter
  • Register Set
  • Stack
So what does the thread share with other threads of the same process?
  • Code section
  • Data section
  • OS resources
Lets talk about the advantages of threads.

Thread Advantages:

  1. Thread creation and destruction is faster than process creation and destruction. 
  2. A thread has lower context switching overhead than a process. This is because a thread has a lesser context than a process because threads share address space. Remember here I am talking about thread of the same process.
  3. Information sharing between threads is easier and has less overhead because threads share address space. So, data produced by one thread is immediately available to all other threads of the same process.

Thread Disadvantages:

Since global variables are shared between threads, inadvertent modification of shared variables can be disastrous. It calls for concurrency control measure which have their own complications.

Types of Thread Implementations:

There are 3 types of thread implementations:
  1. User Level Threads
  2. Kernel Level Threads
  3. Hybrid implementation

User Level Threads:

The type of thread implementation puts the thread package entirely in user space. The kernel is not aware of threads. The kernel just knows that it is managing single threaded processes. Like an OS maintains a process table, a process maintains a thread table which does the same job as process table does for operating system. Each process has its own private thread table. In this implementation, when a thread wants to go to the blocked state, it notifies the run time system. The run time system saves the thread state in the thread table and looks for a ready thread in the thread table to run. We see that in case of user level thread implementation, we don't trap to the kernel in case of thread switching. This is at least an order of magnitude faster than trapping to the kernel in case of kernel level thread implementation.

The main problem with this type of thread implementation is that if by chance any user-level thread is blocked in the kernel, all threads of that process are blocked. Another problem with use-level threads is that we don't take advantage of multiprocessing since the kernel is not aware of any threads.

Kernel Level Threads:

In this type of thread implementation, any thread in a process would be mapped to a kernel level thread. Switching between threads in this case requires kernel mode switch which is expensive. When a thread blocks, the kernel, at its option, can run either another thread from the same process or a thread from a different process. With user level threads, the run time system keeps running threads from one process until the kernel takes the CPU away from it.

Saturday 4 August 2012

Website Parsing

Today I would be writing about a website parser which I wrote. In this post, I would show you how to parse www.cricbuzz.com website. But the logic is nearly the same for other websites also. You can play around by changing the logic according to your needs. I have used Python in my code, so you need to know Python to follow this post.

So lets begin parsing cricbuzz site. First go to that website. Go to the page that you want to parse. Suppose I want to parse the ongoing England Vs South Africa test match. Go to the Full scorecard page of cricbuzz as shown below:


.
 If you are using Google Chrome browser, press Shift + Ctrl + J to go into the developer mode. You would see a new split window having some tabs as shown below:




Then click on the Network Tab:




Then click on scorecard.json which is highlighted in the above picture.



We see that this site uses JSON which is a light weight data interchange format to send the data. JSON is easy for machines to parse and generate. It is based on Javascript Programming Language. Now you can use your logic to parse the site. I will be using Python's Json package to parse the Json content. Lets start with the code. First import json package. We would need the URL of the JSON page to begin parsing, so get the url by right clicking on scorecard.json.  You can check that URL by pasting in your web browser. You should see a page like this :




We need to get the data from this URL to begin parsing. We can use urllib2 package for this task. The following statement would get the whole data in result string, where the URL is the copied URL:

result = json.load(urllib2.urlopen(URL))


The logic I have used is that if the score changes after 20 mins, it would send an email to the person. To handle the email part, we have to use smtp package.

So, here is the complete code:




The code I have used has very little practicality, but the idea was to make the concept clear. If the idea was clear, you can play around with the logic.  :)

Sunday 29 July 2012

Hedge Fund Vs Mutual Fund

I myself had this doubt for quite a long time: What is the difference between a Hedge Fund and a Mutual Fund. This month I started working at D.E. Shaw & Co and during the induction program, I was introduced to the financial concepts. This was different from what I has exposed myself since the last 4 years i.e. algorithms, operating systems, databases, compilers, processors, computation and blablabla.This was a slight shift from my regular track which is computer science, but I really enjoyed a lot. I explored a little more and got a clear understanding of the difference between a Hedge Fund and a Mutual Fund. In this post I would share my findings.

Hedge Fund

A hedge fund is an aggressively managed portfolio that uses leveraging to generate high returns. The word 'hedge' means to minimize or to reduce financial risk. But we see that hedge funds are mostly risky. They are so risky that only big investors invest in hedge funds. They are open to only some specific type of investors specified by regulators. These investors are big institutions such as pension funds and high net worth individuals. The goal of a hedge fund is to maximize returns and do achieve this goal, hedge fund uses leveraging. Now there is a very basic theory in finance which goes as: "Higher the returns, higher the risk involved". Since hedge funds give higher returns, they involve more risk. Then why are they called hedge funds since 'hedging' means to minimize or to reduce risk. Like mutual funds, hedge funds also pool money from investors and then manage them. But hedge funds uses leveraging to maximize gains and that make them risky. Let me explain the concept of hedging by the following example:

Suppose a hedge fund collects 100 $ from investors (I have taken this amount just for illustration. The actual amount that hedge fund invests runs into billions of US dollars). It takes 100 $ from a bank (This is what is leveraging). Now hedge fund has a pool of 200 $ which it invests. Now the bank that lends you 100 $ has a condition: No matter what you gain or loose, I should get 120 $ after say 6 months. Now lets examine 2 cases:
  1. Suppose after 6 months, hedge fund makes a profit of 40%. The value of the portfolio is 280 $. Hedge fund gives 120 $ to the bank and distributes 60 $ as profit among investors. Of course, it charges some fees also, but I have not included it in my computations. Hence the investors make a profit of 60%.  Now we see the power of leveraging. Though hedge made a profit of only 40%, it gave 60% profit to investors. This is possible due to leveraging.
  2. Suppose after 6 months hedge made a loss of 10%. The value of the portfolio is 180 $.  Hedge fund gives 120 $ to the bank and the value left in the portfolio is 60 $. Of course, it charges some fees also, but I have not included it in my computations. Hence the investors made a loss of 40%.  Now we see the risk of leveraging. Though hedge made a loss of only 10%, it resulted in 40% loss to investors. This is reason why hedge funds are risky.

Mutual Funds

A mutual fund is a type of professionally managed collective investment scheme that pools money from several investors to purchase securities. But unlike hedge funds, mutual funds are highly regulated. Mutual fund is open to common man unlike hedge funds. Due to this reason, mutual funds are highly regulated. They can't invest in any security, but only those which are approved by the regulators. Hence mutual funds are safer than hedge funds. Then again that basic finance theory comes into picture: "Lower the risk, lower the returns". Hence mutual funds don't generate as much returns as hedge funds.

Similarity between a mutual fund and a hedge fund

  1. The most important similarity between a hedge fund and a mutual fund is that they both pool money from investors. 
  2. One more similarity is that in a hedge fund also investors can withdraw their money as in a mutual fund

Difference between a Mutual fund and a Hedge Fund

In a nut shell, following are the differences between a Hedge Fund and a Mutual Fund:
  1. Hedge funds focus on absolute returns while mutual funds focus on relative returns.
  2. Hedge funds can invest in any asset class- stocks, bonds, sub prime mortgages, commodities, real estate. While mutual funds can only invest in a set of asset class. Mutual funds have to follow compliance framework set up by the regulator. Hence risky asset classes are debared from  investment.
  3. Hedge funds use leverage. Though mutual funds can also borrow to some extent but they are highly regulated.
  4. Hedge Fund can run concentrated portfolios. In case of Mutual Funds we have to protect investors' money and hence they always use diverse portfolios.
  5. Hedge Funds are meant for richer people to become more rich. Whereas even low worth individuals can invest in a mutual fund.
  6. Hedge Funds are unregulated whereas mutual funds are highly regulated.

Sunday 22 July 2012

Money Markets

Money

Money is the current medium of exchange. I have seen that often people get confused with money. The money that is in our bank accounts is not real money. Money that is on account of the Central Bank of a country is the Real Money.  All other forms for example, the account balances with the commercial banks, even cash are promises to pay money, but not real money. Like individuals, banks and larger institutions also transact money amongst each other. In these transactions cash is not involved, but real money kept in accounts with the Central Bank of a country is involved.

Money Markets

Money market provides short term finance(for a period less than 1 year). The parties involved in Money Markets are Central Banks, Commercial Banks, FIs, Mutual Funds and Primary Dealers. One of the main differences between money markets and stock markets is that most money market securities trade in very high volume thus limiting accessing individual investors. The easiest way for an individual to access money market is through money market mutual funds. However, some money market instruments like Treasury bills may be purchased directly. Below we will have a look at major money market instruments:

Treasury Bills(T-Bills)

T-Bills are the most liquid money market securities. T-Bills are a way for the US government to raise money from the public. I am refering to the T-Bills issued by the US government, but many governments issue T-Bills in a similar fashion. Treasury Bills are issued through a competitive bidding process. The biggest reason that the T-Bills are so popular is that they one of the few money market instruments that are affordable by individual investors. Another important reason for their popularity is that they are considered to be the safest investment in the world because they are backed by US government. But this safety comes at a cost. They have very low returns. And this is the basic rule in finance: The higher the risk, the higher the return.

Certificate of Deposit

A certificate of deposit is a promissory note issued by a bank. It is a time deposit that restricts holders from withdrawing funds on demand. CDs are similar to saving accounts in that they are insured and hence virtually risk-free. They are different from saving accounts in that CDs have a specific and fixed term(often 1 month, 3 months, 6 months, 1 year). CDs generally give higher rate of return than Bank term deposit.

Commercial Paper

An unsecured, short-term debt instrument issued by a corporation, typically for the financing of accounts receivable, inventories and meeting short-term liabilities. Maturities on commercial paper rarely range any longer than 270 days. Commercial papers are not backed by collateral. Since these are not backed by collateral, only firms with high credit ratings from a recognized rating agency would be able to sell its commercials papers at a reasonable price.

Banker's Acceptance

BA is a promised future payment which is guaranteed by a bank and drawn on a deposit at a bank. A BA specifies the amount of money, date and the person to which the payment is due. Now the holder of the draft can sell it for cash to a buyer who is willing to wait until the matutity date of the funds in the deposit. BAs make the transaction between 2 parties who do not know each other to be more safe because they allow parties to substitute the banks's creditworthiness for that who owes the payment.

Eurodollars

Eurodollars are US dollar denominated deposits at banks outside United States and are thus not under the jurisdiction of Federal Reserve. These are called Eurodollars because most of the initially most of the US dollar reserves outside the United States were in Europe. Eurodollar market is relatively free of regulations and hence banks can operate at lower margins than their counterparts in United States.

Repo

A repurchase(repo) agreement can be seen as a short term swap between cash and securities. If a security holder wants to maintain his long-term position but needs cash for a short term period, he or she can enter into a repo contract whereby the securities are sold together with a binding agreement to repurchase them at a future date.. The effect is to provide the security holder with a short-term loan based on the collateral of the government securities he or she owns.

Friday 1 June 2012

GC overhead limit exceeded error




Recently I was struggling with a very unusual error in one of the batches in production environment. The error read like:
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded

Here is the snapshot of the error when I ran it through command prompt:

Explored on this error and in this post I would like to highlight my findings:

This message means that for some reason the garbage collector is taking an excessive amount of time (by default 98% of all CPU time of the process) and recovers very little memory in each run (by default 2% of the heap). This effectively means that your program stops doing any progress and is busy running only the garbage collection at all time. To prevent your application from soaking up CPU time without getting anything done, the JVM throws this Error so that you have a chance of diagnosing the problem.
The rare cases where I've seen this happen is where some code was creating tons of temporary objects and tons of weakly-referenced objects in an already very memory-constrained environment. This feature is designed to prevent applications from running for an extended period of time while making little or no progress because the heap is too small. You can turn this off with the command line option -XX:-UseGCOverheadLimit
In my case the data was huge. We had deployed a batch in production which was implemented using stateful framework. But it had to be changed to the stateless code which took around a month. During that time that batch didn’t process any records. So the records had piled up which needs to be processed. When we deployed the stateless code, it gave this error. When I was running this batch process, I had allocated maximum of 1 GB memory to it. Then I removed this limit. Even then it gave this error. So, I had to turn off this feature to get rid of it by the command line option I had mentioned above.

Now the obvious question that comes to the mind is that what happens to the Java process in case of OutOfMemoryError.
And OutOfMemoryError is handled like any other exception:
·         If it is caught, then nothing more happens.
·         If it is not caught, then either the threads or the threads groups uncaught exception handler handles it. This pretty much always leads to the thread being stopped.
However there are two factors that are not really there in other exceptions:
·         OutOfMemoryError is an Error and not an Exception. This means that it's very unlikely to be caught anywhere: You should not try to catch an Error generally (with very few exceptions) and it's not usually done, so the chances of it being handled are rather low.
·         When an OutOfMemoryError happens and no object become eligible for GC because of that, then you'll still have little memory left and chances are that you'll run into the exact same problem again later on.
And if the thread this happens to is the only non-daemon thread (often, but not necessarily, that's the main thread, that executes the main method), then that thread getting killed results in the whole JVM shutting down (which is often perceived as "a crash").
So it will probably kill the thread, and if the memory-issue is not solved, then this can happen to more and more threads.

OutOfMemoryError should be considered unrecoverable and the behavior of the JVM after such an error has been raised is undefined, so there is no point in expending effort to handle it. Any operations done after this exception is thrown by the JVM will have undefined behavior. They may execute, but more likely they will just cause another error to be thrown.

Saturday 26 May 2012

Mounting partitions in Linux through command line

In this post, I would add a partition in Linux through command line. Your first task should be to know which partitions are already mounted on your system and where they are mounted. So, run the following command which gives you this information:
$ sudo mount


If the partition which you wish to mount is shown, you can navigate to its mount directory. Lets say the mount directory of the partition you wish to mount is /mnt/partition1, then run the following command:
$ cd /mnt/partition1
And you be able to access its file system.

But if the partition you wish to mount is not shown by the command "sudo mount", you will have to mount it first. So, you would like to know the device identifier for the partition. The following command would help to do that:
$ sudo fdisk -l
it is a lowercase L. The identifier will look something like
/dev/sda1
Now you have to create a mount point(you can give any path for it). mount point is a directory (typically an empty one) in the currently accessible filesystem on which an additional filesystem is mounted (i.e., logically attached). The mount point becomes the root directory of the newly added filesystem, and that filesystem becomes accessible from that directory.
$ sudo mkdir /mnt/ankitpartition
I used the path /mnt/ankitpartition. You can use some other path.

Since you probably want it to be mounted all the time we’ll skip the mount command and go right into the fstab i.e. we would make this mount point default. The default mount points are the directories in which file systems will be mounted automatically when the computer is booted. Default mount points are listed in the file /etc/fstab.
So now we would make this partition default. First open fstab file in a text editor:
$ sudo nano /etc/fstab
You can use any other text editor, but I like nano.
Within the fstab you have to add a line which tells Linux to mount the partition. There should already be some entries which will give you a general guideline about how the line has to look like. Some examples:
mounting a FAT32 partition
/dev/sda2 /mnt/mypartition vfat umask=000,defaults 0 0
mounting a NTFS partition
/dev/sda2 /mnt/mypartition ntfs umask=000,defaults 0 0
To quit nano simply press ctrl + x. It will then ask you if you want to save the changes, press Y, and if you want to save it into the same fstab file, press Enter

And the last step would be:
$ sudo mount -a

Saturday 28 April 2012

Linux Kernel Mode


There are 2 modes in Linux: Kernel mode and User mode. Kernel mode is at a higher privilege level than user mode. All user programs and the applications running on the machine run in user mode. Only the kernel executes in kernel mode. A program executing in user mode can’t access kernel code, so can’t directly call system calls. This is done for security purposes because if a user program can access the kernel code, he can change it or access the memory which he should not access. So, the kernel assigns itself the highest privilege level: kernel mode. User processes are at the least privilege level: user mode. The kernel is protected by hardware, because programs executed in user mode can’t access the memory that belongs to programs executed in kernel mode.

This protection by hardware has a problem. User processes can’t access any feature provided by the kernel. In other words, user processes can’t make use of the system calls provided by the kernel. Again to cope up with this problem we go take help from the hardware. The only possible way to use system calls is to escalate the privilege level of the user process. So, we make use of interrupts, in this case software interrupt. A software interrupt can be seen as a special jump instruction whose target address is restricted by the kernel. The kernel sets the target address of the software instruction to the special routine that handles the system calls. Now suppose the user wants to use a system call. First of all what is done is that the code corresponding to the system call is put in a register for the kernel to understand which system call the user wants to call. The task of setting the system call code is done by the library procedure and is hidden from the user. After setting the code for the specific system call, a trap to the kernel takes place. Then the dispatcher in the kernel calls the specific system call using the code set in the register. Then a context switch takes place in which it saves the content of the registers of the user program. Then finally the control passes to the kernel procedure which performs its task.

But there is another side of the story. The system call by this hardware approach can become very slow because the software interrupt and the context switch, which takes place in the kernel require heavy and complex operations. This can be gauged by seeing that a system call is on an average about 30 times slower than a simple function call.

So the next question which comes to mind is how to accelerate system call? The obvious way to do this is to execute the user process in kernel mode. Then no software interrupt and context switch is needed. System calls would be just like normal function calls because the user process can access the kernel directly. But this is not as easy as it sounds. There are a lot of issues with this approach, foremost of which is security.

Sunday 8 April 2012

SOAP protocol

SOAP is a protocol for accessing a Web Service. SOAP is a simple XML-based protocol to let applications exchange information over HTTP. SOAP is designed to be a new protocol for the decentralized, distributed environment, which utilises the power of the Internet and XML to pass typed information between nodes. Originally, SOAP stood for Simple Object Access Protocol, but was later changed simply to SOAP with version 1.2, because it did not directly use Objects. SOAP is a light weight protocol that is platform independent, transport independent and operating system independent.



There are two types of SOAP requests. The first is the Remote Procedure Call (RPC) style request similar to other distributed architectures. This is usually synchronous; the client sends a message and waits to get a response or fault message back from the server. The second type of SOAP request is the document request. In this case, a full XML document is passed to/from the client and server, in side a SOAP message.

RPC

SOAP-RPC is an implementation of a Remote Procedure Call (RPC). In this case, a function on a remote machine is called as if it were a local function. All of the marshalling and unmarshalling of data is handled by SOAP and is performed in XML. RPC-style web services are tightly coupled and interface-driven. The clients invoke the web service by sending parameters and receiving return values. RPC-style web services follow call/response semantics; therefore, they are usually synchronous, which means that the client sends the request and waits for the response until the request is processed completely.


Document

With a document style message the client and/or server passes an XML document as the body of the SOAP message instead of parameters. An example of this is an invoice. The document is marked-up with both XML and a schema, which is common to both the sender and receiver. The sender is the consumer, which makes a function call to the web service with some parameters, and in return the vendor sends back, not data results, but an XML document that contains all of the information that a normal invoice would have, the difference being that it is marked-up with XML, and is machine-readable. Document style messaging has other advantages over a remote procedure call, which is meant to be relatively static, and any changes to the RPC interface would break the contract between the service and the application. Changing the RPC description would cause all of the applications that rely on a specific method signature of SOAP-RPC to break. Good design dictates that the method signature of an RPC message service should never change. With document messaging, the rules are less rigid and many enhancements and changes can be made to the XML schema without breaking the calling application, because what is sent is an XML document rather than a structured return value. Web service applications should be able to use a guaranteed delivery mechanism to improve its reliability, scalability, and performance. Since a document message is usually a self contained XML file, it is better suited for asynchronous processing and can be placed directly into the queue. The reliability of the application is improved because the message queue guarantees the delivery of the message even if the target application is not currently active, performance is improved because the web application simply delivers the document to a queue and is then free to perform other tasks, and scalability is improved because the document is offloaded to one or more instances of an application that handles its processing.

Applications of SOAP

  • Business to Business integration : SOAP allows businesses to develop their applications, and then make those applications available to other companies.
  • Distributed applications : Programs like databases could be stored on one server and accessed and managed by clients across the Internet.
One thing when we think of going for SOAP on our business server is that there are many other ways to do the same thing that SOAP does. But the most important advantage you would get by going for SOAP is that its not constrained by application language(Java, C#, C++, Perl or any other language) or the platform (Windows, Unix, Mac), and this makes it much more versatile than other solutions.


Lets go little deep into SOAP. As we know SOAP does not use objects. To pass anything besides the basic data types in Java to the web service, you must write your own Serialize and Deserialize functions. These are functions written that convert a class or object into XML and back. So it is true that SOAP does not pass objects, but instead passes representations of the data in the objects, so that it can be used to create new objects. Apache SOAP comes with a built-in serialization class called BeanSerializer. This class can take a Java Bean and automatically serialize and deserialize it.