EMAs Crossover

 

Hi everyone... I have a code that works well, but when for example the slow ema crosses the fast ema and the ea has to sell , It works but sometimes immediately after this the next candle makes the cross over to the way that we have to buy so the previous cross over was fake actually and the ea continues to sell according to that instead of buying.

Here is my code ...Please help me

#include<Trade\Trade.mqh>

// create an instance of CTrade

input int SmallMovingAverage=8;
input int BigMovingAverage=21;
input double TargetProfit = 100;
CTrade trade;
bool startTrading = false;

   
void OnTick()
{
    // we calculate the Ask price
    double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
    // we calculte the Bid price
    double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   
    // create a string for the signal
    string signal="";
   
    // create an array for several prices
    double smallMovingAverageArray[],BigMovingAverageArray[];
   
    // define the properties of the small moving average
    int SmallMovingAverageDefinition=iMA(_Symbol,_Period,SmallMovingAverage,0,MODE_EMA,PRICE_CLOSE);
   
    // define the properties of the big moving average
    int BigMovingAverageDefinition=iMA(_Symbol,_Period,BigMovingAverage,0,MODE_EMA,PRICE_CLOSE);
   
    // defined EA, one line, current candle, 3 candles, store result
    CopyBuffer(SmallMovingAverageDefinition,0,0,3,smallMovingAverageArray);
   
    // defined EA, one line, current candle, 3 candles, store result
    CopyBuffer(BigMovingAverageDefinition,0,0,3,BigMovingAverageArray);
   
    // if BigMovingAverage>SmallMovingAverage
    if(BigMovingAverageArray[1]>smallMovingAverageArray[1])
   
    //if BigMovingAverage<SmallMovingAverage before
    if(BigMovingAverageArray[2]<smallMovingAverageArray[2])
    {
        if(startTrading)
        {
            signal="buy";
        }
        else
        {
            startTrading = true;
        }
    }
   
    // if BigMovingAverage<SmallMovingAverage
    if(BigMovingAverageArray[1]<smallMovingAverageArray[1])
   
    //if BigMovingAverage>SmallMovingAverage before
    if(BigMovingAverageArray[2]>smallMovingAverageArray[2])
    {
        if(startTrading)
        {
            signal="sell";
        }
        else
        {
            startTrading = true;
        }
    }
   
    // sell 10 microlot
    if(signal=="sell" && PositionsTotal()<1)
    {
        trade.Sell(1,NULL,Bid,0,0,NULL);
    }
   
     // buy 10 microlot
    if(signal=="buy" && PositionsTotal()<1)
    {
        trade.Buy(1,NULL,Ask,0,0,NULL);
    }
    
    double profit = AccountInfoDouble(ACCOUNT_EQUITY) - AccountInfoDouble(ACCOUNT_BALANCE);
    
    bool isProfit = profit >= TargetProfit;
    if(isProfit){
      for(int i = PositionsTotal()-1; i >= 0; i--){
         ulong posTicket = PositionGetTicket(i);
         if(PositionSelectByTicket(posTicket)){
            if(trade.PositionClose(posTicket)){
               if(isProfit) Print(__FUNCTION__," > Pos #", posTicket, "was closed because of profit...");
            }
         }
      }
    }
}
 
SabaSoltani: ..Please help me

Help you with what? Seeing the future?

 
Your topic has been moved to the section: Expert Advisors and Automated Trading — In the future, please consider which section is most appropriate for your query.
 
SabaSoltani:

Hi everyone... I have a code that works well, but when for example the slow ema crosses the fast ema and the ea has to sell , It works but sometimes immediately after this the next candle makes the cross over to the way that we have to buy so the previous cross over was fake actually and the ea continues to sell according to that instead of buying.

Here is my code ...Please help me

Do you see the links MQ placed right below you post:

  • MA cross over logic
  • How to close trade on MA crossover???
  • how can i find the price of previous MA crossover (buy or sell) signal

  • Have you read this?

    Bear in mind there's virtually nothing that hasn't already been programmed for MT4/MT5 and is ready for you - and searching gives better results than ChatGPT!
    MA cross over logic
    MA cross over logic
    • 2016.04.18
    • www.mql5.com
    Hello everyone. I am new to the coding trying to learn from this page and found that there is a builder on EA. And so I used it...
     
    SabaSoltani:

    Hi everyone... I have a code that works well, but when for example the slow ema crosses the fast ema and the ea has to sell , It works but sometimes immediately after this the next candle makes the cross over to the way that we have to buy so the previous cross over was fake actually and the ea continues to sell according to that instead of buying.

    Here is my code ...Please help me

    Your code had minor errors, compare it yourself

    Files:
    alaki2.mq5  3 kb