mismatch in backtesting.....pl. help...AMIBROKER

bharatk8

Active Member
#1
I am using a code to test my semi mechanical trading system.

This code marks buy/sell/short/cover arrows when I press appropriate

tab in properties window during bar replay.

And at the end of bar replay,when I run backtest in AA window of AB,it

displays PnL and other statistics.

While checking these results,I found that last trade of 20th Dec. was

marked as loss making trade while it was actually a profitable trade.

When I took close look at chart and back testing results,I found a

mismatch on this bar.However all other trades have been correctly

displayed in BT.

My last trade was just before market close (square off of long

trade).This trade was correctly marked on chart on last bar of the

day.but BT results shows that trade was squared up at 4538 (on 3.15

P.M. bar). I am not able to find reason for mis-match.Pl. guide.

http://img15.imageshack.us/img15/884...tradingmis.png

http://imageshack.us/photo/my-images/15/discretionarytradingmis.png/

Uploaded with ImageShack.us
 

bharatk8

Active Member
#2
_SECTION_BEGIN( "USING A TRADE COMPOSITE" );
function DeleteComposite( CompositeName )
{
global Ticker;
oAB = CreateObject( "Broker.Application" );
oStocks = oAB.Stocks();
oStocks.Remove( CompositeName );
oAB.RefreshAll();
}

function AddTradeToComposite( Ticker, Action, LMTPrice )
{
global Ticker;
BI = BarIndex();
LBI = LastValue( BI );

if ( Action != "" )
{
SignalArray = Nz( Foreign( Ticker + "~SignalArrays", "V", False ) );
BuyPriceArray = Nz( Foreign( Ticker + "~SignalArrays", "O", False ) );
SellPriceArray = Nz( Foreign( Ticker + "~SignalArrays", "H", False ) );
ShortPriceArray = Nz( Foreign( Ticker + "~SignalArrays", "L", False ) );
CoverPriceArray = Nz( Foreign( Ticker + "~SignalArrays", "C", False ) );

switch ( Action )
{

case "BUY":
SignalArray[LBI] = SignalArray[LBI] | 1;
BuyPriceArray[LBI] = LMTPrice;
break;

case "SELL":
SignalArray[LBI] = SignalArray[LBI] | 2;
SellPriceArray[LBI] = LMTPrice;
break;

case "SHORT":
SignalArray[LBI] = SignalArray[LBI] | 4;
ShortPriceArray[LBI] = LMTPrice;
break;

case "COVER":
SignalArray[LBI] = SignalArray[LBI] | 8;
CoverPriceArray[LBI] = LMTPrice;
break;

case "REVLONG":
SignalArray[LBI] = SignalArray[LBI] | 8 | 1;
CoverPriceArray[LBI] = BuyPriceArray[LBI] = LMTPrice;
break;

case "REVSHORT":
SignalArray[LBI] = SignalArray[LBI] | 2 | 4;
SellPriceArray[LBI] = ShortPriceArray[LBI] = LMTPrice;
break;
}

AddToComposite( SignalArray, Ticker + "~SignalArrays", "V", 7 | atcFlagEnableInIndicator );
AddToComposite( BuyPriceArray, Ticker + "~SignalArrays", "O", 7 | atcFlagEnableInIndicator );
AddToComposite( SellPriceArray, Ticker + "~SignalArrays", "H", 7 | atcFlagEnableInIndicator );
AddToComposite( ShortPriceArray, Ticker + "~SignalArrays", "L", 7 | atcFlagEnableInIndicator );
AddToComposite( CoverPriceArray, Ticker + "~SignalArrays", "C", 7 | atcFlagEnableInIndicator );
}
}

Ticker = Name();
Action = "";

if ( ParamTrigger( "Buy", "BUY" ) ) Action = "BUY";
if ( ParamTrigger( "Sell", "SELL" ) ) Action = "SELL";
if ( ParamTrigger( "Short", "SHORT" ) ) Action = "SHORT";
if ( ParamTrigger( "Cover", "COVER" ) ) Action = "COVER";
if ( ParamTrigger( "Reverse to Long", "REVLONG" ) ) Action = "REVLONG";
if ( ParamTrigger( "Reverse to Short", "REVSHORT" ) ) Action = "REVSHORT";
if ( ParamTrigger( "Delete Signal Compoiste", "DELETE" ) ) DeleteComposite( Ticker + "~SignalArrays" );
AddTradeToComposite( Ticker, Action, LastValue( Close ) );

RequestTimedRefresh( 0.1 );
if ( SetForeign( Ticker + "~SignalArrays" ) )
{
SignalArray = Nz( Foreign( Ticker + "~SignalArrays", "V", False ) );
Buy = IIf( SignalArray & 1, 1, 0 );
Sell = IIf( SignalArray & 2, 1, 0 );
Short = IIf( SignalArray & 4, 1, 0 );
Cover = IIf( SignalArray & 8, 1, 0 );

BuyPrice = Nz( Foreign( Ticker + "~SignalArrays", "O" ) );
SellPrice = Nz( Foreign( Ticker + "~SignalArrays", "H" ) );
ShortPrice = Nz( Foreign( Ticker + "~SignalArrays", "L" ) );
CoverPrice = Nz( Foreign( Ticker + "~SignalArrays", "C" ) );

PlotShapes( IIf( Buy, shapeSmallUpTriangle, shapeNone ), 5, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeSmallDownTriangle, shapeNone ), 4, 0, SellPrice, 0 );
PlotShapes( IIf( Short, shapeHollowDownTriangle, shapeNone ), 4, 0, ShortPrice, 0 );
PlotShapes( IIf( Cover, shapeHollowUpTriangle, shapeNone ), 5, 0, CoverPrice, 0 );

}
RestorePriceArrays();

Plot( C, "", 1, 128 );
_SECTION_END();
 
#6
Hi,

Many possibilities. Check one by one.
1. Are you using SAME FORMULA for chart as well as for Backtesting ? If not, change Formula File in AA Window (_Better 2.afl) to the file which is making up the chart.
2. Are you using SAME SETTINGS for Paramters in Chart as well as for Backtesting ? Verify this. Backtester Parameters settings can be set by clicking on Parameters button in AA.

To me, it appears that both AFLs / Parameters are not same. Because exit of Long trade (at 1PM) is not seen on chart at 1:15PM (although it shows up on backtest).

3. It seems that you are using builtin Stops provided by AmiBroker. Either that code is missing in the code you have given here OR you have specified the stops under Stops tab in AA>>Settings>>Stops>>Maximum Loss Stop. A lot many possibilities open up with this as I am not sure what setting you used here for Exit.

Also - see ApplyStop() help here : http://tradinganalysis.co.in/AmiBroker_Help/index.html?applystop.html . It is also not clear which backtest mode was used as the results will vary based on backtest mode + apply stop exit price combinations.

A sidenote : I have not gone through your code minutely but I am doubtful if you can achieve following in real-life scenario.

SellPrice = Nz( Foreign( Ticker + "~SignalArrays", "H" ) );
ShortPrice = Nz( Foreign( Ticker + "~SignalArrays", "L" ) );

Hope this helps.

Thanks,
CA


bumping...............
 

Similar threads