101
DESIGN AND IMPLEMENTATION OF STOCK TRADING ALGORITHMS College Mentor :- Dr. Neena Gupta Mohit 10105037 At- uTrade Solutions Pvt. Ltd

STOCK MARKET (1)

Embed Size (px)

Citation preview

Page 1: STOCK MARKET (1)

DESIGN AND IMPLEMENTATIONOF STOCK TRADING ALGORITHMS

College Mentor :-Dr. Neena Gupta

Mohit10105037At- uTrade Solutions Pvt. Ltd.

Page 2: STOCK MARKET (1)

Company’s Profile

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

Page 3: STOCK MARKET (1)

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.

Page 4: STOCK MARKET (1)

PRODUCTS

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

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

Page 5: STOCK MARKET (1)

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.

Page 6: STOCK MARKET (1)

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

Page 7: STOCK MARKET (1)

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.

Page 8: STOCK MARKET (1)

STOCK MARKETBASICS

Page 9: STOCK MARKET (1)

What is a stock?

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

Page 10: STOCK MARKET (1)

Where Can I buy Stock?

You can buy stock from exchange.

Page 11: STOCK MARKET (1)

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

Page 12: STOCK MARKET (1)

Exchanges in India

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

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

Page 13: STOCK MARKET (1)

National Stock Exchange

Mumbai

Page 14: STOCK MARKET (1)

Bombay Stock Exchange

As Clear from nameLocated inMUMBAI

Page 15: STOCK MARKET (1)

Do I have to personally go to exchange?

Yes if you were in 1980’s

Page 16: STOCK MARKET (1)

How do I buy Stock Today?

Computer or Phone

I am more faster than youI know that

Page 17: STOCK MARKET (1)

Who is a Broker?

Man who connects you with exchange.

Page 18: STOCK MARKET (1)

Why we need broker to trade?

Exchange only allows its members to trade who are brokers.

Page 19: STOCK MARKET (1)

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

Market Watch

Page 20: STOCK MARKET (1)

Who will provide me Market Watch?

The person whom you are paying.Your Broker

Page 21: STOCK MARKET (1)

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.

Page 22: STOCK MARKET (1)

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

Page 23: STOCK MARKET (1)

When does trade occur?

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

Page 24: STOCK MARKET (1)

Derivatives

As the name implies they are derived from something.

Page 25: STOCK MARKET (1)

Stock Derivatives

STOCK

FUTURES OPTIONS

Page 26: STOCK MARKET (1)

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.

Page 27: STOCK MARKET (1)

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.

Page 28: STOCK MARKET (1)

What if he don’t have enough money?

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

Page 29: STOCK MARKET (1)

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.

Page 30: STOCK MARKET (1)

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.

Page 31: STOCK MARKET (1)

But wait there is one Bad News?

Large profit percentage comes at large risk.

Page 32: STOCK MARKET (1)

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.

Page 33: STOCK MARKET (1)

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.

Page 34: STOCK MARKET (1)

Motto is simple !!

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

Page 35: STOCK MARKET (1)

What are Options?

Page 36: STOCK MARKET (1)

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.

Page 37: STOCK MARKET (1)

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.

Page 38: STOCK MARKET (1)

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.

Page 39: STOCK MARKET (1)

What if market falls?

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

Break the promise to prevent loss

Page 40: STOCK MARKET (1)

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.

Page 41: STOCK MARKET (1)

Waiting for Bad News?

• Here it is……..

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

Page 42: STOCK MARKET (1)

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.

Page 43: STOCK MARKET (1)

What’s the benefit of buying an option?

Risk is very less

Page 44: STOCK MARKET (1)

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.

Page 45: STOCK MARKET (1)

Now the ghost arrives!!

Price of uTrade’s share falls to Rs 0.

BANKRUPTCY

Page 46: STOCK MARKET (1)

If you have purchased share?

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

Page 47: STOCK MARKET (1)

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.

Page 48: STOCK MARKET (1)

Now as usual !!!!

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

Page 49: STOCK MARKET (1)

Tasks Assigned

Page 50: STOCK MARKET (1)

1. Excel Live Market Feeder

OBJECTIVE : -

To show live market watch on Microsoft Excel

Page 51: STOCK MARKET (1)

PURPOSE

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

Page 52: STOCK MARKET (1)

Why Excel?

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

Page 53: STOCK MARKET (1)

Design

EXCHANGEConversion of packets

to local formatDisplay inMicrosoft Excel

Page 54: STOCK MARKET (1)

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.

Page 55: STOCK MARKET (1)

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.

Page 56: STOCK MARKET (1)

PURPOSE

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

Page 57: STOCK MARKET (1)

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.

Page 58: STOCK MARKET (1)

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.

Page 59: STOCK MARKET (1)

3. MIS Report Generation

Management Information System

Page 60: STOCK MARKET (1)

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.

Page 61: STOCK MARKET (1)

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.

Page 62: STOCK MARKET (1)

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.

Page 63: STOCK MARKET (1)

4. Design and Implementation of Stock Trading Algorithms

Page 64: STOCK MARKET (1)

What is Algorithmic Trading?

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

Page 65: STOCK MARKET (1)

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.

Page 66: STOCK MARKET (1)

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.

Page 67: STOCK MARKET (1)

Requirements

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

• Low latency order execution infrastructure.

Page 68: STOCK MARKET (1)

Different Types of Algorithms

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

etc.

Page 69: STOCK MARKET (1)

Statistical Arbitrage

Page 70: STOCK MARKET (1)

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.

Page 71: STOCK MARKET (1)

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

Page 72: STOCK MARKET (1)

Requirements for Arbitrage

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

Page 73: STOCK MARKET (1)

Different Arbitrage Strategies

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

Page 74: STOCK MARKET (1)

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.

Page 75: STOCK MARKET (1)

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.

Page 76: STOCK MARKET (1)

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.

Page 77: STOCK MARKET (1)

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

Page 78: STOCK MARKET (1)

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.

Page 79: STOCK MARKET (1)

Now wait for sometime……..

Till price difference again reaches to constant of Rs. 10

Page 80: STOCK MARKET (1)

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

Page 81: STOCK MARKET (1)

FRONTEND

Page 82: STOCK MARKET (1)

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

Page 83: STOCK MARKET (1)

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

Page 84: STOCK MARKET (1)

Conversion Reversal Strategy

Page 85: STOCK MARKET (1)

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

Page 86: STOCK MARKET (1)

EXAMPLE

Reliance Call option with Strike price 900 +

900

Reliance Put option with Strike price 900+

Future of Reliance

Call + Strike = Put + Future

Page 87: STOCK MARKET (1)

FRONTEND

Page 88: STOCK MARKET (1)

RISK MANAGEMENT

Page 89: STOCK MARKET (1)

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.

Page 90: STOCK MARKET (1)

QUANTITY CHECKS

Page 91: STOCK MARKET (1)

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.

Page 92: STOCK MARKET (1)

Value Checks

Page 93: STOCK MARKET (1)

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.

Page 94: STOCK MARKET (1)

Price Range Checks

Page 95: STOCK MARKET (1)

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.

Page 96: STOCK MARKET (1)

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.

Page 97: STOCK MARKET (1)

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.

Page 98: STOCK MARKET (1)

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.

Page 99: STOCK MARKET (1)

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.

Page 100: STOCK MARKET (1)

QUESTIONS?

Page 101: STOCK MARKET (1)