wrapper - how to get series of requests in ibapi on python -


i new both in python , ibapi, have ask interactive broker itself, told me :

the self.done defined in parent class eclient used indicate current api connection "done". should not use indicator stop loop build yourself. can keep 1 api client connection live until have finished downloaded historical data need. if need slow down pace in requesting data, can use other thread control in python, such sleep() function, add more wait time in loop not send historical data requests contracts @ once.

so think error came from

self.done = true 

this code

from ibapi import wrapper ibapi.client import eclient ibapi.wrapper import ewrapper ibapi.contract import contract ibcontract threading import thread import queue import datetime ibapi.utils import iswrapper #just decorator ibapi.common import * ibapi.contract import * ibapi.ticktype import *  class testapp(wrapper.ewrapper, eclient):  def __init__(self):     wrapper.ewrapper.__init__(self)     eclient.__init__(self, wrapper=self)   @iswrapper def historicaldata(self, reqid:int, bar: bardata):     print("historicaldata. ", reqid, " date:", bar.date, "open:", bar.open,           "high:", bar.high, "low:", bar.low, "close:", bar.close, "volume:", bar.volume,           "count:", bar.barcount, "wap:", bar.average)     if  all_data.iloc[reqid,7] == 0:         all_data.iloc[reqid,7] = bar.close     self.done = true  @iswrapper def historicaldataend(self, reqid: int, start: str, end: str):     super().historicaldataend(reqid, start, end)     print("historicaldataend ", reqid, "from", start, "to", end)  @iswrapper def historicaldataupdate(self, reqid: int, bar: bardata):     print("historicaldataupdate. ", reqid, " date:", bar.date, "open:", bar.open,           "high:", bar.high, "low:", bar.low, "close:", bar.close, "volume:", bar.volume,           "count:", bar.barcount, "wap:", bar.average)  def main():     t = time()     max_amount_per_iter = 70 #max number per iter save cost     max_iter = ceil(len(all_data)/max_amount_per_iter)     in range (0,max_iter):         print('====================round : ',i+1,'===============================')         app = testapp()         app.connect("127.0.0.1", 7496, clientid=i)         print("serverversion:%s connectiontime:%s" % (app.serverversion(),app.twsconnectiontime()))         j in range (0,min(max_amount_per_iter,len(all_data)-i*max_amount_per_iter)):             print(j+i*70)         app.i = j+i*max_amount_per_iter          contract = contract()         contract.symbol = all_data.iloc[app.i,0]         contract.sectype = all_data.iloc[app.i,1]         contract.currency = all_data.iloc[app.i,3]         contract.exchange = all_data.iloc[app.i,2]                      app.reqhistoricaldata(app.i, contract, "","1 w", "1 day", "adjusted_last", 1, 1, false, [])           app.run()      sleep(1)     app.disconnect()     sleep(0.02)     print('=========end round : ',i+1,'with time :',time() - t,'==============')  if __name__ == "__main__":     main() 

i hope see suggestion how pass next iteration without using self.done or disconnect tws. if don't include self.done program run on infinite loop @ first i=0,j=0 iteration, nobody tells quit.

so if don't know direct answer can solve mine, can suggest:

  1. is there other flow control can use in case?

  2. or cancelhistoricaldata command, if can use case?

  3. or there way build class in class , execute self.done = true in subclass out disconnect id in class?

ps. have same problem requestmarketdata

update v2 result try print for

