Gamestudio Links
Zorro Links
Newest Posts
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Data from CSV not parsed correctly
by EternallyCurious. 04/20/24 21:39
M1 Oversampling
by 11honza11. 04/20/24 20:57
Scripts not found
by juergen_wue. 04/20/24 18:51
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
folder management functions
by VoroneTZ. 04/17/24 06:52
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (rki, AndrewAMD), 407 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Zorro closes trades at prices outside of the range of the bar #479004
02/03/20 11:54
02/03/20 11:54
Joined: Feb 2020
Posts: 2
T
Taarn Offline OP
Guest
Taarn  Offline OP
Guest
T

Joined: Feb 2020
Posts: 2
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
Re: Zorro closes trades at prices outside of the range of the bar [Re: Taarn] #479017
02/05/20 10:02
02/05/20 10:02
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
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.



Re: Zorro closes trades at prices outside of the range of the bar [Re: Taarn] #479026
02/06/20 12:15
02/06/20 12:15
Joined: Feb 2020
Posts: 2
T
Taarn Offline OP
Guest
Taarn  Offline OP
Guest
T

Joined: Feb 2020
Posts: 2
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 Files MU2.png
Re: Zorro closes trades at prices outside of the range of the bar [Re: Taarn] #479044
02/08/20 09:07
02/08/20 09:07
Joined: Apr 2008
Posts: 585
Austria
Petra Offline
Support
Petra  Offline
Support

Joined: Apr 2008
Posts: 585
Austria
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.


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1