Amibroker AFL Code - Pi Help Needed

Hi,

Following sample code in Nifty symbol for Amibroker is not registering any order with Pi Bridge. Please analyze code and let me know the reasons for the same.

brd.GetConnectionStatus() always registers status as -1 in log file and orders don’t reach to Pi.

_SECTION_BEGIN(“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 ) ) ));
Plot( C, “Close”, ParamColor(“Color”, colorDefault ), styleNoTitle | ParamStyle(“Style”) | GetPriceStyle() );
_SECTION_END();

_SECTION_BEGIN(“Nifty_SMA”);

fh = fopen( “C:\OUTPUT\”+“test.txt”, “a”);

SMA7 = MA(Close,7);
SMA21 = MA(Close,21);

Sig = “”;
CurrSig = “”;
Chk0 = (Cross( SMA7, SMA21));
Chk1 = (Cross( SMA21, SMA7));

Buy = Cross( SMA7, SMA21 );
Sell = Cross( SMA21, SMA7);
Short = Cross( SMA21, SMA7) ;
Cover = Cross( SMA7, SMA21 ) ;

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Short = ExRem( Short, Cover );
Cover = ExRem( Cover, Short );

BuyFlag = LastValue(Buy);
SellFlag = LastValue(Sell);
ShortFlag = LastValue(Short);
CoverFlg = LastValue(Cover);

shape = Buy * shapeUpArrow +Sell * shapeDownArrow ;
PlotShapes( shape, IIf( Buy, colorGreen, colorRed ),0, IIf( Buy, Low, High ) );
Shortshape = Short * shapeDownArrow;
CoverShape = Cover * shapeUpArrow;
PlotShapes( Shortshape, colorOrange, 0, High, -30);
PlotShapes( CoverShape, colorTurquoise, 0, Low, -30 );

ClientId = “TESTCLIENT”;
TrdSymbol = “NIFTY15APRFUT”;
Symbol = “NIFTY”;
Qty = “25”;
Exch =“NFO”;
PType = “NRML”;
OType = “MKT”;

BuyP = LastValue(ValueWhen(Buy,C),1);
ShortP = LastValue(ValueWhen(Short,C),1);
SellP = LastValue(ValueWhen(Sell,C),1);
CoverP = LastValue(ValueWhen(Cover,C),1);

Checkone=Nz(StaticVarGet(“Check”),0);
Checktwo = LastValue(BarIndex(),1);
OrdCond = LastValue(Buy) OR LastValue(Short) OR LastValue(Sell) OR LastValue(Cover);
GfxTextOut("Last Buy : " + BuyP + " Last Short : " + ShortP,25,125);

brd = Null;

if((OrdCond))
{
if(IsNull(brd))
{
brd = CreateStaticObject(“pibridge.Bridge”);
}

if(!brd.GetConnectionStatus()) brd.Reconnect();
fputs("Date : " + NumToStr(DateTime()) + " Pi Bridge Status - Checked " + brd.GetConnectionStatus() + "\n",fh);

if(LastValue(Buy))
{
fputs("Date : " + NumToStr(DateTime(),formatDateTime) + " Buy @ " + BuyP + "\n" , fh);
brd.PlaceOrder (Exch, TrdSymbol ,Symbol,"MyAFL", 1,Qty,0, BuyP, 0, OType, PType, ClientId,"Day");
}

if(LastValue(Sell))
{
fputs("Date : " + NumToStr(DateTime(),formatDateTime) + " Sell @ " + SellP + "\n" , fh);
brd.PlaceOrder (Exch, TrdSymbol,Symbol,"MyAFL", 2,Qty,0, SellP, 0, OType,PType, ClientId,"Day");
}

if(LastValue(Cover))
{
fputs("Date : " + NumToStr(DateTime(),formatDateTime) + " Cover @ " + CoverP + "\n" , fh);
brd.PlaceOrder (Exch, TrdSymbol,Symbol,"MyAFL", 1,Qty,0, CoverP, 0, OType, PType, ClientId,"Day");
}

if(LastValue(Short))
{
fputs("Date : " + NumToStr(DateTime(),formatDateTime) + " Short @ " + ShortP + "\n" , fh);
brd.PlaceOrder (Exch, TrdSymbol,Symbol,"MyAFL", 2,Qty,0, ShortP, 0, OType,PType, ClientId,"Day");
}
StaticVarSet("Check",CheckTwo );

}
fclose( fh );
_SECTION_END();

Please support.

Thanks,

Birju

1 Like

Hi Birju,

I Checked ur code..It is firing orders to Pi
Few points in ur code and if u feel u can modify ur code else u can use as it is ,

1.At present stage,ur AFL will fire multiple orders for single signal..U need to modify static var
2.Ur code opens and closes the test.txt file every time,the chart refreshes..which hampers the formula executions speed..instaed u can open & write & close the text file within  " if((OrdCond)) "
3.For Futures, qty should be number of Lots NOT Lot Size..I Think U used Lot Size in Ur Formula
4.instead  of defining buyp,sellp etc U can simply use TrdPrice = lastvalue(C,1);


now Just follow the below checklist to make it work.

1.Ensure Pi & Pi Bridge is installed
2.Ensure pibridge.net.dll file is present in amibroker directory
3.Always first start Pi and then Amibroker..If u opened amibroker first and then pi..then Just close and reopen Amibroker only
4.In Pi-->> User Settings under PiBridge Tab Select - Live Semi Automatic
5.Ensure in Ur AFL u have correctly mentioned ClientId,TrdSymbol,Symbol,Qty,OType,PType
6.And Finally in ur formula after the line OType = "MKT"; write Val = StrToUpper("Day");
and replace "Day" with Val in ur Formula

To Check,Use BarReplay

Ur fired orders will be available in Generated alerts window on Pi

U can also Use the ReadyMade PiBridge AFL.U can download from below link...
https://drive.google.com/uc?export=download&id=0B-AWKNHhYByLZmVEVFNxRVBuNUU
Instructions given in the AFL itself