Equal weights portfolio strategy  [SOLVED]

Questions about MultiCharts and user contributed studies.
andrea.venuta
Posts: 3
Joined: 16 May 2018

Equal weights portfolio strategy

Postby andrea.venuta » 16 May 2018

Hi,

I'm a new user of MultiCharts and I'm trying to understand whether it is fit for my use case of asset allocation strategies. To do that, I started by attempting to solve a very simple problem, that is, building an equally weighted portfolio strategy with monthly rebalancing in Portfolio Trader. I studied all of the documentation I could find related to portfolio signals (money management keywords, strategy examples on the wiki and whatnot) but can't wrap my head around MC's way of doing things. I understood a buy/sell signal has to be applied to each instrument to begin with, and once the signal has been processed for each instrument in a given bar, it is time for the portfolio money management signal to be evaluated over the whole portfolio. So my pseudocode idea would be the following:

Code: Select all

signal ->
sell at the end of the month;
buy the next bar
money management ->
set position size = total portfolio equity / number of instruments;
close all the long positions;
buy enough contracts to reach position size
Which, albeit crude (there's no need to dump the whole position if all I need is to sell a couple of contracts...), is still enough for my immediate learning needs.
After perusing the documentation to find the keywords matching what I needed (suggestion: I'd work on improving the documentation, it is very hard to read and sift through, and the function names are too long, confusing and difficult to understand, plus there's mixed snake_case and camelCase which makes it even more confusing) I implemented the two signals as follows:

Code: Select all

sell this bar at close;
buy next bar at open;
pmm_set_my_named_num("price", open);

Code: Select all

variables: weight (0), ii (0), ncontracts(0), nstrats(0);

if getappinfo(aiisportfoliomode) <> 1 then
raiseruntimeerror("Signal can be applied (as Money Management Signal) in Portfolio only.");

for ii = 0 to pmms_strategies_count - 1 begin
if pmms_get_strategy_named_num(ii, "price") > 0 then nstrats = nstrats + 1; end;

weight = 1 / nstrats;

for ii = 0 to pmms_strategies_count - 1 begin
pmms_strategy_allow_long_entries(ii);
pmms_strategy_allow_short_entries(ii);

if pmms_get_strategy_named_num(ii, "price") > 0 then begin
ncontracts = weight * Portfolio_Equity / pmms_get_strategy_named_num(ii, "price");
pmms_strategy_set_entry_contracts(ii, ncontracts - pmms_strategy_currentcontracts(ii));
end;
end;
While the logic looks reasonably good to me (but I'm not sure I actually understood correctly what each keyword is supposed to mean, so there may be big obvious mistakes in it), running a portfolio backtest yielded results I couldn't understand. The instrument data is correct, but somehow MC enters only a few positions a couple months after the backtest is started, and even then the trades highlight numbers of contracts that look like everything but equal weighting.
Could you please give me some pointers as to what I'm doing wrong?

Thanks.
Andrea

Zheka
Posts: 223
Joined: 13 Jan 2016
Has thanked: 8 times
Been thanked: 53 times

Re: Equal weights portfolio strategy

Postby Zheka » 16 May 2018

How did you set-up your portfolio?
Is it one "signal" (=strategy logic) applied to many instruments or many (same) "strategies" each applied to one instrument?

andrea.venuta
Posts: 3
Joined: 16 May 2018

Re: Equal weights portfolio strategy

Postby andrea.venuta » 17 May 2018

How did you set-up your portfolio?
Is it one "signal" (=strategy logic) applied to many instruments or many (same) "strategies" each applied to one instrument?
It is the same signal (the one in the second code box) applied to each of the instruments, and then a money management signal applied to the whole portfolio.

Zheka
Posts: 223
Joined: 13 Jan 2016
Has thanked: 8 times
Been thanked: 53 times

Re: Equal weights portfolio strategy

Postby Zheka » 17 May 2018

This might be connected with Required Capital assumptions (see top-right part in Portfolio settings)/Margin per contract settings.

Try to debug with 1 strategy, 2 strategies..

andrea.venuta
Posts: 3
Joined: 16 May 2018

Re: Equal weights portfolio strategy  [SOLVED]

Postby andrea.venuta » 17 May 2018

I solved my issue by re-initializing `nstrats` to 0 every time, and by changing the

Code: Select all

pmms_strategy_set_entry_contracts(ii, ncontracts - pmms_strategy_currentcontracts(ii));
part to

Code: Select all

pmms_strategy_set_entry_contracts(ii, ncontracts);
I was under the (wrong) assumption that variables got garbage collected at the end of each signal, and then reinitialized to the value in parenthesis at the start of the next signal -- which would be a good thing, albeit not necessarily easy to implement; it would be at least advisable to make this clear to users via documentation as it is counterintuitive. Now my MM code looks like this and the portfolio in the backtest looks sort-of balanced. Are the discrepancies related to the fact that MC assumes an integer number of contracts?

Code: Select all

variables: weight (0), ii (0), ncontracts(0), nstrats(0);

if getappinfo(aiisportfoliomode) <> 1 then
raiseruntimeerror("Signal can be applied (as Money Management Signal) in Portfolio only.");

nstrats = 0;
for ii = 0 to pmms_strategies_count - 1 begin
if pmms_get_strategy_named_num(ii, "price") > 0 then nstrats = nstrats + 1;
end;

if nstrats <> 0 then begin
weight = 1 / nstrats;

for ii = 0 to pmms_strategies_count - 1 begin
pmms_strategy_allow_long_entries(ii);

if pmms_get_strategy_named_num(ii, "price") > 0 then begin
ncontracts = weight * Portfolio_Equity / pmms_get_strategy_named_num(ii, "price");
print(nstrats, " ", ncontracts, " ", weight, " ", Portfolio_Equity, " ", pmms_get_strategy_named_num(ii, "price"));
pmms_strategy_set_entry_contracts(ii, ncontracts);
end;
end;
end else pmms_strategies_deny_entries_all;


Return to “MultiCharts”