MY ADX EA NOT TRADING ! NEED HELP !

 

Hi,

I created an EA for study perpous on the base of ADX, which take a long +DI > -DI and Short when -DI>+DI, but the EA which i created is not trading at all, need help from

the programers or some one who familiar with this.

i am posting my ea here 

Thanks in advance

Suresh 

India

//+------------------------------------------------------------------+
//|                                                         ADX1.mq5 |
//|                                        Copyright 2013, Surubabs. |
//|                               https://www.mql5.com/en/forum/11033 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, Surubabs"
#property link      "https://www.mql5.com/en/forum/11033"
#property version   "1.00"

//--- input parameters
input int      iADXWilder_Period=14;     // ADX Period
input double   iADXWilder_Min=20.0;     // Minimum ADX Value
input double   iADXWilder_Max=35.0;     // Minimum ADX Value
input double   Lot=0.5;            // Lots to trade
//--- global variables
int         iADXWilder_handle;           // handle of the indicator iADXWilder
double      Main[];    // array for MAIN_LINE of iADXWilder
double      PlusDI[];  // array for PLUSDI_LINE of iADXWilder
double      MinusDI[]; // array for MINUSDI_LINE of iADXWilder
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Do we have sufficient bars to work
   if(Bars(_Symbol,_Period)<60) // total number of bars is less than 60?
     {
      Alert("We have less than 60 bars on the chart, an Expert Advisor terminated!!");
      return(-1);
     }
//--- get handle of the Bollinger Bands and DEMA indicators
   iADXWilder_handle=iADXWilder(NULL,0,iADXWilder_Period);
//--- Check for Invalid Handle
   if(CopyBuffer(iADXWilder_handle,0,0,3,Main) <0 || CopyBuffer(iADXWilder_handle,1,0,3,PlusDI)<0
      || CopyBuffer(iADXWilder_handle,2,0,3,MinusDI)<0)
     {
      Alert("Error copying ADX indicator Buffers - error:",GetLastError(),"!!");
      ResetLastError();
      return(-1);
     }
   return(0);
  }  
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- release indicator handles
   IndicatorRelease(iADXWilder_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
     {
      Alert("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;
     }

//--- do we have enough bars to work with
   int Mybars=Bars(_Symbol,_Period);
   if(Mybars<60) // if total bars is less than 60 bars
     {
      Alert("We have less than 60 bars, EA will now exit!!");
      return;
     }

   MqlRates mrate[];          // To be used to store the prices, volumes and spread of each bar   

/*
     Let's make sure our arrays values for the Rates and Indicators 
     is stored serially similar to the timeseries array
*/

// the rates arrays
   ArraySetAsSeries(mrate,true);
// the ADX DI+values array
   ArraySetAsSeries(PlusDI,true);
// the ADX DI-values array
   ArraySetAsSeries(MinusDI,true);
// the ADX values arrays
   ArraySetAsSeries(Main,true);


//--- Get the details of the latest 3 bars
   if(CopyRates(_Symbol,_Period,0,3,mrate)<0)
     {
      Alert("Error copying rates/history data - error:",GetLastError(),"!!");
      return;
     }

//--- Copy the new values of our indicators to buffers (arrays) using the handle
   if(CopyBuffer(iADXWilder_handle,0,0,3,Main) <0 || CopyBuffer(iADXWilder_handle,1,0,3,PlusDI)<0
      || CopyBuffer(iADXWilder_handle,2,0,3,MinusDI)<0)
     {
      Alert("Error copying ADX indicator Buffers - error:",GetLastError(),"!!");
      ResetLastError();
      return;
     }

   

   double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);   // Ask price
   double Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);   // Bid price

//--- Declare bool type variables to hold our Buy and Sell Conditions
   bool Buy_Condition =(PlusDI[0]>MinusDI[1]);  
                        

   bool Sell_Condition = (MinusDI[0]>PlusDI[1]);  
                          
   bool Buy_Close=(MinusDI[0]>PlusDI[1]);              

   bool Sell_Close=(PlusDI[0]>MinusDI[1]);           

   if(Buy_Condition && !PositionSelect(_Symbol))    
     {                                              
      LongPositionOpen();                           
     }

   if(Sell_Condition && !PositionSelect(_Symbol))   // Open short position
     {                                              
      ShortPositionOpen();                          
     }

   if(Buy_Close && PositionSelect(_Symbol))         // Close long position
     {                                              
      LongPositionClose();
     }

   if(Sell_Close && PositionSelect(_Symbol))        // Close short position
     {                                              
      ShortPositionClose();
     }

   return;
  }
