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
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;
Could you please give me some pointers as to what I'm doing wrong?
Thanks.
Andrea