2012-11-03 9 views
7

Sto cercando se IbPy può essere un buon modo per collegarmi all'API di trading di Interactive Brokers. Ora, come prova, sto provando a raccogliere alcune istantanee dei prezzi delle azioni, per vedere se riesco a far funzionare IbPy per me. Sto utilizzando il gateway Brokertron per connettermi con IB.IbPy: Come estrarre risposta API in una variabile

Ricevo il prezzo delle azioni richiesto indietro dall'API IB bene (da un codice di esempio con la gestione degli errori che ho trovato, vedi sotto), quindi tecnicamente funziona verso l'API IB, ma non riesco a capire come estrarre il campo specifico (campo = 4, prezzo = 175.95 sotto) in una variabile per un uso successivo.

Qualche idea su come ottenere il contenuto del campo 4 in una variabile? Grazie!

Python script di esempio:

import ib 
from ib.ext.Contract import Contract 
from ib.opt import ibConnection, message 
from time import sleep 

class Downloader(object): 
    def __init__(self,debug=False): 
     self.tws = ibConnection('localhost', 4001, 0) 

     if debug: 
      self.tws.registerAll(self.debugHandler) 

     self.tws.connect() 
     self._reqId = 1 # current request id 

    def debugHandler(self,msg): 
     print '[debug]', msg 

    def requestData(self,contract): 
     self.tws.reqMktData(self._reqId,c,'',1) 
     self._reqId+=1 
     return "???" 

if __name__=='__main__': 
    dl = Downloader(debug=True) 
    c = Contract() 
    c.m_symbol = 'SPY' 
    c.m_secType = 'STK' 
    c.m_exchange = 'SMART' 
    c.m_currency = 'USD' 
    laststockpricefield4 = dl.requestData(c) 
    print laststockpricefield4 
    sleep(3) 
    print 'Done.' 

Commande uscita di linea:

01-Nov-12 22:30:43 DEBUG Server Version: 65 
01-Nov-12 22:30:43 DEBUG TWS Time at connection: 20121101 22:30:43 GMT 
??? 
[debug] ManagedAccounts accountsList=DU15144> 
[debug] NextValidId orderId=1> 
[debug] TickString tickerId=1, tickType=45, value=1351808899> 
[debug] TickPrice tickerId=1, field=4, price=175.95, canAutoExecute=0> 
[debug] TickSize tickerId=1, field=5, size=1> 
[debug] TickGeneric tickerId=1, tickType=49, value=0.0> 
[debug] TickPrice tickerId=1, field=1, price=176.03, canAutoExecute=1> 
[debug] TickSize tickerId=1, field=0, size=378> 
[debug] TickPrice tickerId=1, field=2, price=176.05, canAutoExecute=1> 
[debug] TickSize tickerId=1, field=3, size=344 
+0

Problema risolto: –

risposta

5

questo funziona!

import re 
import ib 
from ib.ext.Contract import Contract 
from ib.opt import ibConnection, message 
from time import sleep 

class Downloader(object): 
    field4price = '' 

    def __init__(self): 
     self.tws = ibConnection('localhost', 4001, 0) 
     self.tws.register(self.tickPriceHandler, 'TickPrice') 
     self.tws.connect() 
     self._reqId = 1 # current request id 

    def tickPriceHandler(self,msg): 
     if msg.field == 4: 
      self.field4price = msg.price 
      #print '[debug]', msg 

    def requestData(self,contract): 
     self.tws.reqMktData(self._reqId, contract, '', 1) 
     self._reqId+=1 

if __name__=='__main__': 
    dl = Downloader() 
    c = Contract() 
    c.m_symbol = 'SPY' 
    c.m_secType = 'STK' 
    c.m_exchange = 'SMART' 
    c.m_currency = 'USD' 
    dl.requestData(c) 
    sleep(3) 
    print 'Price - field 4: ', dl.field4price 
Problemi correlati