ranaroussi / qtpylib

QTPyLib, Pythonic Algorithmic Trading
http://qtpylib.io
Apache License 2.0
2.15k stars 511 forks source link

get_portfolio() is empty #54

Closed fxmeister closed 7 years ago

fxmeister commented 7 years ago

I am using your QTPyLib to paper trade some algorithm ideas. I have troubles however when trying to get the portfolio details (equity positions, cash, etc) when up and running with a paper trading account.

In the on_quote() method, I am making calls to self.get_portfolio(), but the return type is empty, despite having open positions and cash visible in TWS itself.

I have referenced your documentation, and it seems my approach is correct, but it is also unclear if get_portfolio only returns positions for a particular stock.

Can you clarify this problem?

ranaroussi commented 7 years ago

If you're using self.get_portfolio() you should get a dict with your portfolio information. instrument.get_portfolio() will return the portfolio for the specific instrument.

I just bought a few shares of Twitter using the paper account and ran a test with a strategy that trades the ES. Here's my on_quote():

def on_quote(self, instrument):
    print(">>>", self.get_portfolio() )
    print("+++", self.get_portfolio() )
    print("")

...and the output:

>>> {'TWTR': {'averageCost': 17.49, 'unrealizedPNL': -2.0, 'account': 'XXXXXXX', 'marketValue': 1747.0, 'position': 100, 'realizedPNL': 0.0, 'marketPrice': 17.4699993, 'symbol': 'TWTR'}}
+++ {'averageCost': 0.0, 'unrealizedPNL': 0.0, 'account': None, 'marketValue': 0.0, 'position': 0.0, 'realizedPNL': 0.0, 'marketPrice': 0, 'symbol': 'ESH2017_FUT'}

>>> {'TWTR': {'averageCost': 17.49, 'unrealizedPNL': -2.0, 'account': 'XXXXXXX', 'marketValue': 1747.0, 'position': 100, 'realizedPNL': 0.0, 'marketPrice': 17.4699993, 'symbol': 'TWTR'}}
+++ {'averageCost': 0.0, 'unrealizedPNL': 0.0, 'account': None, 'marketValue': 0.0, 'position': 0.0, 'realizedPNL': 0.0, 'marketPrice': 0, 'symbol': 'ESH2017_FUT'}

>>> {'TWTR': {'averageCost': 17.49, 'unrealizedPNL': -1.0, 'account': 'XXXXXXX', 'marketValue': 1748.0, 'position': 100, 'realizedPNL': 0.0, 'marketPrice': 17.4799993, 'symbol': 'TWTR'}}
+++ {'averageCost': 0.0, 'unrealizedPNL': 0.0, 'account': None, 'marketValue': 0.0, 'position': 0.0, 'realizedPNL': 0.0, 'marketPrice': 0, 'symbol': 'ESH2017_FUT'}

>>> {'TWTR': {'averageCost': 17.49, 'unrealizedPNL': 0.0, 'account': 'XXXXXXX', 'marketValue': 1749.0, 'position': 100, 'realizedPNL': 0.0, 'marketPrice': 17.4899993, 'symbol': 'TWTR'}}
+++ {'averageCost': 0.0, 'unrealizedPNL': 0.0, 'account': None, 'marketValue': 0.0, 'position': 0.0, 'realizedPNL': 0.0, 'marketPrice': 0, 'symbol': 'ESH2017_FUT'}

>>> {'TWTR': {'averageCost': 17.49, 'unrealizedPNL': 1.0, 'account': 'XXXXXXX', 'marketValue': 1750.0, 'position': 100, 'realizedPNL': 0.0, 'marketPrice': 17.4999993, 'symbol': 'TWTR'}}
+++ {'averageCost': 0.0, 'unrealizedPNL': 0.0, 'account': None, 'marketValue': 0.0, 'position': 0.0, 'realizedPNL': 0.0, 'marketPrice': 0, 'symbol': 'ESH2017_FUT'}

Can you post here your on_quote() and your Strategy's instruments list so I can better understand where the problem is coming from?

ranaroussi commented 7 years ago

Closing for now. Please re-open if problem persists