ONCE LONGlosses  = 0
ONCE SHORTlosses = 0
ONCE TradeLONG   = 1
ONCE TradeSHORT  = 1
ONCE Nbars       = 20    //Number of bars (time) after which trading in a directuion is resumed
ONCE BARtally    = 0
// when a direction is disabled, increment the BAR count, till it reaches Nbars to reenable that direction
IF (TradeLONG = 0) OR (TradeSHORT = 0) THEN
   BARtally = BARtally + 1
   IF BARtally >= Nbars THEN
      TradeLONG  = 1
      TradeSHORT = 1
      BARtally   = 0
   ENDIF
ENDIF
IF (StrategyProfit < StrategyProfit[1]) THEN
   // in case of a LOSS, increment the counter to tell when there are 2 consecutive losses in the same direction
   IF LongOnMarket[1] THEN
      LONGlosses  = LONGlosses + 1
      SHORTlosses = 0
      IF LONGlosses  = 2 THEN
         TradeLONG   = 0
         BARtally    = 0
      ENDIF
   ELSIF ShortOnMarket[1] THEN
      SHORTlosses = SHORTlosses + 1
      LONGlosses  = 0
      IF SHORTlosses = 2 THEN
         TradeSHORT  = 0
         BARtally    = 0
      ENDIF
   ENDIF
ELSIF (StrategyProfit > StrategyProfit[1]) THEN
   // in case of a WINNING trade, clear the consecutive LOSS count for both directions
   LONGlosses  = 0
   SHORTlosses = 0
ENDIF
// LONG  positions
IF Not OnMarket AND (close CROSSES OVER  average[20,0](close)) AND TradeLONG  THEN
   BUY 1 Contract at Market
   TradeSHORT  = 1
ENDIF
// SHORT positions
IF Not OnMarket AND (close CROSSES UNDER average[20,0](close)) AND TradeSHORT THEN
   SELLSHORT 1 Contract at Market
   TradeLONG   = 1
ENDIF
// SL & TP
SET STOP   %LOSS   1
SET TARGET %PROFIT 2
//     debugging aids
//graph LONGlosses  coloured("Blue")
//graph SHORTlosses coloured("Red")
//graph BARtally    coloured("Fuchsia")
//graph TradeLONG
//graph TradeSHORT