使用python

时间:2019-06-14 16:13:49

标签: python

这可以使用ib_sync正确地从Interactive Brokers获得实时报价,并可以将其打印到Jupter平台上:

from ib_insync import * 

stocks = ['SPY','ANGL','GDX','TMV']

test = list()
for stock in stocks:
    stock = Stock(stock, 'SMART','USD')
    contract = ib.qualifyContracts(stock)
    test.append(ib.reqMktData(contract[0],snapshot=True))
    ib.sleep(1)
for stock in test:
    print(stock.last)

这可以正确打开CSV文件并在其中写入文本:

with open('stocks-test.csv','w',newline='') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=',')
    spamwriter.writerow(['stock','price','datetime'])

f = open('stocks-test.csv','w')
f.write('hi there\n') #Give your csv text here.

如何将两者结合在一起?

到目前为止,我有:

import csv

with open('stocks-test.csv','w',newline='') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=',')
    spamwriter.writerow(['stock','price','datetime'])
    spamwriter.writerow(time.strftime("%Y-%m-%d %H:%M") )
#file closed
stocks = ['SPY','ANGL','GDX','TMV']

test = list()
for stock in stocks:
    stock = Stock(stock, 'SMART','USD')
    contract = ib.qualifyContracts(stock)
    test.append(ib.reqMktData(contract[0],snapshot=True))
    ib.sleep(1)
for stock in test:
    f.write(stock.last)
    f.write('hi there\n') #Give your csv text here.
    f.close()

由于某种原因,f.write无法读取列表?

2 个答案:

答案 0 :(得分:1)

在组合版本中,您永远不会声明f。插入

<button type="submit" class="btn btn-primary disabled">Submit</button>

在for循环之前

答案 1 :(得分:1)

f'w'模式下打开,这意味着一个人只能向其中写入str 。显然,stock.last不是字符串,因此无法直接将其写入文件。您应该以某种方式将其转换为字符串,可能使用str(stock.last)