@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.
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.