Trading signal light

Here is a project that I started some time ago and I make it available to everyone. I would be interested in any improvements. Also, I originally was interested in placing several signal lights on the chart reacting to different signals but never got around to it.

//Trading signal light
//Anthony Faragasso

_SECTION_BEGIN("Trading Light");
//=========================================================================

//==================================================================
// FUNCTION WHICH CALCULATES AND RETURN THE DIFFERENT 
// SHADES OF GREEN AND RED COLORS
//==================================================================
function FillColor(Value, MaxPosVal, MinPosVal, MaxNegVal, MinNegVal)
{
 UpRed		= 0;
 UpGreen	= 255;
 UpBlue		= 0;
 DnRed		= 255;
 DnGreen	= 0;
 DnBlue		= 0;
 DarkColor	= 0;
 LightColor	= 230;

 Upred = IIf(Value > 0, LightColor - int((LightColor-(((MaxPosVal-Value)*(LightColor-DarkColor))/(MaxPosVal-MinPosVal)))), IIf(Value
== 0, 255, 0));
 UpBlue = UpRed;
 DnGreen = IIf(Value <= 0, LightColor - int((LightColor-(((abs(MaxNegVal)-abs(Value))*(LightColor-DarkColor))/(abs(MaxNegVal)-abs(MinNegVal))))),
IIf(Value == 0, 255, 0));
 DnBlue = DnGreen;
 Color	= IIf(Value >= 0, ColorRGB(UpRed, UpGreen, UpBlue), ColorRGB(DnRed,DnGreen, DnBlue));
return Color;
}
//===============================================================================
//===========================================
// Indicator calculations
// User indicators here
// 
//===========================================
 indicator=ema(C,10);   
//==========================================

Buygreencondition=indicator;
Sellredcondition=indicator;

StatusCross=Flip(Buygreencondition,Sellredcondition);

// Check the difference between C and indicator and plot the strength of the colors
// accordingly
Value= C - indicator ;
MaxValue 		= HHV(Value, Status("lastvisiblebar") - Status("firstvisiblebar"));
MinValue 		= LLV(Value, Status("lastvisiblebar") - Status("firstvisiblebar"));
RColor 			= IIf(Value >= 0, FillColor(Value, MaxValue, 0, MinValue, 0),FillColor(Value, MaxValue, 0, MinValue, 0));




Plot(C,"",IIf(C > Ref(C,-1),colorGreen,colorRed),styleCandle);
Plot(indicator,"",IIf(indicator > C,colorRed,colorGreen),styleLine);


function DrawButton(  x1, y1, x2, y2, BackColor )
{

    GfxSetOverlayMode( 0 );
    GfxSelectFont( "Tahoma", 12, 800 );
    GfxSelectPen( colorBlack );
    //GfxSetBkMode( 1 );
    GfxSelectSolidBrush( BackColor );
    GfxSetBkColor( BackColor );
    GfxSetTextColor( 1 );
   GfxRectangle( x1, y1, x2, y2 );
    //GfxDrawText( Text, x1, y1, x2, y2, 32 | 1 | 4 );
}
///////////////////////////////////////////////////////
//Light change conditions

redcondition=indicator > C;//Cross(indicator,C);
greencondition=indicator < C;//Cross(C,indicator);


//////////////////////////////////////////////////////
Color=Null;
Color2=Null;
Color3=Null;
lightsbackcolor=colorLightGrey;

for(i=1;i<=BarCount-1;i++)
{
if(redcondition[i])
Color=colorRed;

else
Color=lightsbackcolor;
}
for(i=1;i<=BarCount-1;i++)
{
if(sellredcondition[i-1] )
Color2=RColor[i];
else
Color2=lightsbackcolor;//RColor[i];
}
for(i=1;i<=BarCount-1;i++)
{
if(greencondition[i])
Color3=colorGreen;
else
Color3=lightsbackcolor;
}
function Drawlights( x1,y1,radius,Color)
{
    GfxSetOverlayMode( 0 );
    GfxSelectPen( colorBlack, 2 ); 
    GfxSelectSolidBrush( Color ); 
    GfxCircle( x1, y1, radius );
       //GfxSetBkMode( 1 );
       GfxSelectSolidBrush( Color );
       GfxSetBkColor( Color );

} 

ChartPixelheight	= Status( "pxheight" );
ChartPixelWidth	= Status( "pxwidth" );
Yoffset = Param( "Button Row Offset (px)", 50, 0, 100*ChartPixelheight, 1 );
Xoffset	= Param( "Button Column Offset (px)", 50, 0,100*ChartPixelWidth, 1 );
CellHeight = 110;//Param("Cell Height",20,5,200,5);   
CellWidth = 40;//Param("Cell Width",120,5,200,5);
  
DrawButton(  Xoffset, yoffset, Xoffset + CellWidth, yOffset+ CellHeight , colorYellow ); 

Drawlights(  Xoffset+(CellWidth/2) , yOffset+(CellWidth/2) ,(CellWidth/2.5),color);
Drawlights(  Xoffset+(CellWidth/2) , yOffset+(CellWidth/0.74) ,(CellWidth/2.5),color2);
Drawlights( Xoffset+(CellWidth/2) , yOffset+(CellWidth/0.45) ,(CellWidth/2.5),color3);

_SECTION_END();
2 Likes

you would have to optimize your code at many places just as I am browsing through it.

GfxSetOverlayMode( 0 );    // Move it out of function

No need to call it repeatedly if you aren't switching modes.
Check other GFX functions that maybe called repeatedly but not changing.

You are using this 4 times

for(i=1;i<=BarCount-1;i++)

when only one loop would suffice. This is 4X the load.

Now the whole thing of looping can be written in AFL with IIF()

Color3 = IIf( greencondition, colorGreen, lightsbackcolor );

Way more performance, 3 such statements for each color..

This one is confusing

RColor = IIf(Value >= 0, FillColor(Value, MaxValue, 0, MinValue, 0),
                         FillColor(Value, MaxValue, 0, MinValue, 0));
// Calling same function for both conditions?

There are a lot more, will check later.

4 Likes