Improving MAE

Posted By: MatPed

Improving MAE - 01/21/16 15:03

Hi, I have a profitable Algos that has a relevant difference between max dd and MAE
Max drawdown -2373$ 9% (MAE -12567$ 48%)

My understanding is that the trade management is not good. I'd like to investigate and improve it, but I do not know how to move forward.

Any Idea? Thank you
Posted By: Veratyr

Re: Improving MAE - 01/21/16 22:54

I'm similarly new to algo trading of forex so I can't say I can help that much but I think this depends a lot on what's causing that loss.

If it's individual trades having huge losses, I'd say tighten your stop loss. If you're using a trend following algo, http://www.financial-hacker.com/the-market-meanness-index/ might be useful.
Posted By: jcl

Re: Improving MAE - 01/22/16 14:51

Find out first if the problem comes from only a few extreme trades, or from many trades. Problems in trade management can be examined by plotting price profiles of the trades.
Posted By: MatPed

Re: Improving MAE - 01/22/16 16:11

@jcl, thank you, what do you mean by plotting the price profile I will copy the algo, its an evolution of your code, so its not a secret... laugh I would like your help in order to improve my analysis capabilities.

@Veratyr, thank you for your post. The Algo was taken from the blog you have referred wuth minor changes.


function TFM()
{
/* Annual return 245%
Profit factor 4.23 (PRR 3.61)
Sharpe ratio 0.88
Kelly criterion 0.31
R2 coefficient 0.808
Ulcer index 10.0%
Prediction error 36%
*/
algo("TFM");
var mmiPeriod = optimize(100, 25, 600, 25 );
var lpPeriod = optimize( 90, 30, 150, 6 );
Stop = optimize(4, 2, 8, 0.5) * ATR(100);

var stopPip = Stop / PIP;
var minPip = 10;
Trail = stopPip * minPip;
TrailSlope = (minPip * 100) / stopPip;
TrailLock = optimize (0, 0, 75, 5);

vars Price = series(price());
vars Trend = series(LowPass(Price, lpPeriod));
vars MMI_Raw = series(MMI(Price, mmiPeriod));
vars MMI_Smooth = series(LowPass(MMI_Raw, mmiPeriod));

if(Train && ParCycle <= 2) {
Stop = 4 * ATR(100);
TakeProfit = 8 * ATR(100);
Trail = 0;
TrailSlope = 100;
TrailLock = 0;
}

if(falling(MMI_Smooth)) {
if(valley(Trend)) {
enterLong();
}
else if(peak(Trend)) {
enterShort();
}
}
}
Posted By: MatPed

Re: Improving MAE - 01/26/16 16:40

Ok,
the above script has the following result:

Max draw-down -1670$ 4% (MAE -13289$ 35%)

The the MAE distribution is attached it seems that few trades generate the issue. What do you recommend?

Attached picture MAE.png
Posted By: Finstratech

Re: Improving MAE - 01/30/16 21:31

Cut off stop loss at a maximum of -500, or even -400 perhaps.
Posted By: Sphin

Re: Improving MAE - 01/30/16 23:25

To avoid the trades with the highest losses stop any trade if TradeMAE > 210. Above there is only little chance to get a profitable trade with relatively little profit but a high chance to get a sometimes significant loss.
Posted By: MatPed

Re: Improving MAE - 01/31/16 12:29

Thank you, I'll give it a try and keep you posted. Ciao
Posted By: MatPed

Re: Improving MAE - 01/31/16 13:28

Defined the following TMF, but it does not work ie it does not block the trade at the required level. I have used just onw asseet for testing. Still trying...

int maxMAE(var maxPIP){
if (abs(TradeMAE) > maxPIP*PIP) return (1);
else return(0);
}

...
enterLong(maxMAE, 250);
enterShort(maxMAE, 250);
...




Attached picture Untitled.png
Posted By: Sphin

Re: Improving MAE - 04/30/16 14:12

Did you make any progress here? I have the same problem using the routine
Code:
if (TradeMAE/PIP > 90)
		return 1;
	else
		return 0;


in a TMF expecting a plotMAEGraph with many trades ending around 90 but many trades go far beyond this border.
Posted By: MatPed

Re: Improving MAE - 04/30/16 22:37

yes, I have the same issue.
Posted By: Mithrandir77

Re: Improving MAE - 05/01/16 05:29

Matped, what period did you use to train? How many wfo cycles did you use? I ask you because I tried to reproduce your figures in order to help you but I got nowhere near, I used this script:

