Portfolio.py

Python Example:


from mplpy import mpl
from numpy import arange, float32
from matplotlib import pyplot as plot

modelFilename = mpl.ModelDirectory + "Portfolio.mpl"
result = mpl.model.SolveModel(modelFilename, mpl.cplex)

investVect = mpl.model.Invest
investVect.ZeroTol = 0.001

count = investVect.Nonzeros.Count
investAmount = arange(count, dtype=float32)
stockNames = range(count)

for i, var in enumerate(investVect.Nonzeros):
   investAmount[i] = var.Activity
   stockNames[i] = investVect.stock.ValueStr
   print((stockNames[i] + ":").ljust(8) + ("%1.1f%%" % (investAmount[i] * 100.0)).rjust(6))

# plot the investments as pie chart
fig = plot.figure(1, figsize=(7,7))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.pie(investAmount, labels=stockNames, autopct='%1.1f%%', shadow=True)
ax.set_title('Stock Portfolio', bbox={'facecolor':'0.9', 'pad':15})
plot.show()


        

Back To Top | Maximal Home Page | List of Samples | Previous Page | Next Page