for loop beginner

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

for loop beginner

Postby aczk » 20 Jun 2012

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");

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: for loop beginner

Postby TJ » 20 Jun 2012

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: 71
Joined: 08 Feb 2012
Has thanked: 8 times
Been thanked: 1 time

Re: for loop beginner

Postby aczk » 20 Jun 2012

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) Downloaded 706 times

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: for loop beginner

Postby TJ » 20 Jun 2012

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: 71
Joined: 08 Feb 2012
Has thanked: 8 times
Been thanked: 1 time

Re: for loop beginner

Postby aczk » 20 Jun 2012

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

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: for loop beginner

Postby TJ » 21 Jun 2012

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: 71
Joined: 08 Feb 2012
Has thanked: 8 times
Been thanked: 1 time

Re: for loop beginner

Postby aczk » 21 Jun 2012

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

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: for loop beginner

Postby TJ » 21 Jun 2012

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"?

sptrader
Posts: 742
Joined: 09 Apr 2010
Location: Texas
Has thanked: 483 times
Been thanked: 274 times
Contact:

Re: for loop beginner

Postby sptrader » 21 Jun 2012

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..

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

Re: for loop beginner

Postby aczk » 22 Jun 2012

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

thx

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: for loop beginner

Postby TJ » 22 Jun 2012

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

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: for loop beginner

Postby TJ » 22 Jun 2012

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: 71
Joined: 08 Feb 2012
Has thanked: 8 times
Been thanked: 1 time

Re: for loop beginner

Postby aczk » 22 Jun 2012

To exclude today's range! no?

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: for loop beginner

Postby TJ » 22 Jun 2012

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.


Return to “MultiCharts”