Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to move historical data to main() #3

Open
wczubal1 opened this issue Aug 14, 2019 · 0 comments
Open

How to move historical data to main() #3

wczubal1 opened this issue Aug 14, 2019 · 0 comments

Comments

@wczubal1
Copy link

wczubal1 commented Aug 14, 2019

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant