Download MetaTrader 5

Looking for help on this EA

To add comments, please log in or register
MQL5 Reference is available in 8 languages. Learn MQL5 in the language that suits you best!
Scott Watson
134
Scott Watson 2011.02.22 23:23 

Hello Everyone,

 

I cant seem to figure out this EA, when I place it on a chart it works as intended. When I backtest it no trades are made.

 

Can you eyeball this code and tell me what I am doing wrong?

Most of the code is from this article

 https://www.mql5.com/en/articles/100

input uchar    Smooth=9;
input uchar    Length=9;
input float    Min_Threshold=0.0;
input uchar    Stop_Loss=100;
input uchar    Take_Profit=100;

int Trigger_Handle;
int EA_Magic = 19288;

double Trigger[];

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   Trigger_Handle = iCustom(NULL, 0, "MyMACross", Smooth, Length, Min_Threshold);
   
   if ( Trigger_Handle < 0 )
      Print("Failed to get Handles");
      
   ArraySetAsSeries(Trigger,true);    
   return(0);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   IndicatorRelease(Trigger_Handle);   
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   // We will use the static Old_Time variable to serve the bar time.
   // At each OnTick execution we will check the current bar time with the saved one.
   // If the bar time isn't equal to the saved time, it indicates that we have a new tick.
   static datetime Old_Time;
   datetime New_Time[1];
   bool IsNewBar=false;
   
   // copying the last bar time to the element New_Time[0]
   int copied=CopyTime(_Symbol,_Period,0,1,New_Time);
   if(copied>0) // ok, the data has been copied successfully
   {
      if(Old_Time!=New_Time[0]) // if old time isn't equal to new bar time
      {
         IsNewBar=true;   // if it isn't a first call, the new bar has appeared
         if(MQL5InfoInteger(MQL5_DEBUGGING)) Print("We have new bar here ",New_Time[0]," old time was ",Old_Time);
         Old_Time=New_Time[0];            // saving bar time
      }
   }
   else
   {
      Print("Error in copying historical times data, error =",GetLastError());
      ResetLastError();
      return;
   }

   //--- EA should only check for new trade if we have a new bar
   if(IsNewBar==false)
      return;
   
   int BarCount = Bars(NULL,0);
   
   MqlTick m_price;              // To be used for getting recent/latest price quotes
   MqlTradeRequest m_request;    // To be used for sending our trade requests
   MqlTradeResult m_result;      // To be used to get our trade results
   MqlRates m_rate[];            // To be used to store the prices, volumes and spread of each bar
      
   CopyRates(_Symbol,_Period,0,3,m_rate);
   CopyBuffer(Trigger_Handle, 0, 0, 30, Trigger);
      
   bool Buy_opened=false;  // variable to hold the result of Buy opened position
   bool Sell_opened=false; // variable to hold the result of Sell opened position
    
   if (PositionSelect(_Symbol) ==true)  // we have an opened position
   {
      if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
      {
         Buy_opened = true;  //It is a Buy
      }
      else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
      {
         Sell_opened = true; // It is a Sell
      }
   }
      
   bool Buy_Condition = (Trigger[1] == -1);
   bool Sell_Condition = (Trigger[1] == 1);
   double MyLots = 0.1;

   if ( Buy_Condition == true)
   {
      if (!Buy_opened)
      {
         if(Sell_opened == true)
            MyLots = 0.2;
            
         m_request.action = TRADE_ACTION_DEAL;                                      // immediate order execution
         m_request.price = NormalizeDouble(m_price.ask,_Digits);                    // latest Bid price
         m_request.sl = NormalizeDouble(m_price.ask - Stop_Loss*_Point,_Digits);    // Stop Loss
         m_request.tp = NormalizeDouble(m_price.ask + Take_Profit*_Point,_Digits);  // Take Profit
         m_request.symbol = _Symbol;                                                // currency pair
         m_request.volume = MyLots;                                                 // number of lots to trade
         m_request.magic = EA_Magic;                                                // Order Magic Number
         m_request.type= ORDER_TYPE_BUY;                                            // Buy Order
         m_request.type_filling = ORDER_FILLING_AON;                                // Order execution type
         m_request.deviation=100;                                                   // Deviation from current price
         //--- send order
         OrderSend(m_request, m_result);
            
         // get the result code
         if(m_result.retcode==10009 || m_result.retcode==10008) //Request is completed or order placed
         {
            Print("A Buy order has been successfully placed with Ticket#:",m_result.order,"!!");
         }
         else
         {
            Print("The Buy order request could not be completed -error:",GetLastError());
            ResetLastError();           
            return;
         }
      }
   }
      
   if ( Sell_Condition == true)
   {
      if (!Sell_opened)
      {
         if(Buy_opened == true)
            MyLots = 0.2;
          
         m_request.action = TRADE_ACTION_DEAL;                                      // immediate order execution
         m_request.price = NormalizeDouble(m_price.bid,_Digits);                    // latest Bid price
         m_request.sl = NormalizeDouble(m_price.bid + Stop_Loss*_Point,_Digits);    // Stop Loss
         m_request.tp = NormalizeDouble(m_price.bid - Take_Profit*_Point,_Digits);  // Take Profit
         m_request.symbol = _Symbol;                                                // currency pair
         m_request.volume = MyLots;                                                 // number of lots to trade
         m_request.magic = EA_Magic;                                                // Order Magic Number
         m_request.type= ORDER_TYPE_SELL;                                           // Sell Order
         m_request.type_filling = ORDER_FILLING_AON;                                // Order execution type
         m_request.deviation=100;                                                   // Deviation from current price
         //--- send order
         OrderSend(m_request, m_result);
            
         if(m_result.retcode==10009 || m_result.retcode==10008) //Request is completed or order placed
         {
            Print("A Sell order has been successfully placed with Ticket#:",m_result.order,"!!");
         }
         else
         {
            Print("The Sell order request could not be completed -error:",GetLastError());
            ResetLastError();
            return;
         }
      }
   }
   
   return;
}

Thank you

EK 

Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners
  • 2010.06.09
  • Samuel
  • www.mql5.com
The Expert Advisors programming in MQL5 is simple, and you can learn it easy. In this step by step guide, you will see the basic steps required in writing a simple Expert Advisor based on a developed trading strategy. The structure of an Expert Advisor, the use of built-in technical indicators and trading functions, the details of the Debug mode and use of the Strategy Tester are presented.
Scott Watson
134
Scott Watson 2011.02.23 22:43  

I have figured it out, my problem was due to the way I coded the indicator.

 

Thanks

EK 

To add comments, please log in or register