portfolio backtesting ascii map files starts on line 52

Questions about MultiCharts .NET and user contributed studies.
novaleaf
Posts: 49
Joined: 17 Apr 2014
Has thanked: 9 times
Been thanked: 4 times

portfolio backtesting ascii map files starts on line 52

Postby novaleaf » 15 May 2015

pretty strange bug I'm running into.

I load a bunch of ascii-mapped instruments via quote manager. I can chart (and load chart signals) fine.

But when I add these to portfolio trader and try to backtest, it skips the first 51 lines of the file!! (skips 50 data-rows if you exclude the header).

So the first bar processed by the portfolio trader backtester is always line 52. everything before that is skipped.

I have verified this on 3 separate ascii files, which I have attached. when trying to backtest 2015/04/27 the first bar calculated is on line 52.
Attachments
20150427-instruments.zip
(8.08 KiB) Downloaded 885 times

novaleaf
Posts: 49
Joined: 17 Apr 2014
Has thanked: 9 times
Been thanked: 4 times

Re: BUG: portfolio backtesting ascii map files starts on lin

Postby novaleaf » 15 May 2015

I pretty much verified this. the first 51 bars "to be used" in the file are eaten.

if I add 51 extra lines on the weekend (20150426) they are ignored, and the first 51 lines from 20150427 are still eaten.

if I add 51 extra lines for friday (20150424) those are eaten and all my lines for 20150427 are backtested properly.

See the attached ascii file. for the Portfolio Backtest, I'm using exchange time, with a data range from "4/24/2015" to "4/30/2015"

I verified this by adding a signal which will output the Bar.TimeValue during CalcBar()
Attachments
CCCC_201504270930_201504291600.zip
(2.55 KiB) Downloaded 1113 times

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: BUG: portfolio backtesting ascii map files starts on lin

Postby JoshM » 17 May 2015

I didn't import the attached files and tried to replicate it so I might be wrong here, but can the value of 50 (51 if the header is included) also be caused by the MaxBarsBack setting?

(Right-click the strategy in the Portfolio Trader, select 'show properties', and look for the 'maximum number of bars study will reference' setting in the 'Properties' tab).

What MaxBarsBack value do you use in the Portfolio Trader? If you increase it to 100 for example, are the first 50 values still skipped or the first 100 then?

novaleaf
Posts: 49
Joined: 17 Apr 2014
Has thanked: 9 times
Been thanked: 4 times

Re: BUG: portfolio backtesting ascii map files starts on lin

Postby novaleaf » 19 May 2015

Reading the docs it looks very suspicious that the default value for MaxBarsBack is 50 (since I am seeing 51 bars skipped) so seems likely to be related, but unfortunately, I do not know where to set the MaxBarsBack setting in the portfolio trader.

When I set the "Data Range" to 5000 Bars Back from 04/27/2015, I still see the same problem (the first 51 bars are skipped).

Also in the various files I have tested, the first 51 lines are skipped, regardless as to how many lines are in the actual file (each file is different)

Additionally, my signal has no problems obtaining the first 51 bars is I apply it to a normal chart, only the portfolio trader is seeing this issue.

So, I just read that I should submit this to the Proejct Management section, so I'll do that now. EDIT: here's the issue: https://www.multicharts.com/pm/viewissu ... no=MC-1871
Last edited by novaleaf on 19 May 2015, edited 1 time in total.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: portfolio backtesting ascii map files starts on line 52

Postby Henry MultiСharts » 19 May 2015

Hello novaleaf,

• MaxBarsBack is the amount of bars that would be used for initial script calculation, to get the initial value of functions, variables. This amount of bars would be skipped, so you will see a first signal on maxbarsback+1 bar (for ex, may vary depending on the strategy).
• For signals - the minimum MaxBarsBack is the largest MaxBarsBack required by all signals applied to the chart. For ex. you have data1=1 min, data2=1 day. If an Average in your code references data2 with Length=10, then the required MaxBarsBack value is 10. 10 days of data will be skipped on both series. If your code does not reference additional data series then bars of data1 will be skipped only.
• For indicators - the minimum MaxBarsBack is the largest look back (Length) required for calculation in this indicator.
• Chart/Portfolio data range should be higher than MaxBarsBack value.
• If your MaxBarsBack value is not enough the study would not be calculated and will show an error message.
• If your MaxBarsBack is too high (more than the amount of bars on your chart) then the study will be waiting for the required amount of bars. On the chart you can see "Strategy calculating" message in such case or "Calculating" for an indicator.

