New to AFL help needed to color the bar depending on some indicator

#1
I want to achieve following with AFL

1. The default color of price bar should be BLACK.
2. If MACD() is above the zero line AND above the signal() line, color the bars in GREEN.
3. If MACD() is below the zero line AND below the signal() line, color the bars in RED.

Help please ..

Thanks
Sandipan
 

mehtaka

Active Member
#3
_SECTION_BEGIN("Macd Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g,
Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

//MACD
r1 = Param( "Impulse Fast avg", 12, 2, 200, 1 );
r2 = Param( "Impulse Slow avg", 26, 2, 200, 1 );
r3 = Param( "Impulse Signal avg", 9, 2, 200, 1 );

ml = MACD(r1, r2);
sl = Signal(r1,r2,r3);

MACUP = sl < ml AND ml > 0;
MACDN = sl > ml AND ml < 0;

SetBarFillColor (IIf(MACUP,colorGreen,IIf(MACDN, colorRed,colorBlack)));

Plot( C, "Price", IIf(MACUP,colorGreen,IIf(MACDN, colorRed,colorBlack)) , styleNoTitle | ParamStyle("Style") |
GetPriceStyle() );
_SECTION_END();
 
#4
_SECTION_BEGIN("Macd Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g,
Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

//MACD
r1 = Param( "Impulse Fast avg", 12, 2, 200, 1 );
r2 = Param( "Impulse Slow avg", 26, 2, 200, 1 );
r3 = Param( "Impulse Signal avg", 9, 2, 200, 1 );

ml = MACD(r1, r2);
sl = Signal(r1,r2,r3);

MACUP = sl < ml AND ml > 0;
MACDN = sl > ml AND ml < 0;

SetBarFillColor (IIf(MACUP,colorGreen,IIf(MACDN, colorRed,colorBlack)));

Plot( C, "Price", IIf(MACUP,colorGreen,IIf(MACDN, colorRed,colorBlack)) , styleNoTitle | ParamStyle("Style") |
GetPriceStyle() );
_SECTION_END();
Thanks man. This works perfectly. Great help.

Sandipan
 
#5
mehtaka, I am new to amibroker afl and trying hard to learn it. Could you please help me to achieve following.

My idea is to plot a up arrows ( green color ) at low price position of every bar when RSI > 70 and down arrows ( red color ) at high price position of every bar when RSI < 30.


My code is :

_SECTION_BEGIN(" My RSI of period 7");
periods = Param( "Periods", 7, 1, 200, 1 );

Bull = Cross(RSI(periods),70);
Bear = Cross(30,RSI(periods));

shape = Bull * shapeUpArrow + Bear * shapeDownArrow;
PlotShapes(shape, IIf( Bull , colorGreen, colorRed ), 0, IIf( Bull , Low, High ) );

_SECTION_END();

This code works ( when overlay to price graph ) but put a SINGLE Bull (up arrow)/Bear(down arrow) signal when RSI>70 and RSI <30 instead of putting for all bars when RSI > 70 and RSI < 30 respectively.

Please help me to correct the code.

Thanks
Sandipan
 

KelvinHand

Well-Known Member
#6
mehtaka, I am new to amibroker afl and trying hard to learn it. Could you please help me to achieve following.

My idea is to plot a up arrows ( green color ) at low price position of every bar when RSI > 70 and down arrows ( red color ) at high price position of every bar when RSI < 30.


My code is :

_SECTION_BEGIN(" My RSI of period 7");
periods = Param( "Periods", 7, 1, 200, 1 );

Bull = Cross(RSI(periods),70);
Bear = Cross(30,RSI(periods));

shape = Bull * shapeUpArrow + Bear * shapeDownArrow;
PlotShapes(shape, IIf( Bull , colorGreen, colorRed ), 0, IIf( Bull , Low, High ) );

_SECTION_END();

This code works ( when overlay to price graph ) but put a SINGLE Bull (up arrow)/Bear(down arrow) signal when RSI>70 and RSI <30 instead of putting for all bars when RSI > 70 and RSI < 30 respectively.

Please help me to correct the code.

Thanks
Sandipan
Same code put into indicator, You can trying hard to understand why print multiple arrows for 70 or 30 levels.


PHP:
_SECTION_BEGIN(" My RSI of period 7");
periods = Param( "Periods", 7, 1, 200, 1 );

xrsi= RSI(periods);
Plot(xrsi, "rsi", colorWhite);
PlotGrid(70, colorRed);
PlotGrid(30, colorLime);

Bull = Cross(xrsi,70);
Bear = Cross(30,xrsi);

shape = Bull * shapeUpArrow + Bear * shapeDownArrow;
PlotShapes(shape, IIf( Bull , colorAqua, ColorRGB(255,0,255) ), 0, IIf( Bull , xrsi, xrsi) );

_SECTION_END();
 
#7
KelvinHand, I don't understand your point. May be I'm too new to afl to understand your point. I tested code given by you. But as a ovlerlay to price chart , your code does not give the desired result. :confused:
 

KelvinHand

Well-Known Member
#8
KelvinHand, I don't understand your point. May be I'm too new to afl to understand your point. I tested code given by you. But as a ovlerlay to price chart , your code does not give the desired result. :confused:
You done the AFL is correct.
But don't understand how, why it work that way.

Plot the rsi indicator and study with afl you done for price chart.

Then you see how the rsi cross 70 and rsi cross 30 and plot the arrows.

You learn how to program ALF, but you don't grab the concept.
 
#9
You done the AFL is correct.
But don't understand how, why it work that way.

Plot the rsi indicator and study with afl you done for price chart.

Then you see how the rsi cross 70 and rsi cross 30 and plot the arrows.

You learn how to program ALF, but you don't grab the concept.
I agree with you KelvinHand. I'm working hard to understand the concept.

Thanks
Sandipan
 

Similar threads