Help to fix EA

 

Dear my friends,

I already code an ea followed this rule:

1. Open Order Buy when Price touch Low bollinger Bands, Close once it touch Mid band( SMA20), Sl 25 pip, tp 50 pip

2. Open Order Sell when Price touch Upper bollinger Bands, Close once it touch Mid band( SMA20), Sl 25 pip, tp 50 pip.

it work well except i doest not automatically close once the price (Bid/Ask) touch MiD Bands.

I already highligh it

below is its code. kindly help me :((


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

//|                                    EA-BollingerBreakout_v1-0.mq4 |

//|                                                    Luca Spinello |

//|                                https://mql4tradingautomation.com |

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


#property copyright     "Luca Spinello - mql4tradingautomation.com"

#property link          "https://mql4tradingautomation.com"

#property version       "1.00"

#property strict

#property description   "This Expert Advisor open orders at the breakout of Bollinger Bands"

#property description   " "

#property description   "DISCLAIMER: This code comes with no guarantee, you can use it at your own risk"

#property description   "We recommend to test it first on a Demo Account"


/*

The Alligator Indicator is basically a combination of 3 MA with different periods and shifts

ENTRY BUY: The price breaks above the top bollinger band

ENTRY SELL: The price breaks below the low bollinger band

EXIT: Fixed take profit or hit stop loss

Only 1 order at a time

*/



extern double LotSize=0.1;             //Position size


extern double StopLoss=20;             //Stop loss in pips

extern double TakeProfit=5;           //Take profit in pips


extern int Slippage=2;                 //Slippage in pips


extern bool TradeEnabled=true;         //Enable trade


//Functional variables

double ePoint;                         //Point normalized


bool CanOrder;                         //Check for risk management

bool CanOpenBuy;                       //Flag if there are buy orders open

bool CanOpenSell;                      //Flag if there are sell orders open


int OrderOpRetry=10;                   //Number of attempts to perform a trade operation

int SleepSecs=3;                       //Seconds to sleep if can't order

int MinBars=60;                        //Minimum bars in the graph to enable trading


//Functional variables to determine prices

double MinSL;

double MaxSL;

double TP;

double SL;

double Spread;

int Slip; 



//Variable initialization function

void Initialize(){          

   RefreshRates();

   ePoint=Point;

   Slip=Slippage;

   if (MathMod(Digits,2)==1){

      ePoint*=10;

      Slip*=10;

   }

   TP=TakeProfit*ePoint;

   SL=StopLoss*ePoint;

   CanOrder=TradeEnabled;

   CanOpenBuy=true;

   CanOpenSell=true;

}



//Check if orders can be submitted

void CheckCanOrder(){            

   if( Bars<MinBars ){

      Print("INFO - Not enough Bars to trade");

      CanOrder=false;

   }

   OrdersOpen();

   return;

}



//Check if there are open orders and what type

void OrdersOpen(){

   for( int i = 0 ; i < OrdersTotal() ; i++ ) {

      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ) {

         Print("ERROR - Unable to select the order - ",GetLastError());

         break;

      } 

      if( OrderSymbol()==Symbol() && OrderType() == OP_BUY) CanOpenBuy=false;

      if( OrderSymbol()==Symbol() && OrderType() == OP_SELL) CanOpenSell=false;

   }

   return;

}



//Close all the orders of a specific type and current symbol

void CloseAll(int Command){

   double ClosePrice=0;

   for( int i = 0 ; i < OrdersTotal() ; i++ ) {

      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ) {

         Print("ERROR - Unable to select the order - ",GetLastError());

         break;

      }

      if( OrderSymbol()==Symbol() && OrderType()==Command) {

         if(Command==OP_BUY && Bid>iMA(NULL, PERIOD_CURRENT, 20, 0, MODE_SMA, PRICE_CLOSE, 0)) ClosePrice=Bid;

         if(Command==OP_SELL&& Ask<iMA(NULL, PERIOD_CURRENT, 20, 0, MODE_SMA, PRICE_CLOSE, 0)) ClosePrice=Ask;

         double Lots=OrderLots();

         int Ticket=OrderTicket();

         for(int j=1; j<OrderOpRetry; j++){

            bool res=OrderClose(Ticket,Lots,ClosePrice,Slip,Red);

            if(res){

               Print("TRADE - CLOSE - Order ",Ticket," closed at price ",ClosePrice);

               break;

            }

            else Print("ERROR - CLOSE - error closing order ",Ticket," return error: ",GetLastError());

         }

      }

   }

   return;

}



