How to display previous 20,50,100 bars (close)price?

Hi, All

Now I am writing a code which the idea is as follows:
In a bar chart, I randomly chick for one bar. How can I display the previous 20,50 100 closes price of that bar? Can anyone help me??

Search this forum for "zoom"
https://forum.amibroker.com/search?q=zoom

I think it is not used the 'zoom' function.

@roychan, your question is unclear. See How to ask a good question

You will get better responses if you post clear questions without room for guessing. Now to the question, if you want to reference any past bar you should use Ref() function http://www.amibroker.com/f?ref

Now I am writing a code which the idea is as follows:
In a bar chart, I randomly click for one bar. How can write a afl to display the previous 20,50 100 close price(s) of that bar? Actually I want use these close price(s) as support level.

@roychan,

Like @Tomasz, I'm finding it hard to understand what it is that you want to do.

If you simply want to display the "Close" price for all prior bars:

Plot(Close, "Close", ParamColor("Color", colorDefault ), styleLine); 

Otherwise, can you please explain more clearly what you mean by:

display the previous 20,50 100 close price(s) of that bar

, and by:

I want use these close price(s) as support level

I wrote you already, use the Ref() function

Plot( Ref( C, -20 ), "Price 20 bars ago", colorRed );

@roychan you keep repeating the same nonspecific request.

"Display" how? Text on a chart?

So do you want lines like support and resistance lines on a chart?

I am a novice/beginner with regards to AmiBroker charting (it's unnecessary in any of my strategies), so take this advice with "a large grain of salt" (a North American expression for - this advice may be worthless). But I am up late after too many beers watching a boring championship game here in North American sports. So a little AmiBroker experiment before bed.

bars1 = 10;
bars2 = 50;

bi = BarIndex();

Close_bars1_back = SelectedValue( Ref( Close, -bars1 ) );
Close_bars2_back = SelectedValue( Ref( Close, -bars2 ) );
sBI = SelectedValue( bi );

FirstLine = IIf( ( bi > sBI - bars1 ) AND( bi <= sBI ), Close_bars1_back, Null );
SecondLine = IIf( ( bi > sBI - bars2 ) AND( bi <= sBI ), Close_bars2_back, Null );

Plot( Close, "Close", colorDefault, styleBar );
Plot( FirstLine, "Close " + bars1 + " Bars Ago", colorGold, styleLine | styleThick );
Plot( SecondLine, "Close " + bars2 + " Bars Ago", colorAqua, styleLine | styleThick );

fnt = "Arial";
fntsize = 10;
txtdist = 50;
PlotTextSetFont( "", fnt, fntsize, sBI, 0, -1 );
PlotText( "Close" + bars1 + " bars ago = " + Close_bars1_back, sBI, L[ sBI ], colorGreen, colorDefault, -txtdist );

Produces a chart like this after you "randomly click on one bar" (I am using 10 and 50 bars back, but you can substitute your choice.

image

1 Like

Firstly, thank you for Tomasz,portfoliobuilder and phase21.
Talk back to the afl: (Tomasz)Your code measure from current bar to price 20 bars ago. How about if I choose the bar of HHV or LLV? It is why I say I randomly click for a bar.32
The 20,60,120 (close bars ago) seems to have a pressure or support positions. I want to have automated display.
In fact, now I manually use to measure by myself. I think amibroker can do that.

_SECTION_BEGIN("Q_Lucas draw");
function DrawSeries( start, series, Color )
{
  x = BarsSince( start );
  result = 0;
  for( i = 0; 
  ( bar = StrToNum( StrExtract( series, i ) ) ) != 0; i++ )
  {
     result += x == bar;
     
    if (bar == 20)	//Conditional check refer to Lucseries value //
    {    
		Plot( result, "", color,
		styleHistogram | styleOwnScale | styleNoLabel, 0, 1 ,-40);
	}

	if (bar == 60)
		{	
		Plot( result, "", colorYellow,
		styleHistogram | styleOwnScale | styleNoLabel, 0, 1 ,-120);
		}
	if (bar == 120)
		{
		Plot( result, "", colorRed,
		styleHistogram | styleOwnScale | styleNoLabel, 0, 1 ,-240);
		}
   }  

	Plot( result, "", Color, 
        styleHistogram | styleOwnScale | styleNoLabel, 0, 1 );
	//Plot(Close,"Close",colorBlack,styleCandle); 
	//Plot(MA(Close,20), "Shifted MA", colorRed, styleLine, Null, Null, 10 );            
}

Plot( C, "Price", colorBlack, styleCandle );

LucSeries = "20,60,120";

dn = ParamDate("Date", "", 0 );
tn = ParamTime("Time", "00:00:00", 0 );

bi = BarIndex();

if( ParamToggle("Start point", "Mouse select|Date input", 0 ) )
start = DateNum() == dn AND TimeNum() == tn;
else
start = bi == SelectedValue( bi );

if( start[ BarCount -1 ] ) 
  Title = "Click on chart to select starting point or enter date in the Parameters window";

Lucon = ParamToggle("Lucas Series", "Off|On", 1 );
luccolor = ParamColor("Lucas Color", colorGreen );

if( lucon ) DrawSeries( start, LucSeries , luccolor );
_SECTION_END();

_SECTION_BEGIN("MA1");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1, 10 );
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
_SECTION_END();

@roychan Why don't you just use GFX instead

bi = BarIndex();
sbi = SelectedValue(bi);

function DrawSeries( start, series, Color )
{
    GfxSetCoordsMode(1);
    GfxSelectPen(Color,0,2);

    for( i = 0; 
	( bar = StrToNum( StrExtract( series, i))) != 0; i++ ) {
	f = sbi - bar;
	if( f > 1 ) {
	    GfxMoveTo( f, C[f]);
	    GfxLineTo( sbi, C[f]);
	}
        // else Error("Not enough Bars");
        // if you want an Error Message
    }
    GfxSetCoordsMode(0);  
}

DrawSeries( start=1, series="5,20,50", colorAqua );
// Just modified a bit of your code, start variable is redundant.
// an example of GFX, hardcoded color and series etc

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

// compute Bars as required
// SetBarsRequired( 300, 0);

image

3 Likes