Watch how to download trading robots for free
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal

2 MA Crossing - expert for MetaTrader 4

Yulianto Hiu | English Español Português 日本語 Deutsch

votes: 20
2021.03.30 10:18
2021.03.30 10:20

We will start creating this EA by defining the input variables.

//--- input parameters
input    int      period_ma_fast = 8;  //Period Fast MA
input    int      period_ma_slow = 20; //Period Slow MA

input    double   takeProfit  = 20.0;  //Take Profit (pips)
input    double   stopLoss    = 20.0;  //Stop Loss (pips)

input    double   lotSize     = 0.10;  //Lot Size
input    double   minEquity   = 100.0; //Min. Equity ($)

input    int Slippage = 3;       //Slippage
input    int MagicNumber = 889;  //Magic Number

followed by defining global variables. variables with this global scope will be known or accessible to all functions.

//Variabel Global
double   myPoint    = 0.0;
int      mySlippage = 0;
int      BuyTicket   = 0;
int      SellTicket  = 0;

When EA is executed, the first function that is executed is OnInit (). So that we often use this function to validate and initialize global variables that will be used.

int OnInit()
   //validasi input, sebaiknya kita selalu melakukan validasi pada initialisasi data input
   if (period_ma_fast >= period_ma_slow || takeProfit < 0.0 || stopLoss < 0.0 || lotSize < 0.01 || minEquity < 10){
      Alert("WARNING - Input data inisial tidak valid");
   double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
      string pesan =StringFormat("Volume lebih kecil dari batas yang dibolehkan yaitu %.2f",min_volume);
      Alert (pesan);
   myPoint = GetPipPoint(Symbol());
   mySlippage = GetSlippage(Symbol(),Slippage);


When the market price moves (tick), the OnTick () function will be called and execute all instructions / functions contained in this OnTick () function block.

Inside the OnTick () function will call various other functions.

Starting to call the checkMinEquity () function to control the adequacy of trading equity. If the equity funds are sufficient (exceeding the minimum equity), it will be followed by a signal variable declaration and followed by a call to the NewCandle () function which functions to inform when a new candle is formed.

The getSignal () function will read the values on both moving average indicators and return signal information whether an upward or downward cross occurs as a signal for a buy / sell signal.

Based on this signal information, it will be forwarded to the transaction () function to set open buy or sell positions.

And it will be continued by calling the setTPSL () function which functions to set the take profit and stop loss prices.

If equity does not meet the minimum equity requirement, an alert will be displayed and this EA will be terminated.

void OnTick()
   if (cekMinEquity()){
      int signal = -1;
      bool isNewCandle = NewCandle(Period(), Symbol());
      signal = getSignal(isNewCandle);
      transaction(isNewCandle, signal);
      //Stop trading, karena equity tidak cukup
      Print("EA akan segera diberhentikan karena equity tidak mencukup");

Function to setTPSL()

void setTPSL(){
   int   tOrder = 0;
   string   strMN = "", pair = "";
   double sl = 0.0, tp = 0.0;
   pair = Symbol();
   tOrder = OrdersTotal();
   for (int i=tOrder-1; i>=0; i--){
      bool hrsSelect = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      strMN = IntegerToString(OrderMagicNumber());
      if (StringFind(strMN, IntegerToString(MagicNumber), 0) == 0 && StringFind(OrderSymbol(), pair, 0) == 0 ){
         if (OrderType() == OP_BUY && (OrderTakeProfit() == 0 || OrderStopLoss() == 0) ){
            if (takeProfit > 0) {
               tp = OrderOpenPrice() + (takeProfit * myPoint);
               tp = OrderOpenPrice();
            if (stopLoss > 0) {
               sl = OrderOpenPrice() - (stopLoss * myPoint);
               sl = OrderStopLoss();
            if (OrderTakeProfit() != tp || OrderStopLoss() != sl ){
               if(OrderModify(OrderTicket(), OrderOpenPrice(), sl, tp, 0, clrBlue)){
                  Print ("OrderModify Successful");
         if (OrderType() == OP_SELL && (OrderTakeProfit() == 0 || OrderStopLoss() == 0) ){
            if (takeProfit > 0) {
               tp = OrderOpenPrice() - (takeProfit * myPoint);
               tp = OrderOpenPrice();
            if (stopLoss > 0) {
               sl = OrderOpenPrice() + (stopLoss * myPoint);
               sl = OrderStopLoss();
            if (OrderTakeProfit() != tp || OrderStopLoss() != sl ){
               if (OrderModify(OrderTicket(), OrderOpenPrice(), sl, tp, 0, clrRed)){
                  Print ("OrderModify Successful");
      }//end if magic number && pair
   }//end for

for education and sharing in Indonesian, please join us in the telegram group

If you are looking for an App to support your trading, please download our SignalForex app in the play store

    EA - The Simple Trading Panel - MT4 EA - The Simple Trading Panel - MT4

    The simple trading panel is a trading tool that is very interesting because it will allow you to predefine your StopLoss and your TakeProfit in term of pips.

    Pivot_Points_Lines_v1.3 MT4 Pivot_Points_Lines_v1.3 MT4

    Draws Pivot Points Formulas

    Trailing Stop with MagicNumber Trailing Stop with MagicNumber

    Add on tool to support our trading by shifting stoploss (SL) to the profit area

    Close Orders By Target or Cut Loss Close Orders By Target or Cut Loss

    This EA is used as a trading tool to help us close all orders with a specific target in the form of money or cut loss. We can filter orders by magic number.