Help! Conditional Background Color in Ami

murthyavr

Well-Known Member
#1
How do I change the background color of the chart window, basing on a
condition?

Suppose if C>MA(C,21), I want the background color to be green. If C is
below MA-21, I want the BkGnd color to be in Red.

How do I code it?

Thanks..
 
#2
Here is my code.
Open parameters dialog to change settings to your needs.

Buy condition
Sell condition

Code:
_SECTION_BEGIN("Conditional Chart Background");
//by detwo of traderjj.com
P = ParamField("Price field", 3);
Periods = Param("Periods MA", 21, 2, 300, 1);
Buycond = C > MA(C, Periods);
bgmode = ParamToggle("Single Background Color","YES|NO", 1);
for(i = 0; i < BarCount; i++)
{
 if(Buycond[i])
   bgcolor[i] =  ParamColor("Background Color Top Buy", colorGreen);
 else
   bgcolor[i] =  ParamColor("Background Color Top Sell", colorRed);

 if(bgmode)
   SetChartBkGradientFill( bgcolor[i], ParamColor("Background Color Bottom", colorWhite), bgcolor[i] );
 else
   SetChartBkGradientFill( bgcolor[i], bgcolor[i], bgcolor[i] );
}

SetChartOptions(0, chartShowArrows|chartShowDates);
SetChartBkColor( ParamColor("Background Color Axes", colorBlack) ); 

upcolor     = ParamColor("Up Bar color ", colorWhite);
downcolor   = ParamColor("Down Bar color ", colorBlack);
Coloutline  = ParamColor("(Out-)Line Color", colorBlack);
barcolor    = IIf(Close > Ref(Close, -1), upcolor, downcolor);
gps         = GetPriceStyle();
style       = gps | styleNoTitle | styleThick;

SetBarFillColor(barcolor);
if(gps == styleBar)
  Plot( C, " " , barcolor, style ); 

else
  Plot( C, " " , Coloutline, style ); 

Colortitle  = ParamColor("Color of Title", colorBlack);
_N(Title = StrFormat(EncodeColor(Colortitle) + Name() + " - {{INTERVAL}} - {{DATE}} Open=%g, Hi=%g, Lo=%g, Close=%g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
_SECTION_END();

_SECTION_BEGIN("EMA");
Plot( EMA( P, Periods ), _DEFAULT_NAME()+"("+periods+")", ParamColor( "Color MA", colorBlack ), ParamStyle("Style MA") ); 
_SECTION_END();
 

sudris

Well-Known Member
#3
what detwo has posted is quite brilliant.

but if you need a simplistic version and have AFL version 3.4 and above:

Code:
_SECTION_BEGIN("Conditional Chart Background Color");
Plot(C,"Close", colorBlack,styleCandle);
M21=LastValue(MA(C,21));
Cl=LastValue(C);
if (Cl>M21)
SetChartBkColor(colorGreen);
else
SetChartBkColor(colorRed);
_SECTION_END();
 
#4
Of course you could do without looping too.

here is my code without looping

Code:
_SECTION_BEGIN("Conditional Chart Background no loop");
//by detwo of traderjj.com
P = ParamField("Price field", 3);
Periods = Param("Periods MA", 21, 2, 300, 1);
Buycond = LastValue(C) > LastValue(MA(C, Periods));
bgmode = ParamToggle("Single Background Color","YES|NO", 1);
if(Buycond)
  bgcolor =  ParamColor("Background Color Top Buy", colorGreen);
else
  bgcolor =  ParamColor("Background Color Top Sell", colorRed);

if(bgmode)
  SetChartBkGradientFill( bgcolor, ParamColor("Background Color Bottom", colorWhite), /*background color title*/ bgcolor );
else
  SetChartBkGradientFill( bgcolor, bgcolor, /*background color title*/ bgcolor );

SetChartOptions(0, chartShowArrows|chartShowDates);
SetChartBkColor( ParamColor("Background Color Axes", colorBlack) ); 

upcolor     = ParamColor("Up Bar color ", colorWhite);
downcolor   = ParamColor("Down Bar color ", colorBlack);
Coloutline  = ParamColor("(Out-)Line Color of Candle", colorBlack);
barcolor    = IIf(Close > Ref(Close, -1), upcolor, downcolor);
gps         = GetPriceStyle();
style       = gps | styleNoTitle | styleThick;

SetBarFillColor(barcolor);
if(gps == styleBar)
  Plot( C, " " , barcolor, style ); 

else
  Plot( C, " " , Coloutline, style ); 

Colortitle  = ParamColor("Color of Title", colorBlack);
_N(Title = StrFormat(EncodeColor(Colortitle) + Name() + " - {{INTERVAL}} - {{DATE}} Open=%g, Hi=%g, Lo=%g, Close=%g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
_SECTION_END();

_SECTION_BEGIN("EMA");
Plot( EMA( P, Periods ), _DEFAULT_NAME()+"("+periods+")", ParamColor( "Color MA", colorBlack ), ParamStyle("Style MA") ); 
_SECTION_END();
 

murthyavr

Well-Known Member
#6
Dear detwo/sudris,

I have another query, hope you will be able to clarify this one also.

1. I obtain the value of StochD(8,3,4) in an hourly time frame.

2. Suppose, if I want to obtain the same hourly value from a 5min TF, I use the
following code:

TimeFrameSet(in1Minute*60);
dHTF = StochD( 8,3,4 );
TimeFrameRestore();

D = TimeFrameExpand(dHTF, in1Minute*60);


My problem is the values obtained in the two methods are not identical..

Can you please suggest how to get the exact hourly value from a 5min TF?

Thanks..
 

murthyavr

Well-Known Member
#8
Code:
D = TimeFrameExpand(dHTF, in1Minute*60, [COLOR="Blue"]expandPoint[/COLOR]);
Hi Murthy,

just add the "mode" and you would see the exact values


PS: You owe me a Hyderabadi Biryaani from Paradise for this :p
Dear Sudris,

Thanks a lot. This mode parameter gets the exact value..

But in terms of plotting, I don't see a continuous line - only a piece of line
at the end of the day. Can the line also be plotted without breaks with exact values?

With this, I would be owing two biryaanis from Paradise.. :)
 
Last edited:

sudris

Well-Known Member
#9
Dear Sudris,

Thanks a lot. This mode parameter gets the exact value..

But in terms of plotting, I don't see a continuous line - only a piece of line
at the end of the day. Can the line also be plotted without breaks with exact values?

With this, I would be owing two biryaanis from Paradise.. :)
It didn't occur to me that you wish to plot the values as you had used "Timeframeexpand" Function, which won't give you smooth lines when plotted under lower TF.

But if you still want to expand it, use "expandFirst" instead but let me tell you this wont give smooth lines as the values get stretched out.

If OTOH you need smooth lines, omit entire line "D=....."
and simply
Plot(dHTF, "dHTF", colorRed);

Two Biryaanis sound good, one for each of us :cool:
 

murthyavr

Well-Known Member
#10
It didn't occur to me that you wish to plot the values as you had used "Timeframeexpand" Function, which won't give you smooth lines when plotted under lower TF.

But if you still want to expand it, use "expandFirst" instead but let me tell you this wont give smooth lines as the values get stretched out.

If OTOH you need smooth lines, omit entire line "D=....."
and simply
Plot(dHTF, "dHTF", colorRed);

Two Biryaanis sound good, one for each of us :cool:

Aaah! You made my day Sudris ji!! It was perfect !!!

Now, I owe you one biryaani every time you visit Hyderabad. When are you
coming down? Please let me know - no smilies, seriously!
 

Similar threads