Problems with SMA/EMA cross program

 

Hi there,


I tried this program, and has the following problem with it.

When the price comes close to the action time, I get several buy or sell actions all ending in losses, they are not that big, but they all add up...

Once the price have moved away from the cross, it is more stable and remains in a buy / sell until it nears the next cross.

I had this problem before with one of these EA's and this was the scenario. And, as you can see below, the way I solved it was to


I had variables like these

   double variable1 =  iMA(NULL,PERIOD_H1,12,1,MODE_EMA,PRICE_CLOSE,1) ;

   double variable2 =  iMA(NULL,PERIOD_H1,24,3,MODE_SMA,PRICE_CLOSE,1) ;

  


   //--  Long Conditions

   TrueLong = variable1 +2 > variable2 ; 

   //--  Short Conditions

   TrueShort = variable2 > variable1 -2 ; 

   //--  Close Long Conditions

   TrueCloseLong = variable2 > variable1 -2 ;

   //--  Close Short Conditions

   TrueCloseShort = variable1 +2 > variable2 ; 


I tried a Similar method in this program, without any success. I still get all these unwanted transactions.


Any chance anyone knows why what worked previously does not work here, and maybe a few suggestions?


Thanks in advance



//+------------------------------------------------------------------+

//|                                           SMA15EMA6RSI14_BOT.mq4 |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

//--  EA  ------------------------------------------------------------------------

//---------------------------------------------------------   FXEABuilder.mq4  ---

//--  The program starts here --------------------------------------------  0  ---

//--  Classes  -----------------------------------------------------------  C  ---

//---------------------------------------------------------  EA Class starts   ---

class EA

  {

public:

//---------------------------------------------------------------  Variables   ---

      double

         StopLoss,

         TakeProfit,

         Lots,

         TrailingStop;

      int   

         Slippage,

         Magic;

      string

         PSymbol;   

//------------------------------------------------------------------  Methods  ---

      void EA();

      void ~EA();

      void Create(

               double ALots,

               int ASlippage,

               int AMagic,

               string APSymbol,

               double AStopLoss,

               double ATakeProfit,

               double ATrailingStop

            );   

      double PipsToDecimal(double Pips);

      int Buy();

      int Sell();

      void CloseBuy(int Ticket);

      void CloseSell(int Ticket);

      void DoLongTrailingStop(int Ticket);

      void DoShortTrailingStop(int Ticket);

  };

//------------------------------------------------------------  Methods Body   ---

//--                                                                           ---

//------------------------------------------------------------  Constructor    ---

void EA::EA()

  {

         

  }

//---------------------------------------------------------------  Destructor  ---

void EA::~EA()

  {

  

  }

//---------------------------------------------  Method to Create the Object   ---

void EA::Create(

         double ALots,

         int ASlippage,

         int AMagic,

         string APSymbol,

         double AStopLoss,

         double ATakeProfit,

         double ATrailingStop

      )

   {

      Lots = ALots;

      Slippage = ASlippage;

      Magic = AMagic;

      PSymbol = APSymbol;

      StopLoss = (AStopLoss>0)?PipsToDecimal(AStopLoss):0;

      TakeProfit = (ATakeProfit>0)?PipsToDecimal(ATakeProfit):0;

      TrailingStop = (ATrailingStop>0)?PipsToDecimal(ATrailingStop):0;

   }      

//---------------------------------------   Method to convert pips to decimal  ---   

double EA::PipsToDecimal(double Pips)

  {

   double ThePoint=SymbolInfoDouble(PSymbol,SYMBOL_POINT);

   if(ThePoint==0.0001 || ThePoint==0.00001)

     {

      return Pips * 0.0001;

     }

   else if(ThePoint==0.01 || ThePoint==0.001)

     {

      return Pips * 0.01;

     }

   else

     {

      return 0;

     }

  }

//----------------------------------------------------------   Method to buy   ---

int EA::Buy()

  {

   double SL = (StopLoss>0)?Bid-StopLoss:0;

   double TP = (TakeProfit>0)?Bid+TakeProfit:0;

   return OrderSend(

                    PSymbol,

                    OP_BUY,

                    Lots,

                    Ask,

                    Slippage,

                    SL,

                    TP,

                    "Long Position",

                    Magic

                    );

         

  }

//----------------------------------------------------------   Method to sell  ---

int EA::Sell()

  {

      double SL = (StopLoss>0)?Bid-StopLoss:0;

      double TP = (TakeProfit>0)?Bid+TakeProfit:0;

      return OrderSend(

                       PSymbol,

                       OP_SELL,

                       Lots,

                       Bid,

                       Slippage,

                       SL,

                       TP,

                       "Short Position",

                       Magic

                       );

  }

//-----------------------------------------------  Method to close buy orders  ---

void EA::CloseBuy(int Ticket)

   {

      OrderClose(Ticket,Lots,Bid,Slippage);       

   }

//----------------------------------------------  Method to close sell orders  ---

void EA::CloseSell(int Ticket)

   {

         OrderClose(Ticket,Lots,Ask,Slippage); 

   }

//-------------------------------------------  Method of Long Trailing Stop    ---

void EA::DoLongTrailingStop(int Ticket)

  {

      if(TrailingStop>0)  

      { 

         RefreshRates();

         OrderSelect(Ticket,SELECT_BY_TICKET);                

         if(Bid-OrderOpenPrice()>TrailingStop)

         {

            if(Bid-TrailingStop > OrderStopLoss()|| OrderStopLoss()==0)

            {

               OrderModify(OrderTicket(),

                           OrderOpenPrice(),

                           Bid-TrailingStop,

                           OrderTakeProfit(),

                           0

                           );

            }

         }

      }        

  }

//------------------------------------------  Method of Short Trailing Stop    ---

void EA::DoShortTrailingStop(int Ticket)

  {

      if(TrailingStop>0)  

      { 

         RefreshRates(); 

         OrderSelect(Ticket,SELECT_BY_TICKET);              

         if(OrderOpenPrice()-Ask>TrailingStop)

         {

            if(OrderStopLoss()>Ask+TrailingStop || OrderStopLoss()==0)

            {

               OrderModify(OrderTicket(),

                           OrderOpenPrice(),

                           Ask+TrailingStop,

                           OrderTakeProfit(),

                           0

                           );

            }

         }

      }        

  }

//--  End Of Class 

//--  Global Variables Declaration and constants initialization   --------  1  ---

   extern double StopLoss = 0;  // SL

   extern double TakeProfit = 0; // TP

   extern double TrailingStop = 0;   // Trailing Stop 

   extern double Lots = 1;  // The Lot Size

   extern int Slippage = 5;   // The Maximum Slippage  

   const int Magic = 75644;        // The magic number

   bool TrueLong;

   bool TrueShort;

   bool TrueCloseLong;

   bool TrueCloseShort;

   int LongTicket;

   int ShortTicket;

   datetime PreviousTime;

   int SignalMode;

   EA Long;

   EA Short;

//--  Expert initialization function   -----------------------------------  I  ---

int OnInit()

  {

//--  Variables Initialization   -----------------------------------------  2  ---

   Long.Create(Lots,Slippage,Magic,Symbol(),StopLoss,TakeProfit,TrailingStop);

   Short.Create(Lots,Slippage,Magic,Symbol(),StopLoss,TakeProfit,TrailingStop);

   TrueLong = false;

   TrueShort = false;

   TrueCloseLong = false;

   TrueCloseShort = false;

   LongTicket = -1;

   ShortTicket = -1;

   PreviousTime = Time[0];

   SignalMode = 0;

   return(INIT_SUCCEEDED);

  }

//--  Expert deinitialization function   --------------------------------   D  ---

void OnDeinit(const int reason)

  {

      

  }

//--  Expert tick function   --------------------------------------------   T  ---

void OnTick()

  {

//--  The algorithm  ----------------------------------------------------   3  ---

   if(SignalMode==1)

   {

      if(PreviousTime==Time[0])

      {

         

      }

      else

      {

         Trade();

      }

      PreviousTime = Time[0];

   } 

   else

   {

      Trade();

   } 

//--  The algorithm has finished  

  }

