Comments (3)
thanks for the detailed report. I can reproduce using the code you provided and will investigate.
from swigibpy.
Looks like a problem accessing the contract object from separate threads.
The updatePortfolio
method is called in the callback thread and it appends to the global currentPositions
list. You then try to read from that in the main thread and kaboom. There is no problem accessing contract.symbol
from within the callback thread in updatePortfolio
.
from swigibpy.
Awesome, you're absolutely right! Thank you again for your help.
Below is the code that successfully grabs each position symbol and quantity, in case any one is interested
import datetime as dt
import sys
from time import sleep
from swigibpy import EWrapper, EPosixClientSocket, Contract, Order, TagValue,\
TagValueList
try:
input = raw_input
except:
pass
###
orderId = None
availableFunds = 0
netLiquidationValue = 0
currentPositions = {}
class PlaceOrderExample(EWrapper):
'''Callback object passed to TWS, these functions will be called directly
by TWS.
'''
def openOrderEnd(self):
'''Not relevant for our example'''
pass
def execDetails(self, id, contract, execution):
'''Not relevant for our example'''
pass
def managedAccounts(self, openOrderEnd):
'''Not relevant for our example'''
pass
###############
def nextValidId(self, validOrderId):
'''Capture the next order id'''
global orderId
orderId = validOrderId
def orderStatus(self, id, status, filled, remaining, avgFillPrice, permId,
parentId, lastFilledPrice, clientId, whyHeld):
print(("Order #%s - %s (filled %d, remaining %d, avgFillPrice %f,"
"last fill price %f)") % (
id, status, filled, remaining, avgFillPrice, lastFilledPrice))
def openOrder(self, orderID, contract, order, orderState):
print("Order opened for %s" % contract.symbol)
####account value
def updateAccountValue(self, key, value, currency, accountName):
global availableFunds
global netLiquidationValue
#get how much current available funds we have, also our net liquidation value
if currency == 'USD':
if key == 'AvailableFunds':
availableFunds = float(value)
elif key=='NetLiquidation':
netLiquidationValue = float(value)
def accountDownloadEnd(self,accountName):
print 'account download ended for %s' % (accountName)
def updateAccountTime(self,timestamp):
print 'account information pulled at %s' % (timestamp)
def updatePortfolio(self, contract, position, marketPrice, marketValue, averageCost, unrealizedPNL, realizedPNL, accoutName):
#this only called when there are actually positions
global currentPositions
symbol = contract.symbol
currentPositions[symbol] = position
# Instantiate our callback object
callback = PlaceOrderExample()
# Instantiate a socket object, allowing us to call TWS directly. Pass our
# callback object so TWS can respond.
tws = EPosixClientSocket(callback)
# Connect to tws running on localhost
tws.eConnect("", 7496, 42)
#account updates
tws.reqAccountUpdates(True,'DU15068')
sleep(3)
print 'available funds: %s' % (availableFunds)
print 'net liquidation value: %s' % (netLiquidationValue)
print 'positions:'
for symbol in currentPositions:
print "%s: %s" % (symbol,currentPositions[symbol])
print("******************* Press ENTER to quit when done *******************\n")
input()
print("\nDisconnecting...")
tws.eDisconnect()
from swigibpy.
Related Issues (20)
- no release / pip for v 0.5.0 ? HOT 7
- Windows installation error HOT 2
- TWS Error when calling placeOrder from placeorder.py
- Undefined symbol error when calling swigibpy HOT 3
- Exchange not set in EWrapper::position HOT 1
- pip install (of 0.4.1) fails in Windows under Python 3.5.1 using VS2015 compiler HOT 2
- Broken socket when using multiple EPosixClientSocket in different processes HOT 2
- auto reconnect parameter seems to raise error HOT 2
- python3.5 unable to get some contractDetails vars HOT 1
- how to add LastTradeDateOrContractMonth to Contract()? HOT 1
- tws.eDisconnect() no worky? HOT 2
- Why I cannot do reqPositions()? HOT 1
- Docker
- Combo Leg Order: Rejected - Invalid value in field # 6079 HOT 3
- Account values not updating
- fatal error C1189: #error: Macro definition of snprintf conflicts with Standard Library function declaration HOT 1
- Calling multiple prices simultaneously
- TWS Client Error - 509: Exception caught while reading socket - Not enough space
- "TypeError: position() takes 4 positional arguments but 5 were given"
- Allocation profiles aren't being generated when sending orders to IB?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swigibpy.