Simple mean reversion trading system that closes trades not correctly

Hi folks,

I came across inconsistencies when testing a simple mean reversion system. The system should buy positions at the opening price the next day when the RSI falls below the 20 level. If the RSI rises above the sell level, all positions will be sold on the same day at the closing price.

As shown in the chart and the backtest window it works sometimes and sometimes not as expected. In the case of 4th December 2015, the trade works properly. But the trade of 15th December 2015 will be closed one day later. How should be added in the code so that the position of 15th December is closed on the same day.

SetBacktestMode( backtestRegularRawMulti );
//buy next day, sell on the current day
SetTradeDelays( 1, 0, 0, 0 );
SetOption( "MaxOpenPositions", 10 );
SetOption( "InitialEquity", 100000 );
SetPositionSize( 10000, spsValue );
SetOption( "AllowSameBarExit", True );

//buy next day market on open if signal occurs
BuyPrice = O;
entrylevel = 20;
//sell on closing price at the same day when rsi rises over the exitlevel
SellPrice = C;
exitlevel = 70;

r = RSI( 2 );

Buy = r < entrylevel;
Sell = r > exitlevel;

Plot( entrylevel, "Entry", colorGreen, styleLine );
Plot( exitlevel, "Exit", colorRed, styleLine );
Plot( r, "RSI", colorBlack, styleLine );

Chart%20small Tradelist%20small

It appears to me that you have overlapping trades on December 15th: the first trade opened on December 14th and closed on the 15th, the second trade opened on the 15th and closed on the 16th. That's the problem that you need to solve.

Plus you really do NOT want to use backtestRegularRawMulti mode for single stock. For single stock you should be using regular mode instead. If you want to pyramid, you should be using

1 Like

Thank you for your prompt reply,

I compared the results with my code (with backtestRegularRawMulti) and with pyramidization (with backtestRegular) and the result is the same (see screenshots below).
I would use a reverse RSI calculator with which I calculate the profit target after the entry into a position. As formulated in the simple rules, the position opened on 15 December should be closed to close price on the same day.

Because the RSI was on December 14th at 2.6 -> it will be bought the next day for Market on Open. On December 15, the RSI was at 74.42. That’s above the sales level. Accordingly, the position should be sold on 15 December at the closing price.

With which lines in the code can I force the backtest to close the position on the 15th.

Refer to first reply "How do I debug my formula" and use techniques presented in that post to find problems with your code/settings/data. It is your code, your computer and your data. We don't sit behind your computer. We don't debug users code. It is your task. Use Detailed log and Exploration and other tools. It is you who needs to understand what happens in your own code. All is explained in "How do I debug my formula". Use exploration (add a couple of AddColumn() calls), not charts. Charts may differ from analysis for example if you have PAD AND ALIGN turned on in Analysis and holes in data. Again use advice given in "How do I debug my formula". Everything is there already.