Values that are between the green and red lines

Hello,
I need to store only the values that are between the green and red lines.
I've done a lot of tests and there's no way I can get it right.
If someone can help me I would appreciate it very much.
Regards,
Carlos

DPO7 = C - MA( C, 7 );

Plot( DPO7,_DEFAULT_NAME(),ParamColor("Color", ColorCycle ) );
Plot(0, "", colorWhite, styleLine | styleNoLabel);

Title = Name() + " - " +
	EncodeColor(colorLightOrange) + "DPO (7) " + 
	EncodeColor(colorWhite) + "= " + NumToStr(DPO7,1.2);

// VerticalLine --> LastBar y -80 Bars
Last_Bar = BarIndex() == LastValue( BarIndex() ); 
Start_Bar	= BarIndex() == SelectedValue(BarIndex())-80;

Plot( Last_Bar, "", colorGreen, styleHistogram | styleOwnScale | styleDashed | styleNoLabel | styleThick, 0, 1 );
Plot( Start_Bar, "", colorRed, styleHistogram | styleOwnScale | styleDashed | styleNoLabel | styleThick, 0, 1 );

// Calculate Positive Values > 0
DndPO = DPO7;
Crucealc = Cross(DndPO,0);
Alto =  HighestSince (Crucealc,DndPO,1);
MaxV = Ref(HHV(DndPO,1),1);

Buy = DndPO==Alto;
Buy1 = DndPO>MaxV;

sampleMatrix = Matrix( 1, colnum = BarCount, null );

for( i = 0; i < BarCount; i++ ) 
{ 
	if( (Buy[i] AND Buy1[i]) ) 
	{
		PlotText( NumToStr(DndPO[ i ],1.2) , i ,  DndPO[ i ]+1, colorTurquoise, bkcolor = colorDefault);
		sampleMatrix[0][i] = DndPO[i];
	}
}

 for( i = 0; i < MxGetSize( sampleMatrix, 1 ); i++ ) 
 { 
    printf( "DPO = %g\n ", sampleMatrix[0][i] ); 
 }  

I don't understand a couple of things here,

  1. Why do you need to create a 1D matrix of size BarCount? That's the normal array
  2. Are you trying to access the data in another AFL?
    One would rather use Static Variables as that would preserve the time stamps, BarCount can be affected by QuickAFL.
  3. Also, if you just need a subset of the all the bars, then your matrix would rather be the size of the Green Line - (Red Line - 80 ) Number of bars

Anyway, I tried to code the requirement the way you wanted without altering any Logic.
For me, the trick was how one can assign proper Matrix Column index when copying the data.

Now, you can select any Bar and see the Matrix dynamically resize itself.

bi        = BarIndex();
LineGreen = LastValue( bi );
LineRed   = SelectedValue( bi ) - 10;

DndPO = C;

MatSize = LineGreen - LineRed;
sampleMatrix = Matrix( 1, MatSize, null );

for( i = LineRed; i < LineGreen; i++ )
{
    sampleMatrix[0][i - LineRed] = DndPO[i];    // the logic part
}

for( i = 0; i < MxGetSize( sampleMatrix, 1 ); i++ ) 
{ 
    printf( "DPO = %g\n", sampleMatrix[0][i] ); 
}

Just a note, i'm subtracting 10 from Red Line, you can change it to 80 as required.
Also, I've used the Close price to populate the Array.

Hello,
I have tried your code but it does not give me the result I want.
I need to store the maxima (blue numbers) between the two bars.
Anyway, thanks for your help.
Regards,
Carlos

Did you replace the Array correctly?
I have clearly mentioned that i'm using Close prices for my test.

You can atleast verify that the Close price match. The matrix is correct, as in it stores, all values b/w the Green and Red Line - 10 as you have tried in your code.
You haven't explained the -80 either.

something like this DndPO = DPO7;

There is not any single loop required to store your DndPO variable


DndPO = /*your array here*/;

mat = Matrix( 1, colnum = BarCount, null );
colnum = MxGetSize( mat, 1 );

