The Martingail Expert Advisor script based upon the Stochastic oscillator and Martingale averaging mechanism.

The Stochastic oscillator signal is used to identify the initial trade, and Martingail mechanism is used to manage subsequent trades. The Stochastic oscillator is a powerful indicator that identifies trends. The concept behind this Expert Advisor is that, when the Stochastic MAIN is above the Stochastic SIGNAL line it is a BULLISH signal, and when the Stochastic MAIN is below the Stochastic SIGNAL line it is bearish signals. The **ZONE_BUY **and **ZONE_SELL **levels are used to further filter out weaker trends.

**When and how Martingale Expert Advisor place BUY order**

If there is no open position, then a BUY trade is opened if the Stochastic MAIN line is above the Stochastic SIGNAL line and the **ZONE_BUY** level. The Stochastic MAIN and SIGNAL values of the previous (last closed candle) are used. The initial lot-size is specified by the **LOTS** parameter.

Once a BUY trade is opened, the subsequent BUY trade is opened **STEP** points below the first BUY trade. The lot-size of the subsequent BUY trade is calculated by multiplying the lot-size of the previous BUY trade with **MULT **parameter.

**When and how Martingale Expert Advisor place SELL order**

If there is no open position, then a SELL trade is opened if the Stochastic MAIN line is below the Stochastic SIGNAL line and the **ZONE_SELL** level. The Stochastic MAIN and SIGNAL values of the previous (last closed candle) are used.The initial lot-size is specified by the **LOTS** parameter.

Once a SELL trade is opened, the subsequent SELL trade is opened **STEP **points above the first SELL trade. The lot-size of the subsequent SELL trade is calculated by multiplying the lot-size of the previous SELL trade with **MULT **parameter.

**When and how Martingale Expert Advisor Exit from trade or opened order**

The Expert Advisor closes all open positions when the net profit reaches a value of **PROF_FACTOR** multiplied by the number of trades. For example if the Expert Advisor has opened 5 trades, and the value is **PROF_FACTOR ** is 5, then the Expert Advisor will close all the open positions when the net profit is $25 or more.

Since the initial position is based on the Stochastic filter, this Expert Advisor will open either BUY or SELL trades exclusively at a time.

**Configurable parameters for Martingale Expert Advisor**

**LOTS –**The lot-size of the first (initial) trade opened by the Expert Advisor.**MULT –**The lot-size multiplier for the subsequent trades opened by the Expert Advisor in the Martingale mode. For example if**LOTS**is 0.1, and**MULT**is 2.0, then the initial lot-size is 0.1 lots and the second lot-size is 0.20 lots, the third lot-size is 0.4 lots.**PROF_FACTOR –**The profit amount (in account currency) for each trade. When the open profit reaches**PROF_FACTOR**multiplied by the number of open trades, the Expert Advisor closes all open positions.**STEP –**The distance between each opened trade in points.**PER_K –**The %K period used to calculate the value of the Stochastic oscillator.**PER_D –**The %d period used to calculate the value of the Stochastic oscillator.**SLOW –**The slowing period used to calculate the value of the Stochastic oscillator.**ZONE_BUY –**The level above which the Stochastic MAIN line should be in order to open the initial BUY trade. For example, if this value is 50, then the Stochastic MAIN line should be above 50 in order for the Expert Advisor to open the initial BUY trade.**ZONE_SELL –**The level below which the Stochastic MAIN line should be in order to open the initial SELL trade. For example, if this value is 50, then the Stochastic MAIN should be below 50 in order for the Expert Advisor to open the initial SELL trade.