Exit on same bar issue


I am backtesting a simple gap up strategy where I go long, if open > yesterday's close.
and exit when close > open.

gap = Open > Ref(close,-1);
pricefilter = Open > MA(open,10); // optional filter

Buy = gap;
BuyPrice = Open;
Sell = Close > Open;
SellPrice = Close;

Code itself works fine, but I noticed that some trades aren't being closed when condition is met, instead, they are executed on the next bar.

on 25th April $PEP opened at 186.5 and closed at $189.71, it should have been closed, but instead, it got closed on the next day.

I am using TC2000 EOD data.


Please read “RESOLVING SAME BAR, SAME SYMBOL SIGNAL CONFLICTS” part of the Tutorial on backtesting


Thank you,

I did follow the instructions and I and did add: SetOption("AllowSameBarExit", True ); rule, but I am still getting an exit on the next bar.

It's actually weird, some bars, even before adding the function, it did exit on the same bar and some it didn't....

SetChartBkColor(colorwhite); // color of outer border
SetChartBkGradientFill(colorlightorange,colorPaleGreen,colorBlack); // color of inner panel
Plot( C, "Portfolio Equity", ColorBlend( colorBrightGreen, colorBlack ), styleGradient | styleLine, Null, Null, 0, -1 );

Buy = 1;
Sell = 1;

AddColumn(Buy,"Buy", 1.0 );
AddColumn(Sell, "Sell", 1.0 );
Filter = Buy OR Sell;

gap = Open > Ref(close,-1);
pricefilter = Open > MA(open,10); // optional filter

SetOption("AllowSameBarExit", True );
SetOption("HoldMinBars", 0);

Buy = Open > Ref(close,-1);
BuyPrice = Open;
Sell = Close > Open;
SellPrice = Close;

in portolio settings menu, I have enabled "allow same bar entry/exit signal"

Hope this helps:

Did you run the code from THE MANUAL?

To exit same bar, you have to have ZERO previously open positions at the beginning of that bar, so position can be open and closed the same bar.
If position is already open, it will just be closed.

Debugging your formula is your task.

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

ok, exRem function worked here.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.