Dynamic MaxBarsBack ?  [SOLVED]

Questions about MultiCharts and user contributed studies.
hbscreener
Posts: 4
Joined: 20 Nov 2018
Has thanked: 2 times

Dynamic MaxBarsBack ?

Postby hbscreener » 04 Apr 2019

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.
Last edited by hbscreener on 04 Apr 2019, edited 1 time in total.

User avatar
TJ
Posts: 7210
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 991 times
Been thanked: 2052 times

Re: Dynamic MaxBarsBack ?

Postby TJ » 04 Apr 2019

See post #1 & post #2
viewtopic.php?t=11713

hbscreener
Posts: 4
Joined: 20 Nov 2018
Has thanked: 2 times

Re: Dynamic MaxBarsBack ?

Postby hbscreener » 04 Apr 2019

Thanks TJ.

Is it impossible to do so?

User avatar
TJ
Posts: 7210
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 991 times
Been thanked: 2052 times

Re: Dynamic MaxBarsBack ?

Postby TJ » 04 Apr 2019

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.

hbscreener
Posts: 4
Joined: 20 Nov 2018
Has thanked: 2 times

Re: Dynamic MaxBarsBack ?

Postby hbscreener » 05 Apr 2019

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.

User avatar
ABC
Posts: 631
Joined: 16 Dec 2006
Has thanked: 119 times
Been thanked: 355 times
Contact:

Re: Dynamic MaxBarsBack ?

Postby ABC » 05 Apr 2019

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

hbscreener
Posts: 4
Joined: 20 Nov 2018
Has thanked: 2 times

Re: Dynamic MaxBarsBack ?  [SOLVED]

Postby hbscreener » 05 Apr 2019

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.


Return to “MultiCharts”