How to Connect Zerodha Pi with Amibroker with Pi Bridge very Easily.






Check

The below Amibroker AFL  of Sample trading system with Pi bridge for Zerodha Pi Order Firing



_SECTION_BEGIN("Chart Settings");
SetChartOptions(0,chartShowArrows|chartShowDates);
SetChartBkColor(colorBlack);
SetChartBkGradientFill( colorBlack,colorBlack);
GfxSelectFont( "Cambria", 11, 400, False );
GfxSetTextColor( colorYellow );
GfxSetBkMode(0);
ChartStyle = ParamList("Select Chart Style","Candle|Line|Dot",0);
Plot( C, "Close", colorDefault, IIf(ChartStyle=="Candle",styleCandle,IIf(ChartStyle=="Line",styleLine,IIf(ChartStyle=="Dot",styleDots|styleNoLine,styleCandle))) );
pxw = Status("pxwidth");
pxh = Status("pxheight");
_SECTION_END();

_SECTION_BEGIN("Basic Variables");
Buy=Sell=Short=Cover=0;
BuySLP=ShortSLP=0;
BuyP=SellP=ShortP=CoverP=0;
BuyTgtP=ShortTgtP=0;
BarB = BeginValue(BarIndex());
BarE = EndValue(BarIndex());
BarC = BarIndex();
BarL = LastValue(BarC,1);
BarT = BarIndex() - Nz(ValueWhen(Day()!= Ref(Day(),-1) ,BarIndex()),0) + 1;
Ltp = LastValue(C,1);
BValue = ValueWhen(BarT==1,O);
IValue = BeginValue(O);
_SECTION_END();

_SECTION_BEGIN("Trade Details");
Exch = StrToUpper(ParamList("Select Exchange","NFO|NSE",0));
TrdSymbol =  StrToUpper(ParamStr("Enter Trade Symbol","NIFTY16APRFUT"));
Symbol =  StrToUpper(ParamStr("Enter Symbol","NIFTY"));
StgyName =   StrToUpper(ParamStr("Strategy Name","VIJAY"));
TrdQty = Param("Number of Qty/Lots to Trade",1,1,500,1);
DiscQty = 0;
LmtPrice = C;
TrgPrice = 0;
OrdType = StrToUpper(ParamList("Select Order Type","L|MKT|SL|SL-M",0));
ProdType = StrToUpper(ParamList("Select Product Type","MIS|NRML|CNC",0));
ClientId =  StrToUpper(ParamStr("Enter Client ID","AB1234"));
Val = StrToUpper("DAY");
TgtPct = Param("Target % From Entry Price..?",3,0.1,10,0.05);
SLPct = Param("Stoploss % From Entry Price..?",2,0.1,10,0.05);
_SECTION_END();

_SECTION_BEGIN("Test Trading System");
psar = SAR(0.02,0.2);

Buy = C > psar;
Short = C < psar;

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

Buy = Ref(Buy,-1);
Short = Ref(Short,-1);

BuyP = ValueWhen(Buy,O);
ShortP = ValueWhen(Short,O);

BuyTgtP = BuyP + (BuyP * (TgtPct/100));
BuySLP =  BuyP - (BuyP * (SLPct/100));

ShortTgtP = ShortP - (ShortP * (TgtPct/100));
ShortSLP =  ShortP + (ShortP * (SLPct/100));

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorGreen, 0,L, Offset=-30);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorRed, 0,H, Offset=-30);
_SECTION_END();

_SECTION_BEGIN("PiBridge");
pLmtPrice = LastValue(LmtPrice,1);
pBuyTgtP = LastValue(BuyTgtP,1);
pBuySLP = LastValue(BuySLP,1);
pShortTgtP = LastValue(ShortTgtP,1);
pShortSLP = LastValue(ShortSLP,1);

OrderStatus = StaticVarGetText(Name()+"-ORDER");
if (LastValue(Buy) AND OrderStatus != "BUY")
{brd = CreateStaticObject("pibridge.Bridge");
brd.PlaceOrder (Exch,TrdSymbol,Symbol,StgyName, 1,TrdQty,DiscQty,pLmtPrice, 0, OrdType,ProdType,ClientID,Val);
brd.PlaceOrder (Exch,TrdSymbol,Symbol,StgyName, 2,TrdQty,DiscQty,pBuyTgtP, 0, "L",ProdType,ClientID,Val);
brd.PlaceOrder (Exch,TrdSymbol,Symbol,StgyName, 2,TrdQty,DiscQty,0,pBuySLP, "SL-M",ProdType,ClientID,Val);
StaticVarSetText(Name()+"-ORDER","BUY");}

OrderStatus = StaticVarGetText(Name()+"-ORDER");
if (LastValue(Short)AND OrderStatus != "SHORT")
{brd = CreateStaticObject("pibridge.Bridge");
brd.PlaceOrder (Exch,TrdSymbol,Symbol,StgyName, 2,TrdQty,DiscQty,pLmtPrice, 0, OrdType,ProdType,ClientID,Val);
brd.PlaceOrder (Exch,TrdSymbol,Symbol,StgyName, 1,TrdQty,DiscQty,pShortTgtP, 0, "L",ProdType,ClientID,Val);
brd.PlaceOrder (Exch,TrdSymbol,Symbol,StgyName, 1,TrdQty,DiscQty,0, pShortSLP, "SL-M",ProdType,ClientID,Val);
StaticVarSetText(Name()+"-ORDER","SHORT");}
_SECTION_END();

_SECTION_BEGIN("Display");
/*Title Display*/
//GfxRectangle(pxw/2,1,(pxw/2)-5,pxh);
GfxGradientRect( 1, 1, pxw, 59, colorBlack, colorBlack);
GfxGradientRect( 1, 59, pxw, 60, colorWhite, colorWhite);

GfxSelectFont( "Cambria", 16, 800, False );
GfxSetTextColor( colorYellow );
GfxTextOut( "SAMPLE TRADING SYSTEM WITH PIBRIDGE", (pxw/2)-200, 8 );

GfxSelectFont( "Cambria", 12, 700, False );
GfxSetTextColor( colorGold );
GfxTextOut( StrToUpper(Name()), 5, 8 );

GfxSetTextColor( colorOrange );
GfxTextOut( Date(),150, 8);

GfxSelectFont( "Cambria", 12, 400, False );
GfxSetTextColor( colorAqua );
GfxTextOut( "Op:"+WriteVal(O,1.2),5, 35 );
GfxSetTextColor( colorLime );
GfxTextOut( "Hi:"+WriteVal(H,1.2),90, 35 );
GfxSetTextColor( colorRed );
GfxTextOut( "Lo:"+WriteVal(L,1.2),175, 35 );
GfxSetTextColor( colorGold );
GfxTextOut( "Cl:"+WriteVal(C,1.2),260, 35 );

GfxSelectFont("Cambria", 16, 800, False, False, 0);
GfxSetTextColor(colorOrange);
GfxTextOut( "LTP "+WriteVal(LastValue(C,1),1.2), (pxw/2)-40, 60);
_SECTION_END();


Comments

Post a Comment

Popular posts from this blog

Algorithmic Trading Life Cycle of Algo Components

Trading Algorithms: Areas of Concern