furas

example with reading again

Aug 4th, 2018
96
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. today = datetime.datetime.today()
  2.  
  3. for x in stocks:
  4.  
  5.     tws.reqHistoricalData(x)
  6.  
  7.     for n in periods:
  8.  
  9.         delta = datetime.timedelta(days=n)
  10.  
  11.         for _ in range(10): # repeat it only 10 times
  12.             data = pd.DataFrame(reqHistoricalData(today-delta).strftime("%Y%m%d %H:%M:%S %Z"))
  13.  
  14.             if not data.empty:
  15.                 data.to_csv("filename{}@{}.csv".format(x, n))
  16.                 break # exit `for _` loop so it doesn't read again for the same period
  17.             else:
  18.                 print("empty data for", x, n, '... reading again')
  19.                 tws.reqHistoricalData(x) # read again for the same period
RAW Paste Data