//Open new order of a given type

void OpenNew(int Command){

   RefreshRates();

   double OpenPrice=0;

   double SLPrice;

   double TPPrice;

   if(Command==OP_BUY){

      OpenPrice=Ask;

      SLPrice=OpenPrice-SL;

      TPPrice=OpenPrice+TP;

   }

   if(Command==OP_SELL){

      OpenPrice=Bid;

      SLPrice=OpenPrice+SL;

      TPPrice=OpenPrice-TP;

   }

   for(int i=1; i<OrderOpRetry; i++){

      int res=OrderSend(Symbol(),Command,LotSize,OpenPrice,Slip,NormalizeDouble(SLPrice,Digits),NormalizeDouble(TPPrice,Digits),"",0,0,Green);

      if(res){

         Print("TRADE - NEW - Order ",res," submitted: Command ",Command," Volume ",LotSize," Open ",OpenPrice," Slippage ",Slip," Stop ",SLPrice," Take ",TPPrice);

         break;

      }

      else Print("ERROR - NEW - error sending order, return error: ",GetLastError());

   }

   return;

}



//Technical analysis of the indicators


bool BandsBreakUp=false;

bool BandsBreakDown=false;


void FindBandsTrend(){

   BandsBreakUp=false;

   BandsBreakDown=false;

   if(   Low[0]<=iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_LOWER,0)



) BandsBreakUp=true;

   if(   High[0]>=iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_UPPER,0)


) BandsBreakDown=true;

}



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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

   //Calling initialization, checks and technical analysis

   Initialize();

   CheckCanOrder();

   FindBandsTrend();

   //Check of Entry/Exit signal with operations to perform

   if(BandsBreakUp){

      if(CanOpenBuy && CanOpenSell && CanOrder) OpenNew(OP_BUY);

   }

   if(BandsBreakDown){

      if(CanOpenSell && CanOpenBuy && CanOrder) OpenNew(OP_SELL);

   }

  }

//+------------------------------------------------------------------+
Metatrader Indicators And Expert Advisors - MQL4 Trading Automation
  • mql4tradingautomation.com
I really want you to enjoy your trading experience and become a better trader. I want you to have less losses and higher profits, I want you to save time, avoid mistakes and catch more trading opportunities, and I hope that here you can find...
Files:
 

Forum on trading, automated trading systems and testing trading strategies

add an alert to an indicator

Sergey Golubev, 2019.11.14 11:16

If someone helps you on this thread so it will be fine.
If not so you may need to consider to use Freelance service for example.

----------------‌

It is standard reply which I am posting on such the requests (because I am not a coder sorry) -

Coders (any coder) are coding for free:

  • if it is interesting for them personally, or
  • if it is interesting for many members of this forum.

and Freelance section of the forum should be used in most of the cases.


 
Sergey Golubev:

Thanks for your advise, coz its just a small error so I dont know how much charging fee should be. 
 
Tien Do Van:

Dear my friends,

I already code an ea followed this rule:

1. Open Order Buy when Price touch Low bollinger Bands, Close once it touch Mid band( SMA20), Sl 25 pip, tp 50 pip

2. Open Order Sell when Price touch Upper bollinger Bands, Close once it touch Mid band( SMA20), Sl 25 pip, tp 50 pip.

it work well except i doest not automatically close once the price (Bid/Ask) touch MiD Bands.

I already highligh it

below is its code. kindly help me :((

It does not close, because despite having a CloseAll() function, it wasn't called.

So somewhere in your code, you need to make calls to iBands() with MODE_MAIN, check it's value against Bid/Ask (depending on what open orders you have), and call CloseAll() when the condition is right.

 
Seng Joo Thio:

It does not close, because despite having a CloseAll() function, it wasn't called.

So somewhere in your code, you need to make calls to iBands() with MODE_MAIN, check it's value against Bid/Ask (depending on what open orders you have), and call CloseAll() when the condition is right.

Dear MrSeng,

Thanks for your advice, but I was really stucked. May you help me to fix it :(?

 
Tien Do Van:

Dear MrSeng,

Thanks for your advice, but I was really stucked. May you help me to fix it :(?

It's not about fixing - it's not a bug, it's about adding codes to do what you want.

You have already done the codes to check the conditions for opening orders, so do it likewise for closing orders.

Reason: