Fetching Financial Data

Having financial data handy is essential for carrying out algorithmic trading. Financial data can be both static and dynamic in nature. Static financial data is data that doesn't change during trading hours. Static data consists of lists of financial instruments, the attributes of financial instruments, the circuit limits of financial instruments, and the recorded close price of the last trading day. Dynamic financial data is data that may change continuously during trading hours. Dynamic data consists of market depth, the last traded prices, the time and quantity of financial instruments, and the recorded high and low prices of the day. This chapter includes recipes on fetching various types of financial data.

The following is a list of the recipes in this chapter:

  • Fetching the list of financial instruments
  • Attributes of a financial instrument
  • Expiry of financial instruments
  • Circuit limits of a financial instrument
  • The market depth of a financial instrument
  • The total pending buy quantity of a financial instrument
  • The total pending sell quantity of a financial instrument
  • The total volume traded for the day of a financial instrument
  • The last traded price of a financial instrument
  • The last traded time of a financial instrument
  • The last traded quantity of a financial instrument
  • The recorded open price of the day of a financial instrument
  • The recorded highest price of the day of a financial instrument
  • The recorded lowest price of the day of a financial instrument
  • The recorded close price of the last traded day of a financial instrument

Technical requirements

You will need the following to successfully execute the recipes in this chapter:

  • Python 3.7+
  • The pyalgotrading Python package ($ pip install pyalgotrading)

The latest Jupyter notebook for this chapter can be found on GitHub at https://github.com/PacktPublishing/Python-Algorithmic-Trading-Cookbook.

The following code will help you set up the broker connection with Zerodha, which will be used in all the recipes in this chapter. Please make sure you have followed these steps before trying out any recipes.

The first thing needed for setting up connectivity with the broker is getting the API keys. The broker would provide unique keys to each customer, typically as an api-key and api-secret key pair. These API keys are chargeable, usually on a monthly subscription basis. You need to get your copy of api-key and api-secret from the broker website before starting this. You can refer to Appendix I for more details.

Execute the following steps:

  1. Import the necessary modules:
>>> from pyalgotrading.broker.broker_connection_zerodha import BrokerConnectionZerodha
  1. Get the api_key and api_secret keys from the broker. These are unique to you and will be used by the broker to identify your Demat account:
>>> api_key = "<your-api-key>"
>>> api_secret = "<your-api-secret>"
>>> broker_connection = BrokerConnectionZerodha(api_key,
api_secret)

You will get the following output:

Installing package kiteconnect via pip...
Please login to this link to generate your request token: https://kite.trade/connect/login?api_key=<your-api-key>&v=3

If you are running this for the first time and kiteconnect is not installed, pyalgotrading will automatically install it for you. The final output of step 2 will be a link. Click on the link and log in with your Zerodha credentials. If the authentication is successful, you will see a link in your browser's address bar similar to this: https://127.0.0.1/?request_token=<alphanumeric-token>&action=login&status=success.

The following is an example:

https://127.0.0.1/?request_token=H06I6Ydv95y23D2Dp7NbigFjKweGwRP7&action=login&status=success
  1. Copy the alphanumeric-token and paste it in request_token:
>>> request_token = "<your-request-token>"
>>> broker_connection.set_access_token(request_token)

The broker_connection instance is now ready to perform API calls.

The pyalgotrading package supports multiple brokers and provides a connection object class per broker, with the same methods. It abstracts broker APIs behind a unified interface, so you need not worry about the underlying broker API calls and you can use all the recipes in this chapter as is. Only the procedure to set up the broker connection will vary from broker to broker. You can refer to the pyalgotrading documentation for setting up the broker connection if you are not using Zerodha as your broker. For Zerodha users, the previous steps will suffice.

Fetching the list of financial instruments

Financial instruments, also known as securities, are assets that can be traded in an exchange. In an exchange, there can be tens of thousands of financial instruments. The list of financial instruments is static in nature, as it doesn't change during the live trading hours. Financial instruments may change from time to time, but never within the same day. Having this data handy is the first step for algorithmic trading. This recipe shows how to fetch the list of financial instruments.

Getting ready

Make sure the broker_connection object is available in your Python namespace. Refer to the Technical requirements section of this chapter to set it up.

How to do it…

Fetch and display all the available financial instruments using broker_connection:

>>> instruments = broker_connection.get_all_instruments()
>>> instruments

We get the following output (your output may differ):

How it works…

This recipe fetches all the available financial instruments using the get_all_instruments() method of broker_connection, which returns a pandas.DataFrame object. This object is assigned to a new attribute, instruments, which is displayed in the output. This output may differ for you as new financial instruments are frequently added and existing ones expire regularly.