//+------------------------------------------------------------------+
//| Open Long position                                               |
//+------------------------------------------------------------------+
void LongPositionOpen()
  {
   MqlTradeRequest mrequest;                             // Will be used for trade requests
   MqlTradeResult mresult;                               // Will be used for results of trade requests
   
   ZeroMemory(mrequest);
   ZeroMemory(mresult);
   
   double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);    // Ask price
   double Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);    // Bid price

   if(!PositionSelect(_Symbol))
     {
      mrequest.action = TRADE_ACTION_DEAL;               // Immediate order execution
      mrequest.price = NormalizeDouble(Ask,_Digits);     // Lastest Ask price
      mrequest.sl = 0;                                   // Stop Loss
      mrequest.tp = 0;                                   // Take Profit
      mrequest.symbol = _Symbol;                         // Symbol
      mrequest.volume = Lot;                             // Number of lots to trade
      mrequest.magic = 0;                                // Magic Number
      mrequest.type = ORDER_TYPE_BUY;                    // Buy Order
      mrequest.type_filling = ORDER_FILLING_FOK;         // Order execution type
      mrequest.deviation=5;                              // Deviation from current price
      OrderSend(mrequest,mresult);                       // Send order
     }
  }
//+------------------------------------------------------------------+
//| Open Short position                                              |
//+------------------------------------------------------------------+
void ShortPositionOpen()
  {
   MqlTradeRequest mrequest;                             // Will be used for trade requests
   MqlTradeResult mresult;                               // Will be used for results of trade requests
   
   ZeroMemory(mrequest);
   ZeroMemory(mresult);
   
   double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);    // Ask price
   double Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);    // Bid price

   if(!PositionSelect(_Symbol))
     {
      mrequest.action = TRADE_ACTION_DEAL;               // Immediate order execution
      mrequest.price = NormalizeDouble(Bid,_Digits);     // Lastest Bid price
      mrequest.sl = 0;                                   // Stop Loss
      mrequest.tp = 0;                                   // Take Profit
      mrequest.symbol = _Symbol;                         // Symbol
      mrequest.volume = Lot;                             // Number of lots to trade
      mrequest.magic = 0;                                // Magic Number
      mrequest.type= ORDER_TYPE_SELL;                    // Sell order
      mrequest.type_filling = ORDER_FILLING_FOK;         // Order execution type
      mrequest.deviation=5;                              // Deviation from current price
      OrderSend(mrequest,mresult);                       // Send order
     }
  }
//+------------------------------------------------------------------+
//| Close Long position                                              |
//+------------------------------------------------------------------+
void LongPositionClose()
  {
   MqlTradeRequest mrequest;                             // Will be used for trade requests
   MqlTradeResult mresult;                               // Will be used for results of trade requests
   
   ZeroMemory(mrequest);
   ZeroMemory(mresult);
   
   double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);    // Ask price
   double Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);    // Bid price

   if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
     {
      mrequest.action = TRADE_ACTION_DEAL;               // Immediate order execution
      mrequest.price = NormalizeDouble(Bid,_Digits);     // Lastest Bid price
      mrequest.sl = 0;                                   // Stop Loss
      mrequest.tp = 0;                                   // Take Profit
      mrequest.symbol = _Symbol;                         // Symbol
      mrequest.volume = Lot;                             // Number of lots to trade
      mrequest.magic = 0;                                // Magic Number
      mrequest.type= ORDER_TYPE_SELL;                    // Sell order
      mrequest.type_filling = ORDER_FILLING_FOK;         // Order execution type
      mrequest.deviation=5;                              // Deviation from current price
      OrderSend(mrequest,mresult);                       // Send order
     }
  }
//+------------------------------------------------------------------+
//| Close Short position                                             |
//+------------------------------------------------------------------+
void ShortPositionClose()
  {
   MqlTradeRequest mrequest;                             // Will be used for trade requests
   MqlTradeResult mresult;                               // Will be used for results of trade requests
   
   ZeroMemory(mrequest);
   ZeroMemory(mresult);
   
   double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);    // Ask price
   double Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);    // Bid price

   if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
     {
      mrequest.action = TRADE_ACTION_DEAL;               // Immediate order execution
      mrequest.price = NormalizeDouble(Ask,_Digits);     // Latest ask price
      mrequest.sl = 0;                                   // Stop Loss
      mrequest.tp = 0;                                   // Take Profit
      mrequest.symbol = _Symbol;                         // Symbol
      mrequest.volume = Lot;                             // Number of lots to trade
      mrequest.magic = 0;                                // Magic Number
      mrequest.type = ORDER_TYPE_BUY;                    // Buy order
      mrequest.type_filling = ORDER_FILLING_FOK;         // Order execution type
      mrequest.deviation=5;                              // Deviation from current price
      OrderSend(mrequest,mresult);                       // Send order
     }
  }
//+------------------------------------------------------------------+
 
surubabs:

Hi,

I created an EA for study perpous on the base of ADX, which take a long +DI > -DI and Short when -DI>+DI, but the EA which i created is not trading at all, need help from

the programers or some one who familiar with this.

i am posting my ea here 

Thanks in advance

Suresh 

India

Your EA is trading with immediate execution, but not with market execution. See documentation.

Did you check the necessary options to allow trading?

 
surubabs:

Hi,

I created an EA for study perpous on the base of ADX, which take a long +DI > -DI and Short when -DI>+DI, but the EA which i created is not trading at all, need help from

the programers or some one who familiar with this.

i am posting my ea here 

Thanks in advance

Suresh 

India

The error from Check invalid handle on OnInit()

You must replace yellow mark :

int OnInit()
  {
//--- Do we have sufficient bars to work
   if(Bars(_Symbol,_Period)<60) // total number of bars is less than 60?
     {
      Alert("We have less than 60 bars on the chart, an Expert Advisor terminated!!");
      return(-1);
     }
//--- get handle of the Bollinger Bands and DEMA indicators
   iADXWilder_handle=iADXWilder(NULL,0,iADXWilder_Period);
//--- Check for Invalid Handle
   if(CopyBuffer(iADXWilder_handle,0,0,3,Main) <0 || CopyBuffer(iADXWilder_handle,1,0,3,PlusDI)<0
      || CopyBuffer(iADXWilder_handle,2,0,3,MinusDI)<0)
     {
      Alert("Error copying ADX indicator Buffers - error:",GetLastError(),"!!");
      ResetLastError();
      return(-1);
     }
   return(0);
  }  

with this one :

int OnInit()
  {
//--- Do we have sufficient bars to work
   if(Bars(_Symbol,_Period)<60) // total number of bars is less than 60?
     {
      Alert("We have less than 60 bars on the chart, an Expert Advisor terminated!!");
      return(-1);
     }
//--- get handle of the Bollinger Bands and DEMA indicators
   iADXWilder_handle=iADXWilder(NULL,0,iADXWilder_Period);
//--- Check for Invalid Handle
   if(iADXWilder_handle<0)
     {
      Alert("Error copying ADX indicator Buffers - error:",GetLastError(),"!!");
      ResetLastError();
      return(-1);
     }
   return(0);
  }  
 

So what should i do, there is nothing wrong with the order type when i looked at,

is there any problem with the buy and sell condition ?

Pleasae help. 

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Order Properties - Documentation on MQL5
 
surubabs:

So what should i do, there is nothing wrong with the order type when i looked at,

is there any problem with the buy and sell condition ?

Pleasae help. 

Do you understand what I mean?. Please look at the script at OnInit(), what you should to do is only REPLACE this line :

if(CopyBuffer(iADXWilder_handle,0,0,3,Main) <0 || CopyBuffer(iADXWilder_handle,1,0,3,PlusDI)<0
      || CopyBuffer(iADXWilder_handle,2,0,3,MinusDI)<0)

with this one :

if(iADXWilder_handle<0)
 
surubabs:

So what should i do, there is nothing wrong with the order type when i looked at,

is there any problem with the buy and sell condition ?

Pleasae help. 

  • Do not send 3 times same comment.
  • You can't simply copy and paste code, change indicator and hope that works. You have to understand what you do.
  • What you should do is to begin to learn to code by reading documentation.
  • Check journal and experts tabs to see error messages.
  • Do the modification that achidayat show you.
  • Verify that your broker allow "immediate execution" of order, if not, modify your code consequently.