• Información general
  • Comentarios
  • Discusión (4)


This Expert Advisor implements a trading system based on moving flat breakout. It monitors the market for a flat state and opens a new position upon breakout, then it can close it on a predefined profit, exit by a stop loss, or add more lots by flexible steps for averaging. Multiple work instruments can be used.

The flat state is detected as a specified number of consecutive bars, during which price remains within a reasonably small range. If one of the next several bars closes outside the range, the signal for corresponding trade is generated.

The Expert Advisor supports two modes: tick by tick trade and bar by bar trade. The latter means explicit control of bar opening, and it is used by default, as demonstrating best results. When in the bar by bar mode, there is no difference in performance between "every tick" and "open prices" testing, so the latter can be used for fast optimization without loss of accuracy.

It is strongly recommended to test the trading robot (as well as any other) while logged in a real account, because demo accounts may provide very poor quality of quotes (depends on broker). Results of testing and optimization on a demo account may be misleading.

Recommended timeframes - H1, M30, M15.

With default settings, the Expert Advisor provides decent results, but some instruments may require the settings adjusted. Examples on the screenshots below were obtained without any optimization except for flat range in points (GridPointsOptimization) for GBPUSD and AUDUSD.


Trading system settings:

  • Instruments - a comma separated list of work symbols, for example, "EURUSD,GBPUSD,USDCHF,USDJPY,AUDUSD,USDCAD" (without quotes, no spaces); if the parameter is empty, current symbol is treated as a work symbol; in the tester, this parameter is always ignored and current symbol is used;
  • GridPointsList - a comma separated list of grid steps used for corresponding symbols; for example, "300,700,300,300,200,300" (5-digits); each value is used as the range for flat detection AND as a distance between averaging orders; if only one number is specified, it is used for all work symbols; if the parameter is empty, default value 300 is used;
  • GridPointsOptimization - a number of points for grid step; this parameter is used during and for optimization ONLY, because you can not optimize GridPointsList as it is a string;
  • TakeAmount - amount of money (in account currency) that floating profit on a separate work symbol should reach for all trades on this symbol are closed ("take profit"); the parameter can hold a negative number, which is treated as a percent of current free margin; for example, default value is -0.01, that means 1% of free margin floating profit will trigger "take profit" exit;
  • DegradeByDay - a number that specify, which fraction of TakeAmount should be discarded on every next day of staying in the market; this is a penalty for long-lasting trades; if the parameter is not 0, the longer orders remain open the less profit you agree to get, up to the point that you can eventually get losses if a trade has stuck in the market; default value is 0.1, which means that every next day of a position life-time (from its very first order) 0.1 of TakeAmount will be deducted from TakeAmount, so after 10 days the position can exit with 0 profit, and after 20 days it can exit with the loss -TakeAmount; consider this as a kind of slow-motion stop loss;
  • FlatDetect - number of bars, during which price should stay in GridPoints range in order to discover flat state; default value - 7; the range is counted using highs and lows of FlatDetect bars;
  • BreakDetect - number of bars used to monitor breakout after the flat state is discovered; default value - 3; if price does not close above or below the range of the flat during BreakDetect bars, the flat state is discarded without a signal;
  • ProgressiveGrid - enable/disable a mode that the step for every next averaging order is enlarged proportionally to number of open orders; default value - true; for example, between 1st and 2nd orders there will be at least GridPoints points, between 2nd and 3rd - at least 2*GridPoints, etc; if it is false, the step is constant and at least GridPoints; it takes effect only if MultipleOrders is true;

Expert Library Settings:

  • MagicBase - custom magic number; default value - 0, meaning that a unique magic is generated for every Expert Advisor instance (this is a matter of symbol and timeframe of current chart, so 2 instances placed on charts with the same symbol and timeframe will generate the same magic, beware); the generated magic is printed to the log;

Orders Parameters: 

  • Lots - number of lots for new orders and increment for lots of averaging orders, when IncrementLots is true; default value - 0.01; when it's 0 - Risk takes effect;
  • StopLoss - stop loss size in points; default value - 0; a special value -2 means stop loss equal to daily range (calculated automatically);
  • Slippage - allowed slippage for requests (in points); default value - 10;
  • TrailStops - enable/disable stop loss trailing; default value - true; it takes effect only if StopLoss is non-zero;
  • TrailingStep - step (in points) for stop loss trailing; default value - 10; it takes effect only if StopLoss is non-zero;
  • TrailNonProfit - enable/disable stop loss trailing for orders with a profit less then StopLoss; when it is false (default value), the Expert Advisor waits when price goes StopLoss points from open price and only then starts trailing; when it is true, every price movement to favourable direction on next TrailingStep points will trigger trailing; it takes effect only if StopLoss is non-zero;

Algorithm Parameters:

  • BarByBar - enable/disable trading mode by bars (true) or by ticks (false); default value - true (recommended); please note, that stop loss trailing is always performed by ticks with TrailingStep accuracy, even if BarByBar is enabled; BarByBar affects only new signals;
  • MultipleOrders - allow/disallow multiple orders for every work symbol; default value - true;

Money Management: 

  • IncrementLots - enable/disable incrementing of lots of averaging orders; default value - true; makes sense only when MultipleOrders is true;
  • MaximumLot - maximum lot allowed for a single order; default value - 0.5;
  • Risk - this paramatere takes effect only if Lots is 0; it specifies a size of order lot as a percent of free margin ("FreeMargin" * Risk / "MarginRequiredFor1Lot"); default value - 0.01 (1%);


  • PrintLog - enable/disable debug logging; default value - false.

Tests and Screenshots

All presented tests were made for averaging strategy - it is enabled by setting MultipleOrders to true and StopLoss to 0 (don't worry - minimal risks were applied and "slow-motion virtual stop loss" described above was used). Also, IncrementLots and ProgressiveGrid were true. By changing these two parameters one can choose how aggresive the averaging is. When ProgressiveGrid is false, the averaging happens more often that also increases exposure - this makes the strategy more risky but may help to recover by shorter corrections. When IncrementLots is false, the strategy is less aggressive and exposure is decreased.

Screenshots 1-6 demonstrate results for EURUSD, GBPUSD, AUDUSD, USDCHF, USDJPY, USDCAD (all symbols - H1 timeframe). Test mode is "open prices", but the results are accurate because BarByBar parameter is true. Just to make sure the accuracy is not affected, screenshot 7 shows the same EURUSD test in "every tick" mode, and it is very close to figure 1.

On figure 8 the same EURUSD test was performed in "ticks" mode with the only difference that BarByBar parameter set to false. This means the Expert Advisor processes all ticks. Results became worse due to the trading system specifics: averaging on closed H1 bars is normally better when in the middle of the bar - lesser number of lots is stacked on larger price range. Other settings may produce different results.

Figure 9 shows a mode with a proportional lot. All tests above were performed with constant minimal lot of 0.01, and it is also used as increment for averaging. This time constant lot is disabled by setting Lots to 0, and parameter Risk takes effect. In the example, the Risk is 0.01, that is 1% of free margin used as lot size at every moment when new signal is generated.

Total combined result for tests 1-6 is shown on Figure 10. MetaTrader 4 tester does not allow multicurrency testing, so this screenshot is produced by an external script from the tester reports "as is".

All 6 test runs are shown on Figure 11 for comparison (also generated by an external script).

No hay comentarios