0

Currently I am using the IB API to request historical data.

I am having some trouble with adding headers to the data. The output I am currently receiving is:

Ticker; Date; None; Time; Open; High;  Low;    Close;   Volume  AAPL ; 20190507; ; 10:19:00 ; 207.87 ; 207.87 ; 207.87 ; 207.87 ; 1

But I would like the output to be

Symbol; Date; None; Time; Open; High; low; Close; Volume

AAPL ; 20190507; ; 16:20:00 ; 205.25 ; 205.38 ; 205.11 ; 205.35 ; 451

The code I am currently using is:

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


def print_to_file(*args):
    with open('text.txt', 'a') as fh:
        fh.write(' '.join(map(str,args)))
print = print_to_file

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

    Layout = "{!s:1} {!s:2} {!s:3} {!s:4} {!s:5} {!s:6} {!s:7} {!s:8} {!s:8}"
    print(Layout.format("Ticker;", "Date;", "None;", "Time;", "Open;", "High;", "Low;", "Close;", "Volume "))


    def historicalData(self, reqId, bar):
        print("AAPL", ";", bar.date.replace(' ', '; '), ";", bar.open, ";", bar.high, ";", bar.low, ";", bar.close, ";", bar.volume)

def main():
    app = TestApp()

    app.connect("127.0.0.1", 7497, 0)

    contract = Contract ()
    contract.symbol = "AAPL"
    contract.secType = "STK"
    contract.exchange = "SMART"
    contract.currency = "USD"
    contract.primaryExchange = "NASDAQ"

    app.reqHistoricalData(0, contract, "", "1 D", "1 min", "TRADES", 0, 1, False, [])

    app.run()

if __name__ == "__main__":
    main()

Aditionally, I would like the data to be saved as a .csv.

Could anyone help me out with the issue?

Hoogoo
  • 15
  • 1
  • 8
  • Seems there is no data getting passed to `def historicalData(self, reqId, bar)` method. Maybe try inspecting/`print`ing `reqId` and `bar` outside and before the method. – chickity china chinese chicken May 07 '19 at 19:31
  • Thank you for your comment. Actually that is being used, as def historicalData is the method that gives me the data. I think that I am looking for a new line of code or a small adjustment to mine – Hoogoo May 07 '19 at 19:34
  • Is the line `print("AAPL"...)` even getting printed within the `historicalData` function? – chickity china chinese chicken May 07 '19 at 19:37
  • Yes, it is. the first line in my question is the data that i receive. I even receive more data but that would be unnecessary to post, sorry for the delayed message. – Hoogoo May 07 '19 at 19:57
  • My apologies, I didn't notice all was output to the same line. Wouldn't it be possible to just add a newline `('\n')` at the end of the header, like `Layout = "{!s:1} {!s:2} {!s:3} {!s:4} {!s:5} {!s:6} {!s:7} {!s:8} {!s:8}'\n'"`? – chickity china chinese chicken May 07 '19 at 20:02
  • Or `print(Layout.format("Ticker;", "Date;", "None;", "Time;", "Open;", "High;", "Low;", "Close;", "Volume ", '\n'))` ... or even before printing the data: `print('\n', "AAPL", ";", bar.date.replace(' ', '; ')...`? – chickity china chinese chicken May 07 '19 at 20:03
  • Let me try it, one sec – Hoogoo May 07 '19 at 20:21
  • EDIT: Alright so if I do that for the header, it works. But after that the data will be put behind everything, so I get 1 row with all the data underneath the header – Hoogoo May 07 '19 at 20:29
  • Meaby it is because it's not a .csv file, ill try if i can write it as .csv but ill have to look into that as I am unsure how to do that effectively – Hoogoo May 07 '19 at 20:37
  • yes the `csv` module can easily write `csv` data, see [`csv.writer`](https://docs.python.org/3/library/csv.html#csv.writer) – chickity china chinese chicken May 07 '19 at 22:53

0 Answers0