Help with MA cross and price crossing another MA

 

Hi all,


This is my first attempt in a very long time at writing an EA. In summary my EA should execute a Buy/Sell if the 1- and 20 EMA has crossed in the previous 10 candles and at the point when the price crosses the 200 EMA. I've done my best so far but when back testing does not pick anything up. I'm sure I've done something stupid as I look at charts and can seethis trend happening a few times when adding indicators manually. Can anyone help please?


#include<Trade\Trade.mqh>

//Create a Ctrade instance
CTrade trade;
input double   MyLotSize= 0.10;
input int      StopLoss= 50;
input int      TakeProfit= 100;

void OnTick()
  {
  
      //Create an empty string
      string entry="";
      
      //Create an empty string
      string emtery ="";
      
      //Get the ask price
      double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
      
      //Get the bid price
      double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
      
      //Create Array for several prices
      double myMovingAverageArray10[],myMovingAverageArray20[],myMovingAverageArray200[];
      
      //Create the properties of Moving Average 10
      int movingaverageDefinition1 = iMA (_Symbol, PERIOD_H4, 10, 0, MODE_EMA, PRICE_CLOSE);
      
      //Create the properties of Moving Average 10
      int movingaverageDefinition2 = iMA (_Symbol, PERIOD_H4, 20, 0, MODE_EMA, PRICE_CLOSE);
      
      //Create the properties of Moving Average 10
      int movingaverageDefinition3 = iMA (_Symbol, PERIOD_H4, 200, 0, MODE_EMA, PRICE_CLOSE);
      
      //Sort the price array from the current candle downwards
      ArraySetAsSeries(myMovingAverageArray10,true);
      
      //Sort the price array from the current candle downwards
      ArraySetAsSeries(myMovingAverageArray20,true);
      
      //Sort the price array from the current candle downwards
      ArraySetAsSeries(myMovingAverageArray200,true);
      
      //Defined MA1, one line, current canfdle, 3 candles, store result
      CopyBuffer(movingaverageDefinition1,0,0,10,myMovingAverageArray10);
      
      //Defined MA2, one line, current canfdle, 3 candles, store result
      CopyBuffer(movingaverageDefinition2,0,0,10,myMovingAverageArray20);
      
      //Defined MA3, one line, current canfdle, 3 candles, store result
      CopyBuffer(movingaverageDefinition3,0,0,3,myMovingAverageArray200);
      
      if (  //Check if EMA10 is above EMA 20 and current price is above EMA 200
            (myMovingAverageArray10[0]>myMovingAverageArray20[0] && Ask>myMovingAverageArray200[0])
         && (myMovingAverageArray10[10]<myMovingAverageArray20[10])
         
         )
            {
            entry="buy";
            }
            
            if (  //Check if EMA10 is below EMA 20 and current price is above EMA 200
            (myMovingAverageArray10[0]<myMovingAverageArray20[0] && Bid<myMovingAverageArray200[0])
         && (myMovingAverageArray10[10]>myMovingAverageArray20[10])
         
         )
            {
            entry="sell";
            }
            
      //Sell 1 nano lot
      if (entry =="sell" && PositionsTotal()<1)
      trade.Sell(0.01,NULL,Bid,0, (Bid-150 * _Point),NULL);
      
      //Buy 1 nano lot
      if (entry =="buy" && PositionsTotal()<1)
      trade.Buy(0.01,NULL,Ask,0, (Bid+150 * _Point),NULL);
         
   
  }
 
  1.     int movingaverageDefinition1 = iMA (_Symbol, PERIOD_H4, 10, 0, MODE_EMA, PRICE_CLOSE);
    

    Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020.03.08)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020.07.05)
              How to call indicators in MQL5 - MQL5 Articles (2010)


  2.       trade.Sell(0.01,NULL,Bid,0, (Bid-150 * _Point),NULL);

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

 
Bravestar100 :

Hi all,


This is my first attempt in a very long time at writing an EA. In summary my EA should execute a Buy/Sell if the 1- and 20 EMA has crossed in the previous 10 candles and at the point when the price crosses the 200 EMA. I've done my best so far but when back testing does not pick anything up. I'm sure I've done something stupid as I look at charts and can seethis trend happening a few times when adding indicators manually. Can anyone help please?


You have a global error - you create several indicator handles at each tick.

Remember: according to the MQL5 style, the indicator handle must be created ONCE and must be done in OnInit.

Example:

Creating an iMA indicator handle, getting indicator values

How to start with MQL5
How to start with MQL5
  • 2020.03.05
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
Reason: