Need Help with ALF programming

Hi,

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 );
Short=Sell;
Cover=Buy;
BuyPrice=ValueWhen(Buy,C);
SellPrice=ValueWhen(Sell,C);
ShortPrice=ValueWhen(Short,C);
CoverPrice=ValueWhen(Cover,C);
	
	
/* 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 )
{
	PlaceBuyOrder(BuyPrice);
	StaticVarSetText(Name()+"-Buy",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)
{
	PlaceSellOrder(SellPrice);	
	StaticVarSetText(Name()+"-Sell",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:

http://amibroker.com/guide/afl/getchartid.html
http://www.amibroker.com/kb/2014/10/06/relationship-between-chart-panes/

1 Like

You might want surround your code with:

_Section_Begin() 
...code
_Section_End()

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.