In portfolio you can configure the MaxBarsBack per JoshM's instructions:
(Right-click the strategy in the Portfolio Trader, select 'show properties', and look for the 'maximum number of bars study will reference' setting in the 'Properties' tab).

novaleaf
Posts: 49
Joined: 17 Apr 2014
Has thanked: 9 times
Been thanked: 4 times

Re: portfolio backtesting ascii map files starts on line 52

Postby novaleaf » 19 May 2015

Hi Henry, In the Portfolio Trader, Signals do not have a "show properties" option. You can only format the signal.

novaleaf
Posts: 49
Joined: 17 Apr 2014
Has thanked: 9 times
Been thanked: 4 times

Re: portfolio backtesting ascii map files starts on line 52

Postby novaleaf » 19 May 2015

Additionally, in charts (where you can set the MaxBarBack property) the default is 50 and ASCII mapped charts+signals work fine, no bars are skipped and I never had to mess with the MaxBarsBack property.

novaleaf
Posts: 49
Joined: 17 Apr 2014
Has thanked: 9 times
Been thanked: 4 times

Re: portfolio backtesting ascii map files starts on line 52

Postby novaleaf » 19 May 2015

slight clarification:

The first 50 bars of the text file are eaten by portfolio trader. I originally said 51, but I forgot that the first line is the column header so that doesn't count.

novaleaf
Posts: 49
Joined: 17 Apr 2014
Has thanked: 9 times
Been thanked: 4 times

Re: portfolio backtesting ascii map files starts on line 52

Postby novaleaf » 19 May 2015

here is my workaround (and I tested it before writing this!)

I duplicate the first (09:30) bar 50 times, but set the times of the duplicates from 08:01 to 08:50. This workaround is obviously not ideal but it'll suffice for my current usage.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: portfolio backtesting ascii map files starts on line 52

Postby JoshM » 19 May 2015

In portfolio you can configure the MaxBarsBack per JoshM's instructions:
(Right-click the strategy in the Portfolio Trader, select 'show properties', and look for the 'maximum number of bars study will reference' setting in the 'Properties' tab).
Hi Henry, In the Portfolio Trader, Signals do not have a "show properties" option. You can only format the signal.
Even though you've already found a workaround, for completeness:

Image

Image
Attachments
scr.20-05-2015 06.18.02.png
(39.8 KiB) Downloaded 2153 times
scr.20-05-2015 06.17.15.png
(11.44 KiB) Downloaded 2085 times

novaleaf
Posts: 49
Joined: 17 Apr 2014
Has thanked: 9 times
Been thanked: 4 times

Re: portfolio backtesting ascii map files starts on line 52

Postby novaleaf » 20 May 2015

EDIT: does not work, see following post.

Thank you so much Josh.

Indeed, once I knew to right-click on the root node of the Portfolio Tree, I was able to access the Properties window as you said, and setting "Maximum number of bars study will reference" to zero solves the problem.

I will update my support ticket with the following:

1) Portfolio Properties should be accessible via the dropdown menu, or a tab-window such as the Data and Portfolio Settings tabs. It is non-obvious that the only way to access Portfolio Properties is to right-click the root.

2) Portfolio Property for "Maximum number of bars study will reference" is broken, or very confusing to use. This is functionally not the same as MaxBarsBack. In practice it seems to be used to skip bars, not include bars.
Last edited by novaleaf on 20 May 2015, edited 1 time in total.

novaleaf
Posts: 49
Joined: 17 Apr 2014
Has thanked: 9 times
Been thanked: 4 times

Re: portfolio backtesting ascii map files starts on line 52

Postby novaleaf » 20 May 2015

UPDATE: *sigh* unfortunatly JoshM's suggestion does not work for me.

It looks as if the "Maximum number of bars study will reference" does also set a strategy's MaxBarsBack value (in addition to truncating the first N bars when portfolio testing)

I did not see this when first testing the workaround, because this setting does NOT limit indicators. it only limits strategies. so when my strategy uses indicators to skip an instrument, everything works (which is the usual case) but when my strategy wishes to access this.Bars.Close[] then I get the error "Tried to reference back more bars (1) than allowed by the current MaxBarsBack setting"

This is a very frustrating and evidently only partially supported feature. (Ascii mapping)

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: portfolio backtesting ascii map files starts on line 52

Postby Henry MultiСharts » 21 May 2015