Code:
function TFM()
{
	/* Annual return 245%
	Profit factor 4.23 (PRR 3.61)
	Sharpe ratio 0.88
	Kelly criterion 0.31
	R2 coefficient 0.808
	Ulcer index 10.0%
	Prediction error 36%
	*/
	algo("TFM");
	var mmiPeriod = optimize(100, 25, 600, 25 );
	var lpPeriod = optimize( 90, 30, 150, 6 );
	Stop = optimize(4, 2, 8, 0.5) * ATR(100);
	
	var stopPip = Stop / PIP;
	var minPip = 10;
	Trail = stopPip * minPip;
	TrailSlope = (minPip * 100) / stopPip;
	TrailLock = optimize (0, 0, 75, 5);
	
	vars Price = series(price());
	vars Trend = series(LowPass(Price, lpPeriod));
	vars MMI_Raw = series(MMI(Price, mmiPeriod));
	vars MMI_Smooth = series(LowPass(MMI_Raw, mmiPeriod));
	
	if(Train && ParCycle <= 2) {
		Stop = 4 * ATR(100);
		TakeProfit = 8 * ATR(100);
		Trail = 0;
		TrailSlope = 100;
		TrailLock = 0;
	}
	
	if(falling(MMI_Smooth)) {
		if(valley(Trend)) {
			enterLong();
		}
		else if(peak(Trend)) {
			enterShort();
		}
	}
}

function run(){
	set(PARAMETERS);
	NumWFOCycles = 10;
	LookBack = 600;
	TFM();
	//plotMAEGraph(100);
}



And after I trained (Zorro 1.44) it produced this result:

Code:
Walk-Forward Test TFM EUR/USD

Simulated account   AssetsFix 
Bar period          1 hour (avg 86 min)
Simulation period   07.02.2011-26.04.2016 (31591 bars)
Test period         11.02.2013-26.04.2016 (19386 bars)
Lookback period     600 bars (5 weeks)
WFO test cycles     9 x 2154 bars (18 weeks)
Training cycles     10 x 12205 bars (105 weeks)
Monte Carlo cycles  200
Assumed slippage    10.0 sec
Spread              0.5 pips (roll -0.02/0.01)
Commission          0.60
Contracts per lot   1000.0

Gross win/loss      719$ / -952$ (-2675p)
Average profit      -73$/year, -6.06$/month, -0.28$/day
Max drawdown        -238$ -102% (MAE -245$ -105%)
Total down time     97% (TAE 66%)
Max down time       163 weeks from Mar 2013
Max open margin     40$
Max open risk       35$
Trade volume        493516$ (154060$/year)
Transaction costs   -20$ spr, -2.37$ slp, -0.49$ rol, -27$ com
Capital required    271$

Number of trades    456 (143/year, 3/week, 1/day)
Percent winning     22.8%
Max win/loss        47$ / -17$
Avg trade profit    -0.51$ -5.9p (+79.4p / -31.1p)
Avg trade slippage  -0.01$ -0.1p (+0.9p / -0.3p)
Avg trade bars      30 (+75 / -17)
Max trade bars      376 (22 days)
Time in market      72%
Max open trades     4
Max loss streak     19 (uncorrelated 26)

Annual return       -27%
Profit factor       0.76 (PRR 0.65)
Sharpe ratio        -0.87
Kelly criterion     -2.86
R2 coefficient      0.419
Ulcer index         100.0%
Prediction error    56%

Confidence level     AR   DDMax  Capital

 10%                -40%   147$  182$
 20%                -36%   166$  201$
 30%                -33%   189$  223$
 40%                -29%   216$  249$
 50%                -26%   245$  277$
 60%                -24%   277$  308$
 70%                -21%   313$  343$
 80%                -19%   351$  380$
 90%                -16%   415$  442$
 95%                -14%   482$  506$
100%                -11%   613$  633$

Portfolio analysis  OptF  ProF  Win/Loss  Wgt%  Cycles

EUR/USD:TFM         .000  0.76  104/352  100.0  \\X\XX\X\
EUR/USD:TFM:L       .000  0.63   49/186   89.2  \\/\\\\/\
EUR/USD:TFM:S       .000  0.94   55/166   10.8  \\\\//\\\



Anyway, looking at the manual about TradeMAE it says 'Maximum adverse excursion, the maximum price movement in adverse direction of the trade. Only valid when the trade is open. TradeMAE*TradeUnits is the highest loss of the trade while it was open (aside from trading costs). '

So maybe use

Code:
int maxMAE(var maxPIP){
    if (TradeIsOpen and TradeMAE*TradeUnits > maxPIP*PIP) 
        return 1;
    else 
        return 0;
}

Posted By: MatPed

Re: Improving MAE - 05/01/16 08:58

Thank you.
You are really digging into this!
Attacched all the files I am use for experimenting. Keep in mind I am not a coding pro and I'm facing several difficulties in improving my Zorro's knowledge.
The workshop provided with the platform are great, but are just the top of the iceberg laugh

Ciao

Attached File
FatStrategy.zip  (156 downloads)
Posted By: Mithrandir77

Re: Improving MAE - 05/01/16 22:54

Originally Posted By: MatPed
Thank you.
You are really digging into this!
Attacched all the files I am use for experimenting. Keep in mind I am not a coding pro and I'm facing several difficulties in improving my Zorro's knowledge.
The workshop provided with the platform are great, but are just the top of the iceberg laugh

Ciao


