Author Topic: Having trouble with refrencing previous elements of array  (Read 8979 times)

maheshn

  • Newbie
  • *
  • Posts: 12
    • View Profile
Having trouble with refrencing previous elements of array
« on: July 12, 2013, 03:01:12 am »
Code: [Select]
_SECTION_BEGIN("Test2");
SetChartBkGradientFill( ParamColor("BgTop", colorBlack),ParamColor("BgBottom", colorBlack),ParamColor("Titleblock",colorLightGrey ));
SetChartOptions(0,chartShowArrows|chartShowDates);
GraphXSpace=5;

//Initialise
TrendHigh[0]=BeginValue(H);
TrendLow[0]=BeginValue(L);

printf("\nBefore Ref(TrendLow,-1)=%g, 1.03*Ref(TrendLow,-1)=%g, IsNull(Ref(TrendLow,-1))=%g\n",SelectedValue(Ref(TrendLow,-1)),SelectedValue(1.03*Ref(TrendLow,-1)),SelectedValue(IsNull(Ref(TrendLow,-1))));
TrendHigh=IIf(((H>Ref(TrendHigh,-1)) AND (L>=0.97*Ref(TrendHigh,-1))) OR IsNull(Ref(TrendHigh,-1)), H, Ref(TrendHigh,-1));
TrendLow=IIf(((L<Ref(TrendLow,-1)) AND (H<=1.03*Ref(TrendLow,-1)))OR IsNull(Ref(TrendLow,-1)), L, Ref(TrendLow,-1));

Buy=Cross(H, TrendLow*1.03);
Sell=Cross(TrendHigh*0.99, L);
Short=Cross(TrendHigh*0.97, L);
Cover=Cross(H, TrendLow*1.01);
printf("\nAfter SelectedValue(TrendHigh)=%g, SelectedValue(TrendLow)=%g",SelectedValue(TrendHigh),SelectedValue(TrendLow));
PlotShapes( shapeUpTriangle*Buy, colorWhite, 0, L, -10 );
PlotShapes( shapeDownTriangle*Sell, colorRed, 0, H, -10 );
PlotShapes( shapeUpTriangle*Cover, colorBlue, 0, L, -12 );
PlotShapes( shapeDownTriangle*Short, colorViolet, 0, H, -12 );

PlotOHLC( O, H, L, C, "" + Name(), colorWhite, styleCandle | styleNoLabel );
_SECTION_END();

The Ref(TrendLow,-1) is returning rubbish values like 8.8293e-036 after the second bar. Pls help. Also pls suggest if my way of initializing is correct for array processing languages such as AFL.
Thanks for reading.

maheshn

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Having trouble with refrencing previous elements of array
« Reply #1 on: July 12, 2013, 12:07:55 pm »
is this because of cyclic dependency?
TrendHigh=IIf(((H>Ref(TrendHigh,-1)) AND (L>=0.97*Ref(TrendHigh,-1))) OR IsNull(Ref(TrendHigh,-1)), H, Ref(TrendHigh,-1));

TrendHigh and Ref(TrendHigh,-1) are getting calculated simultaneously, is it? Pls help.


administrator

  • Administrator
  • Full Member
  • *****
  • Posts: 200
    • View Profile
Re: Having trouble with refrencing previous elements of array
« Reply #2 on: July 13, 2013, 06:07:08 am »
Yes you are correct.

You can do something like that in metastock but not in Amibroker. You need to use a for loop to do it.

maheshn

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Having trouble with refrencing previous elements of array
« Reply #3 on: July 13, 2013, 07:52:08 am »
Thanks for responding admin.
I have a lot of doubts about the array processing methodology that amibroker uses. I have worked with java and vbscript before but this is the first time i am working with array processing language such as AFL. On top of my mind i have the following questions:

1. Is the afl code executed for every bar in the chart window?

2. If i write a for loop for example (i=1;i<barcount-1;i++) and i have 3000 eod quotes for the stock then does this mean the loop will run 3000 times for one bar or (3000*3000=9000000) 9 million times in total.
 
3.Now I have rewritten my code to use loops as suggested by you. The new code is as below. Now when i view the chart window i can see that for some bars the value for variables (TrendHigh, TrendLow) are shown as zero and then when i traverse back using mouse to the same period these same bars have some value for it. Also the buy sell indicators disappear and reappearing when i scroll using the mouse wheel. Why is this happening?