2) Portfolio Property for "Maximum number of bars study will reference" is broken, or very confusing to use. This is functionally not the same as MaxBarsBack. In practice it seems to be used to skip bars, not include bars.
"Maximum number of bars study will reference" = MaxBarsBack. it works as expected and there is nothing wrong with it. As I have described above: MaxBarsBack is the amount of bars that would be used for initial script calculation, to get the initial value of functions, variables. This amount of bars would be skipped, so you will see a first signal on maxbarsback+1 bar (for ex, may vary depending on the strategy).
I did not see this when first testing the workaround, because this setting does NOT limit indicators. it only limits strategies.
That is correct. Indicators have their own MaxBarsBack setting (Format Indicator->Properties->"Maximum number of bars study will reference".
so when my strategy uses indicators to skip an instrument, everything works (which is the usual case) but when my strategy wishes to access this.Bars.Close[] then I get the error "Tried to reference back more bars (1) than allowed by the current MaxBarsBack setting"
That is correct. You cannot look back more bars than your MaxBarsBack.

To learn more about MaxBarsBack - please study the following article: How Scripts Work. Additional information can be found here.

novaleaf
Posts: 49
Joined: 17 Apr 2014
Has thanked: 9 times
Been thanked: 4 times

Re: portfolio backtesting ascii map files starts on line 52

Postby novaleaf » 21 May 2015

Hi Henry, thank you for the details.

I agree/admit that this issue seems to be my misunderstanding on how MaxBarsBack works.

I have an indicator that needs to accumulate each day's O/H/L/C but I can't do that if it's start is delayed by MaxBarsBack, as my ascii file contains only the needed bars. (9:30 to 4pm)

How would you suggest I make this work?

It appears that if I need to use all the bars of my ascii file, yet wish to have a strategy that can peek back a few bars, I will need to create a Indicator that stores the values of "this.Bars" so that my strategy can access it.

EDIT: It seems that perhaps the real issue is that the MaxBarsBack value impacts indicators (makes those first MaxBarsBack inaccessable), not just strategies.

I've attached a repro of the issue I'm seeing if anyone wants to view.
1) load up the ascii file in quote manager (instrument NEO)
2) create a portfolio trader with "SLE_Strategy_1" strategy and the NEO instrument.
3) open the powerLanguage.net editor so you can view the Output window when backtesting.
4) run the backtest from 04/27 to 04/30 (timezone:exchange).
5) Look at the powerLanguage.net editor output: you'll see the output showing the start of the day on 04/27 is not 09:30.
Attachments
mc-function-export.zip
(13.69 KiB) Downloaded 839 times

novaleaf
Posts: 49
Joined: 17 Apr 2014
Has thanked: 9 times
Been thanked: 4 times

Re: portfolio backtesting ascii map files starts on line 52

Postby novaleaf » 21 May 2015

OK, so I think I understand MaxBarsBack. Here is my summary for anybody in the future who's struggling with this issue.

MaxBarsBack is basically there to make the developer/user not have to worry about the length of data that's available.

The reason my daily ohlc indicator works with a zero maxBarsBack is that I accumulate the day from the current bar (never looking back), and I store the daily bars as a List<T>, not as a multicharts VariableSeries<T>, so my List<T> is not auto-pruned by maxBarsBack.

There seem to be two possible solutions:

1) (not the best choice) is to construct my own data storage for each bar (not use VariableSeries) and manage it's length manually (to avoid running out of memory with hundreds of instruments). This would let me use maxBarsBack=0 , but would require me to basically do my own data management, and wouldn't let me use the built-in indicators/strategies very much.

2) (probably the best choice) is to modify my daily ohlc indicator so that in the StartCalc() I retroactively crawl through the last maxBarsBack-number-of-bars bars to generate my history. This will let me use the default MaxBarsBack=50 as my strategy doesn't use the first day's worth of bars (those are for generating yesterday ohlc). If, in the future, my strategy does ever need to use something in the past, I will need to hook StartCalc() and retroactively walk the history, as described for my daily ohlc indicator.

novaleaf
Posts: 49
Joined: 17 Apr 2014
Has thanked: 9 times
Been thanked: 4 times

Re: portfolio backtesting ascii map files starts on line 52

Postby novaleaf » 21 May 2015

for anyone who may be interested, here is my daily ohlcv indicator that takes MaxBarsBack history into account. it seems to be the solution to my issue.

viewtopic.php?f=20&t=48526&p=115658#p115658


Return to “MultiCharts .NET”