0

Currently I am trying to request data from the IB API, but I have a small formatting issue.

The API gives me the following output:

AAPL; 20190507 16:20:00; price; price; price; price; number

I would like the data to return as:

AAPL; 20190507; 16:20:00; price; price; price; price; number

I am using the following code

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


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

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

    def historicalData(self, reqId, bar):
        print("AAPL", ";", bar.date, ";", 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()

bar.date in this case gives me the date and time

print("AAPL", ";", bar.date, ";", bar.open, ";", bar.high, ";", bar.low, ";", bar.close, ";", bar.volume)

Could anyone help me out with this?

mx0
  • 6,445
  • 12
  • 49
  • 54
Hoogoo
  • 15
  • 1
  • 8

2 Answers2

0

Try this:

'; '.join(bar.date.split(' '))

Or:

bar.date.replace(' ', '; ')

Both of these will replace the space in the date/time output with a semicolon and space.

joedeandev
  • 626
  • 1
  • 6
  • 15
0

As I understood, bar.date contains "20190507 16:20:00".

So you can replace bar.date with "; ".join(bar.date.split(" ")) in last print() you've provided.

Olvin Roght
  • 7,677
  • 2
  • 16
  • 35