====================round :  1 =============================== step2 serverversion:124 connectiontime:b'20170821 22:34:09 ict' 0 error:root:error -1 2104 market data farm connection ok:hfarm error:root:error -1 2104 market data farm connection ok:jfarm error:root:error -1 2104 market data farm connection ok:usfuture error:root:error -1 2104 market data farm connection ok:eufarm error:root:error -1 2104 market data farm connection ok:cashfarm error:root:error -1 2104 market data farm connection ok:usfarm.us error:root:error -1 2104 market data farm connection ok:usfarm error:root:error -1 2106 hmds data farm connection ok:ilhmds error:root:error -1 2106 hmds data farm connection ok:euhmds error:root:error -1 2106 hmds data farm connection ok:fundfarm error:root:error -1 2106 hmds data farm connection ok:ushmds historicaldata.  0  date: 20170815 open: 67.2 high: 68.24 low: 66.99 close: 68.02 volume: 13268 count: 9453 wap: 67.8105 historicaldata.  0  date: 20170816 open: 68.28 high: 68.91 low: 67.45 close: 68.01 volume: 11950 count: 9723 wap: 68.457 historicaldata.  0  date: 20170817 open: 67.8 high: 68.53 low: 66.83 close: 66.89 volume: 11407 count: 9432 wap: 67.504 historicaldata.  0  date: 20170818 open: 66.91 high: 67.25 low: 66.57 close: 66.78 volume: 12091 count: 9637 wap: 66.8445 historicaldata.  0  date: 20170821 open: 66.9 high: 66.96 low: 66.14 close: 66.28 volume: 3317 count: 2541 wap: 66.3425 historicaldataend  0 20170814  22:34:14 20170821  22:34:14 error:root:error 1 504 not connected error:root:error 2 504 not connected error:root:error 3 504 not connected error:root:error 4 504 not connected error:root:error 5 504 not connected error:root:error 6 504 not connected error:root:error 7 504 not connected error:root:error 8 504 not connected error:root:error 9 504 not connected error:root:error 10 504 not connected error:root:error 11 504 not connected error:root:error 12 504 not connected error:root:error 13 504 not connected error:root:error 14 504 not connected error:root:error 15 504 not connected error:root:error 16 504 not connected error:root:error 17 504 not connected error:root:error 18 504 not connected error:root:error 19 504 not connected error:root:error 20 504 not connected error:root:error 21 504 not connected error:root:error 22 504 not connected error:root:error 23 504 not connected error:root:error 24 504 not connected error:root:error 25 504 not connected error:root:error 26 504 not connected error:root:error 27 504 not connected error:root:error 28 504 not connected error:root:error 29 504 not connected error:root:error 30 504 not connected error:root:error 31 504 not connected error:root:error 32 504 not connected error:root:error 33 504 not connected error:root:error 34 504 not connected error:root:error 35 504 not connected error:root:error 36 504 not connected error:root:error 37 504 not connected error:root:error 38 504 not connected error:root:error 39 504 not connected error:root:error 40 504 not connected error:root:error 41 504 not connected error:root:error 42 504 not connected error:root:error 43 504 not connected error:root:error 44 504 not connected error:root:error 45 504 not connected error:root:error 46 504 not connected error:root:error 47 504 not connected error:root:error 48 504 not connected error:root:error 49 504 not connected error:root:error 50 504 not connected error:root:error 51 504 not connected error:root:error 52 504 not connected error:root:error 53 504 not connected error:root:error 54 504 not connected error:root:error 55 504 not connected error:root:error 56 504 not connected error:root:error 57 504 not connected error:root:error 58 504 not connected error:root:error 59 504 not connected error:root:error 60 504 not connected error:root:error 61 504 not connected error:root:error 62 504 not connected error:root:error 63 504 not connected error:root:error 64 504 not connected error:root:error 65 504 not connected error:root:error 66 504 not connected error:root:error 67 504 not connected error:root:error 68 504 not connected error:root:error 69 504 not connected 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 =========end round :  1 time : 7.807971477508545 ============== ====================round :  2 =============================== step2 error:root:error -1 2104 market data farm connection ok:hfarm error:root:error -1 2104 market data farm connection ok:jfarm error:root:error -1 2104 market data farm connection ok:usfuture error:root:error -1 2104 market data farm connection ok:eufarm error:root:error -1 2104 market data farm connection ok:cashfarm error:root:error -1 2104 market data farm connection ok:usfarm.us error:root:error -1 2104 market data farm connection ok:usfarm error:root:error -1 2106 hmds data farm connection ok:ilhmds error:root:error -1 2106 hmds data farm connection ok:euhmds error:root:error -1 2106 hmds data farm connection ok:fundfarm error:root:error -1 2106 hmds data farm connection ok:ushmds serverversion:124 connectiontime:b'20170821 22:34:17 ict' 70 historicaldata.  70  date: 20170815 open: 117.23 high: 117.62 low: 116.58 close: 117.43 volume: 7232 count: 6205 wap: 117.1295 historicaldata.  70  date: 20170816 open: 117.49 high: 119.59 low: 117.03 close: 119.25 volume: 16468 count: 11498 wap: 118.8035 historicaldata.  70  date: 20170817 open: 119.19 high: 119.48 low: 116.46 close: 116.47 volume: 12285 count: 10072 wap: 117.4645 historicaldata.  70  date: 20170818 open: 116.0 high: 117.84 low: 115.46 close: 116.88 volume: 14917 count: 10824 wap: 116.9795 historicaldata.  70  date: 20170821 open: 116.85 high: 117.4 low: 116.15 close: 116.77 volume: 3471 count: 2599 wap: 116.5535 historicaldataend  70 20170814  22:34:19 20170821  22:34:19 error:root:error 71 504 not connected error:root:error 72 504 not connected 

