/* Disclaimer: Trading in financial instruments is risky and you can experience total loss of capital. This code is provided for educational purposes only. It has been checked for accuracy but is not guaranteed to be error free. No responsibility shall be accepted for any type of loss. See the full disclaimer at jbmarwood.com/disclaimer. All rights reserved. ŠJBMARWOOD.COM. Notes for Norgate PremiumData users: NDU plugin provides IsIndexConstituent function, determining if a stock is currently in the index Symbols for indexes: Russell1000 $RUI, Russell2000 $RUT, Russell3000 $RUA, Russell MidCap $RMC, Russell MicroCap $RUMIC S&P100 $OEX, S&P500 $SPX, S&P MidCap 400 $MID, S&P SmallCap600 $SML, S&P1500 $SP1500, DowJonesIndustrialAverage $DJI, NASDAQ100 $NDX NDU is in beta test see: http://www.norgatedata.com/beta-testing-program/ */ //#include_once "Formulas\Norgate Data\Norgate Data Functions.afl" //////////////////////////////////////////////////// SetFormulaName("SPYFRAT"); // Controls Portfolio = 1; // 1 = Portfolio, 0 = All Trades SameBarExit = 0; // 1 = allow same bar exit, 0 = disallow EntryTiming = 1; // 0 = Same Bar on Close, 1 = Next Bar at Open ExitTiming = 1; // 0 = Same Bar on Close, 1 = Next Bar at Open PosQty = 2; Margin = 100; pctPosSize = 100/2 * 100/Margin; //////////////////////////////////////////////////// // Set up and verify AB environment SetOption("InitialEquity",IIf(Portfolio,25*1000,100*1000)); SetOption("CommissionMode",3); SetOption("CommissionAmount",IIf(Portfolio, 0.01, 0)); SetOption("AllowSameBarExit", SameBarExit); SetOption("MaxOpenPositions",IIf(Portfolio,PosQty,2500)); SetOption("AccountMargin",IIf(Portfolio,Margin,100)); SetOption("UsePrevBarEquityForPosSizing",IIf(EntryTiming==0,False,True)); SetOption("AllowPositionShrinking", True); SetTradeDelays(EntryTiming,ExitTiming,EntryTiming,ExitTiming); SetOption("ActivateStopsImmediately", IIf(ExitTiming==0,False,True)); SetPositionSize(IIf(Portfolio,pctPosSize,1),IIf(Portfolio,spsPercentOfEquity,spsShares)); RoundLotSize = IIf(Portfolio,1,1); //////////////////////////////////////////////////// // High-level AFL logic // Initialize all arrays so the backtest will always run Buy = Sell = Short = Cover = 0; BuyPrice = ShortPrice = O; SellPrice = CoverPrice = O; ////////////////////////////////////////// // Norgate inactive securities - see http://www.premiumdata.net/support/amibroker.php //OnSecondLastBarOfDelistedSecurity = !IsNull(GetFnData("DelistingDate")) AND (BarIndex() == (LastValue(BarIndex()) -1) OR DateTime() >= GetFnData("DelistingDate") ); //OnLastTwoBarsOfDelistedSecurity = !IsNull(GetFnData("DelistingDate")) AND (BarIndex() >= (LastValue(BarIndex()) -1) OR DateTime() >= GetFnData("DelistingDate") ); ////////////////////////////////////////// // Entry and exit rules TimeFrameSet( inWeekly ); rsiw = RSI(30); TimeFrameRestore(); rsiw = TimeFrameExpand(rsiw,inWeekly); RSIRule1 = RSI(30) > 70; RSIRule2 = rsiw > 70; RSIRule3 = rsiw > RSI(30); SMARule1 = MA(C,10) > MA(C,20) AND MA(C,20) > MA(C,50) AND MA(C,50) > MA(C,100); SMARule2 = C>MA(C,10); PositionScore = rsiw - RSI(30); //InUniverse = NorgateIndexConstituentTimeSeries("$RUMIC") AND NOT OnLastTwoBarsOfDelistedSecurity; // USE WITH NORGATE NDU BUY = RSIRule1 AND RSIRule2 AND RSIRule3 AND SMARule1 AND SMARule2; SELL = C < (MA(C,10) *.98); ApplyStop(stopTypeLoss,stopModePercent,8,2); Filter = Buy; // Output graphics PlotShapes(Buy*shapeUpArrow,colorGreen,0,BuyPrice); //plot arrow on level of entry PlotShapes(Sell*shapeDownArrow,colorRed,0,SellPrice); //plot arrow on level of exit //////////////////////////////////////////End