//+------------------------------------------------------------------+
//|                                             Trendlord EA v10.mq4 |
//|                                      Copyright © 2010, Ido Kasse |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Ido Kasse"
#property link      ""

extern int MagicNumber = 0628;
//++++ These are adjusted for 5 digit brokers.
double  pips2points,    // slippage  3 pips    3=points    30=points
        pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
static datetime barStart;
int count =1, existorder,ready;
double buyarea1, buyarea2, sellarea1, sellarea2, mysl, TP, SL, entry;

void CheckOrders()
{
   int total = OrdersTotal();
   for (int cnt = 0 ; cnt < total ; cnt++)
   {
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()==OP_SELL&& OrderMagicNumber()==MagicNumber) existorder = 1;
      if(OrderType()==OP_BUY&& OrderMagicNumber()==MagicNumber) existorder = 1; 
   }
   if (existorder==0) ready=1;
   if (existorder==1) ready=0;
}

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   if (Digits == 5 || Digits == 3)
   {    // Adjust for five (5) digit brokers.
               pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
   } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
   
   ObjectCreate("today", OBJ_LABEL, 0, 0, 0);
   ObjectSet("today", OBJPROP_CORNER, 1);
   ObjectSet("today", OBJPROP_XDISTANCE, 15);
   ObjectSet("today", OBJPROP_YDISTANCE, 15);
   
   //SIXTH
   ObjectCreate("onesixth",1,0,TimeCurrent(),High[120]);
   ObjectSet("onesixth",OBJPROP_COLOR,Lime);
   ObjectSet("onesixth",OBJPROP_STYLE,STYLE_SOLID);
   ObjectSet("onesixth",OBJPROP_WIDTH,1);     
  
   ObjectCreate("twosixth",1,0,TimeCurrent(),High[120]);
   ObjectSet("twosixth",OBJPROP_COLOR,Red);
   ObjectSet("twosixth",OBJPROP_STYLE,STYLE_SOLID);
   ObjectSet("twosixth",OBJPROP_WIDTH,1);    
      
   ObjectCreate("foursixth",1,0,TimeCurrent(),Low[120]);
   ObjectSet("foursixth",OBJPROP_COLOR,Red);
   ObjectSet("foursixth",OBJPROP_STYLE,STYLE_SOLID);
   ObjectSet("foursixth",OBJPROP_WIDTH,1);
      
   ObjectCreate("fivesixth",1,0,TimeCurrent(),Low[120]);
   ObjectSet("fivesixth",OBJPROP_COLOR,Lime);
   ObjectSet("fivesixth",OBJPROP_STYLE,STYLE_SOLID);
   ObjectSet("fivesixth",OBJPROP_WIDTH,1);
   //END SIXTH
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectDelete("today");
   ObjectDelete("onesixth");
   ObjectDelete("twosixth");
   ObjectDelete("foursixth");
   ObjectDelete("fivesixth");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   ObjectSetText("today", StringConcatenate("", TimeToStr(CurTime())), 12, "Arial Bold", DeepSkyBlue);
   
   RefreshRates();
   
   //ADR
   double HI2 = iHigh(NULL,0,0);
   double LOW2 = iLow(NULL,0,0); 
   double HI3 = iHigh(NULL,0,1);
   double LOW3 = iLow(NULL,0,1);
   double HI4 = iHigh(NULL,0,0);
   double LOW4 = iLow(NULL,0,0);
   double HI5 = iHigh(NULL,0,2);
   double LOW5 = iLow(NULL,0,2);
   double HI6 = iHigh(NULL,0,3);
   double LOW6 = iLow(NULL,0,3);
   double HI7 = iHigh(NULL,0,4);
   double LOW7 = iLow(NULL,0,4);
   double HI8 = iHigh(NULL,0,5);
   double LOW8 = iLow(NULL,0,5);
   double HI9 = iHigh(NULL,0,6);
   double LOW9 = iLow(NULL,0,6);
   double HI10 = iHigh(NULL,0,7);
   double LOW10 = iLow(NULL,0,7);
   double HI11 = iHigh(NULL,0,8);
   double LOW11 = iLow(NULL,0,8);
   double HI12 = iHigh(NULL,0,9);
   double LOW12 = iLow(NULL,0,9);
   double HI13 = iHigh(NULL,0,10);
   double LOW13 = iLow(NULL,0,10);
   double HI14 = iHigh(NULL,0,11);
   double LOW14 = iLow(NULL,0,11);
   double HI15 = iHigh(NULL,0,12);
   double LOW15 = iLow(NULL,0,12);
   double HI16 = iHigh(NULL,0,13);
   double LOW16 = iLow(NULL,0,13);
   double HI17 = iHigh(NULL,0,14);
   double LOW17 = iLow(NULL,0,14);
   double HI18 = iHigh(NULL,0,15);
   double LOW18 = iLow(NULL,0,15);
   double HI19 = iHigh(NULL,0,16);
   double LOW19 = iLow(NULL,0,16);
   double HI20 = iHigh(NULL,0,17);
   double LOW20 = iLow(NULL,0,17);
   double HI21 = iHigh(NULL,0,18);
   double LOW21 = iLow(NULL,0,18);
   double HI22 = iHigh(NULL,0,19);
   double LOW22 = iLow(NULL,0,19);
   double HI23 = iHigh(NULL,0,20);
   double LOW23 = iLow(NULL,0,20);
   double OPEN = iOpen(NULL,0,0);
   double CLOSE = iClose(NULL,0,0);
   double ONE = (HI3-LOW3)/2;
   double FIVE = ((HI3-LOW3)+(HI5-LOW5)+(HI6-LOW6)+(HI7-LOW7)+(HI8-LOW8))/10;
   double TEN = ((HI3-LOW3)+(HI5-LOW5)+(HI6-LOW6)+(HI7-LOW7)+(HI8-LOW8)+
                  (HI9-LOW9)+(HI10-LOW10)+(HI11-LOW11)+(HI12-LOW12)+(HI13-LOW13))/20;
   double TWENTY = ((HI3-LOW3)+(HI5-LOW5)+(HI6-LOW6)+(HI7-LOW7)+(HI8-LOW8)+
               (HI9-LOW9)+(HI10-LOW10)+(HI11-LOW11)+(HI12-LOW12)+(HI13-LOW13)+
               (HI14-LOW14)+(HI15-LOW15)+(HI16-LOW16)+(HI17-LOW17)+(HI18-LOW18)+
               (HI19-LOW19)+(HI20-LOW20)+(HI21-LOW21)+(HI22-LOW22)+(HI23-LOW23))/40; 
   double AV2 = ((FIVE+TEN+TWENTY)/3)*20000;
   mysl=(NormalizeDouble(AV2,0));
   Comment("ADR: ",NormalizeDouble(AV2,0));
   //END ADR
 
   //SIXTH
   double value = WindowPriceMax(0)-WindowPriceMin(0);      //value top of the chart - value buttem
   double sixth = value/6;
   double valueS = (value*(MathPow(10,Digits)));
   double sixthS = (sixth*(MathPow(10,Digits)));
   if(ObjectFind("fivesixth")==-1)init();
   ObjectMove("onesixth",0,TimeCurrent(),WindowPriceMin(0)+sixth*1);
   ObjectMove("twosixth",0,TimeCurrent(),WindowPriceMin(0)+sixth*2);
   ObjectMove("foursixth",0,TimeCurrent(),WindowPriceMin(0)+sixth*4);
   ObjectMove("fivesixth",0,TimeCurrent(),WindowPriceMin(0)+sixth*5);
   buyarea1=ObjectGet("onesixth", OBJPROP_PRICE1);
   buyarea2=ObjectGet("twosixth", OBJPROP_PRICE1);
   sellarea1=ObjectGet("fivesixth", OBJPROP_PRICE1);
   sellarea2=ObjectGet("foursixth", OBJPROP_PRICE1);
   //END SIXTH
   
   existorder=0;
   CheckOrders();
   
   double lots    = 0.1;
   double minlot  = MarketInfo(Symbol(),MODE_MINLOT);
   if (minlot == 0.01)   lots = NormalizeDouble(lots,2);
   if (minlot == 0.1)    lots = NormalizeDouble(lots,1);
   if (minlot == 1)      lots = NormalizeDouble(lots,0);

   //SELL ORDER
   if (Bid<=sellarea1&& Bid>sellarea2&& ready==1)   {
     entry = NormalizeDouble(Bid,Digits);
     TP    = NormalizeDouble(sellarea2,Digits);
     SL    = NormalizeDouble(entry+mysl*pips2dbl,Digits);
     OrderSend(Symbol(),OP_SELL,lots,entry,50,SL,TP,NULL,MagicNumber,0,Red);
   }
   //END SELL ORDER
   
   //BUY ORDER
   if (Ask>=buyarea1&& Ask<buyarea2&& ready==1)    {
     entry = NormalizeDouble(Ask,Digits);
     TP    = NormalizeDouble(buyarea2,Digits);
     SL    = NormalizeDouble(entry-mysl*pips2dbl,Digits);
     OrderSend(Symbol(),OP_BUY,lots,entry,50,SL,TP,NULL,MagicNumber,0,Blue);
   }
   //END BUY ORDER      
   
//----
   return(0);
  }
//+------------------------------------------------------------------+