i'd suggest little redesign here.

in example, disconnect api using self.done=true right after first data. since requested data arrives in rapid events, data request before so. result seen in output: got , processed data of first request, found disconnected. repeats i...

  1. remove 'self.done=true' line. good, don't want that.

  2. restructure code, waits historicaldataend event before sends next request api. api can process parallel requests, server not tolerate frequent historical data requests. if send requests fast, you'll pacing violation error. sleep() between requests might necessary too.

  3. i assume app.run() waits disconnection, that's why needed 'done' flag manipulation. remove line too. if call necessary catch messages (i don't know testapp class , base), make sure call disconnect() after requested data arrived , processed, practically in historicaldataend() . case, reconnection needed before next loop, missing after app.run() call within j...

here suggested modifications:

from ibapi import wrapper ibapi.client import eclient ibapi.wrapper import ewrapper ibapi.contract import contract ibcontract threading import thread import queue import datetime ibapi.utils import iswrapper #just decorator ibapi.common import * ibapi.contract import * ibapi.ticktype import *  class testapp(wrapper.ewrapper, eclient):  def __init__(self):     wrapper.ewrapper.__init__(self)     eclient.__init__(self, wrapper=self)   @iswrapper def historicaldata(self, reqid:int, bar: bardata):     print("historicaldata. ", reqid, " date:", bar.date, "open:", bar.open,           "high:", bar.high, "low:", bar.low, "close:", bar.close, "volume:", bar.volume,           "count:", bar.barcount, "wap:", bar.average)     if  all_data.iloc[reqid,7] == 0:         all_data.iloc[reqid,7] = bar.close     # line below not necessary     #self.done = true  @iswrapper def historicaldataend(self, reqid: int, start: str, end: str):     super().historicaldataend(reqid, start, end)     print("historicaldataend ", reqid, "from", start, "to", end)     print('=========end round : ',app.i+1,'with time :',time() - app.t,'==============')      app.i++     if app.i==len(all_data):         app.disconnect() # disconnect here: out of app.run() when data processed     else:         # add sleep here if necessary         reqnext()   @iswrapper def historicaldataupdate(self, reqid: int, bar: bardata):     print("historicaldataupdate. ", reqid, " date:", bar.date, "open:", bar.open,           "high:", bar.high, "low:", bar.low, "close:", bar.close, "volume:", bar.volume,           "count:", bar.barcount, "wap:", bar.average)   def reqnext():     print('====================round : ',app.i+1,'===============================')     contract = contract()     contract.symbol = all_data.iloc[app.i,0]     contract.sectype = all_data.iloc[app.i,1]     contract.currency = all_data.iloc[app.i,3]     contract.exchange = all_data.iloc[app.i,2]     app.reqhistoricaldata(app.i, contract, "","1 w", "1 day", "adjusted_last", 1, 1, false, [])    def main():     app = testapp()     app.connect("127.0.0.1", 7496, clientid=1234)     print("serverversion:%s connectiontime:%s" % (app.serverversion(),app.twsconnectiontime()))      app.i = 0     app.t = time()     reqnext()     app.run()     # when arrive here, app disconnected   if __name__ == "__main__":     main() 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -