NEW SCALPER EA ISSUES ? !!!

 

Dear all,

 

I am trying to make an ea which trade in 1m chart, with the help of few indicators like MACD,MA,ADX,BB

the strategy for long is  MA8 close abouve BB middle line,ADX should be abouve 25,and MACD moves above 0 leval from down,

trade long close when MACD create a negative crossover from above,

The same opposit for selling side,

i coded,myself but i have some difficulties in it,after the coding EA not trading,if i made some mistake in my coding please help me to correct,

thanks in advance for your helping mind .

//+------------------------------------------------------------------+
//|                                                        JAIL5.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, AM2."
#property link      "http://www.mql5.com"
#property version   "1.05"
//--- input parameters
input int bands_period= 20;          // Bollinger Bands period
input int  ADX_Period=14;             // ADX Period
input int  MACD_Period = 14;
input int MACDLeval = 0;
input int fastEMAPeriod = 12;                      // Fast EMA period
input int slowEMAPeriod = 26;                      // Slow EMA period
input int signalPeriod = 9;                        // Difference period
input double   Adx_Min=25.0;     // Minimum ADX Value
input int   MA_Period=8;            // Moving Average Period
input int bands_shift = 0;         // Bollinger Bands shift
input double deviation= 2;         // Standard deviation
input double   Lot=1;            // Lots to trade
//--- global variables
int adxHandle; // handle for our ADX indicator
int maHandle;  // handle for our Moving Average indicator
int MACDHandle;
double MACDValues[];
double plsDI[],minDI[],adxVal[]; // Dynamic arrays to hold the values of +DI, -DI and ADX values for each bars
int BolBandsHandle;                // Bolinger Bands handle
double BBUp[],BBLow[],BBMidle[];   // dynamic arrays for numerical values of Bollinger Bands
double maVal[];     // dynamic array to hold the values of Moving Average for each bars

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
  //--- get handle for ADX indicator
   adxHandle=iADX(NULL,0,ADX_Period);
   //--- get the handle for Moving Average indicator
   maHandle=iMA(_Symbol,_Period,MA_Period,0,MODE_EMA,PRICE_CLOSE);
   //--- what if handle returns Invalid Handle
   MACDHandle = iMACD(_Symbol,_Period,fastEMAPeriod,slowEMAPeriod,signalPeriod, PRICE_CLOSE);   
     
   if(adxHandle<0 || maHandle<0 || MACDHandle<0)
     {
      Alert("Error Creating Handles for indicators - error: ",GetLastError(),"!!");
     }
//--- 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
   BolBandsHandle=iBands(NULL,PERIOD_CURRENT,bands_period,bands_shift,deviation,PRICE_CLOSE);
   
//--- let's serialize the buffers copied
   ArraySetAsSeries(adxVal,true);
   ArraySetAsSeries(maVal,true);
   ArraySetAsSeries(MACDValues, true); 
   ArraySetAsSeries(minDI,true);
   ArraySetAsSeries(plsDI,true);
//--- Check for Invalid Handle
   if((BolBandsHandle<0) )
     {
      Alert("Error in creation of indicators - error: ",GetLastError(),"!!");
      return(-1);
     }

   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- release indicator handles
   IndicatorRelease(BolBandsHandle);
   IndicatorRelease(adxHandle);
   IndicatorRelease(maHandle);
   IndicatorRelease(MACDHandle);
   

  }
//+------------------------------------------------------------------+
//| 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);
   ArraySetAsSeries(maVal,true);
// the indicator arrays
   ArraySetAsSeries(BBUp,true);
   ArraySetAsSeries(BBLow,true);
   ArraySetAsSeries(BBMidle,true);
   ArraySetAsSeries(MACDValues, true);   
//--- the ADX DI+values array
   ArraySetAsSeries(plsDI,true);
//--- the ADX DI-values array
   ArraySetAsSeries(minDI,true);
