-
Notifications
You must be signed in to change notification settings - Fork 40
How to move historical data to main() #3
Description
Great code examples. I wrote a simple code based on your examples. It prints data under historicalData but not in the main() function. How do I create a dataframe in main() with historical data? For some reason it doesnt work below.
Thanks!
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import *
from ibapi.contract import *
from ibapi.ticktype import TickTypeEnum
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import queue
class TestWrapper(EWrapper,EClient):
def initiate_variables(self):
setattr(self, 'historical_Data', [])
setattr(self, 'historical_DataEnd_flag', False)
def historicalData(self, reqId, bar):
historical_Data = self.historical_Data
historical_Data.append((reqId, bar.date, bar.open, bar.high, bar.low, bar.close, bar.volume))
print("data", reqId, bar.date, bar.open, bar.high, bar.low, bar.close, bar.volume)
def main():
callback = TestWrapper() # Instantiate IBWrapper. callback
tws = EClient(callback) # Instantiate EClientSocket and return data to callback
tws.connect(host='127.0.0.1', port=4002, clientId=5)
callback.initiate_variables()
contract = Contract()
contract.symbol = "EUR"
contract.secType = "CASH"
contract.currency = "USD"
contract.exchange = "IDEALPRO"
tws.reqHistoricalData(1101, contract, "", "6000", "1 min", "MIDPOINT", 0, 1, False, [])
data= pd.DataFrame(callback.historical_Data,
columns = ["reqId", "date", "open",
"high", "low", "close",
"volume"])
data[-10:]
tws.run()
print(data)
tws.disconnect()
if __name__ == '__main__':
main()