STOCK MARKET (1)

Preview:

Citation preview

DESIGN AND IMPLEMENTATIONOF STOCK TRADING ALGORITHMS

College Mentor :-Dr. Neena Gupta

Mohit10105037At- uTrade Solutions Pvt. Ltd.

Company’s Profile

uTrade™ Solutions is a financial trading technology company with various products including multi-asset trading platform, algorithms and analytics.

It’s a Startup !!

It is a startup launched in June 2011 by alumni of PEC University of Technology.

Currently the strength of organization is about 40 employees.

PRODUCTS

uTrade Solution provides three different kind of products to their customers :-

• uTrade (Retail Trading Software)• µTrade (Algorithmic Trading Software)• Money uTrade (Portfolio management)

It is a multi asset trading platform offering a web based, application based and mobile based trading front end for various Indian and Global markets.

muTrade is the quantitative trading platform with various arbitrage, market making, execution and other automated algorithms.

money uTrade is the portfolio analytics platform of uTrade that enables users to seek various relevant smart-information on financial securities, analyze & optimize various financial portfolios.

STOCK MARKETBASICS

What is a stock?

• It is simply a piece your ownership in the company.

Where Can I buy Stock?

You can buy stock from exchange.

What is an Exchange?

It is a common place where buyers and sellers meet.

Hey I got 2 Reliance Shares !

Ok I am ready to buy them!

buyer seller

Exchanges in India

There are many exchanges in India but of those two are main : -

• National Stock Exchange (NSE)• Bombay Stock Exchange (BSE)

National Stock Exchange

Mumbai

Bombay Stock Exchange

As Clear from nameLocated inMUMBAI

Do I have to personally go to exchange?

Yes if you were in 1980’s

How do I buy Stock Today?

Computer or Phone

I am more faster than youI know that

Who is a Broker?

Man who connects you with exchange.

Why we need broker to trade?

Exchange only allows its members to trade who are brokers.

How do I know what is the price of particular stock?

Market Watch

Who will provide me Market Watch?

The person whom you are paying.Your Broker

How to look at market watch?

Two points to remember:-• Buyer is a bidder who bids for a share.• Seller is a asker who asks for money to sell his share.

Market Depth

BID QTY BID PRICE ASK PRICE ASK QTY

10 100 101 20

15 99 102 10

5 98 102.50 100

30 97.50 103 200

25 95 105 20

BUYER 2

BUYER 1

BUYER 3

BUYER 4

BUYER 5

SELLER 1

SELLER 2

SELLER 3

SELLER 4

SELLER 5

When does trade occur?

When there is no difference between price of highest bidder and the cheapest seller.

Derivatives

As the name implies they are derived from something.

Stock Derivatives

STOCK

FUTURES OPTIONS

What are Futures?

• Suppose the price of reliance share today is Rs 840.

• Now a trader thinks that price will rise to Rs 900 by the end of August.

What can he do?

Simple• He buys one reliance share and sells it at the

end of month to earn the profit of Rs 60.

What if he don’t have enough money?

Do not worry he can still trade using future !!!

Buying a Future

• When you buy a future of a stock you have to only give certain percentage of value of stock to your broker.

• Suppose your broker is humble and he only charges you 10%.

• So you can buy a future at Rs. 84 and earn a profit of rupees 60.

Isn’t that cool?

By paying only Rs 84 you earned Rs 60.So the moral of story is always buy Future

instead of stock or equity.

But wait there is one Bad News?

Large profit percentage comes at large risk.

What’s the trick?

• You can only trade future in multiples of certain lot size.

• Suppose lot size of a future of reliance is 250.• So you can buy 250/500/750…… futures.

It is the game of big Players.

Then what’s the benefit in trading futures?

• Suppose price of share of Tata steel is Rs 100.• I want to buy 250 shares of Tata steel.• If I want to trade in equity I have to invest

100x250 = Rs 25000.• But if I buy futures at 10% then I have to invest

10x250 = Rs 2500.

Motto is simple !!

But large quantity at wholesale price.And earn large profit percentage.

What are Options?

Lets Take the same example!

• Suppose price of one share of uTrade (whenever it will be listed) is Rs 100.

• A man expects that it will rise to Rs 120 after one month.

What option does he have now?

• What he can do is, buy an option of uTrade Solutions.• It means that he promises to seller that he will buy

share of uTrade after one month for rupees 100.

What happens after one month?

• Suppose price of share of uTrade rises to expected Rs 120.

• The Buyer of option will fulfill the promise and earn profit.

What if market falls?

• Suppose price of share of uTrade drops to Rs. 80.

Break the promise to prevent loss

Isn’t that cool?

• First you promise to buy that share then if the price does not rise as expected by you, then simply break the promise.

Waiting for Bad News?

• Here it is……..

Their was a cost of promise you made before one month to seller.

What was the cost?

• Suppose you have bought one option of Tata Steel.

• You have to pay Rs 5 to the seller.• If you break your promise after one month

then you will lose these 5 rupees.

What’s the benefit of buying an option?

Risk is very less

Let me Explain!

• Consider the same scenario in which you predicted that price of uTrade share will rise to Rs 120 from the current price of Rs. 100.

• If you buy that share then you have to spend Rs 100.

• If you buy option then you have to spend only Rs 5.

Now the ghost arrives!!

Price of uTrade’s share falls to Rs 0.

BANKRUPTCY

If you have purchased share?

• You will encounter loss of Rs 100 – Rs 0 = Rs 100.

What if you have purchased an option?

• You simply break the promise of buying that share for Rs 100.

• You will only lose Rs 5 which is far less than the loss of Rs 100.

Now as usual !!!!

• Options are also traded in lots like Futures.• Like 250/500/750 and so on…. Thus increasing the risk.

Tasks Assigned

1. Excel Live Market Feeder

OBJECTIVE : -

To show live market watch on Microsoft Excel

PURPOSE

Trader wants to analyze his algorithms on live market feed by applying complex mathematical formulae.

Why Excel?

Because Microsoft Excel is an excellent tool for mathematical as well as graphical analysis and everyone knows how to operate it.

Design

EXCHANGEConversion of packets

to local formatDisplay inMicrosoft Excel

Technologies Used

Three technologies were used to make this application:-

• Winsock (C++) programming based on UDP protocol to establish communication for receiving packets.

• Visual C# (C sharp) to operate excel via programming.

• Sqlite3 as a database management tool.

2. Scripts to Automatically update databases and generate files

OBJECTIVE:-To automatically update all the databases at the end of trading day and to generate appropriate files which are used in next trading day.

PURPOSE

To reduce manual burden and to analyze the performance of algorithms running in live market.

Subtasks Involved

• To update our database with new companies added or old companies removed by exchange.

• To remove all those contracts which have not been traded by anyone in the previous trading session in order to reduce load and increase speed.

• To update our database with latest prices of all the contracts in order to avoid any wrong order from our client.

Technologies Used

• Bash script in Linux.• Python for automatic downloading of files

from exchange’s website.• PostgreSQL as a database management

language.• C++ (SOCI Library) to extract appropriate

information from files and update that in our database.

3. MIS Report Generation

Management Information System

OBJECTIVE

• To generate a report at the end of trading session which includes all the fields such as profit/loss, turnover, quantity traded etc corresponding to the strategies or algorithms launched by the user on different exchanges.

PURPOSE

• The main purpose is to analyze the performance of different strategies running in live market.

• To maintain a record of executed strategies by a particular client of broker.

Design

• After the strategy is terminated it produces some strategy logs indicating the behavior of that particular strategy.

• Using these strategy logs we can find out the profit/loss , volume traded, turnover etc for all the strategies and finally sum them up to find overall profit/loss.

4. Design and Implementation of Stock Trading Algorithms

What is Algorithmic Trading?

It is a trading method in which a computer trades for you.

Advantages

• It is very fast so that you do not even miss a single opportunity of earning in live market.

• Eliminates the manual burden of keeping watch on the live market, just launch the strategy and forget.

• Very useful for analysis of news and other events happening in the market.

Disadvantages

• Very expensive form of trading, usually out of reach of common man as huge amount of commissions are involved.

• As high speed trading is involved anything wrong can happen. E.g. Due to fault in algorithm it can go into infinite loop and will continuously buy the stocks until trader’s limit will breach.

Requirements

• Ultra high speed market feed which is provided by exchange.

• Low latency order execution infrastructure.

Different Types of Algorithms

• News Based Trading• Market Making• Statistical Arbitrage• Pair Trading• Other low latency Strategies like TWAP, VWAP

etc.

Statistical Arbitrage

Same in the case of Stock Market

• Suppose the same company such as Reliance is listed on both exchanges NSE as well as BSE.

• Now let the price of share of Reliance rises up in NSE, so it should also rise in BSE.

• But it takes certain fraction of second for this updated price to show up in BSE.

In the meantime……

• We buy one share of Reliance from BSE as it is cheap there and immediately sells it in NSE.

Buy from BSE Sell in NSE

Requirements for Arbitrage

Their should be two different instruments such that price of one instrument follows the price of other instrument.

Different Arbitrage Strategies