//------------------------------------------------------------------------  F  ---

void Trade(){

   //--  Variables

   double variable1 =  iMA(NULL,PERIOD_M15,6,1,MODE_EMA,PRICE_CLOSE,1) ;

   double variable2 =  iMA(NULL,PERIOD_M15,15,3,MODE_SMA,PRICE_CLOSE,1) ;

   //-- double variable3 = iRSI(NULL, PERIOD_M15,14,PRICE_CLOSE,1) ;


   //--  Long Conditions

   TrueLong = variable1 +0.1 > variable2 ; 

   //--  Short Conditions

   TrueShort = variable2 > variable1 -0.1 ; 

   //--  Close Long Conditions

   TrueCloseLong = variable2 > variable1 -0.1 ;

   //--  Close Short Conditions

   TrueCloseShort = variable1 +0.1 > variable2 ; 

   //-- Trade

   if(OrderSelect(LongTicket,SELECT_BY_TICKET))

      {

         if(OrderCloseTime()!=0)

         {

            LongTicket=-1;

         }   

      } 

   if(OrderSelect(ShortTicket,SELECT_BY_TICKET))

      {

         if(OrderCloseTime()!=0)

         {

            ShortTicket=-1;

         }   

      }

   if(TrueLong)

      {

         if(LongTicket==-1)

         {

            LongTicket = Long.Buy();

         }   

      }   

   if(TrueShort)

      {   

         if(ShortTicket==-1)

         {

            ShortTicket = Short.Sell();

         }

      }

   if(TrueCloseLong)

      {

         if(LongTicket>-1)

         {

            Long.CloseBuy(LongTicket);

            LongTicket = -1;

         }

      }

   if(TrueCloseShort)

      {

         if(ShortTicket>-1)

         {

            Short.CloseSell(ShortTicket);

            ShortTicket = -1;

         }   

      }

   if(LongTicket>-1)

   {   

      Long.DoLongTrailingStop(LongTicket);

   }

   if(ShortTicket>-1)

   {   

      Short.DoShortTrailingStop(ShortTicket); 

   }     

}

//----------------------------------------------------------------------  End  ---  


Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
Ask questions on technical analysis, discuss trading systems and improve your MQL5 programming skills to develop your own trading strategies. Communicate and share your experience with traders from anywhere in the world, answer questions and help beginners — MQL5.community is developing along with you. Help in coding Trading View strategy in...
 

Please use the </> button to insert your above code.


 

I think it's because of this:

FXEABuilder
 
How can it be FXEABUILDER, Marco?
 

Anyway, this giver you an idea of how many transactions I am talking about:


 
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your (original) post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. EA builder, EATree, fxDreema, FxPro, Molanis, Online Forex Expert Advisor Generator, Quant, Visual Trader Studio, MQL5 Wizard, etc. are all the same. You will get something quick, but then you will spend a much longer time trying to get it right, than if you learned the language up front, and then just wrote it.
    • Since you haven't learned MQL4/5, therefor there is no common language for us to communicate.
      If we tell you what you need, you can't code it.
      If we give you the code, you don't know how to integrate it into yours.
      We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem, but we are not going to debug your hundreds of lines of code. You are essentially going to be on your own.

    • EA builder makes bad code counting up while closing multiple orders.
      EA builder makes bad code Bars is unreliable (max bars on chart) volume is unreliable (miss ticks) Always use time. New candle - MQL4 forum
      EA builder makes bad code, not adjusting for 4/5 digit brokers, TP/SL and slippage.
      EA builder makes bad code, not adjusting for ECN brokers. (pre-Build 500)
      EA builder makes bad code, not checking return codes.
      EATree uses objects on chart to save values - not persistent storage (files or GV+Flush.) No recovery (crash/power failure.)
    Learn to code it or or pay (Freelance) someone to code it.

  3. Use the debugger or print out your variables, including _LastError and find out why.