The code below calculates the Compounding
values starting from $100 and the percentage gains gains
. The code below goes from the start off with the entirety of the gains array [20,3,4,55,6.5,-10, 20,-60,5]
resulting in 96.25
at the end and then takes off the first index and recalculates the compounding value [3,4,55,6.5,-10, 20,-60,5]
resulting in 80.20
. It would do this until the end of the gains array [5]
. I want to write a code that calculates maximum drawdown as it is calculating f
. This would be the compounding results for the first iteration of f
[120., 123.6 ,128.544, 199.243, 212.194008 190.9746072, 229.16952864, 91.66781146, 96.25120203]
I want to record a value if it is lower than the initial capital Amount
value. So the lowest value is 91.67
on the first iteration so that would be the output, and on the second iteration it would be 76.37
. Since in the last iteration there is [5]
which results in the compounding output of 105
there are no values that go below 100 so it is None
as the output. How would I be able to implement this to the code below and get the expected output?
import numpy as np
Amount = 100
def moneyrisk(array):
f = lambda array: Amount*np.cumprod(array/100 + 1, 1)
rep = array[None].repeat(len(array), 0)
rep_t = np.triu(rep, k=0)
final = f(rep_t)[:, -1]
gains= np.array([20,3,4,55,6.5,-10, 20,-60,5])
Expected output:
[91.67, 76.37, 74.164, 71.312, 46.008, 43.2, 48., 40., None]
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…