Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marketcalls/374febc80f831a499636b1d70a796053 to your computer and use it in GitHub Desktop.
Save marketcalls/374febc80f831a499636b1d70a796053 to your computer and use it in GitHub Desktop.
VWAP based Intraday Strategy - Amibroker AFL
_SECTION_BEGIN("Yesterdays VWAP Strategy");
TargetPrice = Param("Target Points",30,1,500,1);
StopPrice = Param("Stop Points",20,1,500,1);
N = Param("Limit Trades",1,1,10,1); //Per Symbol Trade Limit
SetPositionSize(1*RoundLotSize,spsShares); // Bactest Calculation for No of Points
marketstarttime = 091500;
sigendtime = 144449;
sqofftime = 151459;
Bars_so_far_today = 1 + BarsSince( Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == marketstarttime , BarIndex());
TodayVolume = iif(Sum(V,Bars_so_far_today)<=0,1,Sum(V,Bars_so_far_today));
average = (H+L+C)/3;
IIf (BarIndex() >= StartBar, VWAP = Sum (average * V, Bars_so_far_today ) / TodayVolume,0);
newday = Ref(Day(),-1) != Day();
yVWAP = ValueWhen(newday, Ref(VWAP,-1) ,1);
Plot(yVWAP,"yVWAP",colorBrown,styleDashed);
Plot(VWAP,"yVWAP",colorYellow,styleLine | styleThick);
sqoff = TimeNum() >=sqofftime;
dn = DateNum();
newDay = dn != Ref( dn,-1);
Buy = C>yVWAP AND Cross(C,VWAP) AND TimeNum()<=sigendtime;
Buy = Buy AND Sum( Buy, BarsSince( newDay) +1 ) <= N; //Limit to one trade per day
BuyPrice = ValueWhen(Buy,Close); //Enter at Close of the candle
TargetBuy = BuyPrice + TargetPrice;
StopBuy = BuyPrice - StopPrice;
Sell = Cross(H,TargetBuy) OR Cross(StopBuy,L) OR sqoff; //Exit if Target/Stop/Square off Time reached
SellPrice = IIf(Cross(H,TargetBuy), TargetBuy, IIf(Cross(StopBuy,L), StopBuy, Close));
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
BuyCont = Flip(Buy,Sell);
SellCont = Flip(Sell,Buy);
Short = C<yVWAP AND Cross(VWAP,C) AND TimeNum()<=sigendtime;
Short = Short AND Sum( Short, BarsSince( newDay) +1 ) <= N; //Limit to one trade per day
ShortPrice = ValueWhen(Short,Close);
TargetShort = ShortPrice - TargetPrice;
StopShort = ShortPrice + StopPrice;
Cover = Cross(H,StopShort) OR Cross(TargetShort,L) OR sqoff; //Exit if Target/Stop/Square off Time reached
CoverPrice = IIf(Cross(H,StopShort), StopShort, IIf(Cross(TargetShort,L), TargetShort, Close));
Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);
ShortCont = Flip(Short,Cover);
CoverCont = Flip(Cover,Short);
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(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(Sell * shapestar, colorBrightGreen, 0, High, 12);
PlotShapes(Cover * shapestar, colorRed, 0, Low, -12);
Plot(VWAP,"VWAP",colorYellow,styleLine | styleThick);
Plot(yVWAP,"yVWAP",colorYellow,styleDashed);
Plot(IIf(Buycont,TargetBuy,IIf(Shortcont,TargetShort,Null)),"Target",colorGreen,styleDashed | styleThick);
Plot(IIf(Buycont,StopBuy,IIf(Shortcont,StopShort,Null)),"Stop",colorRed,styleDashed | styleThick);
_SECTION_END();
_SECTION_BEGIN("Candlestick Charts");
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment