StopLoss not working?

Posted By: trenki2

StopLoss not working? - 08/26/16 17:07

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?
Posted By: Petra

Re: StopLoss not working? - 08/27/16 07:20

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
Posted By: trenki2

Re: StopLoss not working? - 08/27/16 08:04

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

Posted By: Petra

Re: StopLoss not working? - 08/27/16 10:35

What is "toPIP(TradeResult)" ? The manual says the profit of a trade is TradeProfit/TradeUnits/PIP.
Posted By: trenki2

Re: StopLoss not working? - 08/27/16 15:55

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.
Posted By: jcl

Re: StopLoss not working? - 08/29/16 08:50

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.
© 2024 lite-C Forums