ranaroussi / ezibpy

ezIBpy, a Pythonic Client for Interactive Brokers API
Apache License 2.0
324 stars 116 forks source link

Create DisconnectLoopFix #27

Closed Git2TheChoppa closed 5 years ago

Git2TheChoppa commented 5 years ago

Hey again, your library is super useful to me... I did a little digging into the EClientSocket disconnect method (the native API not the IbPy one) and was able to fix the error of the endless loop when trying to disconnect. It now works for submitting multiple different orders even if some are open / pending. This solves the #326 error... I hope I'm being more helpful than annoying. lol

Thanks again,

Tyler

ranaroussi commented 5 years ago

Can you tell me which steps should I take to reproduce the error you're referring to?

Git2TheChoppa commented 5 years ago

As it was after you’d place one order, if you tried to then place another order it would go into a loop and tell you that you were having an error #326 - clientId already in use.

Sent from my iPhone

On Mar 22, 2019, at 7:51 AM, Ran Aroussi notifications@github.com wrote:

Can you tell me which steps should I take to reproduce the error you're referring to?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

ranaroussi commented 5 years ago

Can you post the code that generates this error? I can't reproduce this error. Orders are being submitted without issues here :)

Git2TheChoppa commented 5 years ago

Weird... this is what I was using. Before editing the disconnect function I was getting the loop error on both of my computers...


import ezibpy
import time
import datetime 

# initialize ezIBpy
ibConn = ezibpy.ezIBpy()
ibConn.connect(clientId=100, host="localhost", port=7497)

# create contracts for a short strangle
call_to_sell = ibConn.createOptionContract("SPY", expiry=20190322, strike=287., otype="CALL")
put_to_sell  = ibConn.createOptionContract("SPY", expiry=20190322, strike=281., otype="PUT")

# create combo legs
leg1 = ibConn.createComboLeg(call_to_sell, "SELL", ratio=1)
leg2 = ibConn.createComboLeg(put_to_sell, "SELL", ratio=1)

# build a bag contract with these legs
contract = ibConn.createComboContract("SPY", [leg1, leg2])

# create & place order (negative price means this is a credit spread)
order = ibConn.createOrder(quantity=1, account='DU1410450')
orderId = ibConn.placeOrder(contract, order)

# let order fill
time.sleep(1)

# see the positions
# print("Positions")
# print(ibConn.positions)
# print("\n")

print("Positions")
pos = ibConn.getPositions(account='DU1410450')

# disconnect
ibConn.disconnect()

Thanks, Tyler Martin  

ranaroussi commented 5 years ago

I've just ran the same exact code (different account 😄) without problems:


import ezibpy
import time
import datetime 

# initialize ezIBpy
ibConn = ezibpy.ezIBpy()
ibConn.connect(clientId=100, host="localhost", port=7497)

# create contracts for a short strangle
call_to_sell = ibConn.createOptionContract("SPY", expiry=20190322, strike=287., otype="CALL")
put_to_sell  = ibConn.createOptionContract("SPY", expiry=20190322, strike=281., otype="PUT")

# create combo legs
leg1 = ibConn.createComboLeg(call_to_sell, "SELL", ratio=1)
leg2 = ibConn.createComboLeg(put_to_sell, "SELL", ratio=1)

# build a bag contract with these legs
contract = ibConn.createComboContract("SPY", [leg1, leg2])

# create & place order (negative price means this is a credit spread)
order = ibConn.createOrder(quantity=1, account='DU1410450')
orderId = ibConn.placeOrder(contract, order)

# let order fill
time.sleep(1)

# see the positions
# print("Positions")
# print(ibConn.positions)
# print("\n")

print("Positions")
pos = ibConn.getPositions(account='DU1410450')

# disconnect
ibConn.disconnect()
Git2TheChoppa commented 5 years ago

Did you then try to put in a second order afterwards? That’s when the error happens...

It works the first time but then if you say try to put different strikes in and submit another order it just loops like crazy

Sent from my iPhone

On Mar 22, 2019, at 10:40 AM, Ran Aroussi notifications@github.com wrote:

I've just ran the same exact code (different account 😄) without problems:

import ezibpy import time import datetime

initialize ezIBpy

ibConn = ezibpy.ezIBpy() ibConn.connect(clientId=100, host="localhost", port=7497)

create contracts for a short strangle

call_to_sell = ibConn.createOptionContract("SPY", expiry=20190322, strike=287., otype="CALL") put_to_sell = ibConn.createOptionContract("SPY", expiry=20190322, strike=281., otype="PUT")

create combo legs

leg1 = ibConn.createComboLeg(call_to_sell, "SELL", ratio=1) leg2 = ibConn.createComboLeg(put_to_sell, "SELL", ratio=1)

build a bag contract with these legs

contract = ibConn.createComboContract("SPY", [leg1, leg2])

create & place order (negative price means this is a credit spread)

order = ibConn.createOrder(quantity=1, account='DU1410450') orderId = ibConn.placeOrder(contract, order)

let order fill

time.sleep(1)

see the positions

print("Positions")

print(ibConn.positions)

print("\n")

print("Positions") pos = ibConn.getPositions(account='DU1410450')

disconnect

ibConn.disconnect() — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

ranaroussi commented 5 years ago

I've implemented a slightly modified version of this using the parameter self._disconnected_by_user