¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

using reqAccountUpdates to create pandas dataframe for each new update


 

Is there a way of using reqAccountUpdates and updatePortfolio to create a dataframe with all positions, and then overwrite the dataframe for each refresh?

I am using reqAccountUpdates so that I can get the cash balance and the positions all in one place. My problem is that each symbol comes in separately, so if I do something like this:

```
? ? def updatePortfolio(self, contract: Contract, position: float, marketPrice: float, marketValue: float, averageCost: float, unrealizedPNL: float, realizedPNL: float, accountName: str):
? ? ? ? df = pd.DataFrame([contract.symbol, position, marketValue])
? ? ? ? df.to_csv('positions.csv')
```

The CSV file ends up with the last position only, since its overwriting it for each symbol.


 

In your class __init__, add:
? ? self.positions = pd.DataFrame()

in def updatePortfolio, try this:

def updatePortfolio(self,
contract: Contract,
position: float,
marketPrice: float,
marketValue: float,
averageCost: float,
unrealizedPNL: float,
realizedPNL: float,
accountName: str):
position_dict = {'symbol' : contract.symbol,
'position': position,
'value': marketValue}
if contract.conId in self.positions.index:
self.positions.loc[contract.conId] = pd.series(position_dict)
else:
self.positions = self.positions.append(
pd.DataFrame(positions_dict, index=[contract.conId]))

self.positions.to_csv('positions.csv')

?This may not be ideal, but it might get you on a path that works for now and can be improved upon later.

BTW, I have not tried the code, but despite any errors, it will hopefully get you started. ?


 

Sorry, I notice I can't see the code I posted very well. Some of the
code elements seem to show up so lightly that I can't see them.
If I drag my cursor across it to select it, then I can read it.
Maybe it has something to do with my PyCharm color scheme that makes cut and paste unusable.?
Or, maybe there is a trick I need to learn. Anyone? ?


 

Change the default PyCharm colour scheme?
I use the Intelli Light scheme in PyCharm, and just copied and pasted into a new .py document. Readibility is good.
KH
def updatePortfolio(self,
contract: Contract,
position: float,
marketPrice: float,
marketValue: float,
averageCost: float,
unrealizedPNL: float,
realizedPNL: float,
accountName: str):
position_dict = {'symbol' : contract.symbol,
'position': position,
'value': marketValue}


 

Thanks Kevin, I will give that a try. ?


 

def updatePortfolio(self,
contract: Contract,
position: float,
marketPrice: float,
marketValue: float,
averageCost: float,
unrealizedPNL: float,
realizedPNL: float,
accountName: str):
position_dict = {'symbol' : contract.symbol,
'position': position,
'value': marketValue}
if contract.conId in self.positions.index:
self.positions.loc[contract.conId] = pd.series(position_dict)
else:
self.positions = self.positions.append(
pd.DataFrame(positions_dict, index=[contract.conId]))

self.positions.to_csv('positions.csv')


 

¿ªÔÆÌåÓý

A quick observation tells me that certain items are faint because they are not called up later in the code.


On 28/5/21 12:39 pm, sbtuttle@... wrote:

Sorry, I notice I can't see the code I posted very well. Some of the
code elements seem to show up so lightly that I can't see them.
If I drag my cursor across it to select it, then I can read it.
Maybe it has something to do with my PyCharm color scheme that makes cut and paste unusable.?
Or, maybe there is a trick I need to learn. Anyone? ?


 

Thanks Trevor, that's true - I coded this up in a scratch file without the usual other code that would go along with it.?