Warning: count(): Parameter must be an array or an object that implements Countable in /home/goedin21/domains/quantingdutchman.com/public_html/wp-includes/post-template.php on line 284

Warning: count(): Parameter must be an array or an object that implements Countable in /home/goedin21/domains/quantingdutchman.com/public_html/wp-includes/post-template.php on line 284

Warning: count(): Parameter must be an array or an object that implements Countable in /home/goedin21/domains/quantingdutchman.com/public_html/wp-includes/post-template.php on line 284

Warning: count(): Parameter must be an array or an object that implements Countable in /home/goedin21/domains/quantingdutchman.com/public_html/wp-includes/post-template.php on line 284

Simulating a Portfolio of Strategies in Amibroker (2/2)

In my previous post I introduced the main concept I use to simulate the portfolio of strategies. In this post I will illustrate the mechanics of this approach.We will use a simple portfolio of 3 strategies that are described in this blog: WTAA, MEOM and DVI strategies. We will use Amibroker 5 steps to get to the result.

1. Run individual strategy in Backtester

The basic idea is to run each individual strategy in AB and store the strategy return array for use in the second iteration. If you are familiar with AB this should be straight forward. After a backtest, the strategy return gets automatically stored in the ~~~Equity – ticker in Group253.  This ticker will be overwritten after each backtest, however this will be fixed with step 2.

When running a strategy, it is important to apply money management as if you were trading the strategy standalone (i.e. with % of equity or fixed fraction or … mm scheme). I also make it a habit to define a variable in the formula code that identifies the name of strategy, e.g:

SystemName="sWD_KwantTAA";

2. Use custom backtest logic to generate individual strategy Equity curve

To avoid that the individual strategy return array is overwritten during the next backtest, I have written some Custom Backtest Code that copies the ~~~Equity ticker to a new ticker with a custom name. This code is applied at the end of the backtest run and creates a new ticker with a name  ~~ Systemname. (note that systemname is a variable assigned in the strategy formula code used under step 1. The code for the CBT file can be downloaded at the end of the post.

e= Foreign( "~~~Equity", "C" );
AddToComposite(e/1000,"~~"+SystemName,"X",atcFlagDefaults | atcFlagEnableInPortfolio | atcFlagEnableInBacktest);

3. Repeat 1&2 for all systems of interest

Please make sure when doing this to use the right watchlist for each strategy and to ensure all strategies are run for the same period.

4. Add individual return arrays to watchlist

In the symbol list in Amibroker identify all newly created return arrays. Through my coding rules I can find them easily – the names of the symbols all start with double tildes: ~~… Add the return arrays of the strategies you want to include to a watchlist.

I have name one “Portfolio of Strategies” as shown below:

5. Run meta-strategy in Backtester on watchlist

The last step involves the 2nd iteration. It is basically a backtest using a very simple backtest buy & hold strategy in the 3 strategy return arrays as they are stored in the watchlist. Here is the formula for the portfolio strategy when applying it to the portfolio of the 3 strategies as mentioned before.

SystemName="sD_ETF_PortfolioOfSystems";
_CashPercentage=0;_MaxScaleIn=4; _Cashticker    ="SHY"; _Cashticker2        = "VIX";
#include<Boiler Plate.afl>
#include<Additional Functions.afl>
#include<OutputStatistics.afl>
pos=3;
Size=25000;
SetOption("InitialEquity",size*pos);
SetOption("AccountMargin",100);
SetPositionSize(100/pos,spsPercentOfEquity);
SetOption("MaxOpenPositions", pos );
//SetOption("interestrate",2.4);
Buy = True;  Sell= False;
Short =0;  Cover =0;

The Example / First Results

In the example I have used the 3 strategies as described in this blog. In the portfolio system described in step 5 I allocate 25000$ to each of the three strategies and simulate a “buy & hold” in each of the strategies. No rebalancing or any other fancy logic is applied. The results are shown below in 2 pictures. The first shows the normalised equity curves for the individual strategies and the portfolio. The second picture shows the equity curve, the monthly/yearly returns as well as the rolling 3yr Ann Sharpe ratio for the portfolio.

Code

Finally I you can use this link to download the Custom Backtest file. It has a .docx extension (due to WordPress inability to upload .txt or .afl files). So you have to rename it to .afl and store it in the \include folder. The file contains some instructions at the top of the file (read the note).

Happy trading!
-QD-

11 thoughts on “Simulating a Portfolio of Strategies in Amibroker (2/2)

  1. I like very much this solution. I have made an Access program to mix the equity of my systems, but I think is better to do it in Ami.

    Sadly, I have a big problem: Some of my systems use the foreign command to establish a Filter based in the SP500 index, so I can stop the system if SP500 falls sharply: Something like:
    Cl_SPX=Foreign(“$spx”,”close”); or using the command “setforeign”…

    The problem is that it seems that Ami does not let using Foreign command twice in the same program. At the end of the program I’ve written the foreing lines QDutchman has told us, but the new ticker created with “AddToComposite” is in blank.
    If I take away the first “foreing” it works well.
    Anybody knows any solution?

    Thanx!

  2. OutputStatistics.afl doesn’t compile; there is an error on line 152, perhaps because it doesn’t recognize the Resample function?

    It’s also not necessarily for the meta-strategy code to work, so I’m unclear what the purpose of it is?

    Also, under step 2, it is not custom back code, but regular AFL code. I suspect this is what you meant:

    SetCustomBacktestProc( “” );
    if ( Status( “action” ) == actionPortfolio )
    {
    bo = GetBacktesterObject();
    bo.Backtest();
    AddToComposite( Foreign( “~~~EQUITY”, “C” ),
    “~~~EQUITY_”+SystemName, “X”,
    atcFlagDeleteValues | atcFlagEnableInPortfolio );
    }

  3. QD,

    Would you be willing to upload

    #include
    #include

    I see you reference them in a number of your strategies. Great blog keep up the good work.

    -Brian

  4. Looks like wordpress edited out my request, it was for
    Boiler Plate.afl
    Additional Functions.afl

    • Hi Brian, the .afl do not contain specific functionalities that are needed for the strategies in the blog. They just contain some shortcuts to settings that I do not want to retype in each new strategy. QD

  5. Is it possible to do this if your systems are modeling from 2 separate databases? How to you export the equity curve from one into the other?

Leave a Reply to The Quanting Dutchman Cancel reply