4. I tried to debug using the exploration window but after the 26th bar the value are shown as 0 for both my key variables. Is there a better way to debug other than printf to intrepretation or exploration window.

My code is error free but is not functioning as expected because the values for the key variables are wrongly calculated as zero by amibroker for some bars. The code is as below.

Code: [Select]
_SECTION_BEGIN("Test2");
SetChartBkGradientFill( ParamColor("BgTop", colorBlack),ParamColor("BgBottom", colorBlack),ParamColor("Titleblock",colorLightGrey ));
SetChartOptions(0,chartShowArrows|chartShowDates);
GraphXSpace=5;

//Initialise
TrendHigh[0]=H[0];
TrendLow[0]=L[0];

barColor[0]=colorGreen;

for (i=1;i<BarCount-1;i++){
//TrendHigh[i]=IIf(((H[i]>TrendHigh[i-1]) AND (L[i]>=0.97*TrendHigh[i-1])), H[i], TrendHigh[i-1]);
//TrendLow[i]=IIf(((L[i]<TrendLow[i-1]) AND (H[i]<=1.03*TrendLow[i-1])), L[i], TrendLow[i-1]);

//if last bar was Green
if (barColor[i-1]==colorGreen){
TrendHigh[i]=IIf(H[i]>TrendHigh[i-1],H[i],TrendHigh[i-1]);
TrendLow[i]=IIf(L[i]<TrendLow[i-1],L[i],TrendLow[i-1]);
if (L[i]>0.99*TrendHigh[i]) 
barColor[i]=colorGreen;
else{
if ((L[i]<=0.99*TrendHigh[i]) AND (L[i]>0.97*TrendHigh[i])){ 
barColor[i]=colorPaleGreen;
//Sell[i]=True;
//PlotShapes( shapeDownTriangle, colorWhite, 0, H[i], -10 );
}
else
if (L[i]<=0.97*TrendHigh[i]){
barColor[i]=colorRed;
//Short[i]=True;
//PlotShapes( shapeDownTriangle, colorBlue, 0, H[i], -3 );
TrendHigh[i]=H[i]; }
}
}

//if last bar was LG
if (barColor[i-1]==colorPaleGreen){
TrendHigh[i]=IIf(H[i]>TrendHigh[i-1],H[i],TrendHigh[i-1]);
TrendLow[i]=IIf(L[i]<TrendLow[i-1],L[i],TrendLow[i-1]);
if ((H[i]<1.005*TrendHigh[i])AND (L[i]>0.97*TrendHigh[i]))
barColor[i]=colorPaleGreen;
else{
if (H[i]>=1.005*TrendHigh[i]){
barColor[i]=colorGreen;
//Buy[i]=True;
//PlotShapes( shapeUpTriangle, colorWhite, 0, L[i], -10 );
TrendHigh[i]=H[i]; }
else{
barColor[i]=colorRed;
//Short[i]=True;
//PlotShapes( shapeDownTriangle, colorBlue, 0, H[i], -3 );
TrendLow[i]=L[i];
}
}
}


//if last bar was Red
if (barColor[i-1]==colorRed){
TrendHigh[i]=IIf(H[i]>TrendHigh[i-1],H[i],TrendHigh[i-1]);
TrendLow[i]=IIf(L[i]<TrendLow[i-1],L[i],TrendLow[i-1]);
if (H[i]<1.01*TrendLow[i])
barColor[i]=colorRed;
else{
if ((H[i]>=1.01*TrendLow[i])AND(H[i]<1.03*TrendLow[i])){
barColor[i]=colorPink;
//Cover[i]=True;
//PlotShapes( shapeUpTriangle, colorBlue, 0, L[i], -3 );
}
else
if (H[i]>=1.03*TrendLow[i]){
barColor[i]=colorGreen;
//Buy[i]=True;
//PlotShapes( shapeUpTriangle, colorWhite, 0, L[i], -10 );
TrendHigh[i]=H[i];      }
}
}



//if last bar was Pink
if (barColor[i-1]==colorPink){
TrendHigh[i]=IIf(H[i]>TrendHigh[i-1],H[i],TrendHigh[i-1]);
TrendLow[i]=IIf(L[i]<TrendLow[i-1],L[i],TrendLow[i-1]);
if ((H[i]>1.01*TrendLow[i])AND(H[i]<1.03*TrendLow[i]))
barColor[i]=colorPink;
else{
if (L[i]<=0.995*TrendLow[i]){
barColor[i]=colorRed;
//Short[i]=True;
//PlotShapes( shapeDownTriangle, colorBlue, 0, H[i], -3 );
TrendLow[i]=L[i]; }
else
if (H[i]>=1.03*TrendLow[i]){
barColor[i]=colorGreen;
//Buy[i]=True;
//PlotShapes( shapeUpTriangle, colorWhite, 0, L[i], -10 );
TrendHigh[i]=H[i];  }
}

}

//printf("\nBar=%g",i);
//printf("\nTrendLow[%g]=%g, TrendLow[%g]=%g",i-1,TrendLow[i-1],i,TrendLow[i]);
//printf("\nTrendHigh[%g]=%g, TrendHigh[%g]=%g\n\n",(i-1),TrendHigh[i-1],i,TrendHigh[i]);
}//end for loop

