Need Help with ALF programming


I had written an AFL program to generate Buy and Sell signal based on my custom strategy.

Below is Code:

Buy = Cross(x,y) AND condition1== 1 AND timeOK;
Sell = condition1== -1 AND timeOK;
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
DT = LastValue(DateNum(),1);
TT = LastValue(TimeNum(),1);
_TRACE(" DT == " + DT + "TT ==" + TT);
BuyVar = StaticVarGetText(Name()+"-Buy");
BuyVar1 = Name()+"-Buy-"+ NumToStr(DT,1.0,False)+"-"+NumToStr(TT,1.0,False)+"-"+NumToStr(GetChartID(),1.0,False);
_TRACE("Buy ---> " + BuyVar + ":::" + BuyVar1 + "::" + LastValue(Buy) + ":::" + pEnableAutoTrade);
if(pEnableAutoTrade AND LastValue(Buy) AND BuyVar != BuyVar1 )
SellVar = StaticVarGetText(Name()+"-Sell");
SellVar1 = Name()+"-Sell-"+NumToStr(DT,1.0,False)+"-"+NumToStr(TT,1.0,False)+"-"+NumToStr(GetChartID(),1.0,False);
_TRACE("Sell ---> " + SellVar + ":::" + SellVar1 + "::" + LastValue(Sell) + ":::" + pEnableAutoTrade);
if(pEnableAutoTrade AND LastValue(Sell) AND SellVar != SellVar1)

Code is working fine in case only if 1 chart is open.

When i am trying to run above code for multiple charts i am facing below issues:

  1. LastValue(Buy) and LastValue(Sell) is sometimes becoming 1, even no condition is met. thus generating fake signal.

  2. For 3rd chart not even single signal is getting generated.

Note i am using trial version of amibroker

@DT-Coder you need to make your Static variables’ names unique, adding (for example) GetChartID() to their names:

ChID = GetChartID();
StaticVarSetText( Name() + ChID + "-Sell", SellVar1 );

Read about Chart IDs:

1 Like

You might want surround your code with:


Thanks for quick reply.

I will try your solution. But i still have doubt as my variable are still unique because Name() of both the chart open are different.