maybe somebody more familiar with Python should help you
In general
I see that:
0- You use "global" parameters as way to transmit information from proc to proc (or main)
That is very dangerous, global are the worst thing on earth when the language allow object programming, it should simply not exist!!
in particular you should at least do
??? prixtick =0
??? bid= 0
??? ask =0
in your
for i in range(j)
Loop
1- you use time.sleep(1)
That's a very bad habit, because IBK use an 'asynchronous mode' (Subscribing to events) and nothing guaranty that you will get any answer within your sleep.
asynchronous? is a good thing because you are sure to catch faster and in time what happens, but bad in the sense that if nothing happens you do some processing anyway and on old or false data.
This is a different philosophy of coding: Unless you use a multithread/multiprocess approach (highly commendable) at least you should put a maximum of your processing in the handler of the callback,? Surely not in a main loop.
IBKR will queue events in case you don't process them fast enough (well... to a certain extent)
2- You only did implement tickPrice
While it maybe enough it is a bad practice, you better implement a default for all tick call back
?>>tickSize(id,...), >>tickGeneric(id,...), >>tickPrice(id,...), >>marketDataType(id,...), >>tickSnapshotEnd(id)
Because it may happens (again not familiar with python) that you hang on next call from IBKR that would be tickSize.
All that are old in my mind, I did wrote an abstraction layer and no longer remember details. But I see a comment in my code that may shade some light on this:
/*??? For bid, ask, and last data, there will always be a IBApi::EWrapper::tickSize callback following each IBApi::EWrapper::tickPrice.
?? ?IBApi::EWrapper::tickSize is also invoked every time there is a change in the size of the last traded price.
?? ?For that reason, there will be duplicate tickSize events when both the price and size of a trade changes.
*/
3- I don't use reqMktData to get tick data, I only use it or rates/RT volumes and alike kind of aggregated data set.
I use reqTickByTickData
you can specify the number of tick to look in past , 1 is a valid value.? and you should listen to tickByTickBidAsk? this is safer!
I am even unable to understand how to qualify as Bid or Ask what you receive back from tickPrice? as your request? didn't specify anything about that.
But again you need to implement 2 handler for historical ticks
From comments in my code:
/*
https://interactivebrokers.github.io/tws-api/tick_data.html
?if a non-zero value is input for the argument numberOfTicks in IBApi::EClient::reqTickByTickData,
?historical tick data is first returned to one of the functions
?IBApi::EWrapper::historicalTicksLast, IBApi::EWrapper::historicalTicksBidAsk, or IBApi::EWrapper::historicalTicks, respectively.
*/
4- Tick Types is an important matter
you didn't specify any. While it give you a default result, you better decide by yourself what you want to listen to.
see: