Wu Yue
Wu Yue

Reputation: 1

Issue about dict computation

I need to calculate the profit&Loss by using two dictionaries, but I dont know where I did wrong and how to perform computation with python dicts. I need to use the formula (market price - strike price) * volume.

Here is my code:

    portfolio = {
"AAPL": {
    "volume": 10,
    "strike": 154.12
    },
"GOOG": {
    "volume": 2,
    "strike": 812.56
    },
"TSLA": {
    "volume": 12,
    "strike": 342.12
    },
"FB": {
    "volume": 18,
    "strike": 209.0
    }
}
market = {
"AAPL": 198.84,
"GOOG": 1217.93,
"TSLA": 267.66,
"FB": 179.06
}

def pl(market, portfolio):
global pl
for key, value in portfolio.items():
    pl += (market(value) - (portfolio(value)['strike'])) * (portfolio(value)['volume'])
   

And the error is:

TypeError: 'dict' object is not callable

the screenshot is here

Upvotes: 0

Views: 109

Answers (2)

quamrana
quamrana

Reputation: 39374

You are accessing your dicts incorrectly.

Did you mean:

for key, value in portfolio.items():
    pl += (market[key] - (value['strike'])) * (value['volume'])
   

Upvotes: 2

Diogo
Diogo

Reputation: 101

I'm not sure if is this what you are looking for

for key in portfolio.keys():
    pl += (market[key] - portfolio[key]["strike"]) * portfolio[key]["volume"]

Upvotes: 2

Related Questions