//mat = MxSetBlock( mat, 0, 0, 0, BarCount-1, DndPO );
// OR
mat = MxSetBlock( mat, 0, 0, 0, BarCount-1, IIf( Buy AND Buy1, DndPO, 0) );

start_idx = Max(0, SelectedValue(bi)-80);
dndPO_block = MxGetBlock(mat, 0, 0, start_idx, colnum-1 );

printf(MxToString(dndPO_block));

1 Like

Hello,
I have tried your code but it does not give me the result I want.
I need to store the maxima (blue numbers) between the two bars.
Anyway, thanks for your help.
Regards,
Carlos

DPO7_01
Screenshot_1

Hello,
I've tried it but it does not give me the numbers I need.
Thanks for your help.
Regards,
Carlos

Can you check the values with the actual chart
and interpretation window in main AB instead of the debug in Editor?
It may differ due to the settings.

1 Like

@carlosrodrgueznieto,

Please listen, the code (snippet) I've posted does give you exactly the values of that range you were asking in 1st post.

This is your quote

"I need to store only the values that are between the green and red lines."

That is what the code snippet I've posted above is doing exactly (without loop) to original quote of yours. (FYI zeroes/Nulls(-1e10) are values too).

And you are the one who added -80 to SelectedValue(BarIndex) in first post. So I kept it also (BTW variable bi of my snippet is defined as bi = BarIndex()). If you remove -80 then it will look like in the following one picutre (red line being selected value). Please look at the picture carefully.
242

So indeed stored values are the ones of between red and green lines (again).

So long story short please do create a detailed post in the first place which leaves no room for misunderstanding and missing info.

And just saying

"I have tried your code but it does not give me the result I want."

is not useful at all (besides of being wrong one considering 1st post of this thread).


Now as for the updated info, if you want to remove zeroes (or Nulls) then you can do that via sorting. But you did not say anything about removing zeroes or Nulls originally. You added updated info later when responses were overlapping.

So here is with removing Nulls.

function MxArrayConditionBlock(array_condition, array, start_idx) {
   /// @link https://forum.amibroker.com/t/values-that-are-between-the-green-and-red-lines/12049/8
   /// by fxshrat@gmail.com
   /// Commercial use prohibited!
   local arr_block, mat, mat_sort, matsort_block, null_cnt, rownum, rows; 
   mat = Matrix(BarCount, 2, Null);
   rownum = MxGetSize(mat, 0);

   mat = MxSetBlock(mat, 0, rownum-1, 0, 0, IIf(array_condition, array, Null));
   mat = MxSetBlock(mat, 0, rownum-1, 1, 1, BarIndex());

   start_idx = Max(0, Min(start_idx, rownum-1));
   mat_sort = MxSortRows(MxGetBlock(mat, start_idx, rownum-1, 0, 1 ), 1, 0);
   arr_block = MxGetBlock(mat_sort, 0, rows = MxGetSize(mat_sort,0)-1, 0, 0, True);
   null_cnt = Min(NullCount(arr_block), rows);
   matsort_block = MxSortRows(MxGetBlock(mat_sort, null_cnt, rows, 0, 1 ), 1, 1);
   result = MxGetBlock(matsort_block, 0, MxGetSize(matsort_block, 0)-1, 0, 0);
   return result;
}


DPO7 = C - MA( C, 7 );

Plot( DPO7,_DEFAULT_NAME(),ParamColor("Color", ColorCycle ) );
Plot(0, "", colorWhite, styleLine | styleNoLabel);

//Title = StrFormat( "{{NAME}} - %s DPO (7) %s = %1.2f", EncodeColor(colorLightOrange), EncodeColor(colorWhite), DPO7);

// VerticalLine --> LastBar y -80 Bars
bi = BarIndex();
start_idx = Max(0, SelectedValue(bi)/*-80*/);
Last_Bar = bi == LastValue( bi ); 
Start_Bar = bi == start_idx;

Plot( Last_Bar, "", colorGreen, styleHistogram | styleOwnScale | styleDashed | styleNoLabel | styleThick, 0, 1 );
Plot( Start_Bar, "", colorRed, styleHistogram | styleOwnScale | styleDashed | styleNoLabel | styleThick, 0, 1 );

