MT4 Expert Advisor Full Script for Martingale & Grid Express in Less than 50 Lines of Code

EA automated trading using Martingale and Grid strategy


If you have found this it is likely that you already understood what is this trading strategy. Basically the script will decide on a direction using previous high and low with relative strength indicator (RSI). Once the direction is decided, the script will make a order (either buy or sell) at the designated lots. Every time a trade is made, the script will calculate the take profit price point for all open orders to ensure profitability. If the first order hit take profit, the trade ends and the script will restart. Otherwise, once the live price hit the previous price + pip step, it will open a new trade provided if it is not within the same time bar. The newly opened lot will be in an multiplier according to the LotExponent. It will continue to open trade and draw down till the new take profit is hit.

There are many variants of this script but I have simplified it to only 50 lines with the following benefits.
  1. If your server is closed by whatever reason, simply reopen any MT4, login to the same account and plugin this script where all previous trades will be continued.
  2. As the take profit point is set, whether the server is down or the account being disconnected, the orders will close automatically. This also reduces the server message to your brokerage.
  3. All variables are optimised to the datatype that they are required, unnecessary global variables are being taken out, the loopings are being reduced and no additional functions to reduce the system's memory.
There are only 4 external variables and it can be configured to all timeframe and symbols.
  1. Lot = 0.01
    This is the lot size for the script to start trading. Do note that if a $4,000 account can withstand 0.01 lot, it does not mean $8,000 can withstand a 0.02 lot. Mostly it will be in a multiplier of 2.5 meaning a $10,000 is necessary for a 0.02 lot account. You may need to do a lot of trial and error to find the most optimised number.
  2. LotExponent = 1.667
    The multiplier for each trade after the first. As the next lot size is rounded off, anything below 1.5 will result in the same lot size. To ride through longer trend, it can even be set to 1.1 but the profit will also be greatly affected.
  3. PipStep = 180
    180 is ideal for hourly trade but it still depends on the pair you are trading with. Whether the order is a buy or sell, as long as a new time bar does not exceed or fall short of the previous entry price, the script will not make a new trade.
  4. TakeProfit=70
    When the trend reverses, this is how many points you want to take the profit at. 70 is good for H1 and reduce it if the time period is shortened.
I have tried M1 with PipStep=10 and TakeProfit=12, it can generate more than 80%  at some months while the account can also be wiped out within a day. This script is not rocket science but hardwork to find out what are the best settings and pairs.


This chart shows a better idea of how the Martingale works and it is perfect for a ranging market. The code is below.

extern double Lots=0.01,LotExponent=1.667;
extern short PipStep=180,TakeProfit=70;
int ihl;
int start() {
   if(ihl!=Time[0]) { // Not trading in the same candle
      ihl=Time[0];
      bool c; // Local variables
      char li,i,id=2;
      double dP=PipStep*Point,dL,dT,dO;
      
      for(i=OrdersTotal()-1; i>=0; i--) { // To select number of orders and last price (if any)
         c=OrderSelect(i,0);
         if(OrderSymbol()==Symbol()){
            if(dL==0) dL=OrderOpenPrice();
            li++;
           }
        }
      for(i=OrdersTotal()-1; i>=0; i--) { // Select the previous Order direction
         c=OrderSelect(i,0);
         if(OrderSymbol()==Symbol()){
            id=OrderType();
            break;
           }
        }

      if(li==0||(id==0&&dL-Ask>=dP)||(id==1&&Bid-dL>=dP)) { // Entering the trade
         if((li==0&&iHigh(Symbol(),0,1)>iLow(Symbol(),0,2)&&iRSI(0,60,14,0,1)>30)||id==1)
            c=OrderSend(Symbol(),1,NormalizeDouble(Lots*MathPow(LotExponent,li),2),Bid,3,0,0);
         else
            c=OrderSend(Symbol(),0,NormalizeDouble(Lots*MathPow(LotExponent,li),2),Ask,3,0,0);

         for(i=OrdersTotal()-1; i>=0; i--) { // Recalculating the price to take profit
            c=OrderSelect(i,0);
            if(OrderSymbol()==Symbol()) {
               dT+=OrderOpenPrice()*OrderLots();
               dO+=OrderLots();
              }
           }
         dT=NormalizeDouble(dT/dO,Digits);
         for(i=OrdersTotal()-1; i>=0; i--) { // Modifying all orders
            c=OrderSelect(i,0);
            if(OrderSymbol()==Symbol())
               c=OrderModify(OrderTicket(),dT,OrderStopLoss(),OrderType()==0?dT+TakeProfit*Point:dT-TakeProfit*Point,0);
           }
        }
     }
   return 0;
  }

And once again do support me by subscribing under my introductory broker with IC Markets at https://icmarkets.com/?camp=34606. I have also written another article to create cheap hosting and run the MT4 script. Do comment or email me at aloycwl@gmail.com if there is any question or feedback.

Comments

Popular posts from this blog

Kokology Questions & Answers

Neuro-Linguistic Programming Models Summary (02 of 14)

Neuro-Linguistic Programming Models Summary (11 of 14)