/* 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 Data users: This version can be used on any data. Norgate NDU plugin can be used with some small amendments hidden in comments. NDU 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("FINVIZ System"); // 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 = 20; Margin = 100; pctPosSize = 100/20 * 100/Margin; //////////////////////////////////////////////////// // Set up and verify AB environment SetOption("InitialEquity",IIf(Portfolio,100*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; AvgPrice = (H+L)/2; BuyPrice = ShortPrice = IIf(EntryTiming==0,C,O); SellPrice = CoverPrice = IIf(ExitTiming==0,C,O); ////////////////////////////////////////// /* Norgate inactive securities - For use with Norgate database 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") ); OC = NorgateOriginalCloseTimeSeries(); */ bi = BarIndex(); ExitLastBar = bi == LastValue( bi ); ////////////////////////////////////////// // Back Test Parameters SMA20 = MA(C,20); SMA200 = MA(C,200); DownGap = GapDown(); RelVolume = V/MA(V,60); Truerange = Max(High,Ref(C,-1)) - Min(Low,Ref(C,-1)); FinvizATR = EMA(truerange,14); PositionScore = RelVolume; // Entry and exit rules BUY = RelVolume > 1 AND C>SMA20 AND C>SMA200 AND DownGap AND FinvizATR < 1 AND C<50; SELL = ExitLastBar; ApplyStop(stopTypeTrailing, stopModePercent, 20, 2, False ); // 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