//+------------------------------------------------------------------+
//|                              #5X5 SELL 25 BB by Steve Hopwood.mq4 |
//|                                  Copyright © 2010, Steve Hopwood |
//|                              http://www.hopwood3.freeserve.co.uk |
//|   MODIFIED BY NANNINGBOB OCT. 2010                               |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Steve Hopwood"
#property link      "http://www.hopwood3.freeserve.co.uk"
#include <WinUser32.mqh>
#include <stdlib.mqh>

/*
bool SendSingleTrade(int type, string comment, double lotsize, double price, double stop, double take)
bool DoesTradeExist()

*/

extern double  Lot=0.10;
extern double  TakeProfit=0;
extern double  StopLoss=0;
extern int     MagicNumber=2;
extern string  TradeComment="1.0 5x5 SELL";
extern bool    CriminalIsECN=true;
extern bool    UseSafetyFeature=true;

double         BbUpper, BbLower;
bool           RobotSuspended;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
//----

   //Accommodate different quote sizes
   double multiplier;
   if(Digits == 2 || Digits == 4) multiplier = 1;
   if(Digits == 3 || Digits == 5) multiplier = 10;
   if(Digits == 6) multiplier = 100;   
   TakeProfit*= multiplier;
   StopLoss*= multiplier;
   
   if (UseSafetyFeature)
   {
      
      int retval = MessageBox(Symbol() + ": do you wish to continue to use this robot?", "Question", MB_YESNO|MB_ICONQUESTION);
      if (retval == IDNO) RobotSuspended = true;
      else RobotSuspended = false;   
   }//if (UseSafetyFeature)
   
   if (TradeComment == "") TradeComment = " ";
   
//----
   return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
//----
   
//----
   return(0);
}

bool SendSingleTrade(int type, string comment, double lotsize, double price, double stop, double take)
{
   
   //if (StopTrading) return(true);
   /*
   if (!IsTesting())
   {   
      if (!IsTradeAllowed() ) return(false);
      if (!IsConnected() ) return(false);
      if (!IsExpertEnabled() ) return(false);
      if (!IsTradeContextBusy() ) return(false);
   }//if (!IsTesting)
   */
   
   int slippage = 10;
   if (Digits == 3 || Digits == 5) slippage = 100;
   
   color col = Red;
   if (type == OP_BUY || type == OP_BUYSTOP) col = Green;
   
   int expiry = 0;
   //if (SendPendingTrades) expiry = TimeCurrent() + (PendingExpiryMinutes * 60);
   
   
   if (!CriminalIsECN) int ticket = OrderSend(Symbol(),type, lotsize, price, slippage, stop, take, comment, MagicNumber, expiry, col);
   
   
   //Is a 2 stage criminal
   if (CriminalIsECN)
   {
      ticket = OrderSend(Symbol(),type, lotsize, price, slippage, 0, 0, comment, MagicNumber, expiry, col);
	   if (ticket > -1)
      {
         if (stop >0 && take > 0) bool result = OrderModify(OrderTicket(), OrderOpenPrice(), stop, take, OrderExpiration(), CLR_NONE);
         //Stop loss but no take profit
         if (stop >0 && take == 0) result = OrderModify(OrderTicket(), OrderOpenPrice(), stop, OrderTakeProfit(), OrderExpiration(), CLR_NONE);
         //Take profit but no stop loss
         if (stop ==0 && take > 0) result = OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), take, OrderExpiration(), CLR_NONE);
         if (!result)
         {
             int err=GetLastError();
             Print(Symbol(), " ", type," SL  order modify failed with error(",err,"): ",ErrorDescription(err));
         }//if (!result)			  
      }//if (ticket > -1)
      
   }//if (CriminalIsECN)
   
   //Error trapping for both
   if (ticket < 0)
   {
      string stype;
      if (type == OP_BUY) stype = "OP_BUY";
      if (type == OP_BUYSTOP) stype = "OP_BUYSTOP";
      if (type == OP_SELL) stype = "OP_SELL";
      if (type == OP_SELLSTOP) stype = "OP_SELLSTOP";
      err=GetLastError();
      //Alert(Symbol(), " ", stype," Nanningbob order send failed with error(",err,"): ",ErrorDescription(err));
      Print(Symbol(), " ", stype," #1 Buy Nanningbob order send failed with error(",err,"): ",ErrorDescription(err));
      return(false);
   }//if (ticket < 0)  
   
   //Got this far, so trade send succeeded
   return(true);
   
}//End bool SendSingleTrade(int type, string comment, double lotsize, double price, double stop, double take)

bool DoesTradeExist()
{
   
   
   if (OrdersTotal() == 0) return(false);
   
   for (int cc = OrdersTotal() - 1; cc >= 0 ; cc--)
   {
      if (!OrderSelect(cc,SELECT_BY_POS)) continue;
      
      if (OrderMagicNumber()==MagicNumber && OrderSymbol() == Symbol() )      
      {
         return(true);         
      }//if (OrderMagicNumber()==MagicNumber && OrderSymbol() == Symbol() )      
   }//for (int cc = OrdersTotal() - 1; cc >= 0 ; cc--)

   return(false);

}//End bool DoesTradeExist()


//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
//----
   
   if (RobotSuspended)
   {
      Comment("......................This robot is suspended.................");
      return;
   }//if (RobotSuspended)

   if (DoesTradeExist()) return;
   
   double  Var1, Var2, Var3, Var4;
   //---- indicators
   
   // MOVING AVERAGE
   Var1 = iMA(NULL, PERIOD_M15,  2, 0, MODE_LWMA, PRICE_MEDIAN,0);
   Var2 = iMA(NULL, PERIOD_M15,  1, 0, MODE_LWMA, PRICE_MEDIAN,1);
   Var3 = iMA(NULL, PERIOD_M15, 5, 2, MODE_SMMA, PRICE_MEDIAN,  0); 
   Var4 = iMA(NULL, PERIOD_M15, 5, 5, MODE_SMMA, PRICE_MEDIAN,  0);

   BbLower = iBands(NULL, PERIOD_H4,  25, 2, 0, PRICE_CLOSE, MODE_LOWER, 0); 
   BbUpper = iBands(NULL, PERIOD_H4,  25, 2, 0, PRICE_CLOSE, MODE_UPPER, 0);
 
   RefreshRates();
   //if (Var2 > BbUpper && Var1 < BbUpper)
   if(Bid < Var4 )// && Var3 > Var4 )
   {
      double stop = NormalizeDouble(Bid + (StopLoss * Point),Digits);
      if (StopLoss == 0) stop = 0;
      double take = NormalizeDouble(Bid - (TakeProfit * Point),Digits);
      if (TakeProfit == 0) take = 0;
      bool result = SendSingleTrade(OP_SELL, TradeComment, Lot, Bid, stop, take);
      if (result && UseSafetyFeature) 
      {
         RobotSuspended = true;
         Alert(Symbol() + " NB EA has sent a trade and suspended itself");
      }//if (result && UseSafetyFeature) 
      
   }//if( Var1 < BbUpper)
    

//----
   return(0);
}
//+------------------------------------------------------------------+