It looks like you're new here. If you want to get involved, click one of these buttons!
from itertools import islice
from flask import Flask, jsonify, request
import kiteconnect
import KiteSettings
import json
from kiteconnect import KiteConnect, KiteTicker
import math
from flask_cors import CORS
from datetime import datetime
import time
import math
import csv
from flask_restful import Api, Resource, reqparse
import pandas as pd
import os
app = Flask(__name__)
CORS(app)
# variable used for live tick data
token_to_instrument = {}
myTicks = []
live_data = []
token = []
csvFilePath = r'NFO_Instruments.csv'
jsonFilePath = r'data.json'
subscribetokens = []
subscribeinstrumentLiveData = []
isntrument_token = []
# function to collect the ticks. IMP - Don't add any code here else it will start giving you errors.
def on_ticks(ws, ticks):
for tick in ticks:
myTicks.append(tick)
live_data.append(tick)
# function to subscribe the instruments. Instrument list is based on the selection made by the user
def on_connect(ws, repsonse):
ws.subscribe(subscribetokens)
ws.set_mode(ws.MODE_FULL, subscribetokens)
def readinstruments():
kite = connect()
print("into read instrument")
@app.route('/')
def hello_world():
kite = connect()
def connect():
kite = KiteConnect(KiteSettings.api_key)
kite.set_access_token(KiteSettings.access_token)
return kite
# Function to read the atm values based on user input
@app.route("/ATMValue")
def ATMValue():
kite = connect()
userInput = json.loads(request.args.get('payload'))
# Define instrument-specific settings
# Get the current strike price
# Get the ATM strike price
# Fetch instrument dump for the specified expiry date and instrument type
# Fetch ATM values above and below current Nifty index
# check for the subscribed token with allready subscribed tokens
for token in isntrument_token:
if token not in subscribetokens:
subscribetokens.append(int(token))
# print("sub token",subscribetokens)
try:
time.sleep(2)
kws = KiteTicker(KiteSettings.api_key,
KiteSettings.access_token, reconnect=True)
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.connect(threaded=True)
time.sleep(2)
except:
print("error in in socket connection")
# print(atm_values.sort(reverse = True))
# Create a dictionary to hold the CE and PE instruments
# Convert the dictionary to a JSON object
json_result = json.dumps(result, default=str)
return json_result
@app.route("/send_latest_data")
def send_latest_data():
instrumentTokens = json.loads(request.args.get('payload'))
latest_data = []
for tick in live_data:
if tick['instrument_token'] in instrumentTokens:
latest_data.append(tick)
# Create a dictionary with instrument tokens as keys and latest tick data as values
latest_data_dict = {}
for tick in latest_data:
instrument_token = tick['instrument_token']
latest_data_dict[instrument_token] = tick
# Convert the dictionary to a JSON object
json_result = json.dumps(latest_data_dict, default=str)
return json_result
if __name__ == "__main__":
app.run(debug=True)
readinstruments()
kite = connect()
on_error
method to get the disconnection error message. Go through the example code here.127.0.0.1 - - [23/Aug/2023 10:24:14] "GET /send_latest_data?payload=[12984834,12985346,12991490,12992002,12998146,13000706,13004290,13006338,13007874,13014530,13018114,13018626,13021186,13021698,13022210,13022722,13023234,13031938,13032450,13033474,13035010] HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2023 10:24:14] "GET /send_latest_data?payload=[12985090,12985602,12991746,12992258,12998402,13000962,13004546,13006594,13008130,13014786,13018370,13018882,13021442,13021954,13022466,13022978,13023490,13032194,13032706,13034754,13035266] HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2023 10:24:16] "GET /send_latest_data?payload=[12985090,12985602,12991746,12992258,12998402,13000962,13004546,13006594,13008130,13014786,13018370,13018882,13021442,13021954,13022466,13022978,13023490,13032194,13032706,13034754,13035266] HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2023 10:24:16] "GET /send_latest_data?payload=[12984834,12985346,12991490,12992002,12998146,13000706,13004290,13006338,13007874,13014530,13018114,13018626,13021186,13021698,13022210,13022722,13023234,13031938,13032450,13033474,13035010] HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2023 10:24:18] "GET /send_latest_data?payload=[12985090,12985602,12991746,12992258,12998402,13000962,13004546,13006594,13008130,13014786,13018370,13018882,13021442,13021954,13022466,13022978,13023490,13032194,13032706,13034754,13035266] HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2023 10:24:18] "GET /send_latest_data?payload=[12984834,12985346,12991490,12992002,12998146,13000706,13004290,13006338,13007874,13014530,13018114,13018626,13021186,13021698,13022210,13022722,13023234,13031938,13032450,13033474,13035010] HTTP/1.1" 200 -
Connection error: 1006 - connection was closed uncleanly (None)
ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (None)
ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (WebSocket opening handshake timeout (peer did not finish the opening handshake in time))
ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (WebSocket opening handshake timeout (peer did not finish the opening handshake in time))