Attributes of a financial instrument

Financial instruments have various attributes that give more insight into the instrument, such as the trading symbol, exchange, segment, tick size, and so on. Some of these attributes are also needed while placing orders. This recipe lists and explains all the attributes supported by the broker. All the attributes are static, meaning they don't change during the live trading hours.

Getting ready

Make sure the instruments object is available in your Python namespace. Refer to the Fetching the list of financial instruments recipe of this chapter to set it up.

How to do it…

List all the attributes of a financial instrument provided by the broker:

>>> list(instruments.columns)

We get the following output:

['instrument_token',
'exchange_token',
trading-symbol,
'name',
'last_price',
'expiry',
'strike',
'tick_size',
'lot_size',
'instrument_type',
'segment',
exchange]

How it works…

The Fetching a list of financial instruments recipe fetches all the instruments as a pandas.DataFrame object. Calling its columns attribute returns all the columns available. Each column is an attribute for every financial instrument. You can find more details at https://kite.trade/docs/connect/v3/market-quotes/#csv-response-columns.

Expiry of financial instruments

Financial instruments may or may not have a fixed expiry date. If they do, they are last available for trading on their expiry date. Typically, instruments from a cash segment do not expire, whereas derivative instruments (those from the futures and options segment) have a short validity period, and expire on the given date. This recipe shows both types of instruments and how their expiry date can be fetched. An expiry date is static data, meaning it doesn't change during the live market hours.

Getting ready

Make sure the broker_connection and instruments objects are available in your Python namespace. Refer to the Technical requirements section of this chapter to set up broker_connection. Refer to the first recipe of this chapter to set up instruments.

How to do it…

We execute the following steps for this recipe:

  1. Get an instrument object using broker_connection:
>>> instrument1 = broker_connection.get_instrument('NSE', 
'TATASTEEL')
  1. Check and print whether instrument1 will expire:
>>> print(f'Instrument expires: {instrument1.will_expire()}')

We get the following output:

Instrument expires: False
  1. Get another instrument object using broker_connection:
>>> instrument2 = broker_connection.get_instrument('NFO-FUT', 
TATASTEEL20AUGFUT)

You shouldn't get any output here. This implies you have successfully fetched the instrument.

Please note that if you get the following output for this step, even after typing it correctly, please try this step with the latest available NFO-FUT segment script by referring to the table from the output in the Fetching the list of financial instruments recipe of this chapter:

ERROR: Instrument not found. Either it is expired and hence not available, or you have misspelled the "segment" and "tradingsymbol" parameters.

This can happen because the instrument, with tradingsymbol TATASTEEL20AUGFUT, was available at the time of writing this book, but has since expired and so isn't available anymore.

  1. Check and print whether instrument2 will expire:
>>> print(f'Instrument expires: {instrument2.will_expire()}')

We get the following output:

Instrument expires: True
  1. Print the expiry date of instrument2:
>>> print(f'Expiry date: {instrument2.expiry}')

We get the following output (your output may differ):

Expiry date: 2020-08-27

How it works…

Step 1 uses the get_instrument() method of the BrokerConnectionZerodha class to fetch an instrument and assign it to a new attribute, instrument1. This object is an instance of the Instrument class. The two parameters needed to call get_instrument are the exchange (NSE) and the trading symbol (TATASTEEL). In step 2, we check whether the instrument will expire using the will_expire() method. The output of this step is False. We repeat the same procedure in steps 3 and 4, this time for a different instrument, assigned to a new attribute, instrument2, which gives an output of True for the will_expire() method. This is shown in the output of step 4. Finally, in step 5, we fetch the expiry date of instrument2 using the expiry attribute.

Circuit limits of a financial instrument

Each financial instrument has a well-defined price band. The instrument price is expected to be within this price band for the day. During the market hours, if the instrument price breaches the band on the upper or lower side, trading may be halted for the instrument by the exchange for a certain time or the entire day. This is done to prevent the sudden rise or fall in an instrument's price within a single day. The upper edge of the price band is known as the upper circuit limit and the lower edge of the price band is known as the lower circuit limit. This data is static, meaning it doesn't change during the day. However, it can significantly change from one day to another. This recipe helps find the circuit limits for a financial instrument.

Getting ready

Make sure the broker_connection and instrument1 objects are available in your Python namespace. Refer to the Technical requirements section of this chapter to set up broker_connection. Refer to the Attributes of a financial instrument recipe of this chapter to set up instrument1.

How to do it…

Fetch and print the lower and upper circuit limits of instrument1:

>>> lower_circuit_limit, upper_circuit_limit = 
broker_connection.get_circuit_limits(instrument1)
>>> print(f'Lower circuit limit: {lower_circuit_limit}')
>>> print(f'Upper circuit limit: {upper_circuit_limit}')

We get the following output (your output may differ):

Lower circuit limit: 315.9
Upper circuit limit: 386

How it works…

The get_circuit_limits() method of the BrokerConnectionZerodha class fetches the lower and upper circuit limits as a tuple for the given financial instrument. This method takes an object of the Instrument type as a parameter. We use instrument1 as the parameter here.

The market depth of a financial instrument

The market depth of a financial instrument is a chronological list of data on buyers and sellers in the market. The buyers list is a list of prices and their respective quantities at which the buyers are willing to buy the instrument for. Similarly, the sellers list is a list of prices and their respective quantities at which the sellers are willing to sell the instrument for. If you are new to the concept of market depth, the explanation in the How it works… section of this recipe will give you more clarity.

Market depth helps in predicting where the price of an instrument is heading. It also helps to understand whether an order with a large quantity can change the price significantly or not. Market depth is dynamic in nature, meaning it changes constantly during the live trading hours. This recipe helps find out the market depth of a financial instrument in real time.

Getting ready

Make sure the broker_connection and instrument1 objects are available in your Python namespace. Refer to the Technical requirements section of this chapter to set up broker_connection. Refer to the Attributes of a financial instrument recipe of this chapter to set up instrument1.

How to do it…

Fetch and print the buy market depth and sell market depth of instrument1:

>>> buy_market_depth, sell_market_depth = 
broker_connection.get_market_depth(instrument1)
>>> print(f'Buy Market Depth: {buy_market_depth}')
>>> print(f'Sell Market Depth: {sell_market_depth}')

We get the following output (your output may differ):

Buy Market Depth:
orders price quantity
0 1 350.05 1
1 16 350.00 43294
2 5 349.95 1250
3 8 349.90 3134
4 5 349.85 1078

Sell Market Depth:
orders price quantity
0 1 350.10 25
1 7 350.15 1367
2 13 350.20 4654
3 13 350.25 2977
4 21 350.30 5798

How it works…

The get_market_depth() method of the BrokerConnectionZerodha class fetches the market depth for the given financial instrument. This method takes an object of the Instrument type as a parameter. We use instrument1 as the parameter here. The market depths are shown in separate tables for the buy side and the sell side

The buy market depth is a table of five entries or bids, in descending order of price. Each entry indicates an available buyer in the market at that point in time, with the price being offered and the quantity available at that price. 

The sell market depth is a table of five entries or bids, in ascending order of price. Each entry indicates an existing seller in the market at that point in time, with the price being offered and the quantity available at that price. 

When a buyer and seller match, the order is executed at the exchange and the entries are removed from the buy- and sell-side tables.

The total pending buy quantity of a financial instrument

The total pending buy quantity for a financial instrument is the sum total of the quantity of all the pending buy orders available at an instant. This data is dynamic in nature and may change at any moment during the live trading hours.

Getting ready

Make sure the broker_connection and instrument1 objects are available in your Python namespace. Refer to the Technical requirements section of this chapter to set up broker_connection. Refer to the Attributes of a financial instrument recipe of this chapter to set up instrument1.

How to do it…

Fetch and print the total pending buy quantity of instrument1:

>>> total_pending_buy_quantity = 
broker_connection.get_total_pending_buy_quantity(instrument1)
>>> print(f'Total pending BUY quantity: {total_pending_buy_quantity}')

We get the following output (your output may differ):

Total pending BUY quantity: 1319590

How it works…

The get_total_pending_buy_quantity() method of the BrokerConnectionZerodha class fetches the total buy quantity for the given financial instrument at any given moment. This method takes an object of the Instrument type as a parameter. We use instrument1 as the parameter here. 

The total pending sell quantity of a financial instrument

The total pending sell quantity for a financial instrument is the sum total of the quantity of all pending sell orders available at an instant. This data is dynamic in nature and may change at any moment during the live trading hours.

Getting ready

Make sure the broker_connection and instrument1 objects are available in your Python namespace. Refer to the Technical requirements section of this chapter to set up broker_connection. Refer to the Attributes of a financial instrument recipe of this chapter to set up instrument1.

How to do it…

Fetch and print the total pending sell quantity of instrument1:

>>> total_pending_sell_quantity = 
broker_connection.get_total_pending_sell_quantity(instrument1)
>>> print(f'Total pending SELL quantity: {total_pending_sell_quantity}')

We get the following output (your output may differ):

Total pending SELL quantity: 968602

How it works…

