Download MetaTrader 5

EA showing no errors but failing to place orders.

To add comments, please log in or register
Order a robot or an indicator for MetaTrader. The Freelance service will help you!
Paul Burkett
37
Paul Burkett 2016.10.24 03:28 

Hi, I am new to MQL 4 coding and trying to get up to speed.

This is my first EA and the strategy tester runs successfully but just does not place any trades. 


I have written part of an EA to enter a trade when the current candle has broken out of the 20 candle bollinger with 2 std dev then come back inside 20 candle bollinger with user entered std dev.

I have also used long MA's to determine the direction of the trend.

It will only enter when breaking opposite from trend.

I have no errors with the code except it is not taking trades, I can't work out why.

Any help would be greatly appreciated.


//+------------------------------------------------------------------+
//|                                         Burko's Bollinger EA.mq4 |
//|                                     Copyright 2016, Paul Burkett |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Paul Burkett"
#property link      ""
#property version   "1.00"
#property strict
extern int TakeProfit = 100;
extern int StopLoss = 50;
extern double LotSize = 0.10;
extern double EntryDeviation = 1.5;
extern int TrendDirectionBuffer =0;  //This is a buffer of how many pips to put between Floor & Roof
extern int  PadAmount=5;
extern int  CandlesBack=0;
extern double  RiskPercent=5;
extern double reward_ratio=2;
extern int  MagicNumber=1234;
extern int FloorMA=220;
int        FloorMAShift=0;
int        FloorMAMethod=1;
int        FloorMAAppliedTo=0;
extern int RoofMA=144;
int        RoofMAShift=0;
int        RoofMAMethod=1;
int        RoofMAAppliedTo=0;
int TrendDirection = 0;  // 0=Up,1=Down,2=No Direction
double pips;
int longentry;
int shortentry;
int sellticket;
int buyticket;



//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int Init()
//---
  {
   double ticksize= MarketInfo(Symbol(),MODE_TICKSIZE);
   if(ticksize == 0.00001|| ticksize == 0.001)
      pips=ticksize*10;
   else pips=ticksize;
   return(0);
  }
//---

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  
  }
//+------------------------------------------------------------------+
int start()
  {
  double CurrentFloor = iMA(NULL,0,FloorMA,FloorMAShift,FloorMAMethod,FloorMAAppliedTo,1);
  double CurrentRoof  = iMA(NULL,0,RoofMA,RoofMAShift,RoofMAMethod,RoofMAAppliedTo,1);

//+------------------------------------------------------------------+
//| Get Trend Direction                                              |
//+------------------------------------------------------------------+  
  if (CurrentFloor+TrendDirectionBuffer<CurrentRoof) TrendDirection = 0;
  if (CurrentFloor>CurrentRoof+TrendDirectionBuffer) TrendDirection = 1;
  else TrendDirection = 2;
  
//+------------------------------------------------------------------+
//| Long Entry Criteria                                              |
//+------------------------------------------------------------------+
  if (TrendDirection == 0)
     if (OpenOrdersThisPair(Symbol()) == 0)
     CheckForLongBollingerBandTrade ();
    

  
//+------------------------------------------------------------------+
//| Short Entry Criteria                                             |
//+------------------------------------------------------------------+
  if (TrendDirection == 1)
     if (OpenOrdersThisPair(Symbol()) == 0)
     CheckForShortBollingerBandTrade ();
    


//+------------------------------------------------------------------+
//| Long Exit Criteria                                               |
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//| Short Exit Criteria                                              |
//+------------------------------------------------------------------+
  return (0);
  }
  
//+------------------------------------------------------------------+
//Checks to see if Bollinger has been broken to the down side        |
//and then come back in on current candle.                           |
//+------------------------------------------------------------------+

int LowEntry()
   {
    
      if (Low[1]< iBands(NULL,0,20,2,0,PRICE_OPEN,MODE_LOW,1))
      if (Ask > iBands(NULL,0,20,EntryDeviation,0,PRICE_OPEN,MODE_LOW,0))longentry = 1;
   return (longentry);
   }
  
//+------------------------------------------------------------------+
//Checks to see if Bollinger has been broken to the up side          |
//and then come back in on current candle.                           |
//+------------------------------------------------------------------+

int HighEntry()
   {
  
   if (High[1]> iBands(NULL,0,20,2,0,PRICE_OPEN,MODE_HIGH,0))
      if (Bid < iBands(NULL,0,20,EntryDeviation,0,PRICE_OPEN,MODE_HIGH,1))shortentry = 1;
   return (shortentry);
   }
  
