How to plot so that we can view crossed or not crossed or all?

Please help me to code to only see plot crossed or not crossed or all lines from the below code using paramList function. provide any other suggestion if any.

// Do NOT forget to add upper function to this code before!


function fxMxStopLines(sig, targetarray) {
	/// @link https://forum.amibroker.com/t/plot-horizontal-line-until-crossed/10802/32
	/// by fxshrat@gmail.com
	Version(6.0);
	bir = Status("barvisible" );// chart area array	
	cums = Cum(IIf(bir, sig, 0));
	lastcum = LastVisibleValue(cums);
	stop_mat = Matrix(3, Max(1,lastcum), 0);
	colnum = MxGetSize(stop_mat, 1);
	for ( i = 0, n = 0; i < BarCount; i++ ) {
		if ( bir[i] ) {
			if ( sig[i] ) {				
				if ( n < colnum ) {
					stop_mat[0][n] = i;// start bar
					stop_mat[1][n] = BarCount-1; // end bar
					stop_mat[2][n] = targetarray[i]; // level				
					for ( j = i; j < BarCount; j++ ) {
						if ( L[j] < stop_mat[2][n] ) {
							stop_mat[1][n] = j; // end bar
							break;
						}
					}
				}
				n++;
			}
		}
	}		
	printf( "#lines: %g, BarCount: %g", colnum, BarCount );
	GfxSelectPen( colorRed, 1, 0 );	
	for ( n = 0; n < colnum; n++ ) {
		GfxMoveTo(x1 = stop_mat[0][n], y = stop_mat[2][n] );
		GfxLineTo(x2 = stop_mat[1][n], y ); 
		GfxTextOut(StrFormat("%g", y), x2+0.25, y ); 
		GfxTextOut(StrFormat("%g bars", x2-x1), x2+0.25, y );    
	}
	return stop_mat;
}

//RequestTimedRefresh( 1 );
SetChartOptions( 0, chartShowDates | chartShowArrows | chartWrapTitle );

Plot( C, "Price", colorDefault, styleBar );

Short = Cross( MA( C, 30 ), C );

tlas = HHV(H,10);
stopvalue = 5*ATR(30);
targetarray = tlas-stopvalue;

GfxSetZOrder( 1 );
GfxSetCoordsMode( 1 );
GfxSetBkMode(2);
GfxSetTextAlign(0|0);
GfxSelectFont("Arial", 8, 500 );	
GfxSetBkColor( colorRed);
GfxSetTextColor( colorLightYellow );
GfxSelectPen( colorRed, 1, 0 );

mat = fxMxStopLines(Short, targetarray);
//printf( "\n%s", MxToString(mat));

///////////////////////////////////////////////////////////////////////////
PlotShapes( IIf( Short, shapeSmallDownTriangle, shapeNone ), colorlightyellow, 0, h, -35 );
Plot( ValueWhen(Short, targetarray), "tg", Colorblack, styleStaircase | stylethick, Null, Null, 0, 0, 1 );

Updated code.

// Do NOT forget to add upper function to this code before!

function fxMxStopLines(sig, targetarray, ViewLines) {
	/// @link https://forum.amibroker.com/t/plot-horizontal-line-until-crossed/10802/32
	/// by fxshrat@gmail.com
	Version(6.0);
	bir = Status("barvisible" );// chart area array	
	cums = Cum(IIf(bir, sig, 0));
	lastcum = LastVisibleValue(cums);
	stop_mat = Matrix(3, Max(1,lastcum), 0);
	colnum = MxGetSize(stop_mat, 1);
	
	for ( i = 0, n = 0; i < BarCount; i++ ) {
		if ( bir[i] ) {
			if ( sig[i] ) {				
				if ( n < colnum ) {
					stop_mat[0][n] = i;// start bar
					stop_mat[1][n] = BarCount-1; // end bar
					stop_mat[2][n] = targetarray[i]; // level
					crossed = False;				
					for ( j = i; j < BarCount; j++ ) {
						if ( L[j] < stop_mat[2][n] ) {
							stop_mat[1][n] = j; // end bar
							crossed = True;
							break;
						}
					}
				}
				n++;
			}
		}
	}		
	printf( "#lines: %g, BarCount: %g", colnum, BarCount );
	GfxSelectPen( colorRed, 1, 0 );	
	for ( n = 0; n < colnum; n++ ) {
	
		
		if(ViewLines == "Active") 
		{ 
			if(crossed == False) { GfxMoveTo( x1 = stop_mat[0][n], y = stop_mat[2][n] );
				GfxLineTo(x2 = stop_mat[1][n], y );  };
			  
		}
		if(ViewLines == "Crossed") 
		{ 		 	
			if(crossed == True) { GfxMoveTo( x1 = stop_mat[0][n], y = stop_mat[2][n] );
				GfxLineTo(x2 = stop_mat[1][n], y );  };			
		}
		if(ViewLines == "All")
		{ 			
			GfxMoveTo( x1 = stop_mat[0][n], y = stop_mat[2][n] );
			GfxLineTo(x2 = stop_mat[1][n], y );
			GfxTextOut(StrFormat("%g", y), x2+0.5, y-0.5 ); 
			//GfxTextOut(StrFormat("%g bars", x2-x1), x2+0.25, y ); 
		};			
		   
	}
	return stop_mat;
}

