#property copyright "Drew Nya"
#property link      "joinme@yandex.ru"

extern int WingDings_Symbol = 108;

double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];
double Buffer5[];
double Buffer6[];

extern double pD,D;

extern string t;

int init()
  {
      return(0);
  }

int deinit()
  {
      return(0);
  }

int start()
  {
      int total,cnt;
      double prof;
      
      D = Darma();
      //Print(D);  
      
      if((OrdersTotal()  == 0) && (t != TimeToStr(Time[0])) && (Hour() >= 8) && (Hour() <= 19))
      {
         //if(pD < D)
         if(D < Close[0])
         {
            OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+0.0040,"",2,0,Blue);
            t = TimeToStr(Time[0]);
         }
      
         //if(pD > D)
         if(D > Close[0])
         {
            OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-0.0040,"",1,0,Red);      
            t = TimeToStr(Time[0]);
         }
      }


      total = OrdersTotal();
      for(cnt=0;cnt<total;cnt++)
      {  
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);         
         prof = OrderProfit() / 10;
         if(prof >= 25)
         {
            if(OrderType()==OP_BUY)
            {
               OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);   
               t = TimeToStr(Time[0]);
            }
            if(OrderType()==OP_SELL)
            {
               OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
               t = TimeToStr(Time[0]);
            }
         }
      }


      pD = D;
      
      return(0);
  }

double Darma()
  {
      int counted_bars = 30;

      int adjustment=0;
      int periods=3;
      int extremes=0;
      double smin, smax, SsMax, SsMin, pivothi, pivotlo, margins;
      bool flag1, flag2;
      
      double res;
            
      for(int i=0;i<Bars;i++)
      {
         if(extremes!=0)
         {
            SsMax = High[Highest(NULL,0,MODE_OPEN,periods,i+adjustment)];
            SsMin = Low[Lowest(NULL,0,MODE_OPEN,periods,i+adjustment)];
         }
         else
         {
            SsMax = Open[Highest(NULL,0,MODE_OPEN,periods,i+adjustment)];
            SsMin = Open[Lowest(NULL,0,MODE_OPEN,periods,i+adjustment)];
         }
         smin = SsMin+(SsMax-SsMin)*margins/100;
	      smax = SsMax-(SsMax-SsMin)*margins/100;      
         pivothi=((2*High[i])+Low[i]+(2*Close[i]))/5;
         pivotlo=(High[i]+(2*Low[i])+(2*Close[i]))/5;
         if(pivotlo>smin && pivothi>smax) flag1=true; else flag1=false;
         if(pivothi<smax && pivotlo<smin) flag2=true; else flag2=false;
      
         if(flag1)
         {
            Buffer1[i]=High[i];
            Buffer2[i]=Low[i];
         }
         if(flag2)
         {
            Buffer1[i]=Low[i];
            Buffer2[i]=High[i];
         }
            
         double sig=iSAR(NULL,0,0.5,1,i);
         if(sig<Low[i]) Buffer3[i]=sig;
         if(sig>High[i]) Buffer4[i]=sig;
         
         if((sig<Low[i]) && (sig != 0)) res=sig;
         if((sig>High[i]) && (sig != 0)) res=sig;
         
         //Print(res);
      }

      return(res);
  }