The get_total_pending_sell_quantity() method of the BrokerConnectionZerodha class fetches the total sell quantity of the given financial instrument at any given moment. This method takes an object of the Instrument type as a parameter. We use instrument1 as the parameter here. 

The total volume traded for the day of a financial instrument

The total volume traded for a financial instrument is the sum total of all quantities that were traded (bought and sold, but counted once) in the day. For example, if trader A buys 10 quantities of stock X from trader B, while trader C sells 20 quantities of the same stock X to trader D, the total volume traded for X would be 10 + 20 = 30. It won't be 10 + 10 + 20 + 20 = 60 because the contribution of the trade to the total volume is considered only once. This data is dynamic in nature and may increase at any moment during the live trading hours. 

Getting ready

Make sure the broker_connection and instrument1 objects are available in your Python namespace. Refer to the Technical requirements section of this chapter to set up broker_connection. Refer to the Attributes of a financial instruments recipe of this chapter to set up instrument1.

How to do it…

Fetch and print the total traded volume for the day of an instrument:

>>> total_volume_day = broker_connection.get_total_volume_day(instrument1)
>>> print(f'Total Volume for the day so far: {total_volume_day}')

We get the following output (your output may differ):

Total Volume for the day so far: 24416975

How it works…

The get_total_volume_day() method of the BrokerConnectionZerodha class fetches the total traded volume of the given financial instrument at any given moment since the beginning of the day. This method takes an object of the Instrument type as a parameter. We use instrument1 as the parameter here. 

The last traded price of a financial instrument

The last traded price (LTP) of a financial instrument is the latest price at which an order was executed for that instrument. It is essentially an indicator of the current price at which the instrument can be bought or sold (assuming the liquidity is good). As the description suggests, this data is dynamic in nature and it may change continuously during the live trading hours. This recipe shows how to fetch the LTP of a financial instrument.

Getting ready

Make sure the broker_connection and instrument1 objects are available in your Python namespace. Refer to the Technical requirements section of this chapter to set up broker_connection. Refer to the Attributes of a financial instrument recipe of this chapter to set up instrument1.

How to do it…

Fetch and print the LTP of instrument1:

>>> ltp = broker_connection.get_ltp(instrument1)
>>> print(f'Last traded price: {ltp}')

We get the following output (your output may differ):

Last traded price: 350.95

How it works…

The get_ltp() method of the BrokerConnectionZerodha class fetches the LTP of the given financial instrument at any given moment. This method takes an object of the Instrument type as a parameter. We use instrument1 as the parameter here. The fetched data is of the float type.

The last traded time of a financial instrument

The last traded time (LTT) of a financial instrument is the latest time at which an order was executed for that instrument. This data is dynamic in nature as it may change continuously during the live trading hours. This recipe helps fetch the LTT of a financial instrument.

Getting ready

Make sure the broker_connection and instrument1 objects are available in your Python namespace. Refer to the Technical requirements section of this chapter to set up broker_connection. Refer to the Attributes of a financial instrument recipe of this chapter to set up instrument1.

How to do it…

Fetch and print the LTT of instrument1:

>>> ltt = broker_connection.get_ltt(instrument1)
>>> print(f'Last traded time: {ltt}')

We get the following output (your output may differ):

Last traded time: 2020-07-17 14:42:54

How it works…

The get_ltt() method of the BrokerConnectionZerodha class fetches the LTT of the given financial instrument at any given moment. This method takes an object of the Instrument type as a parameter. We use instrument1 as the parameter here. The fetched data is an instance of the datetime.datetime class.

The last traded quantity of a financial instrument

The last traded quantity (LTQ) of a financial instrument is the quantity that was traded the last time an order was executed for that instrument. This data is dynamic in nature as it may change continuously during the live trading hours. This recipe demonstrates how to fetch the LTQ of a financial instrument.

Getting ready

Make sure the broker_connection and instrument1 objects are available in your Python namespace. Refer to the Technical requirements section of this chapter to set up broker_connection. Refer to the Attributes of a financial instrument recipe of this chapter to set up instrument1.

How to do it…

Fetch and print the LTQ of instrument1:

>>> ltq = broker_connection.get_ltq(instrument1)
>>> print(f'Last traded quantity: {ltq}')

We get the following output (your output may differ):

Last traded quantity: 19

How it works…

The get_ltq() method of the BrokerConnectionZerodha class fetches the LTQ of the given financial instrument at any given moment. This method takes an object of the Instrument type as a parameter. We use instrument1 as the parameter here. The fetched data is of the int type.

The recorded open price of the day of a financial instrument