//RequestTimedRefresh( 1 );
SetChartOptions( 0, chartShowDates | chartShowArrows | chartWrapTitle );

ViewLines = ParamList("View Lines", "All|Crossed|Active", 0);

Plot( C, "Price", colorDefault, styleBar );

Short = Cross( MA( C, 30 ), C );

tlas = HHV(H,10);
stopvalue = 5*ATR(30);
targetarray = tlas-stopvalue;

GfxSetZOrder( 1 );
GfxSetCoordsMode( 1 );
GfxSetBkMode(2);
GfxSetTextAlign(0|0);
GfxSelectFont("Arial", 8, 500 );	
GfxSetBkColor( colorRed);
GfxSetTextColor( colorLightYellow );
GfxSelectPen( colorRed, 1, 0 );

mat = fxMxStopLines(Short, targetarray, ViewLines);
//printf( "\n%s", MxToString(mat));

///////////////////////////////////////////////////////////////////////////
PlotShapes( IIf( Short, shapeSmallDownTriangle, shapeNone ), colorlightyellow, 0, h, -35 );
Plot( ValueWhen(Short, targetarray), "tg", Colorblack, styleStaircase | stylethick, Null, Null, 0, 0, 1 );

for each line you need the crossed state. Expand your matrix and also save crossed for future reference when you teste for it

1 Like

No, you don't have to expand anything.
It's all there already.

// Do NOT forget to add upper function to this code before!

function fxMxStopLines(sig, targetarray) {
  /// @link https://forum.amibroker.com/t/plot-horizontal-line-until-crossed/10802/32
  /// by fxshrat@gmail.com  
  bir = Status("barvisible" );// chart area array  
  cums = Cum(IIf(bir, sig, 0));
  lastcum = LastVisibleValue(cums);
  stop_mat = Matrix(3, Max(1,lastcum), 0);
  colnum = MxGetSize(stop_mat, 1);
  
  for ( i = 0, n = 0; i < BarCount; i++ ) {
    if ( bir[i] ) {
      if ( sig[i] ) {        
        if ( n < colnum ) {
          stop_mat[0][n] = i;// start bar
          stop_mat[1][n] = BarCount-1; // end bar
          stop_mat[2][n] = targetarray[i]; // level  
          for ( j = i; j < BarCount; j++ ) {
            if ( L[j] < stop_mat[2][n] ) {
              stop_mat[1][n] = j; // end bar              
              break;
            }
          }
        }
        n++;
      }
    }
  }    
  return stop_mat;
}

Version(6.0);

//RequestTimedRefresh( 1 );
SetChartOptions( 0, chartShowDates | chartShowArrows | chartWrapTitle );

ViewLines = ParamList("View Lines", "All|Crossed|Active", 0);

Plot( C, "Price", colorDefault, styleBar );

Short = Cross( MA( C, 30 ), C );

tlas = HHV(H,10);
stopvalue = 5*ATR(30);
targetarray = tlas-stopvalue;

GfxSetZOrder( 1 );
GfxSetCoordsMode( 1 );
GfxSetBkMode(2);
GfxSetTextAlign(0|0);
GfxSelectFont("Arial", 8, 500 );  
GfxSetBkColor( colorRed);
GfxSetTextColor( colorLightYellow );
GfxSelectPen( colorRed, 1, 0 );

stop_mat = fxMxStopLines(Short, targetarray);
//printf( "\n%s", MxToString(stop_mat));
colnum = MxGetSize(stop_mat, 1);
printf( "#lines: %g, BarCount: %g", colnum, BarCount );  

for ( n = 0; n < colnum; n++ ) {
  x2 = stop_mat[1][n];
  x1 = stop_mat[0][n];
  y = stop_mat[2][n];    
  if((ViewLines == "Active" AND x2 == BarCount-1)  OR 
    (ViewLines == "Crossed" AND x2 < BarCount-1)  OR
    ViewLines == "All" )  {    
    GfxMoveTo( x1, y );
    GfxLineTo(x2, y ); 
    GfxTextOut(StrFormat("%g", y), x2+0.5, y-0.5 ); 
    //GfxTextOut(StrFormat("%g bars", x2-x1), x2+0.25, y );     
  }
}

