+ Reply to Thread
Results 1 to 7 of 7

Solver using VBA for portfolio optimisation

  1. #1
    Registered User
    Join Date
    03-09-2014
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    3

    Solver using VBA for portfolio optimisation

    Hi,

    I am totally new to VBA and have been working on portfolio optimisation with mean-variance framework using solver with rolling window.

    I have been using Solver manually to compute the weights (minimising the variance as well as maximising the sharpe ratio) over a monthly sample period of 20 years, for 4 portfolios.

    I am totally clueless at using VBA, however, I am currently short of time now to manually compute these weights.


    Would really appreciate if someone is able to help me with the model.

    Many thanks,
    Natalie


    Cross post
    http://www.mrexcel.com/forum/excel-q...ml#post3748213
    Last edited by natalie28; 03-15-2014 at 03:35 PM.

  2. #2
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Solver using VBA for portfolio optimisation

    Perhaps you should start by upoading a model if you wish somebody to help you?

    Alf

  3. #3
    Registered User
    Join Date
    03-09-2014
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: Solver using VBA for portfolio optimisation

    Optimisation v4.xlsx

    Hi Alf,

    I have attached the model that I have been working on.

    Natalie
    Last edited by natalie28; 03-15-2014 at 06:44 AM.

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,936

    Re: Solver using VBA for portfolio optimisation

    Welcome to the forum

    Your post does not comply with Rule 8 of our Forum RULES. Do not crosspost your question on multiple forums without including links here to the other threads on other forums.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).

    http://www.mrexcel.com/forum/excel-q...ml#post3748213
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  5. #5
    Registered User
    Join Date
    03-09-2014
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: Solver using VBA for portfolio optimisation

    Hi FDibbins,

    Thank you and I wasnt aware and I apologise for the cross post.

    regards,
    Natalie

  6. #6
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Solver using VBA for portfolio optimisation

    Not sure I understand your problem properly but to just set up a macro that runs the solver model you have in sheet "MeanVar_MP" this should do

    Please Login or Register  to view this content.
    But I think you need to run solver in a loop chewing on all the colected data on this sheet i.e. columns C, D and E perhaps if so I need more information.

    Before you can use Solver with a VBA macro you must set a reference to solver in Visual Basic. Click on the "Developer" tab -> click "visual basic" in the new windows that opens click "Insert" and then "Module" Paste "SolvRun" macro in this "window". Then go to "Tools" - "References" and find "Solver" tick box in front of solver now you can run solver using a macro.

    I would also recomend you search the Forum with Solver as the search string perhaps you may find a thread that may be of help.

    Alf

  7. #7
    Registered User
    Join Date
    03-09-2014
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: Solver using VBA for portfolio optimisation

    Hi Alf,

    I have followed according to your instructions. however, when i run the VBA code in a new window, it seems to give me a different answer from the answer i obtained when i click on Solver manually.

    As for the looping, for example, in order to obtain the weights for Jan-98 ('sheet MeanVar_MP; cell 04:Q4), i used the figures from the ('inputs' sheet, array E5:G52') Jan -94 to Dec-97 brought forward into ('sheet MeanVar_MP;array C2:E49) and then I use solver to maximise the sharpe ratio (cell I18) with constraints of weights(sum=H5:J5) =1.

    Subsequently, to obtain the weights for Feb-98 ('sheet MeanVar_MP; cell 05:Q5), i used the figures from the 'inputs' sheet, array E5:G52' (Feb -94 to Jan-98) and repeat by maximising using solver.

    I will continue to obtain weights up to Dec-13 ('sheet MeanVar_MP; O195:Q195) using figures from the 'inputs' sheet, array E196:G244 ' (Dec-09 to Nov-13).

    Appreciate your help.

    Regards
    Natalie

  8. #8
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Solver using VBA for portfolio optimisation

    it seems to give me a different answer from the answer i obtained when i click on Solver manually.
    If I assume that the values in sheet “MeanVar_MP” cells H5:J5 and I18 are the result of a solver run in your uploaded file so I would expect these values to stay the same if I did a manually rerun of solver.

    But a rerun of solver changes range H5:J5 from “0.28862, 0.3577494, 0.353634”
    to “0.361779, 0.638221, 0”

    And I18 goes from “0.1340433” to “0.207819” and these results are the same as I get when I’m running the macro.

    A solver solution may differ depending on the solver “starting point” and looking at you model I see that you have not ticked the box “Use Multistart”. Don’t know if this something you should test I’ll add a link explaining the workings of this then you can decide.

    http://www.solver.com/content/basic-...al-solution-17

    As for looping solver I assume you wish to update range “O4:V??” with solver results but I can’t see how you change solver settings to get different results.

    As an example of setting up a solver loop I'll add this link.

    http://www.excelforum.com/excel-prog...ml#post3205531

    Alf

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Solver: Optimal Portfolio
    By leviathan86 in forum Excel General
    Replies: 3
    Last Post: 11-20-2013, 06:20 PM
  2. Optimisation without Solver or Macros
    By Kozy in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 10-24-2011, 03:59 AM
  3. Portfolio Optimization. Solver Constraint Issue
    By spgoofyft in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-23-2011, 07:02 PM
  4. Optimisation problems (beyond Solver)
    By DHP in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-12-2007, 04:59 AM
  5. portfolio optimization with solver
    By jrom1 in forum Excel General
    Replies: 2
    Last Post: 01-13-2007, 12:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1