Often, trading strategies use the current day opening price of a financial instrument as one of the first qualifying conditions before making decisions to place new trades. Comparing the current day's opening price with the previous day's close price may give a hint as to whether the market price is bound to rise or fall for the current day for an instrument. If the open price is significantly higher than the previous day's close price, the price may continue to rise for the day. Similarly, if the open price is significantly lower than the previous day's close price, the price may continue to fall for the day. The recorded open price data is static in nature, meaning it does not change during the live trading hours. This recipe shows how to fetch the current day's opening price of a financial instrument.

Getting ready

Make sure the broker_connection and instrument1 objects are available in your Python namespace. Refer to the Technical requirements section of this chapter to set up broker_connection. Refer to the Attributes of a financial instrument recipe of this chapter to set up instrument1.

How to do it…

Fetch and print the open price of the day of instrument1:

>>> open_price_day = broker_connection.get_open_price_day(instrument1)
>>> print(f'Open price today: {open_price_day}')

We get the following output (your output may differ):

Open price today: 346

How it works…

The get_open_price_day() method of the BrokerConnectionZerodha class fetches the open price of the day for the given financial instrument. This method takes an object of the Instrument type as a parameter. We use instrument1 as the parameter here. The fetched data is of the float type.

The recorded highest price of the day of a financial instrument

Often, trading strategies use the current day's highest price of a financial instrument as one of the qualifying conditions before making decisions to place new trades. This data is dynamic in nature as it may change continuously during the live trading hours. This recipe shows how to fetch the current day's highest recorded price of a financial instrument.

Getting ready

Make sure the broker_connection and instrument1 objects are available in your Python namespace. Refer to the Technical requirements section of this chapter to set up broker_connection. Refer to the Attributes of a financial instrument recipe of this chapter to set up instrument1.

How to do it…

Fetch and print the recorded highest price of the day of instrument1:

>>> high_price_day = broker_connection.get_high_price_day(instrument1)
>>> print(f'Highest price today: {high_price_day}')

We get the following output. Your output may differ:

Highest price today: 356.8

How it works…

The get_high_price_day() method of the BrokerConnectionZerodha class fetches the highest recorded price of the day for the given financial instrument. This method takes an object of the Instrument type as a parameter. We use instrument1 as the parameter here. The fetched data is of the float type.

The recorded lowest price of the day of a financial instrument

Often, trading strategies use the current day's lowest price of a financial instrument as one of the qualifying conditions before making decisions to place new trades. This data is dynamic in nature as it may change continuously during the live trading hours. This recipe demonstrates how to fetch the current day's lowest recorded price of a financial instrument.

Getting ready

Make sure the broker_connection and instrument1 objects are available in your Python namespace. Refer to the Technical requirements section of this chapter to set up broker_connection. Refer to the Attributes of a financial instrument recipe of this chapter to set up instrument1.

How to do it…

Fetch and print the recorded lowest price of the day of instrument1:

>>> low_price_day = broker_connection.get_low_price_day(instrument1)
>>> print(f'Lowest price today: {low_price_day}')

We get the following output (your output may differ):

Lowest price today: 345.15

How it works…

The get_low_price_day() method of the BrokerConnectionZerodha class fetches the lowest recorded price of the day for the given financial instrument. This method takes an object of the Instrument type as a parameter. We use instrument1 as the parameter here. The fetched data is of the float type.

The recorded close price of the last traded day of a financial instrument

Often, trading strategies use the previous day's closing price of a financial instrument as one of the first qualifying conditions before making decisions to place trades. Comparing the current day's opening price with the previous day’s close price may give a hint as to whether the market price is bound to rise or fall for the current day for an instrument. If the open price is significantly higher than the previous day's close price, the price may continue to rise for the day. Similarly, if the open price is significantly lower than the previous day's close price, the price may continue to fall for the day. The recorded close price data is static in nature, meaning it does not change during the live trading hours. This recipe shows how to fetch the previous day's close price of a financial instrument.

Getting ready

Make sure the broker_connection and instrument1 objects are available in your Python namespace. Refer to the Technical requirements section of this chapter to set up broker_connection. Refer to the Attributes of a financial instrument recipe of this chapter to set up instrument1.

How to do it…

Fetch and print the recorded close price of the last trading day of instrument1:

>>> close_price_last_day = 
broker_connection.get_close_price_last_day(instrument1)
>>> print(f'Close price of last trading day: {close_price_last_day}')

We get the following output (your output may differ):

Close price of last trading day: 341.65

How it works…

The get_close_price_day() method of the BrokerConnectionZerodha class fetches the close price of the previous trading day for the given financial instrument. This method takes an object of the Instrument type as a parameter. We use instrument1 as the parameter here. The fetched data is of the float type.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset