'How can i calculate the total percentage change in a portfolio(assume 100 buy in)if i have a list which shows each percentage change of each trade?
I am using the following code but I am missing something
percentagechange = [-2.704974336321264, -9.75579724548381, 161.1083287764476, -2.3049580623481725, -3.2221603096622586, -2.03531529638451, -6.491786990447023, 6.232016977803179, 25.025643012929887, 8.469894128276412, -5.697378424452704, 165.4820717201802]
totalreturns = [1]
for i in percentagechange:
totalreturns = total_returns * ((i / 100) + 1)
I tried the above code but I can't get my head around the calculation. Please help.
Solution 1:[1]
Percentege of change is defined as [ % Increase = Increase / Original Number × 100 ]
your code has a syntax error, totalreturns inside the loop is called totalreturns but also total_returns which is not defined.
But also following the formula (if I understood correctly) you would want to have something like:
percentagechange = [-2.704974336321264, -9.75579724548381, 161.1083287764476, -2.3049580623481725, -3.2221603096622586, -2.03531529638451, -6.491786990447023, 6.232016977803179, 25.025643012929887, 8.469894128276412, -5.697378424452704, 165.4820717201802]
totalreturns = [1]
for i in percentagechange:
totalreturns = i / ( totalreturns * 100 )
Hope it helps
Solution 2:[2]
I think this is what you are asking for:
import pandas as pd
percentagechange = [-2.704974336321264, -9.75579724548381, 161.1083287764476, -2.3049580623481725, -3.2221603096622586, -2.03531529638451, -6.491786990447023, 6.232016977803179, 25.025643012929887, 8.469894128276412, -5.697378424452704, 165.4820717201802]
totalreturns = 100 * (pd.Series((percentagechange))/100 + 1).cumprod().iloc[-1]
print(totalreturns)
#716.1782520447279
I turn the list of percentage changes into a series, divide them all by 100, add 1 and calculate the cumulative product. By using .iloc[-1] I then take the last value in the series (the total percentage change). Multiplying this by the starting value of 100, the output is 716.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | PCastedo |
| Solution 2 | Rawson |
