//+------------------------------------------------------------------+
//|                                                     Blessing 3.3 |
//|                              Copyright © 2007-2008, MQLCoder.com |
//|                                             support@mqlcoder.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007-2008, FiFtHeLeMeNt."
#property link      "http://www.mqlcoder.com"
extern double Lots=0.2;
extern int mm=1;                // if one the lots size will increase based on account size
extern double risk=0.1;         // risk to calculate the lots size (only if mm is enabled)
extern int AccountisNormal=0;   // Zero if account is not mini/micro, one if it is a mini/micro
extern bool Micro=false;
extern int MC=2; // market condition 0=uptrend 1=downtrend 2=range
extern bool MCbyMA=true;
extern int MA_Period=34;
extern double Multiplier=1.5;
extern double LotInc=0;
extern int TimeGrid=2400;
extern int GridSet1=25;
extern int TP_Set1=50;
extern int GridSet2=50;
extern int TP_Set2=100;
extern int GridSet3=100;
extern int TP_Set3=200;
extern int d=5;
extern int Set1Count=4;
extern int Set2Count=4;
extern int MaxLevel=99;
extern int BELevel=99;
bool ca=false;
// Internal settings
//int            Step           = 1;
string EA_name                = "Blessing";
int            Error          = 0;
int            Order          = 0;
int            magic      = 0;
string         TradeComment;
double         TickPrice      = 0;
int            MaxBuys        = 0;
int            MaxSells       = 0;
double         MaxProfit      = 0;
bool           Auditing       = false;
string         Filename;
double         initialBalance;
int            lotPrecision;
bool           TradeAllowed   = true;
int            OpenOrders=0, cnt=0;
int            slippage=3;
double         sl=0, tp=0;
double         BuyPrice=0, SellPrice=0;
double         lotsi=0, mylotsi=0;
int            mode=0, myOrderType=0;
bool           ContinueOpening=True;
double         LastPrice=0;
int            PreviousOpenOrders=0;
double         Profit=0;
int            LastTicket=0, LastType=0;
double         LastClosePrice=0, LastLots=0;
double         Pivot=0;
double         PipValue=0;
bool           Reversed=False;
string         text="";
double         tmp=0;
double         iTmpH=0;
double         iTmpL=0;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   if (Symbol() == "AUDCADm" || Symbol() == "AUDCAD") magic = 101001;
   if (Symbol() == "AUDJPYm" || Symbol() == "AUDJPY") magic = 101002;
   if (Symbol() == "AUDNZDm" || Symbol() == "AUDNZD") magic = 101003;
   if (Symbol() == "AUDUSDm" || Symbol() == "AUDUSD") magic = 101004;
   if (Symbol() == "CHFJPYm" || Symbol() == "CHFJPY") magic = 101005;
   if (Symbol() == "EURAUDm" || Symbol() == "EURAUD") magic = 101006;
   if (Symbol() == "EURCADm" || Symbol() == "EURCAD") magic = 101007;
   if (Symbol() == "EURCHFm" || Symbol() == "EURCHF") magic = 101008;
   if (Symbol() == "EURGBPm" || Symbol() == "EURGBP") magic = 101009;
   if (Symbol() == "EURJPYm" || Symbol() == "EURJPY") magic = 101010;
   if (Symbol() == "EURUSDm" || Symbol() == "EURUSD") magic = 101011;
   if (Symbol() == "GBPCHFm" || Symbol() == "GBPCHF") magic = 101012;
   if (Symbol() == "GBPJPYm" || Symbol() == "GBPJPY") magic = 101013;
   if (Symbol() == "GBPUSDm" || Symbol() == "GBPUSD") magic = 101014;
   if (Symbol() == "NZDJPYm" || Symbol() == "NZDJPY") magic = 101015;
   if (Symbol() == "NZDUSDm" || Symbol() == "NZDUSD") magic = 101016;
   if (Symbol() == "USDCHFm" || Symbol() == "USDCHF") magic = 101017;
   if (Symbol() == "USDJPYm" || Symbol() == "USDJPY") magic = 101018;
   if (Symbol() == "USDCADm" || Symbol() == "USDCAD") magic = 101019;
   if (magic == 0) magic = 701999;
   initialBalance= AccountBalance();   
   TradeComment  = StringConcatenate(Symbol()," ",EA_name);      
   Filename = StringConcatenate(EA_name+"_",Symbol(),"_",Period(),"_M",".txt");
   CalculateLotPrecision();
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
  
  void CalculateLotPrecision(){
   double lotstep=MarketInfo(Symbol(),MODE_LOTSTEP);
   if(lotstep==1) lotPrecision=0;
   if(lotstep==0.1) lotPrecision=1;
   if(lotstep==0.01) lotPrecision=2;
   if(lotstep==0.001) lotPrecision=3;
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   int cnt=0;
   int y;
   int cb=0; // Count buy
   int cs=0; // Count sell
   int cbl=0; // Count buy limit
   int csl=0; // Count sell limit
   int cbs=0; // Count buy limit
   int css=0; // Count sell limit
   double m,lot2;
   double lp; // last buy price
   double ll; //last lot
   double ltp; //last tp
   double lopt; // last open time
   int g2;
   double tp2;
   double entry;
   double pr=0; // profit;
   double sumlot,sumlot2;
   int lticket;
   double mp,mp2; // median price
   
   if (AccountisNormal==1)
   {
	  if (mm!=0) { lotsi=NormalizeDouble(AccountBalance()/10000,lotPrecision)*risk*10; }
		else { lotsi=Lots; }
   } else {  // then is mini
    if (mm!=0) { lotsi=NormalizeDouble(AccountBalance()/10000,lotPrecision)*risk; }
		else { lotsi=Lots; }
   }
   if (lotsi>100){ lotsi=100; }
   
   if (MCbyMA ) {
     MC=-1;
     if (Bid<iMA(Symbol(),0,MA_Period,0,MODE_EMA,PRICE_CLOSE,0)) MC=1;
     if (Bid>iMA(Symbol(),0,MA_Period,0,MODE_EMA,PRICE_CLOSE,0)) MC=0;
   }
   
//   double ma=iMA(Symbol(),0,MAP,0,MODE_EMA,PRICE_CLOSE,0);
   
   
/*   if (DayOfWeek()==5 &&  Hour()>=21) {
     for (y = 0; y < OrdersTotal(); y++)
     {
       OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
       if (OrderType()==OP_BUY || OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),1);
       if (OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT) OrderDelete(OrderTicket());
     }
     return;
   }
*/   
   
   for (y = 0; y < OrdersTotal(); y++)
   {
      OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
      if ((OrderMagicNumber()==magic) && (OrderSymbol()==Symbol()) && (OrderType()==OP_BUY)) {
        cb++;
        pr=pr+OrderProfit();
      }
      if ((OrderMagicNumber()==magic) && (OrderSymbol()==Symbol()) && (OrderType()==OP_SELL)) {
        cs++;
        pr=pr+OrderProfit();
      }
      if ((OrderMagicNumber()==magic) && (OrderSymbol()==Symbol()) && (OrderType()==OP_BUYLIMIT)) cbl++;
      if ((OrderMagicNumber()==magic) && (OrderSymbol()==Symbol()) && (OrderType()==OP_SELLLIMIT)) csl++;
      if ((OrderMagicNumber()==magic) && (OrderSymbol()==Symbol()) && (OrderType()==OP_BUYSTOP)) cbs++;
      if ((OrderMagicNumber()==magic) && (OrderSymbol()==Symbol()) && (OrderType()==OP_SELLSTOP)) css++;
   }
   
   if (cs==0 && cb==0 && cbs==0 && cbl==0 && css==0 && csl==0) ca=false;
   
   if (((cb>=BELevel || cs>=BELevel) && pr>0) || ca) {
     ca=true;
     for (y = 0; y < OrdersTotal(); y++)
     {
       OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
       if ((OrderMagicNumber()==magic) && (OrderSymbol()==Symbol()) && (OrderType()==OP_BUY || OrderType()==OP_SELL)) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),1);
       if ((OrderMagicNumber()==magic) && (OrderSymbol()==Symbol()) && (OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP)) OrderDelete(OrderTicket());
     }
     return;
   }
   g2=GridSet1;
   tp2=TP_Set1;
   if (cb>=Set1Count || cs>=Set1Count) {
     g2=GridSet2;
     tp2=TP_Set2;
   }
   if (cb>=Set2Count+Set1Count || cs>=Set2Count+Set1Count) {
     g2=GridSet3;
     tp2=TP_Set3;
   }
   
  
   if ((cb==0) && (cs==0)) {
     for (y = 0; y < OrdersTotal(); y++)
     {
       OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
       if ((OrderMagicNumber()==magic) && (OrderSymbol()==Symbol()) && (OrderLots()>lotsi)) OrderDelete(OrderTicket());
     }
     if (MC==0) {
        if (cbs==0) {
          m=MathMod(Ask/Point,g2);
          if ((g2-m+d)>MarketInfo(Symbol(),MODE_STOPLEVEL)) {
            OrderSend(Symbol(),OP_BUYSTOP,lotsi,Ask*2-Bid+(g2-m+d)*Point,0,0,Ask*2-Bid+(g2-m+d+tp2)*Point,TradeComment,magic);
            Print("s2");
            return;
          }
        }
        if (cbl==0) {
          m=MathMod(Ask/Point,g2);
          if ((m+d)>MarketInfo(Symbol(),MODE_STOPLEVEL)) {
            OrderSend(Symbol(),OP_BUYLIMIT,lotsi,Ask*2-Bid-(m+d)*Point,0,0,Ask*2-Bid-(m+d-tp2)*Point,TradeComment,magic);
            Print("s1");
            return;
          }
        }
     }
     if (MC==1) {
        if (csl==0) {
          m=MathMod(Bid/Point,g2);
          if ((g2-m-d)>MarketInfo(Symbol(),MODE_STOPLEVEL)) {
            OrderSend(Symbol(),OP_SELLLIMIT,lotsi,Bid+(g2-m-d)*Point,0,0,Bid+(g2-m-d-tp2)*Point,TradeComment,magic);
            Print("s2");
            return;
          }
        }
        if (css==0) {
          m=MathMod(Bid/Point,g2);
          if ((m+d)>MarketInfo(Symbol(),MODE_STOPLEVEL)) {
            OrderSend(Symbol(),OP_SELLSTOP,lotsi,Bid-(m+d)*Point,0,0,Bid-(m+d+tp2)*Point,TradeComment,magic);
            Print("s1");
            return;
          }
        }
     }
        
     
     if (MC==2) {
        if (css==0) {
          m=MathMod(Bid/Point,g2);
          if ((m+d)>MarketInfo(Symbol(),MODE_STOPLEVEL)) {
            OrderSend(Symbol(),OP_SELLSTOP,lotsi,Bid-(m+d)*Point,0,0,Bid-(m+d+tp2)*Point,TradeComment,magic);
            Print("s1");
            return;
          }
        }
        if (cbs==0) {
          m=MathMod(Ask/Point,g2);
          if ((g2-m+d)>MarketInfo(Symbol(),MODE_STOPLEVEL)) {
            OrderSend(Symbol(),OP_BUYSTOP,lotsi,Ask*2-Bid+(g2-m+d)*Point,0,0,Ask*2-Bid+(g2-m+d+tp2)*Point,TradeComment,magic);
            Print("s2");
            return;
          }
        }
      }
   }
   
   if (cb>0 || cs>0) {
     for (y = 0; y < OrdersTotal(); y++)
     {
        OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
        if ((OrderMagicNumber()==magic) && (OrderSymbol()==Symbol()) && (OrderType()!=OP_SELL) && (OrderType()!=OP_BUY) && OrderLots()==lotsi) OrderDelete(OrderTicket());
     }
   }
   