No problem mate! But I downloaded the 2016 and 2015 history from FXCM and was about to train your strategy when I got:

Code:
Zorro 1.44.0 Trading Automaton
Made with Gamestudio by oP group 2015


FatStrategy compiling..............
Error in 'line 13: 
Include file strategy\fatstrategy\aunger.c not found
< #include "Strategy\FatStrategy\AUnger.c"
 >..
Error in 'line 26: 
'ptnBaseSet' undeclared identifier
< 	ptnBaseSet();
 >.



It seems you forgot to include the 'AUnger.c' file where ptnBaseSet() is defined, can you upload it? Or tell me if I can safely comment ptnBaseSet() without effecting the result.

In addition, I see that you defined
Code:
#define ASSETLOOP "AUD/USD", "EUR/USD", "GBP/USD", "GER30", "NAS100", "SPX500", "UK100", "US30", "USD/CAD", "USD/CHF", "USD/JPY", "XAG/USD", "XAU/USD" // ALL z12



I recall reading in the forum to start a loop with the asset with the most complete history and that asset is EUR/USD, maybe it wouldn't hurt to change that.
Posted By: Mithrandir77

Re: Improving MAE - 05/03/16 02:31

I got really curious and think I nailed it. The problem was with the TICKS flag: when you don't set the ticks flag, in the simulation the TMF is called once per bar, so when using the default BarPeriod of 60 minutes for instance, you are evaluating the exit condition every 60 minutes, which isn't very 'granular' enough to stop the MAE from jumping past your limit!

So, try

Code:
set(TICKS);



at the beginning of the run() function and use as TMF (for instance if your objective MAE is 300 pips):

Code:
int maxMAE(){
   //return 0;
	
   if (TradeIsOpen and TradeMAE/PIP >= 300){
       printf("\nExit at %f MAE",(var)(TradeMAE/PIP));
       return 1;
   }
   else
       return 0;
}



Look at what is printed in the Zorro console when you don't use set(TICKS) and when you do. You should see the trades exited more closely to the limit you whant when you use TICKS flag than when you don't.

In my case, I tested with an script similar to Workshop 6, it has BarPeriod=60 and functions with TimeFrame=1 and TimeFrame=4. The data I have are m1 candles from FXCM.

PlotMAEGraph(-15) without setting the TICKS flag:



PlotMAEGraph(-15) setting TICKS flag:



The amount of losing trades lowered, anyway there are trades with 345 MAE although I wanted to stop them at 300...I guess that in a minute -with my m1 FXCM data, 1 tick=1 minute, remember Zorro glosary
Click to reveal..
Tick - event when a new price arrives and is evaluated by the trading software. Either triggered by receiving a price quote in live trading, or by reading the next price out of historical data in the simulation. Ticks in historical data files are stored with a time stamp and a price info. For keeping the files at a reasonable size, price quotes are normally combined to one tick per minute (M1 data), but for special purposes data containing all real price ticks (T1 data) can also be used.

- the trade bounced 45 pips, what intrigues me is that in my script I didn't define Lots -nor Margin nor Risk nor Capital- so the trade should use 1 lot...Is it plausible that in 1 minute 1 (FXCM micro)lot -I read AssetsFix.csv and it says LotAmount=1000 for EUR/USD- bounces 45 pips? Maybe someone else (jcl laugh ) can shed some light...

Also, another question that I have is that since TradeMAE is the MAE of a 'trade', the more Lots or Margin or Risk you assign to a trade the less sensitive it is going to be to changes in prices, isn't it? Say:

trader1 is long 1 lot in EUR/USD
trader2 is long in one trade with 2 lots of EUR/USD

Both trades are at 178 pips loss,the maxMAE is 180, EUR/USD drops 5 pips in a tick

trader1 is going to closehis/her 1 lot trade at 178+5=183 pips loss (3 pips more than the barrier)

trader2 is going to close his/her 2 lots trade at 178+2*5=188
pips loss (8 pips more than the barrier!)

Is there a way to make the TMF stop a trade with an accuracy independent of its trade size? I can see that it has something to do with TradeLots and PipCost or PIP but I haven't been able to derive it so far...
Posted By: MatPed

Re: Improving MAE - 05/03/16 06:19

A lot of things to work with.
Thank you. I am in a business trip. I will work on this during the weekend frown.
I'll be back!

Ciao
Posted By: MatPed

Re: Improving MAE - 05/07/16 14:33

Ok I have tested your function and something happens...
Regarding the ticks flag. I trade the strategy in normal 1h mode and test in the ticks mode.

As you mentioned the calculations seems correct, even some trade are slightly over the limit set. Unfortunately the MAE graph shows a totally different picture.
I can see only 2 Options:
- the trade are not stopped properly
- the MAE graph is incorrect.

The TradeMAE is price difference, accordingly with the manual. the TradeMAE should be size independent.

I will work on a TMF that will close the trade at a specific pip loss and see what happen should be the same than set Stop = 200* Pip;

ciao
© 2024 lite-C Forums