This code correctly prints all four live stock quotes from Interactive Brokers using ib_insync. Stock is an imported function within ib_insync.
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)
This code below will only print the last quote in the list, in this case TMV. I believe this is because the 'w' is overwriting the entire file every time the loop is run? Or is the loop not setup correctly.
import csv
from ib_insync import *
stocks = ['SPY','ANGL','GDX','TMV']
stocks = ['SPY', 'TVIX']
while True:
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(15)
for stock in test:
f = open('spy-price.csv','w')
f.write(str(stock.last))
f.close()
How would I get the quotes to be setup so all four quotes are in the same file?