The idea behind this Expert Advisor is to employ a Martingale trading strategy with the expectation that the market will breakout from sideways to either direction. By using appropriate values for FACTOR and LIMIT, the Expert Advisor recovers all the losses incurred in the sideways markets and makes additional profits.
When the Expert Advisors starts, it checks if a trade is already opened. If there is no trade open, then the first BUY trade is opened for LOTS lot-size if START_TYPE is set to 0. If the START_TYPE is set to 1, then an initial SELL trade is opened for the LOTS lot-size immediately.
The initial stop-loss is set to STOP_LOSS points, and the initial take-profit is set to TAKE_PROFIT points. If the initial trade closes at a loss, the next trade opened is of the opposite type and the lot-size is increased by FACTOR. For example, if the initial trade was a BUY trade for 0.1 lots, and the FACTOR is set to 2.0, then the next trade is a SELL trade for 0.20 lots. This process is repeated till the trade is closed profitable, or when the number of trades in the sequence reaches LIMIT. For example, if LIMIT is set to 5, then after the 5th trade is closed at a loss, the Expert Advisor starts afresh with the original lot-size. However, the direction is reversed from that of the last trade.
By opening alternating BUY and SELL trades the Expert Advisor responds to changes in the market direction appropriately. For example, if the first trade opened was a BUY, and the price rises the Expert Advisor exits in profit and starts again. On the contrary, if the price falls, a new SELL trade is opened. If the price falls further, the cumulative profit of the BUY and SELL trade is in profit, and the Expert Advisor closes both the trades at a cumulative net profit. However, if the price rises after the new SELL trade is opened, there might be a nominal net profit or loss. In such a situation, the Expert Advisor waits for next market move. If the price rises above the initial (first) BUY trade, the Expert Advisor opens a new BUY trade (third trade). The lot-size of this BUY trade is greater than sum of the lot sizes of the previous two trades. By increasing the lot-sizes, the Expert Advisor ensures that it is profitable whenever the price moves in the direction of the last opened trade.