0% found this document useful (0 votes)
0 views

cpchain

The document is a Python script that sets up a WebSocket connection to receive live market data and updates a feed with tick data. It includes functions to handle feed updates, order updates, and to retrieve expiry dates for options. Additionally, it processes option chains and identifies the closest symbols based on the last traded price (LTP).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

cpchain

The document is a Python script that sets up a WebSocket connection to receive live market data and updates a feed with tick data. It includes functions to handle feed updates, order updates, and to retrieve expiry dates for options. Additionally, it processes option chains and identifies the closest symbols based on the last traded price (LTP).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

from time import sleep

import time
import datetime
from datetime import datetime,timedelta

feed_opened = False
socket_opened = False
feedJson={}

def event_handler_feed_update(tick_data):
UPDATE = False
if 'tk' in tick_data:
token = tick_data['tk']
timest = datetime.fromtimestamp(int(tick_data['ft'])).isoformat()
feed_data = {'tt': timest}
if 'lp' in tick_data:
feed_data['ltp'] = float(tick_data['lp'])
if 'ts' in tick_data:
feed_data['Tsym'] = str(tick_data['ts'])
if 'oi' in tick_data:
feed_data['openi'] = float(tick_data['oi'])
if 'poi' in tick_data:
feed_data['pdopeni'] = str(tick_data['poi'])
if 'v' in tick_data:
feed_data['Volume'] = str(tick_data['v'])
if feed_data:
UPDATE = True
if token not in feedJson:
feedJson[token] = {}
feedJson[token].update(feed_data)
if UPDATE:
pass#print(f'Token:{token} Feed:{feedJson[token]}')

def event_handler_order_update(order_update):
pass#print(f"order feed {order_update}")

def open_callback():
global feed_opened
feed_opened = True
print("Websocket opened")

def setupWebSocket():
global feed_opened
print("waiting for socket opening")
api.start_websocket(order_update_callback=event_handler_order_update,
subscribe_callback=event_handler_feed_update,
socket_open_callback=open_callback)
while(feed_opened==False):
pass

setupWebSocket()

def get_expiry_dates(exchange, symbol):


import re
import datetime
sd = api.searchscrip(exchange, symbol)
sd = (sd['values'])
tsym_values = [Symbol['tsym'] for Symbol in sd]
dates = [re.search(r'\d+[A-Z]{3}\d+', tsym).group() for tsym in tsym_values]
formatted_dates = [datetime.datetime.strptime(date, '%d%b%y').strftime('%Y-%m-
%d') for date in dates]
sorted_formatted_dates = sorted(formatted_dates)
sorted_dates = [datetime.datetime.strptime(date, '%Y-%m-%d').strftime('%d%b
%y').upper() for date in sorted_formatted_dates]
expiry_dates = sorted_dates
return expiry_dates

expiry_date = get_expiry_dates('NFO', 'NIFTY')[0]


print(expiry_date)

ret = api.get_quotes(exchange='NSE', token='26000')


ltp = ret.get("lp")
ltp = float(ltp)
ltp_str = str(ltp)
sym = ret.get("symname")
TYPE = "P"
Strike = int(round(ltp/50,0)*50)
For_token = sym+expiry_date+TYPE+str(Strike)
optionchain = api.get_option_chain('NFO', For_token , Strike, 20)
optionchainsym = (optionchain['values'])
for Symbol in optionchainsym:
(Symbol['token'])

token= [Symbol['token'] for Symbol in optionchainsym]


modified_tokens = []
for Symbol in optionchainsym:
token = Symbol['token']
modified_token = 'NFO|' + token
modified_tokens.append(modified_token)

print(modified_tokens)

def get_closest_ltp_symbols(ltp_value):
df = api.subscribe(modified_tokens)
df = pd.DataFrame.from_dict(feedJson,orient='index', columns=['ltp',
'Tsym','openi','pdopeni'])
df['diff'] = abs(df['ltp'] - ltp_value)
df_c = df[df['Tsym'].str.contains('C')]
df_p = df[df['Tsym'].str.contains('P')]
min_diff_c = df_c['diff'].min()
min_diff_p = df_p['diff'].min()
closest_symbols_c = df_c[df_c['diff'] == min_diff_c]['Tsym'].values[0]
closest_symbols_p = df_p[df_p['diff'] == min_diff_p]['Tsym'].values[0]
api.unsubscribe(modified_tokens)
return closest_symbols_c, closest_symbols_p
df = api.subscribe(modified_tokens)
df = pd.DataFrame.from_dict(feedJson,orient='index', columns=['ltp',
'Tsym','openi','pdopeni'])
df

feedJson

closest_symbols_c, closest_symbols_p = get_closest_ltp_symbols(500)


print(closest_symbols_c)
print(closest_symbols_p)

You might also like