• 2 Leg Arbitrage Strategy• Conversion – Reversal Strategy• 2 Leg/ 3 Leg Ratio Spread Strategy• Box Spread Strategy• Four Leg Butterfly Strategy

2 Leg Arbitrage Strategy

• Basic principle is same as that explained above.

• There are two instruments such that price of one follows price of other.

• We will take advantage of arbitrage between two instruments trading on same exchange.

What are these instruments?

• In order to follow prices of each other their must be something common in between these instruments.

• First instrument is stock/equity itself.• Second instrument is derivative of first

instrument i.e. Future.

ExampleRELIANCE (EQUITY)

RELIANCE(Aug FUTURE)

RS. 100 RS. 110

RS. 101 RS. 111

RS. 99 RS. 109

RS. 95 RS. 105

The difference between price of Future and Equity is constant.

Now suppose Market Fluctuates!!

RELIANCE (EQUITY)

RELIANCE(Aug FUTURE)

RS. 100 RS. 110

RS. 101 RS. 111

RS. 98 RS. 109

RS. 95 RS. 105

Difference is Rs. 9

How can we take Advantage of this Fluctuation?

• Simple buy the instrument which is cheap i.e. RELIANCE Equity worth Rs. 98.

• And Sell the instrument which is costly i.e. RELIANCE Future worth Rs. 109.

• So the net credit is 109 – 98 = Rs. 11.• But your position is not closed.• Closed position means that you should not

have anything with you.

Now wait for sometime……..

Till price difference again reaches to constant of Rs. 10

When price Difference reaches Rs. 10

• Simple reverse your previous trades to close your position in market.

REL EQUITY

REL FUTURE

Bought at Rs. 98

Sold at Rs. 109

Sell at Rs. 95

Buy at Rs. 105

PROFIT = RS. -3

PROFIT = RS. 4

NET PROFIT = RS. 1

FRONTEND

Types of Futures

• There are three different kinds of futures with three different dates of expiry which are last Thursdays of next three months.

• E.g. Reliance Future 25 Jul Reliance Future 29 Aug Reliance Future 26 Sep

Different Types of Combination

• Just as price of Future follows the price of equity, price of one Future also follows the price of other Future given that both have different dates of expiry.

RELIANCEFUTURE25 JULY

RELIANCEFUTURE29 AUG

Conversion Reversal Strategy

Logic is same…

• Two instruments with price of one following price of another.

• But this time each instrument is combination of other two instruments.

RELIANCECALL OPTION

+STRIKE PRICE

RELIANCEPUT OPTION

+FUTURE

EXAMPLE

Reliance Call option with Strike price 900 +

900

Reliance Put option with Strike price 900+

Future of Reliance

Call + Strike = Put + Future

FRONTEND

RISK MANAGEMENT

Risks Involved

1. Wrong quantity Entered by User.E.g. in place of 20 shares if 200 shares are entered then it will be a disaster.

QUANTITY CHECKS

2. Order Value Check

• E.g. price of one share of Reliance is Rs 840 but a user enters a limit order of Rs 1000 to buy this share which is not acceptable.

• The valid Order Value Generation takes place only if it compile with the Surveillance Measures set for the User with respect to Maximum Single Order Value (both Buy/Sell), Maximum Total Order Value (both Buy/Sell) and Maximum Net Value.

Value Checks

3. Price Range check

• The order price generation mechanism strictly follows the specified Range provided protection to a trader to limit the risk within a pre-set percentage of the Last Trade Price (LTP). The percentage will greater than zero and less than or equal to the applicable circuit limits including dummy circuit limits in respect of all algorithmic/single orders.

Price Range Checks

Technologies Used

• Qt Creator for all the front ends.• Qt Creator is a cross-platform C+

+integrated development environment which is part of the Qt SDK. It includes a visual debugger and an integrated GUI layout and forms designer.

Sqlite3 as a database management tool

• SQLite is a relational database management system contained in a small C programming library.

• Very easy to use and maintain.• High speed queries.• Uses very less memory.• Only one query at a time.• Have limited functionalities.

TCP Protocol for CommunicationB/W

Front End and Backend

• Connection- oriented protocol with acknowledgements.• Very reliable protocol.• Packets are delivered in order in which they are sent.• Speed is slow due to acknowledgements overhead.

CONCLUSION

• Five Strategies are running live in market with turnover of Rs 200 crores everyday.

• Three strategies are in testing phase.

• Running Strategies cover approximately 10 % of the total algorithmic trading market turnover in India.

FUTURE SCOPE

• Next target is to make an API so that user can write his own algorithms at his end without prior knowledge in coding.

• Main purpose of making this API is to maintain the privacy of user.

QUESTIONS?

Recommended