Zorro closes trades at prices outside of the range of the bar

Posted By: Taarn

Zorro closes trades at prices outside of the range of the bar - 02/03/20 11:54

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);
			}
		}
	}
}

'


Description: MU long trade
Attached picture MU.png

Description: FOSL short trade
Attached picture Fosl.png
Posted By: jcl

Re: Zorro closes trades at prices outside of the range of the bar - 02/05/20 10:02

Hmm, I'm not sure that I understand your problem. Bars are time periods and have no "range" for the close price of your trade. Trades are normally closed at the current best bid or offer.

If something is unclear with close prices, look up "Fill", "Slippage", and "exitTrade" in the manual.


Posted By: Taarn

Re: Zorro closes trades at prices outside of the range of the bar - 02/06/20 12:15

Thank you for your answer. Put very simply, my problem is that Zorro closes trades at prices below the low of the candle (see the attached image) (the bar that I referred to is a bar or candle on a normal technical analysis price chart, so it also has an open, high, low and close. The range that I referred to is the range between the low and the high price).

I reread the manual pages that you referred to and made a simpler example to illustrate the problem, I have included the code below and attached an image of the result. Notice how the trade closes at a price below the low of the day on 2018-05-22. This only seems to happen for "take profit" orders and not for "stop loss" or "entry" orders (e.g. the entry on 2018-05-21 and the stop loss on 2018-05-31 are filled at the open of the day). From my understanding of the manual when Fill=3, "take profit" orders should also fill at the open of the day. Is this a bug in Zorro or have I misunderstood something?

Code
	function run()
{
	StartDate = 20180511;
	EndDate = 20180713;
	BarPeriod = 1440;
	LookBack = 160;
	set(LOGFILE,PLOTNOW);

	assetList("AssetsSP250y.csv");
	asset("MU");
	vars Lows = series(priceLow());
	vars Highs = series(priceHigh());

	Fill=3;
	PIP=0.01;

	if (Bar==200 || Bar==206) {
		var entryPrice=Highs[0]+PIP;
		var stop=Lows[0]-45*PIP;
		var rr=1.2; // reward/risk
		var target=(1+rr)*entryPrice-rr*stop;
		enterLong(1, entryPrice, stop, target);
	}
}


Attached picture MU2.png
Posted By: Petra

Re: Zorro closes trades at prices outside of the range of the bar - 02/08/20 09:07

In trading there is a thing called "Slippage", thats why the close price of a trade is not the same as the close of the candle, and can be of course higher or lower than the last candles high or low.

If you dont want to simulate the effect of slippage in the backtest, set Slippage = 0.
© 2024 lite-C Forums