通过python从IB api获得外汇汇率

时间:2017-03-19 10:01:48

标签: python interactive-brokers ibpy

我试图通过ibpy库从交互式经纪商那里获得货币汇率,并且我在谷歌上发现了一些代码,我改变了一点。

> lseqBy(from = 10, to = 100000, by = 1)
[1] 1e+01 1e+02 1e+03 1e+04 1e+05
> lseqBy(from = 10, to = 100000, by = 2)
[1] 1e+01 1e+03 1e+05
> lseqBy(from = 10, to = 100000, by = 3)
[1]    10 10000

以下是结果

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

# print all messages from TWS
def watcher(msg):
    print msg

# show Bid and Ask quotes
def my_BidAsk(msg):

    if msg.field == 1:
        print ('bid: %s' % ( msg.price)) 
        bid=msg.price
    elif msg.field == 2:
        print ('ask: %s' % (msg.price)) 
        ask=msg.price
    elif msg.field==9:
        print ('last close: %s' % msg.price) 
        last=msg.price

def makeStkContract(contractTuple):
    newContract = Contract() 

    newContract.m_symbol = contractTuple[0]
    newContract.m_secType = contractTuple[1]
    newContract.m_exchange = contractTuple[2]
    newContract.m_primaryExch=contractTuple[3]
    newContract.m_currency = contractTuple[4]

    print ('Contract Values:%s,%s,%s,%s,%s:' % contractTuple)
    return newContract

if __name__ == '__main__':
    global bid,ask,last
    bid=None
    ask=None
    last=None
    # con = ibConnection(port='4001',clientId=100)
    con = Connection.create(port=7497, clientId=1006)
    con.registerAll(watcher)
    showBidAskOnly = True  # set False to see the raw messages

    if showBidAskOnly:
        con.unregister(watcher, message.tickSize, message.tickPrice,
                       message.tickString, message.tickOptionComputation)
        con.register(my_BidAsk, message.tickPrice)
    con.connect() 
sleep(1)
tickId = 1 
# Note: Option quotes will give an error if they aren't shown in TWS
#contractTuple = ('GOOGL', 'STK', 'SMART', 'ISLAND','USD')
#contractTuple = ('QQQQ', 'OPT', 'SMART', 'USD', '20170921', 47.0, 'CALL')
#contractTuple = ('ES', 'FUT', 'GLOBEX', 'USD', '200709', 0.0, '')
#contractTuple = ('ES', 'FOP', 'GLOBEX', 'USD', '20070920', 1460.0, 'CALL')
contractTuple = ('CAD', 'CASH', 'IDEALPRO','IDEALPRO','jpy')
stkContract = makeStkContract(contractTuple)
print ('* * * * REQUESTING MARKET DATA * * * *')
con.reqMktData(tickId, stkContract, '', False)
print ('global variables:',bid, ask,last)
sleep(1)
print ('* * * * CANCELING MARKET DATA * * * *')
con.cancelMktData(tickId)

sleep(1)
con.disconnect()
#sleep(1)

我现在面临的困难是,首先我要消除所有那些无关的消息,其次,我似乎无法获得全局变量(bid,ask,last)。

有什么建议吗? 提前谢谢

1 个答案:

答案 0 :(得分:0)

如果您不想要所有邮件,请不要print这些邮件。

您必须确保您执行操作的顺序异步工作。在您有时间从服务器返回之前,请立即print发出请求。然后你sleep 1秒钟,希望它已经完成。

con.reqMktData(tickId, stkContract, '', False)
print ('global variables:',bid, ask,last)
sleep(1)
print ('* * * * CANCELING MARKET DATA * * * *')
con.cancelMktData(tickId)

这是一种按顺序完成所有事情的方法。

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

def nextValidId_handler(msg):
    global id;
    id = msg.orderId
    # start now when you know it's connected
    makeRequest()

def error_handler(msg):
    # only print interesting errors
    if msg.id > 0:
        print(msg)

# show Bid and Ask quotes
def my_BidAsk(msg):
    global bid,ask    

    if msg.field == 1:
        print ('bid: %s' % ( msg.price)) 
        bid=msg.price
    elif msg.field == 2:
        print ('ask: %s' % (msg.price)) 
        ask=msg.price
    # there is no last price in forex, maybe just the last close.

    if (bid is not None) and (ask is not None):
        midPoint = (bid + ask)/2
        print ('global variables:',bid, ask, midPoint)
        # disconnect after getting all the data we want
        disconnect()

def makeStkContract(contractTuple):
    newContract = Contract() 

    newContract.m_symbol = contractTuple[0]
    newContract.m_secType = contractTuple[1]
    newContract.m_exchange = contractTuple[2]
    newContract.m_primaryExch=contractTuple[3]
    newContract.m_currency = contractTuple[4]

    print ('Contract Values:%s,%s,%s,%s,%s:' % contractTuple)
    return newContract

def makeRequest():
    global tickId
    tickId = 1
    contractTuple = ('CAD', 'CASH', 'IDEALPRO','IDEALPRO','jpy')
    stkContract = makeStkContract(contractTuple)
    print ('* * * * REQUESTING MARKET DATA * * * *')
    con.reqMktData(tickId, stkContract, '', False)

def disconnect():
    print ('* * * * CANCELING MARKET DATA * * * *')
    con.cancelMktData(tickId)
    con.disconnect()

bid = None
ask = None

if __name__ == '__main__':
    con = ibConnection(port=7497, clientId=1006)
    con.register(error_handler, message.Error)
    con.register(nextValidId_handler, message.nextValidId)
    con.register(my_BidAsk, message.tickPrice)
    con.connect()