Hi,
I have tried backtesting TPS code from AFL Library http://www.amibroker.com/members/library/formula.php?id=1380. But when I insert trade delays and buyprice and sell price as open, the position size is incorrect. What am I missing here ? Please see below code...
/*Description:
Based on "High Probability ETF Trading"
Larry Connors and Cesar Alvarez, page 87
This is a mean reversion system
Scale in as RSI falls below entry point during an uptrend, up to three times after initial entry
Connors tested 01/01/1993 through 31/12/2008 on a group of 20 liquid ETFs
Formula:
// Connors TPS - ETFs.afl
//
// Based on "High Probability ETF Trading"
// Larry Connors and Cesar Alvarez, page 87
//
// This is a mean reversion system
// Scale in as RSI falls below entry point during an uptrend, up to three times
// after initial entry
//
// Connors tested 01/01/1993 through 31/12/2008 on a group of 20 liquid ETFs
*/
SetOption( "InitialEquity", 10000000 );
MaxPos = 1;
SetOption( "MaxOpenPositions", MaxPos );
//TradeSize = 10000;
MaxPositionSizePercent = 100;
MALongPeriod = 200;
//---
// Long logic
//---
CloseAboveLongTermMA = C > MA(C, MALongPeriod);
LowerClose = C < Ref(Close, -1);
EntryRSI = 25;
ExitRSI = 70;
RSIPeriod = 2;
LowRSIBars = 2;
CurrentRSI = RSI(RSIPeriod);
MultipleDayLowRSI = Sum(RSI(RSIPeriod) < EntryRSI, LowRSIBars) == LowRSIBars;
FirstScaleIn = 0.1;
SecondScaleIn = 0.2;
ThirdScaleIn = 0.3;
FourthScaleIn = 0.4;
SetTradeDelays(1, 1, 1, 1);
BuyPrice = SellPrice = ShortPrice = CoverPrice = Open;
Sell = Cross( RSI(RSIPeriod), ExitRSI );
BarsSinceSell = BarsSince(Sell);
FirstEntry = CloseAboveLongTermMA AND MultipleDayLowRSI;
InFirstPos = Flip(FirstEntry, Sell);
FirstTrigger = ExRem(InFirstPos, Sell);
BarsSinceFirstTrigger = BarsSince(FirstTrigger);
FirstTriggerPrice = IIf(BarsSinceFirstTrigger < BarsSinceSell,Ref(C,-BarsSinceFirstTrigger), 0 );
SecondEntry = CloseAboveLongTermMA AND C < FirstTriggerPrice AND InFirstPos AND Ref(InFirstPos,-1);
InSecondPos = Flip(SecondEntry, Sell);
SecondTrigger = ExRem(InSecondPos, Sell);
BarsSinceSecondTrigger = BarsSince(SecondTrigger);
SecondTriggerPrice = IIf(BarsSinceSecondTrigger < BarsSinceSell,Ref(C,-BarsSinceSecondTrigger), 0);
ThirdEntry = CloseAboveLongTermMA AND C < SecondTriggerPrice AND InSecondPos AND Ref(InSecondPos,-1);
InThirdPos = Flip(ThirdEntry, Sell);
ThirdTrigger = ExRem(InThirdPos, Sell);
BarsSinceThirdTrigger = BarsSince(ThirdTrigger );
ThirdTriggerPrice = IIf(BarsSinceThirdTrigger < BarsSinceSell,Ref(C,-BarsSinceThirdTrigger), 0);
FourthEntry = CloseAboveLongTermMA AND C < ThirdTriggerPrice AND InThirdPos AND Ref(InThirdPos,-1);
InFourthPos = Flip(FourthEntry, Sell);
FourthTrigger = ExRem(InFourthPos, Sell);
BarsSinceFourthTrigger = BarsSince(FourthTrigger);
FourthTriggerPrice = IIf(BarsSinceFourthTrigger < BarsSinceSell,Ref(C,-BarsSinceFourthTrigger ), 0);
//---
// Trade signals
//---
if( FirstScaleIn + SecondScaleIn + ThirdScaleIn + FourthScaleIn == 1.0 )
{
//PositionScore = PositionScore = 100 - CurrentRSI ; // favour low RSI
Buy = IIf( FirstTrigger, 1,
IIf( SecondTrigger OR ThirdTrigger OR FourthTrigger, sigScaleIn, 0 ) );
SetPositionSize(IIf( FirstTrigger, MaxPositionSizePercent*FirstScaleIn,
IIf( SecondTrigger, MaxPositionSizePercent*SecondScaleIn,
IIf( ThirdTrigger, MaxPositionSizePercent*ThirdScaleIn,MaxPositionSizePercent*FourthScaleIn ) ) ),
IIf( Buy > 0, spsPercentOfEquity, spsNoChange ) );
Sell = ExRem(Sell,Buy);
}
Plot(FirstTrigger, "T1", colorYellow, styleThick );
Plot(SecondTrigger, "T2", colorBlue, styleThick );
Plot(ThirdTrigger, "T3", colorBrown, styleThick );
Plot(FourthTrigger, "T4", colorWhite, styleThick );
//Plot(FirstTriggerPrice, "TP1", colorYellow, styleOwnScale );
//Plot(SecondTriggerPrice, "TP2", colorBlue, styleOwnScale );
//Plot(ThirdTriggerPrice, "TP3", colorBrown, styleOwnScale );
//Plot(FourthTriggerPrice, "TP4", colorBrown, styleOwnScale );
Plot(Sell, "Sell", colorRed, styleThick );
Below is the detailed trade report.

Thanks.