/* 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("DCA"); SetOption("UseCustomBacktestProc", True ); IncludeTopN = 4; // how many top positions to exclude if( Status("action") == actionPortfolio ) { // retrieve the interface to portfolio backtester bo = GetBacktesterObject(); bo.PreProcess(); for( bar = 0; bar < BarCount; bar++ ) { Cnt = 0; // this for loop iterates through all trade signals and adjust pos size for( sig = bo.GetFirstSignal( bar ); sig; sig = bo.GetNextSignal( bar ) ) { if( Cnt >= IncludeTopN ) sig.Price = -1; // setting price to -1 excludes given signal from entry if( IsNull( bo.FindOpenPos(sig.Symbol) ) ) Cnt++; } bo.ProcessTradeSignals( bar ); } bo.PostProcess(); } SetBacktestMode( backtestRegularRaw ); SetOption("MaxOpenPositions", 500 ); SetOption( "InitialEquity", 120000); // 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") ); oc = NorgateOriginalCloseTimeSeries(); ////////////////////////////////////////// Turnover = (C * V); AvgTurnover = MA(close * Volume, 3); AvgVolume = MA(Volume, 10); Liquid = Ref(AvgTurnover,-1) > 500000 AND NorgateIndexConstituentTimeSeries("$SPX") AND NOT OnLastTwoBarsOfDelistedSecurity; m = Month(); Buy = ( m != Ref(m,-1)) AND Liquid; Sell = OnSecondLastBarOfDelistedSecurity; Fixeddollaramount = 250; PositionSize = Fixeddollaramount; PositionScore= 1000 - Ref(ROC(C,1),-1); 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