def create_drawdowns(pnl):
 hwm = [0]
 idx = pnl.index
 drawdown = pd.Series(index=idx)
 duration = pd.Series(index=idx)
 for t in range(1, len(idx)):
  hwm.append(max(hwm[(t - 1)], pnl.ix[t]))
  drawdown.ix[t] = (hwm[t] - pnl.ix[t])
  duration.ix[t] = (0 if (drawdown.ix[t] == 0) else (duration.ix[(t - 1)] + 1))
 return (drawdown, drawdown.max(), duration.max())

def create_drawdowns ( pnl ) : hwm = [ 0 ] idx = pnl . index drawdown = pd . Series ( index = idx ) duration = pd . Series ( index = idx ) for t in range ( 1 , len ( idx ) ) : hwm . append ( max ( hwm [ ( t - 1 ) ] , pnl . ix [ t ] ) ) drawdown . ix [ t ] = ( hwm [ t ] - pnl . ix [ t ] ) duration . ix [ t ] = ( 0 if ( drawdown . ix [ t ] == 0 ) else ( duration . ix [ ( t - 1 ) ] + 1 ) ) return ( drawdown , drawdown . max ( ) , duration . max ( ) )
calculate the largest peak-to-trough drawdown of the pnl curve as well as the duration of the drawdown .