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

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.

主站蜘蛛池模板: 吉水县| 兴仁县| 高清| 易门县| 桐庐县| 景宁| 任丘市| 泽州县| 赤峰市| 利津县| 罗源县| 福安市| 金溪县| 鄂托克旗| 淮安市| 桐柏县| 西平县| 兴义市| 北碚区| 瓮安县| 汕尾市| 交口县| 清苑县| 扎囊县| 保康县| 望江县| 张北县| 新兴县| 西华县| 扎赉特旗| 黔江区| 双柏县| 永春县| 晋州市| 邓州市| 合作市| 顺昌县| 凤冈县| 孙吴县| 若羌县| 十堰市|