/*   if (cb>0) {
     for (y = 0; y < OrdersTotal(); y++)
     {
        OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
        if ((OrderMagicNumber()==magic) && (OrderSymbol()==Symbol()) && OrderSymbol()==Symbol() && ((OrderType()==OP_SELLLIMIT) || (OrderType()==OP_SELLSTOP) || (OrderType()==OP_BUYSTOP))) OrderDelete(OrderTicket());
     }
   }
   if (cs>0) {
     for (y = 0; y < OrdersTotal(); y++)
     {
        OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
        if ((OrderMagicNumber()==magic) && (OrderSymbol()==Symbol()) && OrderSymbol()==Symbol() && ((OrderType()==OP_BUYLIMIT) || (OrderType()==OP_BUYSTOP) || (OrderType()==OP_SELLSTOP))) OrderDelete(OrderTicket());
     }
   }
   
*/
   
   if (cb>0) {
     sumlot=0;
     mp=0;
     for (y = 0; y < OrdersTotal(); y++)
     {
       OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
       if ((OrderMagicNumber() != magic) || (OrderType()!=OP_BUY) || (OrderSymbol()!=Symbol())) { continue; }
       lp=OrderOpenPrice();
       ll=OrderLots();
       ltp=OrderTakeProfit();
       lopt=OrderOpenTime();
       lticket=OrderTicket();
       sumlot=sumlot+ll;
       mp=mp+ll*lp;
     }
     mp=mp/sumlot;
     
/*     if (ltp<mp && NoLoss) {
       OrderModify(lticket,OrderOpenPrice(),0,NormalizeDouble(mp+inLossTP*Point,Digits),0);
       Print("mp1");
     }
*/
//     if ((cbl==0) && (cb<maxlvl2))  {
/*       m=MathMod(lp/Point,g2);
       if ((m-d)>MarketInfo(Symbol(),MODE_STOPLEVEL)) {
         OrderSend(Symbol(),OP_BUYLIMIT,lot2,Ask-(m-d)*Point,0,0,Ask-(m-d-tp2)*Point,TradeComment,magic);
         Print("s1");
         return;
       }*/
//       if ((Ask-(lp-g2*Point))>MarketInfo(Symbol(),MODE_STOPLEVEL)*Point && TimeCurrent()-TimeGrid>lopt) {
       if ((TimeCurrent()-TimeGrid>lopt) && (cb<MaxLevel)) {
         if (lp>Ask) entry=NormalizeDouble(lp-(MathRound((lp-Ask)/Point/g2)+1)*g2*Point,Digits); else entry=NormalizeDouble(lp-g2*Point,Digits);
         if (Micro) lot2=NormalizeDouble(ll*Multiplier+LotInc,2); else lot2=NormalizeDouble(ll*Multiplier+LotInc,1);
/*         sumlot2=sumlot+lot2;
         mp2=(mp*sumlot+lot2*entry)/sumlot2;
         while (entry+tp2*Point<mp2) {
           lot2=NormalizeDouble(lot2+lot,1);
//           Print("******************************************* "+lot2);
           sumlot2=sumlot+lot2;
           mp2=(mp*sumlot+lot2*entry)/sumlot2;
         } */
         
         if (cbl==0) {
           OrderSend(Symbol(),OP_BUYLIMIT,lot2,entry,0,0,entry+tp2*Point,TradeComment,magic);
           Print("s3");
           return;
         }
         if (cbl==1) {
           for (y = 0; y < OrdersTotal(); y++)
           {
              OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
              if (OrderType()==OP_BUYLIMIT && OrderMagicNumber()==magic && (OrderSymbol()==Symbol()) && entry-OrderOpenPrice()>g2/2*Point) {
                OrderModify(OrderTicket(),entry,OrderStopLoss(),entry+tp2*Point,0);
                Print("mo1");
              }
            }
         }
         
       } 
//     }
     for (y = 0; y < OrdersTotal(); y++) // Sync TPs
     {
       OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
       if ((OrderMagicNumber() != magic) || (OrderType()!=OP_BUY) || (MathAbs(OrderTakeProfit()-ltp)<Point) || (OrderSymbol()!=Symbol())) { continue; }
       OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),ltp,0,Red);
       Print("m1");
       return;
     }
     
   }
   if (cs>0) {
     sumlot=0;
     mp=0;
     for (y = 0; y < OrdersTotal(); y++)
     {
       OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
       if ((OrderMagicNumber() != magic) || (OrderType()!=OP_SELL) || (OrderSymbol()!=Symbol())) { continue; }
       lp=OrderOpenPrice();
       ll=OrderLots();
       ltp=OrderTakeProfit();
       lopt=OrderOpenTime();
       lticket=OrderTicket();
       sumlot=sumlot+ll;
       mp=mp+ll*lp;
     }
     mp=mp/sumlot;
     
/*     if (ltp>mp && NoLoss) {
       OrderModify(lticket,OrderOpenPrice(),0,NormalizeDouble(mp-inLossTP*Point,Digits),0);
       Print("mp2");
     }
*/
//     if ((csl==0) && (cs<maxlvl2)) {
//       if (((lp+g2*Point)-Bid)>MarketInfo(Symbol(),MODE_STOPLEVEL)*Point && TimeCurrent()-TimeGrid>lopt) {
       if ((TimeCurrent()-TimeGrid>lopt) && (cs<MaxLevel)) {
         if (Bid>lp) entry=NormalizeDouble(lp+(MathRound((-lp+Bid)/Point/g2)+1)*g2*Point,Digits); else entry=NormalizeDouble(lp+g2*Point,Digits);
         if (Micro) lot2=NormalizeDouble(ll*Multiplier+LotInc,2); else lot2=NormalizeDouble(ll*Multiplier+LotInc,1);
//         sumlot2=sumlot+lot2;
//         mp2=(mp*sumlot+lot2*entry)/sumlot2;
         
/*         while (entry-tp2*Point>mp2) {
           lot2=NormalizeDouble(lot2+lot,1);
           Print("******************************************* "+lot2);
           sumlot2=sumlot+lot2;
           mp2=(mp*sumlot+lot2*entry)/sumlot2;
         } */
         if (csl==0) {
            OrderSend(Symbol(),OP_SELLLIMIT,lot2,entry,0,0,entry-tp2*Point,TradeComment,magic);
            Print("s4");
            return;
         }
         if (csl==1) {
           for (y = 0; y < OrdersTotal(); y++)
           {
              OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
              if (OrderType()==OP_SELLLIMIT && OrderMagicNumber()==magic && (OrderSymbol()==Symbol()) && OrderOpenPrice()-entry>g2/2*Point) {
                OrderModify(OrderTicket(),entry,OrderStopLoss(),entry-tp2*Point,0);
                Print("mo2");
              }
            }
         }
         
         
         
       }
//     }
/*       m=MathMod(Bid/Point,g2);
       if ((g2-m-d)>MarketInfo(Symbol(),MODE_STOPLEVEL)) {
         OrderSend(Symbol(),OP_SELLLIMIT,lot2,Bid+(g2-m-d)*Point,0,0,Bid+(g2-m-d-tp2)*Point,TradeComment,magic);
         Print("s2");
         return;
       } 
  */   
     for (y = 0; y < OrdersTotal(); y++) // Sync TPs
     {
       OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
       if ((OrderMagicNumber() != magic) || (OrderType()!=OP_SELL) || (MathAbs(OrderTakeProfit()-ltp)<Point) || (OrderSymbol()!=Symbol())) { continue; }
       OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),ltp,0,Red);
       Print("m2");
       return;
     }
   }
   
  
   return(0);
  }
//+------------------------------------------------------------------+
             
            
Comments