Drawing line just disappeared when scrolling

Hi,

When I'm trying to draw lines between every pair of Buy signal and Sell signal, the line will just disappear if part of line is not visible. Is it possible to keep line visible even if some bars between trade goes invisible during scrolling?

Here is my codes:

Buy = Cross(MA(C, 5), MA(C, 10));
Sell = Cross(MA(H, 10), MA(H, 3));

FirstVisibleBar = Status( "FirstVisibleBar" );
Lastvisiblebar = Status( "LastVisibleBar" );

inTrade = False;
sig = False;
x0 = y0 = x1 = y1 = 0;

for ( b = Firstvisiblebar; b <= Lastvisiblebar AND b < BarCount; b++ )
{
	
    if ( Buy[b] && !inTrade )
    {
        x0 = b;
        y0 = BuyPrice[b];   
        inTrade = True;     
    }
    
    if (inTrade && Sell[b] )
    {
		inTrade = False;
		sig = True;		
        Co = colorBrightGreen;
        x1 = b;
        y1 = SellPrice[b];
    }      
    
    if ( Sig )
    {
        Line = LineArray( x0, y0, x1, y1 );
        Plot(line, "", co); 
    }
    
    sig = False;

}

Below are the screenshot :
screen%20shot

The code that you are using is incorrect. Why it is incorrect and how to write correct code is explained here: http://www.amibroker.org/userkb/2007/04/20/plotting-trade-zigzag-lines/

1 Like

In addition to Tomasz's recommendation, this might help you:

/*
Since you have not shown conditions for Short-Cover
guessing that you are using a Long-only strategy
*/
_Buy = Cross( MA( C, 5 ), MA( C, 10 ) );
_Sell = Cross( MA( H, 10 ), MA( H, 3 ) );

Buy  = ExRem( _Buy, _Sell );
Sell = ExRem( _Sell, _Buy );
/*
Exrem is ideal most of the time.
But, if you happen to deal with other strategies where both Buy-Sell occur simultaneously at a same bar,
please also refer to these examples:
https://forum.amibroker.com/t/need-help-in-afl-code-for-pyramiding-in-intraday/11859/6
https://forum.amibroker.com/t/need-help-in-afl-code-for-pyramiding-in-intraday/11859/7
*/

fvb = Status( "FirstVisibleBar" );
lvb = Status( "LastVisibleBar" );

//Using GFX
GfxSetCoordsMode( 1 );
GfxSelectPen( ParamColor( "CO", colorAqua ), 2 );
for( i = fvb; i <= Min( lvb, BarCount - 1 ); i++ ) {
	 if( Buy[ i ] ) {
		 GfxMoveTo( i, C[ i ] ); //Buyprice as Close
		 j = i;
		 While( !Sell[ j ] && j < BarCount - 1 ) j++;
		 GfxLineTo( j, H[ j ] ); //Sellprice as High
	 }
}

Plot( C, "Price", colorDefault, styleBar | styleThick );
1 Like

Your code does help me a lot. Many thanks.

1 Like