# for loop beginner

Questions about MultiCharts and user contributed studies.
aczk
Posts: 67
Joined: 08 Feb 2012
Has thanked: 8 times
Been thanked: 1 time

### for loop beginner

Hi

I am trying to create my first for loop, because I want to plot the max daily ranges of the last x days above/below todays open. Works perfect when just writing it all out like:

MaxR = maxlist( highd(1) - lowd(1), highd(2) - lowd(2), highd(3) - lowd(3), highd(4) - lowd(4), highd(5) - lowd(5));

..and then plotting that, but I wanna go much further back in time and add the flexibility tweaking the lookback.

This is what I came up, with (doesn't work, simply plots the range from lookkback days ago):

Any help very appreciated

Code: Select all

`vars:       x(0), HistRange(0), Lookback(3);HistRange = 0; For x = 0  to Lookback begin   HistRange = maxlist(dailyrange(x));   //dailyrange:  highd(numeric) - lowd(numeric)end;plot1(OpenD(0) + HistRange,"Top Intradday MAX range");plot2(OpenD(0) - HistRange,"Bottom Intradday MAX range");`

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: for loop beginner

aczk wrote:Hi

I am trying to create my first for loop, because I want to plot the max daily ranges of the last x days above/below todays open. Works perfect when just writing it all out like:

MaxR = maxlist( highd(1) - lowd(1), highd(2) - lowd(2), highd(3) - lowd(3), highd(4) - lowd(4), highd(5) - lowd(5));

..and then plotting that, but I wanna go much further back in time and add the flexibility tweaking the lookback.

This is what I came up, with (doesn't work, simply plots the range from lookkback days ago):

Any help very appreciated

Code: Select all

`vars:       x(0), HistRange(0), Lookback(3);HistRange = 0; For x = 0  to Lookback begin   HistRange = maxlist(dailyrange(x));   //dailyrange:  highd(numeric) - lowd(numeric)end;plot1(OpenD(0) + HistRange,"Top Intradday MAX range");plot2(OpenD(0) - HistRange,"Bottom Intradday MAX range");`

can you draw a diagram to illustrate what you want to do?

what is the chart resolution?

aczk
Posts: 67
Joined: 08 Feb 2012
Has thanked: 8 times
Been thanked: 1 time

### Re: for loop beginner

hourly bars on the dax, so plot the dailyranges on intraday. In the picture...

1. the first blue box is a big range day
2. the next two days ranges are smaller, so I plot the big range day onto them (0range), as it is the maximum of the last 1,2,3 days.
3. The next blue box, the range is bigger than anythin before and thus the maximum of last 4 days.
4. the next few days ranges are small again, thus plot the maximum of the last few days (orange)
Attachments
Picture1.png (228.83 KiB) Viewed 525 times

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: for loop beginner

aczk wrote:hourly bars on the dax, so plot the dailyranges on intraday. In the picture...

1. the first blue box is a big range day
2. the next two days ranges are smaller, so I plot the big range day onto them (0range), as it is the maximum of the last 1,2,3 days.
3. The next blue box, the range is bigger than anythin before and thus the maximum of last 4 days.
4. the next few days ranges are small again, thus plot the maximum of the last few days (orange)

you have to be more precise with your logic.

When do you switch between 3 days and 4 days?

What do you mean by maximum of the last few days?

aczk
Posts: 67
Joined: 08 Feb 2012
Has thanked: 8 times
Been thanked: 1 time

### Re: for loop beginner

Simple:

It plot the maximum past daily range for a certain lookback on todays chart.
If there is a new maximum range then it substitutes the old one.

for example for a 3 day lookback:

MaximumThreeDayRange = maxlist(highd(1) - lowd(1), highd(2) - lowd(2),highd(3) - lowd(3));
plot1(opend(0) + MaximumThreeDayRange /2);
plot2(opend(0) - MaximumThreeDayRange /2);

But I need it to work in a for..loop or array or something flexible where I can change the lookback etc.
The maxlist works fine in the above simple way, but I somehow can't get it to work on this loop??

thx & appreciate

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: for loop beginner

aczk wrote:Simple:

It plot the maximum past daily range for a certain lookback on todays chart.
If there is a new maximum range then it substitutes the old one.

for example for a 3 day lookback:

MaximumThreeDayRange = maxlist(highd(1) - lowd(1), highd(2) - lowd(2),highd(3) - lowd(3));
plot1(opend(0) + MaximumThreeDayRange /2);
plot2(opend(0) - MaximumThreeDayRange /2);

But I need it to work in a for..loop or array or something flexible where I can change the lookback etc.
The maxlist works fine in the above simple way, but I somehow can't get it to work on this loop??

thx & appreciate

you have to set up an array to do that.

ps. please use code tag when posting codes.

aczk
Posts: 67
Joined: 08 Feb 2012
Has thanked: 8 times
Been thanked: 1 time

### Re: for loop beginner

thx for the array tip, figured it out. works great to get the for instance 30 day max range...but there is a problem if I want a longer lookback like 120 days then it displays really weird/wrong stuff. Code below

Code: Select all

`inputs:   Lookback(30); vars:       x(0), MaxRange(0);array:      DailyRangesArray[120](1);      For x = 1  to Lookback begin   DailyRangesArray[x] = DailyRange(x);      MaxRange = HighestArray(DailyRangesArray,Lookback);end;plot4(MaxRangeShort,"short");`

Is there some sort of time/size limit on arrays if done like this? Am I doing something wrong or is it my data?

help pls

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: for loop beginner

aczk wrote:thx for the array tip, figured it out. works great to get the for instance 30 day max range...but there is a problem if I want a longer lookback like 120 days then it displays really weird/wrong stuff. Code below

Code: Select all

`inputs:   Lookback(30); vars:       x(0), MaxRange(0);array:      DailyRangesArray[120](1);      For x = 1  to Lookback begin   DailyRangesArray[x] = DailyRange(x);      MaxRange = HighestArray(DailyRangesArray,Lookback);end;plot4(MaxRangeShort,"short");`

Is there some sort of time/size limit on arrays if done like this? Am I doing something wrong or is it my data?

help pls

what do you mean by "it displays really weird/wrong stuff"?

Posts: 674
Joined: 09 Apr 2010
Has thanked: 438 times
Been thanked: 251 times
Contact:

### Re: for loop beginner

you said ".but there is a problem if I want a longer lookback like 120 days then it displays really weird/wrong stuff. "
*****************************************************************************
I think that you have to increase your array size, if you want a lookback greater than 120..
These users thanked the author sptrader for the post:
JoshM

aczk
Posts: 67
Joined: 08 Feb 2012
Has thanked: 8 times
Been thanked: 1 time

### Re: for loop beginner

I did that long ago, to no avail. Might this be the data?? Are there any naturalbounds for fixed arrays in MC???

thx

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: for loop beginner

aczk wrote:thx for the array tip, figured it out. works great to get the for instance 30 day max range...but there is a problem if I want a longer lookback like 120 days then it displays really weird/wrong stuff. Code below

Code: Select all

`inputs:   Lookback(30); vars:       x(0), MaxRange(0);array:      DailyRangesArray[120](1);      For x = 1  to Lookback begin   DailyRangesArray[x] = DailyRange(x);      MaxRange = HighestArray(DailyRangesArray,Lookback);end;plot4(MaxRangeShort,"short");`

Is there some sort of time/size limit on arrays if done like this? Am I doing something wrong or is it my data?

help pls

if you are doing daily range,
all you need is to calculate the range once a day.

Code: Select all

`if d > d[1] then begin`

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: for loop beginner

aczk wrote:thx for the array tip, figured it out. works great to get the for instance 30 day max range...but there is a problem if I want a longer lookback like 120 days then it displays really weird/wrong stuff. Code below

Code: Select all

`inputs:   Lookback(30); vars:       x(0), MaxRange(0);array:      DailyRangesArray[120](1);      For x = 1  to Lookback begin   DailyRangesArray[x] = DailyRange(x);      MaxRange = HighestArray(DailyRangesArray,Lookback);end;plot4(MaxRangeShort,"short");`

Is there some sort of time/size limit on arrays if done like this? Am I doing something wrong or is it my data?

help pls

why are you initializing the array to 1 ?

aczk
Posts: 67
Joined: 08 Feb 2012
Has thanked: 8 times
Been thanked: 1 time

### Re: for loop beginner

To exclude today's range! no?

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: for loop beginner

aczk wrote:To exclude today's range! no?

No.

You are setting up an array with 121 elements (zero to 120),

and putting the value of "ONE" into every element during initialization.
These users thanked the author TJ for the post:
aczk