//+------------------------------------------------------------------+
//checks to see if any orders open on this currency pair.            |
//+------------------------------------------------------------------+
int OpenOrdersThisPair(string pair)
  {
   int total=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){continue;}
      if(OrderSymbol()==pair) total++;
     }
   return (total);
  }

//+------------------------------------------------------------------+
//order entry function                                               |
//+------------------------------------------------------------------+
void OrderEntry(int direction)
{
   if(direction==0)
   {
      double tp=Ask+TakeProfit*pips;
      double sl=Ask-StopLoss*pips;
      if(OpenOrdersThisPair(Symbol())==0)
      buyticket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,0,0,NULL,MagicNumber,0,Green);
      if(buyticket>0)int buymodify=(OrderModify(buyticket,OrderOpenPrice(),sl,tp,0,CLR_NONE));
   }
  
   if(direction==1)
   {
      double tp=Bid-TakeProfit*pips;
      double sl=Bid+StopLoss*pips;
      if(OpenOrdersThisPair(Symbol())==0)
      sellticket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,0,0,NULL,MagicNumber,0,Red);
      if(sellticket>0)int sellmodify=(OrderModify(sellticket,OrderOpenPrice(),sl,tp,0,CLR_NONE));
   }

}
//+------------------------------------------------------------------+
//| Check for short Bollinger trade                                  |
//+------------------------------------------------------------------+
void CheckForShortBollingerBandTrade ()
   {
   double  High2DevBand=iBands(NULL,0,20,2,0,PRICE_OPEN,MODE_HIGH,0);
   double  HighEntryDeviationBand = iBands(NULL,0,20,EntryDeviation,0,PRICE_OPEN,MODE_HIGH,0);
   if ((High[0]>High2DevBand) && (Bid < HighEntryDeviationBand))OrderEntry(1);
   }
  
//+------------------------------------------------------------------+
//| Check for long Bollinger trade                                   |
//+------------------------------------------------------------------+
void CheckForLongBollingerBandTrade ()
   {
   double Low2DevBand=iBands(NULL,0,20,2,0,PRICE_OPEN,MODE_LOW,0);
   double LowEntryDeviationBand = iBands(NULL,0,20,EntryDeviation,0,PRICE_OPEN,MODE_LOW,0);
   if ((Low[0]< Low2DevBand) && (Ask > LowEntryDeviationBand))OrderEntry(0);  
   }


GBWT094114
3343
GBWT094114 2016.10.24 06:38  
I hope you will realize soon, that no matter how complex your code will be, in the current MetaQuotes MQL programming options, any kind of expert programmer will not succeed ever creating any expert that will make money. Do you know Why?
Carl Schreiber
6588
Carl Schreiber 2016.10.26 20:06  
Paul Burkett:

Hi, I am new to MQL 4 coding and trying to get up to speed.

This is my first EA and the strategy tester runs successfully but just does not place any trades. 


I have written part of an EA to enter a trade when the current candle has broken out of the 20 candle bollinger with 2 std dev then come back inside 20 candle bollinger with user entered std dev.

I have also used long MA's to determine the direction of the trend.

It will only enter when breaking opposite from trend.

I have no errors with the code except it is not taking trades, I can't work out why.

Any help would be greatly appreciated.

You don't check the error code in case you haven't got a ticket number!!

E.g.:

      sellticket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,0,0,NULL,MagicNumber,0,Red);
      if(sellticket<0Print("no ticket number: "+(string)GetLastError());  // -1 if error!! very simple not checked!!
     
else int sellmodify=(OrderModify(sellticket,OrderOpenPrice(),sl,tp,0,CLR_NONE));

Mladen Rakic
15640
Mladen Rakic 2016.10.27 00:33  
Paul Burkett:

Hi, I am new to MQL 4 coding and trying to get up to speed.

This is my first EA and the strategy tester runs successfully but just does not place any trades. 


I have written part of an EA to enter a trade when the current candle has broken out of the 20 candle bollinger with 2 std dev then come back inside 20 candle bollinger with user entered std dev.

I have also used long MA's to determine the direction of the trend.

It will only enter when breaking opposite from trend.

I have no errors with the code except it is not taking trades, I can't work out why.

Any help would be greatly appreciated.


You have both OnTick()  (with empty body) and start() in the same EA. In that case empty OnTick() is executed - your code in the start() is never reached

Anyway : some more code changes made and now it trades. Happy coding :)


To add comments, please log in or register