//--- the ADX values arrays
   ArraySetAsSeries(adxVal,true);
//--- the MA-8 values arrays
   ArraySetAsSeries(maVal,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(BolBandsHandle,0,0,3,BBMidle)<0 || CopyBuffer(BolBandsHandle,1,0,3,BBUp)<0
      || CopyBuffer(BolBandsHandle,2,0,3,BBLow)<0)
     {
      Alert("Error copying Bollinger Bands indicator Buffers - error:",GetLastError(),"!!");
      return;
     }
//--- copy the new values of our indicators to buffers (arrays) using the handle
   if(CopyBuffer(adxHandle,0,0,3,adxVal)<0 || CopyBuffer(adxHandle,1,0,3,plsDI)<0
      || CopyBuffer(adxHandle,2,0,3,minDI)<0)
     {
      Alert("Error copying ADX indicator Buffers - error:",GetLastError(),"!!");
      ResetLastError();
      return;
     }
   if(CopyBuffer(maHandle,0,0,3,maVal)<0)
     {
      Alert("Error copying Moving Average indicator buffer - 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 =( adxVal[0]>adxVal[1] && adxVal[0]>Adx_Min && MACDValues[2]<MACDLeval && MACDValues[1] > MACDLeval && maVal[1]>BBMidle[1]);  // White (bull) candle crossed the Lower Band from below to above
                        
   bool Sell_Condition = ( adxVal[0]>adxVal[1] && adxVal[0]>Adx_Min && MACDValues[2]>MACDLeval && MACDValues[1]<MACDLeval && maVal[1]<BBMidle[1]);
                            
                          
   bool Buy_Close=(MACDValues[1] < signalPeriod );              // Black candle crossed the Upper Band from above to below

   bool Sell_Close=(MACDValues[1] > signalPeriod);           // White candle crossed the Lower Band from below to above

   if(Buy_Condition && !PositionSelect(_Symbol))    // Open long position
     {                                              // DEÌÀ is growing up
      LongPositionOpen();                           // and white candle crossed the Lower Band from below to above
     }

   if(Sell_Condition && !PositionSelect(_Symbol))   // Open short position
     {                                              // DEÌÀ is falling down
      ShortPositionOpen();                          // and Black candle crossed the Upper Band from above to below
     }

   if(Buy_Close && PositionSelect(_Symbol))         // Close long position
     {                                              // Black candle crossed the Upper Band from above to below
      LongPositionClose();
     }

   if(Sell_Close && PositionSelect(_Symbol))        // Close short position
     {                                              // White candle crossed the Lower Band from below to above
      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
     }
  }
//+------------------------------------------------------------------+

 Thank you

Suresh

India 

 
surubabs:

Dear all,

 

I am trying to make an ea which trade in 1m chart, with the help of few indicators like MACD,MA,ADX,BB

the strategy for long is  MA8 close abouve BB middle line,ADX should be abouve 25,and MACD moves above 0 leval from down,

trade long close when MACD create a negative crossover from above,

The same opposit for selling side,

i coded,myself but i have some difficulties in it,after the coding EA not trading,if i made some mistake in my coding please help me to correct,

thanks in advance for your helping mind .

 Thank you

Suresh

India 

What is your problem exactly ? No trade open, on strategy tester ? on live chart ?

Do you have error message in Journal and/or Experts tabs ?

May be you have there, a message similar to :

2013.03.29 18:13:15    Core 1    2013.03.01 00:00:00   array out of range in 'JAIL5.mq5' (193,30)

 
surubabs:

Dear all,

 

I am trying to make an ea which trade in 1m chart, with the help of few indicators like MACD,MA,ADX,BB

the strategy for long is  MA8 close abouve BB middle line,ADX should be abouve 25,and MACD moves above 0 leval from down,

trade long close when MACD create a negative crossover from above,

The same opposit for selling side,

i coded,myself but i have some difficulties in it,after the coding EA not trading,if i made some mistake in my coding please help me to correct,

thanks in advance for your helping mind .

 Thank you

Suresh

India 


I think you miss this line (yellow mark)


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);
   ArraySetAsSeries(maVal,true);
