Hello,

I have encountered several situations where Zorro closes trades outside the range of the bar on which the trade is closed, I have attached two examples of this. The first is a situation with a gap and is generated with the code below. The second situation has no gap, but the take profit is changed immediately before the bar and is generated by changing the 0 to a 1 in line 65 of the code below. In both cases I would have expected the trade to be closed at the opening price minus slippage. Is there a good way to get Zorro to close such trades at the opening price minus slippage? I feel that I need to use a trade management function to prevent erroneous exits on the bar that the entry occurs on.

Thanks in advance smile

Code
#include <profile.c>
#include "MsjLib.c"

void exittTrade() {
	if (TradeIsLong)
		exitLong();
	else if (TradeIsShort)
		exitShort();
}

int msj180Tmf(var DetailMode, var Sl, var Tp) {
	if (!TradeIsPending) {
		TradeStopLimit=Sl;
		TradeProfitLimit=Tp;
	}
	return 0;
}

	function run()
{
	StartDate = 20170411;//20171001;
	EndDate = 20181113;//20190630; // fixed simulation period 
	BarPeriod = 1440;
	LookBack = 160;
	Verbose = 2;
	set(LOGFILE,PLOTNOW); // log all trades

	assetList("AssetsSP250y.csv");
	while(asset(loop(Assets))) {
		var PositionSize = 50000; // The size of a position in USD
		var AccountSize = 5000; // The size of a position in USD
		vars Closes = series(priceClose());
		vars Opens = series(priceOpen());
		vars Lows = series(priceLow());
		vars Highs = series(priceHigh());
		vars TLine = series(EMA(Closes,8));
		vars Ema20 = series(EMA(Closes,20));
		msjChart Chart;
		Chart.O=Opens; Chart.H=Highs; Chart.L=Lows; Chart.C=Closes;

		Fill=2;
		int AssetHasOpenTrade=0;
		if(is(INITRUN)) { //(AssetBar==0)//(BarOffset==1)
			if(0) printf("%s: bar=%d, x=%f\n", Symbol, AssetBar, priceClose());
		} else {
			for (current_trades) {
				if (TradeIsPending /* && Lows[0]<=Lows[1] */) {
					exittTrade();
				} else if (!TradeIsClosed) {
					AssetHasOpenTrade=1;
				}
			}
		}

		if (1 && !AssetHasOpenTrade && entryCondition(&Chart, 0) && TLine[0]>1.03*Ema20[0]) {
			var entryPrice=Highs[0]+PIP;
			var stop=Lows[0]-PIP;
			var trail=0;//entryPrice-stop;
			var rr=1; // reward/risk
			var target=(1+rr)*entryPrice-rr*stop;
			int numShares = (int)min(PositionSize/entryPrice, 0.02*AccountSize/(entryPrice-stop));
			Stop=0;//stop;
			TakeProfit=0;//target;
			Lots=numShares;
			if (0) {
				Entry=-Highs[0]-PIP;
				enterShort(msj180Tmf,3,target,stop);
			} else {
				Entry=Highs[0]+PIP;
				enterLong(msj180Tmf,3,stop,target);
			}
		}
	}
}

'

Attached Files MU.pngFosl.png