官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 应城市| 连江县| 富平县| 诏安县| 马鞍山市| 宁安市| 五原县| 施甸县| 龙泉市| 阜南县| 徐水县| 万山特区| 宜章县| 化隆| 平远县| 灵寿县| 新安县| 巴青县| 新干县| 若羌县| 汤原县| 龙口市| 洪江市| 宝山区| 扎兰屯市| 嵊州市| 怀化市| 郓城县| 丹巴县| 咸宁市| 博白县| 新昌县| 阜南县| 介休市| 长沙市| 永嘉县| 徐州市| 黔东| 清新县| 上犹县| 文水县|