Anyone check this MLQ4 code for me please? - MA Crossover with RSI and Stochastic indicators

 

Hi there,

I'd be very grateful if any programmer out there could have a look at my code? It compiles with zero errors but when I install it in MT4 Strategy Tester it generates zero trades...

What have a missed out? Its just a simple MA crossover strategy using the RSI and Stochastic indicators as two further confirmations...

Thanks, Simon


// Declare variables

double ema50, ema200, stochK, stochD, stochK1, stochD1, rsi;

int stopLoss = 100;

int MagicNumber = 7813;



// Define position size as a percentage of account balance

double positionSize = AccountBalance() * 0.0025;



// Define indicators

int start()

{

   // Calculate indicators

   ema50 = iMA(NULL, 0, 50, 0, MODE_EMA, PRICE_CLOSE, 0);

   ema200 = iMA(NULL, 0, 200, 0, MODE_EMA, PRICE_CLOSE, 0);

   stochK = iStochastic(NULL, 0, 14, 3, 3, MODE_EMA, 0, MODE_MAIN, 0);

   stochD = iStochastic(NULL, 0, 14, 3, 3, MODE_EMA, 0, MODE_SIGNAL, 0);

   stochK1 = iStochastic(NULL, 1, 14, 3, 3, MODE_EMA, 0, MODE_MAIN, 1);

   stochD1 = iStochastic(NULL, 1, 14, 3, 3, MODE_EMA, 0, MODE_SIGNAL, 1);

   rsi = iRSI(NULL, 0, 9, PRICE_CLOSE, 0);



   // Entry rules

   if (ema50 > ema200 && stochK < 30 && stochD < 30 && stochD < stochK && stochD1 > stochK1 && rsi > 50)

   {

      // Buy

      int ticketBuy = OrderSend(Symbol(), OP_BUY, positionSize, Ask, 3, Bid - stopLoss * Point, 0, "", MagicNumber, 0, Green);

      if(ticketBuy > 0) {

          // trade opened successfully, do something

      } else {

          // trade failed to open, handle the error

          Print("Failed to open buy trade. Error code: ", GetLastError());

      }

      }

else if (ema50 < ema200 && stochK > 60 && stochD > 60 && stochD > stochK && stochD1 < stochK1 && rsi < 50)

{

      // Sell

      int ticketSell = OrderSend(Symbol(), OP_SELL, positionSize, Bid, 3, Ask + stopLoss * Point, 0, "", MagicNumber, 0, Red);

      if(ticketSell > 0) {

          // trade opened successfully, do something

      } else {

          // trade failed to open, handle the error

          Print("Failed to open sell trade. Error code: ", GetLastError());

      }





      // Exit rules

      if ((ema50 < ema200 && OrderType() == OP_BUY) || (ema50 > ema200 && OrderType() == OP_SELL) || rsi == 50)

      {

          // Close trade

          bool closedOrder = OrderClose(OrderTicket(), OrderLots(), Bid, 3, White);

          if(!closedOrder) {

              Print("Error in closing order with error code ", GetLastError());

          }

      }

      else if (OrderStopLoss() > 0 && (Bid - OrderStopLoss()) >= stopLoss * Point)

      {

          // Hit stop loss

          bool closedOrder2 = OrderClose(OrderTicket(), OrderLots(), Bid, 3, Magenta);

          if(!closedOrder2) {

              Print("Error in closing order with error code ", GetLastError());

          }

      }

   }



   // Return value

   return(0);

}


 
11594950:

Hi there,

I'd be very grateful if any programmer out there could have a look at my code? It compiles with zero errors but when I install it in MT4 Strategy Tester it generates zero trades...

What have a missed out? Its just a simple MA crossover strategy using the RSI and Stochastic indicators as two further confirmations...

Thanks, Simon



YOU should check your logs in the tester… plenty of errors… your SL is wrong … your size is wrong 
 
double positionSize = AccountBalance() * 0.0025;

That is not an assignment; it's initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

  1. They are initialized once on program load.

  2. They don't update unless you assign to them.

  3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
              external static variable - MQL4 programming forum #2 (2013)

Reason: