mplPortfolio.py

Python Example:


from mplpy import *


class MplPortfolio(object):

   def __init__(self):
      self._investAmounts = []
      self._investNames = []
      self._riskVariance = 0.0
      self._resultString = ""

   @property
   def InvestAmounts(self):
      return self._investAmounts

   @property
   def InvestNames(self):
      return self._investNames

   @property
   def RiskVariance(self):
      return self._riskVariance

   @property
   def ResultString(self):
      return self._resultString

   @property
   def DataDir(self):
      return mpl.HomeDirectory

   def _loadModel(self, mod, targetReturn, monthNames, stockNames, stockPrices):
      maxInvest = 1
      minInvest = 0
      maxStock = 10
      minStock = 5
      mpl.Options["ModelType"].ValueStr = "Quadratic";
      modelFileName = mpl.HomeDirectory + "Portfolio.mpl"
      try:
         mod.IndexSets.Add("stock", stockNames)
         mod.IndexSets.Add("stock2", stockNames)
         mod.IndexSets.Add("month", monthNames)
         mod.IndexSets.Add("period[month]", "month - (First(month))");
         mod.DataConstants.Add("MaxInvest", maxInvest)
         mod.DataConstants.Add("MinInvest", minInvest)
         mod.DataConstants.Add("MaxStocks", maxStock)
         mod.DataConstants.Add("MinStocks", minStock)
         mod.DataConstants.Add("TargetReturn", targetReturn)
         mod.DataVectors.Add("Price[stock,month]", stockPrices)

         result = mod.ReadFilePart(modelFileName, "DATA_RETURN_MARKER")
      except Exception as ex:
         print(ex)
      result = mod.LastResult
      return result


   def _getSolutionValues(self, model):
      solValues = []
      solNames = []
      investVect = model.VariableVectors["Invest"]
      investVect.ZeroTol = 0.0001;
      for var in investVect.Nonzeros:
         stName = investVect.Subscripts[1].ValueStr
         solNames.append(str(stName))
         solValues.append(var.Activity)
      self._investAmounts = solValues
      self._investNames = solNames
      self._riskVariance = model.Solution.ObjectValue


   def SolveModel(self, solverName, targetReturn, monthNames, stockNames, stockPrices):
      solver = mpl.Solvers.Add(solverName)
      if solver == None:
         self._resultString = "Solver " + solverName + " + was not found."
         return False
      model = mpl.model
      model.UseExceptions = True
      model.ClearModel()
      model.Name = "Portfolio"
      result = self._loadModel(model, targetReturn, monthNames, stockNames, stockPrices)
      if not result == ResultType.Success:
         if result == ResultType.AddFailed:
            self._resultString = model.ResultString
         else:
            self._resultString = str(model.Error)
         model.ClearModel()
         return False
      try:
         model.Solve(solver)
      except Exception as ex:
         print(ex)
         pass
      result = model.LastResult
      if not result == ResultType.Success:
         self._resultString = "Solver " + solver.Name + ": " + model.ResultString + "\n"
                              + model.Solution.ResultString
         model.ClearModel()
         return False
      self._resultString = model.Solution.ResultString
      self._getSolutionValues(model)
      model.ClearModel()
      return True

        

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