@sujith , @rakeshr Am facing Historical data last 1 Minute Close Price is inconsistent with Kite API data vs Kite Candle in Live Market
- I used to fetch last 5 One Min data in historic data in live Market (ie, every 5 min I hit the historic API) - Where first 4 One Min Close price is correct, but the 5th one Min Close Price is inconsistent with Kite API data vs Kite Candle - As referred through other thread am using the From and To date as below - from_date='XXXX-XX-XX 09:15:00', to_date='XXXX-XX-XX 09:24:59' - Kindly assist me
Below is the request Historic API request (used for BN Option data)
bruv, this seems to be a common problem many people are facing.
Couple of things: - API does not directly tell you which tick is the last tick of that minute. - So, you need to have your custom condition to check, the naive way is to add some condition like 'if second==59', I am not sure how you are checking the last tick, if you can provide me the code, I might be able to help you with the specifics. - The naive way I just mentioned where you basically check something like 'if second==59' has a few problems: 1. in a one second interval, the API usually pushes 1-4 ticks depending on liquidity of the instrument itself so if you want the close data exactly as that of historical data, you can't use the 'if second==59' method because you don't know how many ticks will be pushed in that last 59th second. 2. Next, if the stock is not liquid enough, you might not even get one tick at the 59th second, the last quote can be at the 45th second or the 52nd second and you will miss that tick.
The better way to get the exact last close data: - Subscribe to the API in full mode. - Here there will be a field 'exchange_timestamp' this is the timestamp that is marked at the exchange itself, this is what we will use. - Now we start collecting data for the entire minute and store it somewhere, however you prefer - Better explained with an example: Start with collecting all the ticks somewhere Keep a variable variable that holds the current minute 'current_minute = exchange_timestamp.minute', keep this as the last line in the code block where you receive new ticks. Next, at each new tick received, check something like "if exchange_timestamp.minute != current_minute", if they are not equal, then you can mark the close, the stored tick's last tick is your close, so: open = stored_ticks[0] high = stored_ticks.max() low = stored_ticks.min() close = stored_ticks[-1] don't forget to empty out the the list or whatever data structure you are using, here, stored_ticks = []
Note that doing as above will have a lag of about ~1 second lag in marking the close because we can't confirm the close until we receive the next minute's first tick. So if you are doing something that requires sub 1 second marking of the close, then don't use the above the method and I am not really sure what the work around for that would be.
# Variables to store ticks and current minute stored_ticks = [] current_minute = None
# Function to process ticks def on_ticks(ws, ticks): global stored_ticks, current_minute
for tick in ticks: exchange_timestamp = tick['exchange_timestamp'] tick_minute = exchange_timestamp.minute
# Initialize current_minute on the first tick if current_minute is None: current_minute = tick_minute
# Check if the minute has changed if tick_minute != current_minute: # Process the stored ticks for the previous minute if stored_ticks: open_price = stored_ticks[0]['last_price'] high_price = max(tick['last_price'] for tick in stored_ticks) low_price = min(tick['last_price'] for tick in stored_ticks) close_price = stored_ticks[-1]['last_price']
# Store the current tick stored_ticks.append(tick)
# Function to handle connection open event def on_connect(ws, response): print("WebSocket connected") ws.subscribe([your_instrument_token]) ws.set_mode(ws.MODE_FULL, [your_instrument_token])
# Function to handle connection close event def on_close(ws, code, reason): print("WebSocket closed", code, reason)
Explanation Initialization: Initialize KiteConnect and KiteTicker with your API key and access token. Define variables to store ticks and the current minute. Tick Processing: In the on_ticks function, process each tick received. Use the exchange_timestamp to determine the minute of each tick. If the minute changes, process the stored ticks to calculate the open, high, low, and close prices for the previous minute. Reset the stored ticks and update the current minute. WebSocket Events: Define functions to handle WebSocket connection and disconnection events. Subscribe to the instrument in full mode to receive detailed tick data. Running the Script:
Connect to the WebSocket and keep the script running to continuously process ticks.
Couple of things:
- API does not directly tell you which tick is the last tick of that minute.
- So, you need to have your custom condition to check, the naive way is to add some condition like 'if second==59', I am not sure how you are checking the last tick, if you can provide me the code, I might be able to help you with the specifics.
- The naive way I just mentioned where you basically check something like 'if second==59' has a few problems:
1. in a one second interval, the API usually pushes 1-4 ticks depending on liquidity of the instrument itself so if you want the close data exactly as that of historical data, you can't use the 'if second==59' method because you don't know how many ticks will be pushed in that last 59th second.
2. Next, if the stock is not liquid enough, you might not even get one tick at the 59th second, the last quote can be at the 45th second or the 52nd second and you will miss that tick.
The better way to get the exact last close data:
- Subscribe to the API in full mode.
- Here there will be a field 'exchange_timestamp' this is the timestamp that is marked at the exchange itself, this is what we will use.
- Now we start collecting data for the entire minute and store it somewhere, however you prefer
- Better explained with an example:
Start with collecting all the ticks somewhere
Keep a variable variable that holds the current minute 'current_minute = exchange_timestamp.minute', keep this as the last line in the code block where you receive new ticks.
Next, at each new tick received, check something like "if exchange_timestamp.minute != current_minute", if they are not equal, then you can mark the close, the stored tick's last tick is your close, so:
open = stored_ticks[0]
high = stored_ticks.max()
low = stored_ticks.min()
close = stored_ticks[-1]
don't forget to empty out the the list or whatever data structure you are using, here, stored_ticks = []
Note that doing as above will have a lag of about ~1 second lag in marking the close because we can't confirm the close until we receive the next minute's first tick.
So if you are doing something that requires sub 1 second marking of the close, then don't use the above the method and I am not really sure what the work around for that would be.
Good luck out there in the markets.
sample code...
import datetime
from kiteconnect import KiteConnect, KiteTicker
# Initialize KiteConnect
kite = KiteConnect(api_key="your_api_key")
kite.set_access_token("your_access_token")
# Initialize KiteTicker
kws = KiteTicker(api_key="your_api_key", access_token="your_access_token")
# Variables to store ticks and current minute
stored_ticks = []
current_minute = None
# Function to process ticks
def on_ticks(ws, ticks):
global stored_ticks, current_minute
for tick in ticks:
exchange_timestamp = tick['exchange_timestamp']
tick_minute = exchange_timestamp.minute
# Initialize current_minute on the first tick
if current_minute is None:
current_minute = tick_minute
# Check if the minute has changed
if tick_minute != current_minute:
# Process the stored ticks for the previous minute
if stored_ticks:
open_price = stored_ticks[0]['last_price']
high_price = max(tick['last_price'] for tick in stored_ticks)
low_price = min(tick['last_price'] for tick in stored_ticks)
close_price = stored_ticks[-1]['last_price']
print(f"Minute: {current_minute}, Open: {open_price}, High: {high_price}, Low: {low_price}, Close: {close_price}")
# Reset stored_ticks and update current_minute
stored_ticks = []
current_minute = tick_minute
# Store the current tick
stored_ticks.append(tick)
# Function to handle connection open event
def on_connect(ws, response):
print("WebSocket connected")
ws.subscribe([your_instrument_token])
ws.set_mode(ws.MODE_FULL, [your_instrument_token])
# Function to handle connection close event
def on_close(ws, code, reason):
print("WebSocket closed", code, reason)
# Assign the event handler functions
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
# Connect to WebSocket
kws.connect(threaded=True)
# Keep the script running
while True:
pass
Explanation
Initialization:
Initialize KiteConnect and KiteTicker with your API key and access token.
Define variables to store ticks and the current minute.
Tick Processing:
In the on_ticks function, process each tick received.
Use the exchange_timestamp to determine the minute of each tick.
If the minute changes, process the stored ticks to calculate the open, high, low, and close prices for the previous minute.
Reset the stored ticks and update the current minute.
WebSocket Events:
Define functions to handle WebSocket connection and disconnection events.
Subscribe to the instrument in full mode to receive detailed tick data.
Running the Script:
Connect to the WebSocket and keep the script running to continuously process ticks.