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...

Last edited by Mithrandir77; 05/03/16 02:41. Reason: corrected example