Generating and printing statistics for a portfolio

Questions about MultiCharts and user contributed studies.
User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Generating and printing statistics for a portfolio

Postby furytrader » 30 Mar 2015

I am looking to calculate and average the maximum drawdown per year for a portfolio of markets. I have the math pretty much nailed down, but I'm not sure how to print this out so that it only prints the data out one time (for the portfolio as a whole).

If I put this code as a new strategy into portfolio trader (along with my signal generating strategies), it prints out the portfolio data multiple times equal to the number of markets I have in my portfolio. I only want it printed out once. Is there a way to do that?

orion
Posts: 250
Joined: 01 Oct 2014
Has thanked: 65 times
Been thanked: 104 times

Re: Generating and printing statistics for a portfolio

Postby orion » 30 Mar 2015

PT has two types of signals: the base signals and the PMM signal. The base signals run for each instrument while the PMM signal runs only once. So make all portfolio statistics output a part of the PMM signal.

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Generating and printing statistics for a portfolio

Postby furytrader » 30 Mar 2015

Thank you for this suggestion. It seems to have gotten me closer to the solution I am looking for. However, it looks like I still have some work to do.

I added my signal as the Money Management signal from with the Portfolio Settings but it looks like for each year, it is calculating three separate values (I have 19 markets being traded).

Consequently, when the code seeks to average all of the annual maximum drawdown values, it incorporates all 57 elements (19 x 3) of the calculated data.

I am not sure why it would calculate each year 3 times (and get 3 different values for each year).

I am new to the new aspects of the Portfolio Trader, so clearly I am missing something here.

orion
Posts: 250
Joined: 01 Oct 2014
Has thanked: 65 times
Been thanked: 104 times

Re: Generating and printing statistics for a portfolio

Postby orion » 30 Mar 2015

Not being privy to your code or the bar data, I can only hazard a guess as to why this is happening and my guess is that it is because different instruments have different ending bars. In other words, the bar end times are not synchronized across the instruments. Note that the signal will run once for each new bar time encountered. I enclose my PMM portfolio statistics signal in two conditionals like following to allow for bar times not being synchronized across the instruments and everything runs as it is supposed to.

Code: Select all

if (lastDate <> date) then begin
// daily portfolio computation goes here
end

if (lastBarOnChart) then begin
// final portfolio computation goes here
end

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Generating and printing statistics for a portfolio

Postby furytrader » 30 Mar 2015

Thanks again for your help. I am using end-of-day bars, and I have included code similar to what you provided above in my code.

Here is my code. Note that it is designed to work for single systems (in MultiCharts) as well as within Portfolio Trader. Note also that it uses the ELCollections.dll:

Code: Select all

// This code calculates the maximum annual drawdown for a single system or portfolio

Inputs: TradingEquity(1000000); // Used for analyzing single systems outside of Portfolio Trader
Variables: BeginningEquity(0), MaxDD(0), HighWaterMark(0), DailyEquity(0), DailyDD(0);
Variables: OkayToAdd(False),DDList(ListN.New);

// We're going to assume that we're starting mid-year
If BarNumber = 1 Then Begin
If GetAppInfo(aiIsPortfolioMode) = 1 Then BeginningEquity = Portfolio_Equity Else BeginningEquity = TradingEquity;
HighWaterMark = BeginningEquity;
End;

// Print out each year as it happens
If (Year(Date) > Year(Date[1])) OR (LastBarOnChart) And BarStatus = 2 Then begin
Print(Year(Date[1])+1900," ",NumToStr(MaxDD*100,4),"%");
If OkayToAdd = True Then ListN.PushBack(DDList,MaxDD);
MaxDD = 0;
BeginningEquity = DailyEquity[1];
HighWaterMark = BeginningEquity;
OkayToAdd = True;
End;

// If we're at the last bar, print statistics
If LastBarOnChart = True Then begin
Print("Average Annual Drawdown = ",NumToStr(ListN.Average(DDList,1,ListN.Count(DDList))*100,4),"%");
End;

// Update the daily drawdown (if it exists)
If GetAppInfo(aiIsPortfolioMode) = 1 Then DailyEquity = Portfolio_Equity + Portfolio_OpenPositionProfit Else DailyEquity = TradingEquity + OpenPositionProfit + NetProfit;
If DailyEquity > HighWaterMark Then HighWaterMark = DailyEquity;

// Capture when we're at a new drawdown low
DailyDD = (DailyEquity - HighWaterMark)/HighWaterMark;
If DailyDD < MaxDD Then MaxDD = DailyDD;
Maybe someone from MultiCharts will know the answer?

orion
Posts: 250
Joined: 01 Oct 2014
Has thanked: 65 times
Been thanked: 104 times

Re: Generating and printing statistics for a portfolio

Postby orion » 30 Mar 2015

To troubleshoot lets reduce your code to bare essentials. Using the following code as PMM signal in my portfolio of 40+ futures with daily bars prints out each year only once. Does it do the same for your database or does it print each year 3 times? (Note that the PMM signal is only the following 3 lines of code and nothing else.)

Code: Select all

if (year(date) > year(date[1])) then begin
print((date + 19000000):0:0);
end;

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Generating and printing statistics for a portfolio

Postby furytrader » 30 Mar 2015

It prints out three times at the end of each year.

Code: Select all

...
20110103
20110103
20110103
20120103
20120103
20120103
20130102
20130102
20130102
...

orion
Posts: 250
Joined: 01 Oct 2014
Has thanked: 65 times
Been thanked: 104 times

Re: Generating and printing statistics for a portfolio

Postby orion » 30 Mar 2015

Just one last experiment. Attached is image capture of my simple PT setup with a base signal called pc.s.DummySignal2 and a PMM signal called pc.s.TestPortfolio. See the red boxes highlighted on where these signals go. The code for these signals are as below. The base signal simply counts the number of bars in the instrument and outputs it. The PMM signal prints the date on start of a new year.

Code: Select all

// base signal
vars: count (0);
count += 1;
if (lastBarOnChart) then print(symbol, ",", count:0:0);

Code: Select all

// PMM signal
if (year(date) > year(date[1])) then begin
print((date + 19000000):0:0);
end;
Running this setup with 8 years of daily bar history produces following:

Code: Select all

20080102
20090102
20100104
20110103
20120103
20130102
20140102
@AD,1859
@BO,1859
@BP,1859
@C,1859
@CC,1855
...
The years are printed only once (as they should be). Would recommend stripping your signals to the essentials as above and running it one more time. If you are still getting the year printed 3 times then that would be really bizarre and we will need to escalate to MC support since it is then likely to be some kind of bug or some data corruption issue with your database. Please post back on progress.
Attachments
PT.jpg
(342.79 KiB) Downloaded 1216 times

orion
Posts: 250
Joined: 01 Oct 2014
Has thanked: 65 times
Been thanked: 104 times

Re: Generating and printing statistics for a portfolio

Postby orion » 30 Mar 2015

If the above still prints 3 times per year, also try the following as PMM signal and see if this changes anything.

Code: Select all

vars: lastDate (0);
if (year(date) > year(lastDate)) then begin
lastDate = date;
print((date + 19000000):0:0);
end;

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Generating and printing statistics for a portfolio

Postby furytrader » 30 Mar 2015

The last one was the charm - you figured it out! It must have something to do with how the portfolio trader was handling the date comparison. Weird!

Thanks a million!!!

orion
Posts: 250
Joined: 01 Oct 2014
Has thanked: 65 times
Been thanked: 104 times

Re: Generating and printing statistics for a portfolio

Postby orion » 30 Mar 2015

Glad your problem got fixed. Now for the explanation on why the first PMM signal did not work for you but worked for me and why we needed the second PMM signal as the workaround in your case...

I am reasonably confident the problem in your case was that your futures database has bars that have different session end times. The PMM signal will run each time it encounters a new bar session end time as discussed here. My futures database uses the same session end time for all symbols since a custom session template is used for all futures.

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Generating and printing statistics for a portfolio

Postby furytrader » 30 Mar 2015

What's weird is that the price data I'm using (end-of-day) doesn't have a time stamp, just a date stamp.

orion
Posts: 250
Joined: 01 Oct 2014
Has thanked: 65 times
Been thanked: 104 times

Re: Generating and printing statistics for a portfolio

Postby orion » 30 Mar 2015

What's weird is that the price data I'm using (end-of-day) doesn't have a time stamp, just a date stamp.
It is not due to absence or presence of time stamps on price data for daily bars. Most daily bar data will not have time stamps (and will have only date stamps as you mention). However, each symbol belongs to an exchange and the exchange has session times associated with it in your database (as you can see in your Quote Manager). The reason your PMM signal ran 3 times for each day is because you have symbols from 3 different exchanges in your setup (CME, CBOT, and ICE perhaps?) and the 3 exchanges have different session settings.


Return to “MultiCharts”