0

i need help to get data history from time and sale tws api. i try get by this code, But I did not understand what is Ticks BY Ticks and more where am i wrong in the code, and how should it show to get the information I want? I would be happy Exmaple to help Thank you https://interactivebrokers.github.io/tws-api/historical_time_and_sales.html

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum

class TestApp(EWrapper,EClient):
    def __init__(self):
        EClient.__init__(self, self)
    def error(self, reqId, errorCode, errorString):
        print("ERRRR",reqId, " ",errorCode," ",errorString)
    def historicalTicks(self, reqId: int, ticks: ListOfHistoricalTick, done: bool):
       print("HistoricalTick. ReqId:", reqId, ticks)
    

def main():
    app = TestApp()
    app.disconnect()
    app.connect("127.0.0.1", 7496, 0)
    contract = Contract()
    contract.symbol = "AAPL"
    contract.secType = "STK"
    contract.exchange = "SMART"
    contract.currency = "USD"
    contract.primaryExchange = "NASDAQ"
    app.reqHistoricalTicks(18001, eurusd_contract,"20170712 21:39:33", "", 10, "TRADES", 1, True, [])

    app.run()
if __name__ == "__main__":
    main()
Chani
  • 11
  • 3
  • Are you sure you want tick by tick? Maybe just historical data, https://stackoverflow.com/a/57502748/2855515 Read the manual http://interactivebrokers.github.io/tws-api/historical_data.html – brian Feb 25 '21 at 21:28
  • I know the code is not written well ,I edit how I think it should look, but I do not get any information, I just want to get the deals that are in TIME AND SALE not in real time by day – Chani Feb 26 '21 at 04:24

1 Answers1

0

You have errors that the IDE should spell out for you. ListOfHistoricalTick needs to be imported, eurusd_contract needs to be defined.

"TRADES" will be returned in historicalTicksLast like in the instructions

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
# from ibapi.ticktype import TickTypeEnum # unused
from ibapi.common import ListOfHistoricalTick # need this import for midpoint

class TestApp(EWrapper,EClient):
    def __init__(self):
        EClient.__init__(self, self)

    def error(self, reqId, errorCode, errorString):
        print("ERRRR",reqId, " ",errorCode," ",errorString)

    # this comes from midpoint
    def historicalTicks(self, reqId: int, ticks: ListOfHistoricalTick, done: bool):
        print("HistoricalTick. ReqId:", reqId, ticks)
        
        if done:# use this to know when to disconnect
            print("done, disconnecting...")
            self.disconnect()
    

def main():
    app = TestApp()
    #app.disconnect()#you can't be connected yet
    app.connect("127.0.0.1", 7497, 123) # should use paper account for testing
    
    contract = Contract()
    contract.symbol = "EUR" # I'm using forex to make sure you have a subscription
    contract.secType = "CASH"
    contract.exchange = "IDEALPRO"
    contract.currency = "USD"
    
    # use a recent time and midpoint only for forex
    app.reqHistoricalTicks(18001, contract,"20210226 09:25:00", "", 10, "MIDPOINT", 1, True, [])

    # supposed to call this before making requests but it should work
    app.run() 
    
if __name__ == "__main__":
    main()
brian
  • 10,619
  • 4
  • 21
  • 79
  • Thanks for the answer, but i not understand I need this to view the latest trades in stocks. I tried to change for example to get "WDRP" the lasts trades and I get data That does not exist in the TIME AND SALE window and the size i get in all its 0 – Chani Feb 26 '21 at 17:22
  • I tried wdrp with TRADES and using the correct callback and I only got 3 trades returned from pink sheets. Maybe call IB and see if that data is available in the API. I doubt it. You can get it in a time and sales window from TWS. – brian Feb 26 '21 at 18:39
  • Would it be okay to ask you for a sample code? It's just me returning empty cells – Chani Feb 26 '21 at 19:24
  • Maybe the data isn't available. If the forex sample code works for you then you can learn from there. Read the docs and spend a few weeks learning python and the API. – brian Feb 26 '21 at 19:38
  • @Chani did you work this out? I'm also having issues with getting tick data. Would appreciate very much if you could post your solution on here. Thanks. – cephalopod Oct 15 '22 at 20:33