As an exercise, I decided to code a tutorial strategy MACO from Omega Research System Trading and Development Club (STAD01.pdf). I would appreciate your critique.
Especially inetersting thing was using time-limited stop orders for entering and also RE-entering trades, while ensuring at the same time "Hedge=0" behaviour.

Task (position sizing ommited, factors adjusted for forex):
Quote:
Long and short entries reverse your position, whereas the exits will close out your existing position and exit you from the market.
===Long Entries:
a) If the fast-moving average (9 bars) crosses over the slow-moving average (18 bars), find the highest high price of the last 12 bars and multiply it by 1.0003. This is the long entry price.
b) Once a cross over occurs, place a buy stop order at the long entry price and keep the order active for the 12 bars after the cross over.
c) If the system is in a long position and exited by any of the exit rules, place a buy stop order at the highest high price of the last 10 bars and keep the order active for 15 bars after the exit.
===Short Entries
d) If the fast-moving average crosses under the slow-moving average, find the lowest low price of the last 12 bars and multiply it by .9997. This is the short entry price.
e) Once the cross over occurs, place a sell stop order at the short entry price and keep the order active for the 12 bars after the cross over.
f) If the system is in a short position and this position was covered (exited), place a sell stop order at the lowest low price of the last 10 bars and keep the order active for 15 bars after the exit.
===Exits
g) Exit from any long position if the lowest low price of the last 8 bars is reached.
h) Exit from any short position if the highest high price of the last 8 bars is reached.


My solution:
Code:
function run()
{
  var FastLen = 9;
  var SlowLen = 18;
  var ChLen = 12;
  var TrailBar = 8;
  var ReBars = 15;
  var ReEnt = 10;

  vars Price = series(priceClose());
  vars FastMA = series(SMA(Price, FastLen));
  vars SlowMA = series(SMA(Price, SlowLen));
  static int Dir;
  bool prn = false; 
  
  set(LOGFILE);
  if(is(INITRUN)) {
    Dir = 0;
  }
  Hedge = 0;
  if (crossOver(FastMA, SlowMA) && (NumOpenLong == 0) && (NumPendingLong == 0)) {
    EntryTime = ChLen;
    enterLong(0, HH(ChLen)*1.0003, LL(TrailBar)); 
  }
  if (crossUnder(FastMA, SlowMA) && (NumOpenShort == 0) && (NumPendingShort == 0)) {
    EntryTime = ChLen;
    enterShort(0, LL(ChLen)*0.9997, HH(TrailBar));
  }
		
  for(open_trades) {
    if (TradeIsLong && TradeIsOpen) {
    exitShort(); //discard pending trades
    TradeStopLimit = LL(TrailBar);
    }
    if (TradeIsShort && TradeIsOpen) {
      exitLong(); //discard pending trades
      TradeStopLimit = HH(TrailBar);
    }
  } 
	
  if (NumOpenLong > 0) {
    Dir = 2;
  }
  if (NumOpenShort > 0) {
    Dir = -2;
  }
	
  if (NumOpenTotal+NumPendingTotal==0) {
    if (Dir > 1) {
      if (prn) printf("\nbeing flat after long, decided to reenter long at %f", HH(ReEnt));
      Dir = 1;
      EntryTime = ReBars;
      enterLong(0, HH(ReEnt), LL(TrailBar));
    }
    if (Dir < -1) {
      if (prn) printf("\nbeing flat after short, decided to reenter short at %f", LL(ReEnt));
      Dir = -1;
      EntryTime = ReBars;
      enterShort(0, LL(ReEnt), HH(TrailBar));
    }
  }	
}