// Calculate Positive Values > 0
DndPO = DPO7;
Crucealc = Cross(DndPO,0);
Alto =  HighestSince (Crucealc,DndPO,1);
MaxV = Ref(HHV(DndPO,1),1);

Buy = DndPO==Alto;
Buy1 = DndPO>MaxV;

DndPO_block = MxArrayConditionBlock( Buy AND Buy1, DndPO, start_idx );
printf(MxToString(MxTranspose(DndPO_block)));

for( i = 0; i < BarCount; i++ ) 
{ 
   if( (Buy[i] AND Buy1[i]) ) 
   {
      PlotText( NumToStr(DndPO[ i ],1.6) , i ,  DndPO[ i ]+1, colorTurquoise, bkcolor = colorDefault);
   }
}

//for( i = 0; i < colnum; i++ ) 
//{ 
//   printf( "DPO = %g\n ", mat[0][i] ); 
//} 

Please watch carefully again.
1

2 Likes

FWIW, there isn't even sorting required...
So here is shorter way...

function MxArrayConditionBlock(array_condition, array, start_idx ) {
	/// @link https://forum.amibroker.com/t/values-that-are-between-the-green-and-red-lines/12049/10
	/// by fxshrat@gmail.com
	/// Commercial use prohibited!
	local sparray, cols, mat, nullcnt; 
	sparray = SparseCompress(array_condition AND BarIndex() >= start_idx, array);	
	mat = Matrix(1, BarCount, Null);
	cols = MxGetSize(mat, 1)-1;
	nullcnt = Min(NullCount(sparray), cols);
	mat = MxSetBlock( mat, 0, 0, 0, cols, sparray );
	result = MxGetBlock( mat, 0, 0, nullcnt, cols );
	return result;
}

DPO7 = C - MA( C, 7 );

Plot( DPO7,_DEFAULT_NAME(),ParamColor("Color", ColorCycle ) );
Plot(0, "", colorWhite, styleLine | styleNoLabel);

Title = StrFormat( "{{NAME}} - %s DPO (7) %s = %1.2f", EncodeColor(colorLightOrange), EncodeColor(colorWhite), DPO7);

// VerticalLine --> LastBar y -80 Bars
bi = BarIndex();
start_idx = Max(0, SelectedValue(bi)/*-80*/);
Last_Bar = bi == LastValue( bi ); 
Start_Bar = bi == start_idx;

Plot( Last_Bar, "", colorGreen, styleHistogram | styleOwnScale | styleDashed | styleNoLabel | styleThick, 0, 1 );
Plot( Start_Bar, "", colorRed, styleHistogram | styleOwnScale | styleDashed | styleNoLabel | styleThick, 0, 1 );

// Calculate Positive Values > 0
DndPO = DPO7;
Crucealc = Cross(DndPO,0);
Alto =  HighestSince (Crucealc,DndPO,1);
MaxV = Ref(HHV(DndPO,1),1);

Buy = DndPO==Alto;
Buy1 = DndPO>MaxV;

DndPO_block = MxArrayConditionBlock( Buy AND Buy1, DndPO, start_idx );
printf( EncodeColor(colorBlack) + MxToString(DndPO_block));

for ( i = 0; i < BarCount; i++ ) 
{ 
	if( (Buy[i] AND Buy1[i]) ) 
		PlotText( NumToStr(DndPO[ i ],1.6), i,  DndPO[ i ]+1, colorTurquoise, bkcolor = colorDefault);
}

//for( i = 0; i < colnum; i++ ) 
//{ 
//   printf( "DPO = %g\n ", mat[0][i] ); 
//}  

1

6 Likes

Hello,
First apologize for not having explained myself well.
Second by answering your answers without seeing the interpretation window to see the results, but I do not know much about programming.
Third, thank you very much for your help, I've tried it and it works well.
I need those values to do some calculations and I did not know how to obtain them. Now I'm perfect.
Again, thank you very much to both of you.
Regards,
Carlos