///////////////////////////////////////////////////////////////////////////
PlotShapes( IIf( Short, shapeSmallDownTriangle, shapeNone ), colorlightyellow, 0, h, -35 );
//Plot( ValueWhen(Short, targetarray), "tg", Colorblack, styleStaircase | stylethick, Null, Null, 0, 0, 1 );
1 Like

Can you please help me to understand it .
It would help if you could share the code for above

Also in my code, i could see that there is a repetition of the plotshape. could anyone advice how to avoid multiple repetition of the plotshape in time frame.

1

My code

// Do NOT forget to add upper function to this code before!

function fxMxStopLines(sig, targetarray) {
  /// @link https://forum.amibroker.com/t/plot-horizontal-line-until-crossed/10802/32
  /// by fxshrat@gmail.com  
  bir = Status("barvisible" );// chart area array  
  cums = Cum(IIf(bir, sig, 0));
  lastcum = LastVisibleValue(cums);
  stop_mat = Matrix(4, Max(1,lastcum), 0);
  colnum = MxGetSize(stop_mat, 1);  
  
  for ( i = 0, n = 0; i < BarCount; i++ ) {
    if ( bir[i] ) {
      if ( sig[i] ) {        
        if ( n < colnum ) {
          stop_mat[0][n] = i;// start bar
          stop_mat[1][n] = BarCount-1; // end bar
          stop_mat[2][n] = targetarray[i]; // level            
          for ( j = i; j < BarCount; j++ ) {
            if ( L[j] < stop_mat[2][n] ) {
              stop_mat[1][n] = j; // end bar 
                                          
              break;
            }
          }
        }
        n++;
      }
    }
  }    
  return stop_mat;
}

Version(6.0);

//RequestTimedRefresh( 1 );
SetChartOptions( 0, chartShowDates | chartShowArrows | chartWrapTitle );

ViewLines = ParamList("View Lines", "All|Crossed|Active", 0);
NumberOfLineToView = Param("Number Of Line To View",1, 1, 20, 1);
//ShowLines = ParamToggle("Show Lines", "Yes|No", 0);  

Plot( C, "Price", colorDefault, styleBar );

tf = Param( "TF", 15, 1, 100000, 1 ) ;
isexpandLast = ParamToggle( "Expand Mode", "First|Last", 1 ) ;
expandmode = IIf( isexpandLast, expandLast, expandFirst ) ;

tfs = tf * in1Minute ;
TimeFrameSet( tfs ) ;

Short_1 = Cross( MA( C, 30 ), C );

tlas = HHV(H,10);
stopvalue = 5*ATR(30);
targetarray_1 = tlas-stopvalue;

TimeFrameRestore() ;

GfxSetZOrder( 1 );
GfxSetCoordsMode( 1 );
GfxSetBkMode(2);
GfxSetTextAlign(0|0);
GfxSelectFont("Arial", 8, 500 );  
GfxSetBkColor( colorRed);
GfxSetTextColor( colorLightYellow );
GfxSelectPen( colorRed, 1, 0 );

Short = TimeFrameExpand(Short_1, tfs, expandmode );
targetarray = TimeFrameExpand(targetarray_1, tfs, expandmode );

Short = ExRem(Short, targetarray);

stop_mat = fxMxStopLines(Short, targetarray);
//printf( "\n%s", MxToString(stop_mat));


colnum = IIf(NumberOfLineToView < MxGetSize(stop_mat, 1), NumberOfLineToView, MxGetSize(stop_mat, 1));
printf( "#lines: %g, BarCount: %g", colnum, BarCount );  

for ( n = 0; n < colnum; n++ ) {
  x2 = stop_mat[1][n];
  x1 = stop_mat[0][n];
  y = stop_mat[2][n];    
  if((ViewLines == "Active" AND x2 == BarCount-1)  OR 
    (ViewLines == "Crossed" AND x2 < BarCount-1)  OR
    ViewLines == "All" )  {    
    GfxMoveTo( x1, y );
    GfxLineTo(x2, y ); 
    GfxTextOut(StrFormat("%g", y), x2+0.5, y-0.5 ); 
    //GfxTextOut(StrFormat("%g bars", x2-x1), x2+0.25, y );     
  }
}

///////////////////////////////////////////////////////////////////////////
PlotShapes( IIf( Short, shapeSmallDownTriangle, shapeNone ), colorlightyellow, 0, h, -35 );
//Plot( ValueWhen(Short, targetarray), "tg", Colorblack, styleStaircase | stylethick, Null, Null, 0, 0, 1 );

Below code worked for removing repetition of plotshape in timeframe, Please suggest if there is any other workaround apart from this.

PlotShapes( IIf( Short AND Ref(Short, -1) != True, shapeSmallDownTriangle, shapeNone ), colorlightyellow, 0, h, -35 );