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

Could anyone help me out with the issue?

Hoogoo
  • 15
  • 1
  • 8

1 Answers1

0

Following code can help you write values in csv file row by row.

def header():
    h1 = ['col1']  
    h2 = ['col2']
    h3 = ['col3']
    h4 = ['col4']
    rows = zip(h1, h2, h3, h4)

    with open('text.txt', 'a') as f:
        wr = csv.writer(f)
        for row in rows:
            wr.writerow(row)
        f.close()

based on changes in question, suggesting this:

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

Abdul Salam
  • 522
  • 2
  • 7
  • 26
  • In my answer lists h1,h2,h3,h4 are hard coded, it can also be dynamically assigned – Abdul Salam May 07 '19 at 10:06
  • Thank you for your comment, but unfortunately your code does not seem to change anything in the output EDIT: Your code just adds the header again after each line, but it does not change anything to csv. It just adds the headers again – Hoogoo May 07 '19 at 10:11
  • The idea here is to show you the usage of zipping lists and csv.writer method. don't expect ready to use solution, you can develop this into something you are looking for. – Abdul Salam May 07 '19 at 10:20
  • Fair enough, but than still, this does not change my output to .csv, it just copies the header each time which is not what I am looking for, and eventhough i need to import csv and you use the csv.writer, i do not get a csv output – Hoogoo May 07 '19 at 10:26
  • Alright so I have made some adjustments to my code and right now I am getting the following as an output: AAPL, Date Time, Open, High, Low, Close, Volume. Unfortunately I am not receiving headers, and therefore I will need to transform a part of your code, would you be able to help me with that? – Hoogoo May 07 '19 at 11:09
  • Sure, If you just needs headers call header function appropriately in your code. and define my function to create static headers. if you need dynamic headers, pass variables.. Please share code and error msg for more clarity. – Abdul Salam May 07 '19 at 13:47
  • I suggest you to go with this: `def print_to_file(*args): with open('text.txt', 'a') as fh: fh.write(' '.join(map(str,args))) fh.write('\n') fh.close()` – Abdul Salam May 08 '19 at 07:39
  • modified my answer. – Abdul Salam May 08 '19 at 07:42