Different exit & entry prices in Backtest vs Exploration

When I run the formula below, the entry and exit prices are different when I run a Backtest vs an Exploration. I'm making progress slowly with Amibroker and things like this make me realize how little I know.

Can somebody explain why the exit and entry prices are different?

Both tests done with: 1sec database, 5-min Periodicity,

I'm using the 5-min TimeFrameSet for other things not related to this particular question. It doesn't impact the backtest/exploration results so I just left it in.
Also... I understand that ExRem is not recommended in Backtesting, I ran the backtest with and without ExRem but it didn't make any difference in the outcome.

myTimeFrame = in5Minute;


MA1 = 106 ;
MA2 = 16 ;
// Optimize( "description", default, min , max, step )
//MA1 = Optimize("MA1",1,1,120,5);
//MA2 = Optimize("MA2",1,1,25,5);

    ema1 = EMA( O, MA1 );
    ema2 = EMA( O, MA2 );

Buy = Cross( TimeFrameExpand(ema1, myTimeFrame, expandLast), TimeFrameExpand(ema2, myTimeFrame, expandLast) );
Sell = Cross( TimeFrameExpand(ema2, myTimeFrame, expandLast), TimeFrameExpand(ema1, myTimeFrame, expandLast) );
Short = Sell;
Cover = Buy;

Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
Short = ExRem( Short, Cover );
Cover = ExRem( Cover, Short );

Filter = (Buy OR Sell);
//Filter = 1;
AddColumn(Open, "Open", 1.2);
AddColumn(Buy, "Buy", 1.2);    
AddColumn(Sell, "Sell", 1.2); 
AddColumn(Cover, "Cover", 1.2);    
AddColumn(Short, "Short", 1.2); 
AddColumn(BarIndex(), "BarIndex");

Result of Backtest

Result of Exploration

Where are you setting your entry and exit prices, i.e. BuyPrice, SellPrice, ShortPrice, CoverPrice? If you're not setting them in your code, then they will be controlled with the values in the Trades tab of the Analysis Settings.

Also, your Exploration is showing the Open price. I haven't tested your code, but you probably can't trade at the open if you're waiting for MA's to cross at the end of the bar. AmiBroker will let you specify this arrangement, but it's untradeable in real life. In most cases, you would only trade using the Open price if you were using trade delays or using another mechanism to generate your signals on the previous bar.

1 Like

Thanks for the feedback Matt. So I think the entry and exits in this formula are defined by the MA1 and MA2 crossovers and the 5-min periodicity. I expected the crossover would be the same in the Exploration and the Backtest. That is probably just showing my ignorance.

You make a good point about Open vs Close. I was experimenting with SetTradeDelays and was using 1,1,1,1 with the Open. Forgot to change it back to Close when changed SetTradeDelays back to 0,0,0,0.

That said, after changing 'Open' back to 'Close', there is still a significant difference in the exit/entry prices when running Backtest vs Exploration. I assume that there is an explanation for the difference and I just don't understand it.

It has been said several times in forum already that Exploration is not backtest and that exploration outputs raw values/signals while backtest outputs trades.

Also SetTradeDelays function and many fields of SetOption function (e.g. "MaxOpenPositions", "ReverseSignalForcesExit" and so on) work in backtest only. SetPositionSize is backtest setting also. Also Backtest may ignore trade if you have insufficient funds or stop trading because of "being broke" in simulation.

So you compare apples to oranges.

In addtion... rounding difference. In exploration you have set to two decimal places output (while there exist prices with ..., 3,4 decimal places in your BT result list).


Well... that all makes me feel kinda dumb! I spent a lot of time trying to understand this so it's not that I'm lazy.

Anyway, many thanks. This will really help me make some more progress and not be second-guessing results between Explorations and Backtests.

Another question... I've been able to run some automated trades with charts, but I'd like to experiment with automated trading of a portfolio of stocks. As a first step, is it typically better to look at auto-trading with Explorations, or would it be better to look into CBT?

Thanks again.