Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (Imhotep, opm), 785 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
StopLoss not working? #461872
08/26/16 17:07
08/26/16 17:07
Joined: Aug 2016
Posts: 95
Wien
T
trenki2 Offline OP
Junior Member
trenki2  Offline OP
Junior Member
T

Joined: Aug 2016
Posts: 95
Wien
I experimented with the MAE Graph and set up some trades with fixed Stop and TakeProfit values and noticed in the graph some outlier trades.

Some trade was closed beyound its set stop loss value which seemed odd. I verified this by iterating over all trades and calculating the max loss in pips. It was -300pips while the Stop was set to 100pips.

The following code reproduces this:

Code:
#include <profile.c>

function run()
{
	// Output:
	// 
	// MaxMAE: 456.152495
	// MaxMFE: 296.190463
	// MinTradeResult: -303.420043
	// MaxTradeResult: 99.170744
	//   Step: 10
	// Monte Carlo Analysis... Median AR 24%
	// Profit 52$  MI 2$  DD 94$  Capital 121$
	// Trades 176  Win 52.8%  Avg +3.4p  Bars 2
	// AR 20%  PF 1.07  SR 0.34  UI 40%  R2 0.16
	
	set(LOGFILE);
	
	BarPeriod = 1440;
	LookBack = 1000;
	StartDate = 2010;
	EndDate = 2015;
	
	asset("EUR/USD");

	Stop = TakeProfit = 100 * PIP;

	if (NumOpenTotal == 0)
	{
		if (Bar % 2 == 1)
			enterLong();
		else
			enterShort();
	}
	
	if (is(EXITRUN))
	{
		var vMinTradeResult = 0.0;
		var vMaxTradeResult = 0.0;
		
		var vMaxMAE = 0.0;
		for (all_trades)
			vMaxMAE = max(vMaxMAE, TradeMAE/PIP);
			
		var vMaxMFE = 0.0;
		for (all_trades)
			vMaxMFE = max(vMaxMFE, TradeMFE/PIP);
			
		for (all_trades)
		{
			vMinTradeResult = min(vMinTradeResult, toPIP(TradeResult)); 	
			vMaxTradeResult = max(vMaxTradeResult, toPIP(TradeResult));
		}
			
		printf("\nMaxMAE: %f", vMaxMAE);
		printf("\nMaxMFE: %f", vMaxMFE);
		printf("\nMinTradeResult: %f", vMinTradeResult);
		printf("\nMaxTradeResult: %f", vMaxTradeResult);
		printf("\n");
	}
	
	plotMAEGraph(0);
}



I first suspected it might have been a weekend gap but the code closes all trades before the weekend. I also tried with the TICKS flag. That did not seem to help either.

Setting BarPeriod to 60 and TimeFrame to 24 resulted in a change but the trade was also stopped way past its set stop value.

Why does this happen?

Re: StopLoss not working? [Re: trenki2] #461875
08/27/16 07:20
08/27/16 07:20
Joined: Apr 2008
Posts: 585
Austria
Petra Offline
Support
Petra  Offline
Support

Joined: Apr 2008
Posts: 585
Austria
Youre not the first one who looked at the MAE graph and thinks the stop is not working, Stop Loss and MAE are easily confused. But they are something entirely different: http://www.opserver.de/ubb7/ubbthreads.p...true#Post455047

Re: StopLoss not working? [Re: Petra] #461878
08/27/16 08:04
08/27/16 08:04
Joined: Aug 2016
Posts: 95
Wien
T
trenki2 Offline OP
Junior Member
trenki2  Offline OP
Junior Member
T

Joined: Aug 2016
Posts: 95
Wien
That confused me initially too. I know that post. But in my code I did not compare the Stop value to the MAE but I calculated the max loss of any trade and it was a lot greater than stop value set.

Just look at how vMinTradeResult is calculated. vMinTradeResult is -300pips while I think it should not be less than -100pips as there is a stop set at 100pips.

The TakeProfit seems to work as there is no trade result with a profit of more than 100pips.

You can remove all the MAE specific code and then it is probably clearer:

Code:
#include <profile.c>

function run()
{
	// Output:
	// 
	// MinTradeResult: -303.420043
	// MaxTradeResult: 99.170744
	//   Step: 10
	// Monte Carlo Analysis... Median AR 24%
	// Profit 52$  MI 2$  DD 94$  Capital 121$
	// Trades 176  Win 52.8%  Avg +3.4p  Bars 2
	// AR 20%  PF 1.07  SR 0.34  UI 40%  R2 0.16
	
	set(LOGFILE);
	
	BarPeriod = 1440;
	LookBack = 1000;
	StartDate = 2010;
	EndDate = 2015;
	
	asset("EUR/USD");

	Stop = TakeProfit = 100 * PIP;

	if (NumOpenTotal == 0)
	{
		if (Bar % 2 == 1)
			enterLong();
		else
			enterShort();
	}
	
	if (is(EXITRUN))
	{
		var vMinTradeResult = 0.0;
		var vMaxTradeResult = 0.0;
		
		for (all_trades)
		{
			vMinTradeResult = min(vMinTradeResult, toPIP(TradeResult)); 	
			vMaxTradeResult = max(vMaxTradeResult, toPIP(TradeResult));
		}
			
		printf("\nMinTradeResult: %f", vMinTradeResult);
		printf("\nMaxTradeResult: %f", vMaxTradeResult);
		printf("\n");
	}
}


Last edited by trenki2; 08/27/16 08:14.
Re: StopLoss not working? [Re: trenki2] #461881
08/27/16 10:35
08/27/16 10:35
Joined: Apr 2008
Posts: 585
Austria
Petra Offline
Support
Petra  Offline
Support

Joined: Apr 2008
Posts: 585
Austria
What is "toPIP(TradeResult)" ? The manual says the profit of a trade is TradeProfit/TradeUnits/PIP.

Re: StopLoss not working? [Re: Petra] #461888
08/27/16 15:55
08/27/16 15:55
Joined: Aug 2016
Posts: 95
Wien
T
trenki2 Offline OP
Junior Member
trenki2  Offline OP
Junior Member
T

Joined: Aug 2016
Posts: 95
Wien
From profile.c:
Code:
// convert trade profit to pips
var toPIP(var x) { return x/TradeUnits/PIP; }



toPIP() is the function used in the source code of the plotMAEGraph() function to calculate the PIP value of the trade.

Last edited by trenki2; 08/27/16 15:55.
Re: StopLoss not working? [Re: trenki2] #461909
08/29/16 08:50
08/29/16 08:50
Joined: Jul 2000
Posts: 27,977
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,977
Frankfurt
Yes, the MinTradeResult with this script must indeed be -100 $. At least when I run your script:

MinTradeResult: -104.332444
MaxTradeResult: 101.864881

Profit -47$ MI -2$ DD 193$ Capital 238$
Trades 184 Win 48.9% Avg -3.0p Bars 2
AR -9% PF 0.94 SR -0.32 UI 89% R2 0.00

Look in the log for the trade that generated the -300$ in your test. This way you can quickly find the answer to such issues. Could be a large gap in the price data.


Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1