Creating indicators

I have 2 MA’s on my chart. 8 SMA of the open and 8 SMA of the close. I want to create an indicator in the indicator pane (not over the price chart), that measures the difference between the 2 MA’s and plots a 3 period MA of that in one colour and then a 3 period MA of that overlaid in a different colour.
I also want the price bars to change colour when the 2 MA’s on the price chart cross one another.
Also, is it possible to highlight 2 price bars or 5 bars if they are entirely above or below a MA?

Here is starting point for you

m1 = MA( Open, 8 );
m2 = MA( Close, 8 );

diff = m1 - m2;
Plot( diff, "difference", colorRed );
Plot( MA( diff, 3 ), "Ma of difference", colorBlue );

As to changing bar colors refer to manual:
http://www.amibroker.com/guide/h_indbuilder2.html

@Jonesy Any progress? The basic calculations have been given to you by Tomasz but here are a few more lines you could use.

maOpen = MA( Open, 8 );
maClose = MA( Close, 8 );

Diff = maOpen - maClose;
AveDiff = MA( Diff, 3 );
SecondAve = MA( AveDiff, 3 );

dynamic_color = IIf( AveDiff > SecondAve, colorRed, colorLightBlue );

Plot( C, "C", dynamic_color, styleBar | styleThick );
Plot( AveDiff, "AveDiff", colorRed );
Plot( SecondAve, "SecondAve", colorBlue );

You can put those into different afl's and chart them into different panes or use other methods that I am not that familiar with to produce a chart like this.

image

I thought to experiment with this so it may be more complex than necessary.

For that chart to be in a single afl I used this,

// forum question
// http://forum.amibroker.com/t/creating-indicators/2033

maOpen = MA( Open, 8 );
maClose = MA( Close, 8 );

Diff = maOpen - maClose;
AveDiff = MA( Diff, 3 );
SecondAve = MA( AveDiff, 3 );

dynamic_color = IIf( AveDiff > SecondAve, colorRed, colorLightBlue );

ChartType = ParamList( "Chart Type", "Price Chart|Averages" ); //

if( ChartType == "Price Chart" ) // Price Chart
{
    SetChartOptions( 0, chartShowArrows | chartShowDates );
    _N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g \n{{VALUES}}", O, H, L, C ) );

    //Plot( C, "C", dynamic_color, styleBar | styleThick );
	PlotOHLC( Open, High, Low, Close, "Jonesy's Chart Request", dynamic_color, styleBar | styleThick  ); 
}

if( ChartType == "Averages" ) // the two averages Jonesy wants
{
    SetChartOptions( 0, 0, chartGrid0 );
    _N( Title = StrFormat( "{{VALUES}}" ) );

    Plot( AveDiff, "AveDiff", colorRed );
    Plot( SecondAve, "SecondAve", colorBlue );
}

By clicking on "apply" or pulling down the insert indicator on that "apply" menu, you can insert the indicator twice. Then click on your chart and use the parameters to set the pane's to different choices.
image

2 Likes

Cheers Larry. I am not a programmer and so just starting out. Grateful for your input.

Jeff

Hey Larry, just noticed that the code you wrote for the price bars to change isn’t what I meant, crossed wires there, my mistake, not very clear. The MA’s on the price chart are the open of the 8 sma and the other is the close of the 8 sma. It is when these cross, that the price bars change colour, not when the indicator below crosses.

Cheers,

Jeff

@Jonesy That is a simple alteration to the code. Change,

dynamic_color = IIf( AveDiff > SecondAve, colorRed, colorLightBlue );

To this,

dynamic_color = IIf( maOpen > maClose, colorRed, colorLightBlue );

To add a little visual confirmation you can add plots for those two moving averages and (they are very very close to each other) try to visualize bar color changes match up with moving average crossovers.
So add a couple of Plot lines like

Plot(maOpen, "maOpen",  colorWhite, styleDashed);
Plot(maClose, "maClose", colorYellow, styleLine);

And chart turns into this,
image

That’s cool Larry and visually pleasing. Thanks for helping.

Jonesy