Page 1 of 1

Dynamic MaxBarsBack ?

Posted: Apr 04 2019
by hbscreener
Hi all,

I use the Portfolio Trader to load, say 100 symbols. Different symbols contain different number of bars. If the symbol has >= 250 bars, the highest high of the recent 250 bars is obtained. If the symbol has < 250 bars, the highest high of all the bars is obtained.

The code is shown below.

Code: Select all

if LastBarOnChart then
begin
if BarNumber >= 250 then
high_250d = highest(high, 250)
else
high_250d = highest(high, BarNumber);
end;
Now, the problem is that the maxbarsback is set to be 0 so that all symbols can be loaded into the Portfolio Trader, but then there will be runtime error showing that I have tried to reference back more bars (1) ..... If the maxbarsback is set to be 250, the symbols with less than 250 bars will not be loaded into the Portfolio Trade at the beginning but I just want to include all of the symbols.

I know maxbarsback cannot be changed at runtime by code, so any other means to achieve what I want to do?

Thanks in advance.

Re: Dynamic MaxBarsBack ?

Posted: Apr 04 2019
by TJ
See post #1 & post #2
viewtopic.php?t=11713

Re: Dynamic MaxBarsBack ?

Posted: Apr 04 2019
by hbscreener
Thanks TJ.

Is it impossible to do so?

Re: Dynamic MaxBarsBack ?

Posted: Apr 04 2019
by TJ
Why do you have less than 250 bars of data?
That's not a lot of data.

This is like going to a soccer match with 6 players.
You have to decide: do you want to play the game or not.

Re: Dynamic MaxBarsBack ?

Posted: Apr 05 2019
by hbscreener
Why do you have less than 250 bars of data?
That's not a lot of data.

This is like going to a soccer match with 6 players.
You have to decide: do you want to play the game or not.
Thanks, TJ.

The portfolio actually contains a large basket of stocks, with long history and short. I just want to calculate the % of correction from the 250-day high or from the X-day high if it is less than 250-day.

I still can't think of any workaround.

Re: Dynamic MaxBarsBack ?

Posted: Apr 05 2019
by ABC
hbscreener,

you have to avoid looking back at previous bars values, like you do within the highest function. One way to deal with that could be to keep an array of the last 250 bar values and find your highest value within the array.

Regards,

ABC

Re: Dynamic MaxBarsBack ?  [SOLVED]

Posted: Apr 05 2019
by hbscreener
hbscreener,

you have to avoid looking back at previous bars values, like you do within the highest function. One way to deal with that could be to keep an array of the last 250 bar values and find your highest value within the array.

Regards,

ABC
Thank you so much, ABC.

It is definitely a way out for me.