// the indicator arrays
   ArraySetAsSeries(BBUp,true);
   ArraySetAsSeries(BBLow,true);
   ArraySetAsSeries(BBMidle,true);
   ArraySetAsSeries(MACDValues, true);   
//--- the ADX DI+values array
   ArraySetAsSeries(plsDI,true);
//--- the ADX DI-values array
   ArraySetAsSeries(minDI,true);
//--- the ADX values arrays
   ArraySetAsSeries(adxVal,true);
//--- the MA-8 values arrays
   ArraySetAsSeries(maVal,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(BolBandsHandle,0,0,3,BBMidle)<0 || CopyBuffer(BolBandsHandle,1,0,3,BBUp)<0
      || CopyBuffer(BolBandsHandle,2,0,3,BBLow)<0)
     {
      Alert("Error copying Bollinger Bands indicator Buffers - error:",GetLastError(),"!!");
      return;
     }
//--- copy the new values of our indicators to buffers (arrays) using the handle
   if(CopyBuffer(adxHandle,0,0,3,adxVal)<0 || CopyBuffer(adxHandle,1,0,3,plsDI)<0
      || CopyBuffer(adxHandle,2,0,3,minDI)<0)
     {
      Alert("Error copying ADX indicator Buffers - error:",GetLastError(),"!!");
      ResetLastError();
      return;
     }
   if(CopyBuffer(maHandle,0,0,3,maVal)<0)
     {
      Alert("Error copying Moving Average indicator buffer - error:",GetLastError());
      ResetLastError();
      return;
     }
   if(CopyBuffer(MACDHandle,0,0,3,MACDValues)<0)
     {
      Alert("Error copying MACD indicator buffer - 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 =( adxVal[0]>adxVal[1] && adxVal[0]>Adx_Min && MACDValues[2]<MACDLeval && MACDValues[1] > MACDLeval && maVal[1]>BBMidle[1]);  // White (bull) candle crossed the Lower Band from below to above
                        
   bool Sell_Condition = ( adxVal[0]>adxVal[1] && adxVal[0]>Adx_Min && MACDValues[2]>MACDLeval && MACDValues[1]<MACDLeval && maVal[1]<BBMidle[1]);
                            
                          
   bool Buy_Close=(MACDValues[1] < signalPeriod );              // Black candle crossed the Upper Band from above to below

   bool Sell_Close=(MACDValues[1] > signalPeriod);           // White candle crossed the Lower Band from below to above

   if(Buy_Condition && !PositionSelect(_Symbol))    // Open long position
     {                                              // DEÌÀ is growing up
      LongPositionOpen();                           // and white candle crossed the Lower Band from below to above
     }

   if(Sell_Condition && !PositionSelect(_Symbol))   // Open short position
     {                                              // DEÌÀ is falling down
      ShortPositionOpen();                          // and Black candle crossed the Upper Band from above to below
     }

   if(Buy_Close && PositionSelect(_Symbol))         // Close long position
     {                                              // Black candle crossed the Upper Band from above to below
      LongPositionClose();
     }

   if(Sell_Close && PositionSelect(_Symbol))        // Close short position
     {                                              // White candle crossed the Lower Band from below to above
      ShortPositionClose();
     }

   return;
  }
 
achidayat:

I think you miss this line (yellow mark)

Thank you achidayat, u did it for, u are correct now its working fine.
 

hiiiii i am jignesh if u have any scalp ea will you send me on my mail id  pathak_jignesh@yahoo.co.in

thanks 

surubabs: 

Thank you achidayat, u did it for, u are correct now its working fine.hii
 
pathakjignesh:
The same i have posted abouve u can copy from there, i dont keep that in my MT5, i code it only for studyes perpous,