printf("\SelectedBar=%g",SelectedValue(BarIndex()));
printf("\nTrendLow[%g]=%g, TrendHigh[%g]=%g\n\n",SelectedValue(BarIndex()),TrendLow[SelectedValue(BarIndex())], SelectedValue(BarIndex()), TrendHigh[SelectedValue(BarIndex())]);


Buy=Cross(H, TrendLow*1.03);
Sell=Cross(TrendHigh*0.99, L);
Short=Cross(TrendHigh*0.97, L);
Cover=Cross(H, TrendLow*1.01);


PlotShapes( shapeUpTriangle*Buy, colorWhite, 0, L, -3 );
PlotShapes( shapeDownTriangle*Sell, colorWhite, 0, H, -3 );
PlotShapes( shapeUpTriangle*Cover, colorBlue, 0, L, -12 );
PlotShapes( shapeDownTriangle*Short, colorBlue, 0, H, -12 );

PlotOHLC( O, H, L, C, "" + Name(), colorWhite, styleCandle | styleNoLabel );

Filter = 1;
AddColumn (TrendHigh,"TrendHigh");
AddColumn (TrendLow,"TrendLow");

_SECTION_END();

administrator

  • Administrator
  • Full Member
  • *****
  • Posts: 200
    • View Profile
Re: Having trouble with refrencing previous elements of array
« Reply #4 on: July 15, 2013, 02:05:49 am »
Code: [Select]
TrendHigh = 0;
for (i=1;i<BarCount;i++){
    if((H[i] > TrendHigh[i - 1] AND L[i] >= (0.97 * TrendHigh[i - 1])) OR IsNull(TrendHigh[i - 1]))
        TrendHigh[i] = H[i];
    else
        TrendHigh[i] = TrendHigh[i - 1];
}

Plot(TrendHigh, "TrendHigh", colorRed);

1. No.

2. No

3. Your doing something wrong in your code.

4. Nope.

Take at look at my code snippet.

Aminerd

  • Full Member
  • ***
  • Posts: 108
    • View Profile
Re: Having trouble with refrencing previous elements of array
« Reply #5 on: July 15, 2013, 06:36:24 pm »
You can do something like that in metastock but not in Amibroker.

Your post is misleading. Of course you can do it in AmiBroker but not the way the OP is wrongly doing it.
The problem is that the thread starter has no clue how to code in AFL.

I have a lot of doubts about the array processing methodology that amibroker uses. I have worked with java and vbscript before but this is the first time i am working with array processing language such as AFL.

You have not much knowledge. That's your problem.

administrator

  • Administrator
  • Full Member
  • *****
  • Posts: 200
    • View Profile
Re: Having trouble with refrencing previous elements of array
« Reply #6 on: July 15, 2013, 07:53:22 pm »
Quote
Your post is misleading. Of course you can do it in AmiBroker but not the way the OP is wrongly doing it.
The problem is that the thread starter has no clue how to code in AFL.

Nope seems pretty clear to me.

maheshn

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Having trouble with refrencing previous elements of array
« Reply #7 on: July 15, 2013, 08:35:12 pm »
You can do something like that in metastock but not in Amibroker.

Your post is misleading. Of course you can do it in AmiBroker but not the way the OP is wrongly doing it.
The problem is that the thread starter has no clue how to code in AFL.

I have a lot of doubts about the array processing methodology that amibroker uses. I have worked with java and vbscript before but this is the first time i am working with array processing language such as AFL.

You have not much knowledge. That's your problem.

If my way is wrong then what is the right way? Can you write the below code without loop?

Code: [Select]
TrendHigh = 0;
for (i=1;i<BarCount;i++){
    if((H[i] > TrendHigh[i - 1] AND L[i] >= (0.97 * TrendHigh[i - 1])) OR IsNull(TrendHigh[i - 1]))
        TrendHigh[i] = H[i];
    else
        TrendHigh[i] = TrendHigh[i - 1];
}

Plot(TrendHigh, "TrendHigh", colorRed);
« Last Edit: July 15, 2013, 08:57:23 pm by maheshn »

Aminerd

  • Full Member
  • ***
  • Posts: 108
    • View Profile
Re: Having trouble with refrencing previous elements of array
« Reply #8 on: July 16, 2013, 08:41:32 am »
Code: [Select]
TrendHigh = 0;
for (i=1;i<BarCount;i++){
    if((H[i] > TrendHigh[i - 1] AND L[i] >= (0.97 * TrendHigh[i - 1])) OR IsNull(TrendHigh[i - 1]))
        TrendHigh[i] = H[i];
    else
        TrendHigh[i] = TrendHigh[i - 1];
}

Plot(TrendHigh, "TrendHigh", colorRed);


This one

FirstBarOfRange = BarIndex() == 0;
TrendHigh = HighestSince( FirstBarOfRange, H );

shows the same graph as your loop version.
« Last Edit: July 16, 2013, 08:43:36 am by Aminerd »

Aminerd

  • Full Member
  • ***
  • Posts: 108
    • View Profile
Re: Having trouble with refrencing previous elements of array
« Reply #9 on: July 16, 2013, 08:47:54 am »
Or this way for visible range

firstBarOfRange = BarIndex() == Status("firstvisiblebarindex");
TrendHigh = HighestSince( FirstBarOfRange, H );
« Last Edit: July 16, 2013, 08:49:39 am by Aminerd »

Aminerd

  • Full Member
  • ***
  • Posts: 108
    • View Profile
Re: Having trouble with refrencing previous elements of array
« Reply #10 on: July 16, 2013, 09:07:15 am »
Code: [Select]
firstBarOfRange = BarIndex() == Status("firstvisiblebarindex");
TrendHigh = HighestSince( FirstBarOfRange, H );
PrevTrendHigh = Ref(TrendHigh, -1);
Trendhigh = ValueWhen( ( H == Trendhigh AND L >= 0.97 * PrevTrendHigh ) OR IsNull( PrevTrendHigh ), High );

Plot(TrendHigh, "TrendHigh", colorGreen);

Multiple times faster than loop.
« Last Edit: July 16, 2013, 09:14:40 am by Aminerd »

administrator

  • Administrator
  • Full Member
  • *****
  • Posts: 200
    • View Profile
Re: Having trouble with refrencing previous elements of array
« Reply #11 on: July 17, 2013, 06:26:05 am »
I like it. Very simple.

Aminerd

  • Full Member
  • ***
  • Posts: 108
    • View Profile
Re: Having trouble with refrencing previous elements of array
« Reply #12 on: July 17, 2013, 07:23:05 pm »
Didn't you write that it can not be done without loop? :p

Aminerd

  • Full Member
  • ***
  • Posts: 108
    • View Profile
Re: Having trouble with refrencing previous elements of array
« Reply #13 on: July 17, 2013, 07:30:49 pm »
FirstBarOfRange = BarIndex() == 0;
TrendHigh = HighestSince( FirstBarOfRange, H );

shows the same graph as your loop version.

Alternative:

TrendHigh = Highest( H );

administrator

  • Administrator
  • Full Member
  • *****
  • Posts: 200
    • View Profile
Re: Having trouble with refrencing previous elements of array
« Reply #14 on: July 18, 2013, 03:10:09 am »
Quote
Didn't you write that it can not be done without loop? :p

I will admit when I'm wrong :)