//+------------------------------------------------------------------+
//|                                                                  |
//|                                Steady Gold BET $10 Grid v1.2     |
//|                                GoldMine  Rev 5                   |
//+------------------------------------------------------------------+
#property copyright "©PIPSTER"
#property link      ""
#include <stdlib.mqh>
#include <stderror.mqh> 

// Input parameters
input double TradeAmount = 0.1;
input bool   UseMicroCompounding=false;
input double FixedLots=0.0;
input int unique = 123;
input double Spread = 50;
input double Buffer = 50;
input double GridStep = 50;
input double PriceGrid=10;
input double Sensitivity=50;
input double MinGridGain=10.00;
input bool UseDynamicGrid=false;
input int NoOfSteps=30;
input bool UseSlippage=false;
input double Slip = 50;
input double Target = 1.0;
input double EquityLossMultiple = 9999;
input color LineColor1=clrGold;
input color LineColor2=clrAqua;
input int LineWidth=2;
input int LineStyle=0;
input bool UseTimeToStart=false;
input int StartingHour = 11;
input int StartingMin = 30; 
input int StoppingHour = 11;
input int StoppingMin = 29;

double CurrentPL;
string Pair;
double StartingEquity, LS;
double DesiredEquity, MaxLoss;
string StartEquity;
int corcount, NumSteps;
double GridTop, GridBottom;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
     StartEquity="StartEquity"+unique;
     if(GlobalVariableCheck(StartEquity)==0) 
         GlobalVariableSet(StartEquity,AccountBalance());
     Pair = Symbol();
     if(UseDynamicGrid)NumSteps=10;
     else(NumSteps)=NoOfSteps;
     GridBottom=10000;
     GridTop=0;
     return(0);
}
  
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
   ObjectDelete(0,"BEBuy");
   ObjectDelete(0,"TGTBuy");
   ObjectDelete(0,"BESell");
   ObjectDelete(0,"TGTSell");
   return(0);
}

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   double LotSize=MarketInfo(Pair,MODE_LOTSIZE);
   double MinLotSize=MarketInfo(Pair,MODE_MINLOT);
   double LotStep=MarketInfo(Pair,MODE_LOTSTEP);
   double GS, BF, SP, CurrentEquity,cpdiv;
   bool StartTrade=false;
   
   int dgt=Digits;
   if(dgt==3) 
   {
      GS=GridStep*10;
      BF=Buffer*10;
      SP=Spread*10;
   }
   else
   {
      GS=GridStep;
      BF=Buffer;
      SP=Spread;
   }      
   
   if(UseMicroCompounding)cpdiv=100;
   else cpdiv=1000;
   
   if(FixedLots==0)
      LS=(AccountBalance()-MathMod(AccountBalance(),cpdiv))*TradeAmount/(100*LotSize);
   else LS=FixedLots;
   
   if(LS<MinLotSize) LS=MinLotSize;
   if(MathMod(LS,LotStep)!=0) LS-=MathMod(LS,LotStep);

   bool OpenOrdersExist=false;

   StartTrade=CheckTime();
   
   if(StartTrade) OpenOrdersExist = CheckForOpenOrders();
   
   if(!OpenOrdersExist && StartTrade) 
   {
      GlobalVariableSet(StartEquity,AccountBalance());
      bool PendDone=PlacePendingOrders(SP, BF, GS);
      corcount=0;
      if(PendDone) DrawVert(LineColor1);
      else return(0);
   }
   
   StartingEquity = GlobalVariableGet(StartEquity);
   double EquityGain = Target/100*(StartingEquity-MathMod(StartingEquity,100));
   double EquityLoss = EquityLossMultiple/100*(StartingEquity-MathMod(StartingEquity,100));   
   
   DesiredEquity = StartingEquity + EquityGain;
   MaxLoss = StartingEquity - EquityLoss;
   
   CurrentPL=CheckPL();
   CurrentEquity=StartingEquity+CurrentPL;
   bool GridPL=PriceGridCheck(SP); 
   
   if(CurrentEquity>DesiredEquity || CurrentEquity<MaxLoss || GridPL)
   { 
      CloseAllOrders();
      DrawVert(LineColor2);
   }
   int OT=0;
   if(UseSlippage==false)
   {
      OT=CheckNoOfTrades();
      if(OT!=0 && OT<NumSteps*2-10) CloseAllOrders();

   }
   else if(UseSlippage)
   {
      OT=CheckNoOfTrades();
      if(corcount<3 && OT!=0 && OT<NumSteps*2-10) 
      {
         Alert("Grid is corrupted");
         corcount++;
      }
   }
   
   string crtpl=DoubleToStr(CurrentPL,2);
   string dqty=DoubleToStr(DesiredEquity,2);


   double BuyBE = CheckBE(1,SP, BF, GS);
   double SellBE = CheckBE(0,SP, BF, GS);
   double BuyProfit=CheckTarget(1,EquityGain,SP, BF, GS);
   double SellProfit=CheckTarget(0,EquityGain,SP, BF, GS);
   
   if(UseDynamicGrid)
   {
      if(GridTop<BuyBE-1 && BuyBE!=0) GridExtend(GridTop, BuyBE, LS);
      if(GridBottom>SellBE+1 && SellBE!=0) GridExtend(GridBottom, SellBE, LS);
   }
   string cmtBuyBE=DoubleToStr(BuyBE,2);
   string cmtSellBE=DoubleToStr(SellBE,2); 
   string cmtBuyT=DoubleToStr(BuyProfit,2);
   string cmtSellT=DoubleToStr(SellProfit,2);
   string maxBuy=DoubleToStr(GridTop,2);
   string minSell=DoubleToStr(GridBottom,2);
  
   Comment("Current Time:"+Hour()+":"+Minute()+" Starting Equity: "+StartingEquity+
          "\nGain Sought: "+EquityGain+"    Desired Equity: "+dqty+
          "\nCurrent P/L: "+crtpl+"    Current LotSize: "+LS+
          "\nBreakEven Buy: "+cmtBuyBE+"   Buy Target: "+cmtBuyT+
          "\nBreakEven Sell:  "+cmtSellBE+"   Sell Target: "+cmtSellT+
          "\nGridTop: "+maxBuy+"  GridBottom: "+minSell
          );
   
 
   
   DrawLines(BuyBE,BuyProfit,SellBE,SellProfit);
   
//   if(UseBE)
//      ModifyOrders();
   
   return(0);
}

//+------------------------------------------------------------------+
//|  Check Time function                                             |
//+------------------------------------------------------------------+
bool CheckTime()
{
   bool ST=false;
   if(DayOfWeek()>=0 && DayOfWeek()<=5)
   {
      if(UseTimeToStart)
      {
         if(Hour() == StartingHour && Minute() == StartingMin) ST=true;
         if(Hour() == StoppingHour && Minute() == StoppingMin) ST=false;
      }
      else ST=true;
   }
   else ST=false;
   return(ST);
}


//+------------------------------------------------------------------+
//|  Check for Open Orders function                                  |
//+------------------------------------------------------------------+
bool  CheckForOpenOrders()
{
   bool OOE = false;
   int i;

   for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)
   {
      i=OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Pair)
      {
         if(OrderComment()=="Gold Mine Buy"||OrderComment()=="Gold Mine Sell")
         {
            OOE = true;
            return(OOE);
         }  
      }
   }
   return(OOE);
}

//+------------------------------------------------------------------+
//|  Place Pending Orders function                                   |
//+------------------------------------------------------------------+
bool PlacePendingOrders(double spd, double bfr, double gsp)
{
   
   double BuyOpenPrice, SellOpenPrice;
   bool BuyTradePlacedProperly, SellTradePlacedProperly, TradesPlaceProperly;
   int cnt;//, trdcnt;
   //uint starttimer, timer;

   double BidPrice=Bid;
   double AskPrice = BidPrice+spd*Point;
   
   double AskMod=MathMod(AskPrice,PriceGrid);
   double BidMod=MathMod(BidPrice,PriceGrid);
   
   double lowerT=PriceGrid-Sensitivity/100;
   double sens=Sensitivity/100;
   
   if(BidMod>lowerT || AskMod<sens)
   {
      for(cnt=1;cnt<NumSteps+1;cnt++)
      {
         
         if(cnt == 1)
         {
            BuyOpenPrice = AskPrice+bfr*Point;
            BuyTradePlacedProperly = OpenBuyPendingOrder(BuyOpenPrice);
            
            SellOpenPrice = BidPrice-bfr*Point;
            SellTradePlacedProperly = OpenSellPendingOrder(SellOpenPrice);
            
         }
         if(cnt > 1)
         {
            BuyOpenPrice = BuyOpenPrice+(gsp*Point);
            BuyTradePlacedProperly = OpenBuyPendingOrder(BuyOpenPrice);
            
            SellOpenPrice = SellOpenPrice-(gsp*Point);
            SellTradePlacedProperly = OpenSellPendingOrder(SellOpenPrice);
         }   
         if(!BuyTradePlacedProperly&&!SellTradePlacedProperly)
         {
          return(false);  
         }
         else TradesPlaceProperly=true;
      }
      //starttimer=GetTickCount();
      //while(trdcnt<NoOfSteps*2-5)
      //{
      //   trdcnt=OrdersTotal();
      //   timer=GetTickCount()-starttimer;
      //   if(timer>60000) Alert("$10 Grid not building on time");
      //   if(timer>60000) break;    
      //}
   }
   else return(false);
   

   GridTop=BuyOpenPrice;
   GridBottom=SellOpenPrice;
   
   return(TradesPlaceProperly);
}

//+------------------------------------------------------------------+
//|  Open Buy Pending Orders function                                |
//+------------------------------------------------------------------+
bool OpenBuyPendingOrder(double BOP)
{
 int TicketNumberBuy;
 
 //double Spread = MarketInfo(Pair,MODE_SPREAD)*Point;
 //double Free = AccountBalance();
 //double OneLot = MarketInfo(Pair,MODE_MARGINREQUIRED );
 //double Step = MarketInfo(Pair,MODE_LOTSTEP);

 TicketNumberBuy = OrderSend(Pair,OP_BUYSTOP,LS,BOP,0,0,0,"Gold Mine Buy");
 if(TicketNumberBuy == -1)
 {
    Alert("$10 Buy Order failed, Error NO: "+GetLastError()+" Price "+BOP);
    return(false);
 }
 return(true);
}


//+------------------------------------------------------------------+
//|  Open Sell Pending Orders function                               |
//+------------------------------------------------------------------+
bool OpenSellPendingOrder(double SOP)
{
 int TicketNumberSell;
 //double Spread = MarketInfo(Pair,MODE_SPREAD)*Point;
 //double Free = AccountBalance();
 //double OneLot = MarketInfo(Pair,MODE_MARGINREQUIRED );
 //double Step = MarketInfo(Pair,MODE_LOTSTEP);

 TicketNumberSell = OrderSend(Pair,OP_SELLSTOP,LS,SOP,0,0,0,"Gold Mine Sell");
 if(TicketNumberSell == -1)
 {
    
    Alert("$10 Sell Order failed, Error NO: "+GetLastError()+" Price "+SOP);
    return(false);
 }
 return(true);
}

//+------------------------------------------------------------------+
//|  CheckPL function                                                |
//+------------------------------------------------------------------+
double CheckPL()
{
   double CPL=0;
   int j;
   for (int i=OrdersTotal()-1;i>=0;i--) 
   {
      j=OrderSelect(i,SELECT_BY_POS);
      if(OrderSymbol()==Pair)
      {
         if(OrderType()==OP_BUY||OrderType()==OP_SELL) CPL +=OrderProfit();
      }
   }
   return(CPL);
}

//+------------------------------------------------------------------+
//|  Check No of trades function                                     |
//+------------------------------------------------------------------+
int CheckNoOfTrades()
{
   int OC=0;
   int j;
   for (int i=OrdersTotal()-1;i>=0;i--) 
   {
     j= OrderSelect(i,SELECT_BY_POS);
      if(OrderSymbol()==Pair)
      {
         if(OrderType()==OP_BUY||OrderType()==OP_BUYSTOP) OC++;
         if(OrderType()==OP_SELL||OrderType()==OP_SELLSTOP) OC++;
      }
   }
   return(OC);
}

//+------------------------------------------------------------------+
//|  Close All Orders function                                       |
//+------------------------------------------------------------------+
void CloseAllOrders()
{
int j,k,m;
   for(int count = 0; count<3; count ++)
   {
      // iterate the orders
      for (int i=0; i<OrdersTotal();i++) 
      {
         // select the order
        j= OrderSelect(i,SELECT_BY_POS);
         // close or kill depending on order type
         if(OrderSymbol()==Pair)
         {
            if(OrderComment()=="Gold Mine Buy"||OrderComment()=="Gold Mine Sell")
            {
               switch(OrderType()) 
               {
                  case OP_BUY:  
                  {
                     if(UseSlippage) m=OrderClose(OrderTicket(),OrderLots(),Bid,Slip); 
                     else m=OrderClose(OrderTicket(),OrderLots(),Bid,0); 
                  }
                  case OP_SELL: 
                  {
                     if(UseSlippage) m=OrderClose(OrderTicket(),OrderLots(),Ask,Slip); 
                     else  m=OrderClose(OrderTicket(),OrderLots(),Ask,0); 
                  }
                  default:      break;
               }
            }   
         }       
      }
      //Sleep(300);
   }
   for(count = 0; count<3; count ++)
   {
      // iterate the orders
      for (i=0;i<OrdersTotal();i++) 
      {
         // select the order
         j=OrderSelect(i,SELECT_BY_POS);
         // close or kill depending on order type
         if(OrderSymbol()==Pair)
         {
            if(OrderComment()=="Gold Mine Buy"||OrderComment()=="Gold Mine Sell")
            {
               k=OrderDelete(OrderTicket());
            }   
         }       
      }
      //Sleep(300);
   }
   return;
}

//+------------------------------------------------------------------+
//|  Modify Orders function                                          |
//+------------------------------------------------------------------+
/*
void ModifyOrders()
{
   int j;
   double SLoss;
   for (int i=OrdersTotal()-1;i>=0;i--) 
   {
     j=OrderSelect(i,SELECT_BY_POS);
      if(OrderSymbol()==Pair)
      {
         if(OrderType()==OP_BUY)
         {
            if(OrderComment()=="London Grid EA Buy")
            {
               if(OrderStopLoss()==0)
               {
                  if(Bid > (OrderOpenPrice()+BE*Point))
                  {
                     SLoss = OrderOpenPrice()+LockIn*Point;
                     if(OrderModify(OrderTicket(),OrderOpenPrice(),SLoss,OrderTakeProfit(),0,0)==true)
                     {
                     }
                     else
                        GetLastError();
                  }
               }
            }
         }
         if(OrderType()==OP_SELL)
         {
            if(OrderComment()=="London Grid EA Sell")
            {
               if(OrderStopLoss()==0)
               {
                  if(Ask < (OrderOpenPrice()-BE*Point))
                  {
                     SLoss = OrderOpenPrice()-LockIn*Point;
                     if(OrderModify(OrderTicket(),OrderOpenPrice(),SLoss,OrderTakeProfit(),0,0)==true)
                     {
                     }
                     else
                        GetLastError();
                  }
               }
            }
         }
      }
   }   
}
*/
//+------------------------------------------------------------------+
//|  Find Breakevens                                                 |
//+------------------------------------------------------------------+
double CheckBE(int flag, double spd, double bfr, double gsp)
{
   double midspread, step, hedgeloss, size, potential, k, m, n, beprice, spred;
   double BuyFirstLvl, SellFirstLvl;
   int buycnt=0, sellcnt=0;
   int j, stepcount;
   
   spred=spd*Point;

   midspread=spred+2*bfr*Point;
   
   step=gsp*Point;
   
   size=MarketInfo(Symbol(),MODE_LOTSIZE)*LS;
   
   for (int i=0;i<OrdersTotal();i++) 
   {
      j= OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()==OP_BUY) 
      {
         buycnt++;
         if(buycnt==1) BuyFirstLvl=OrderOpenPrice();
      }
      if(OrderType()==OP_SELL) 
      {
         sellcnt++;
         if(sellcnt==1) SellFirstLvl=OrderOpenPrice();
      }
   }  
   if(buycnt>0 && sellcnt==0) SellFirstLvl=BuyFirstLvl-midspread;
   if(buycnt==0 && sellcnt>0) BuyFirstLvl=SellFirstLvl+midspread;
      
   if(flag==1)
   {  
      if(sellcnt>0)
      {
         hedgeloss=(step*(sellcnt-1)+midspread)*sellcnt*size+spred;
         stepcount=0;
         for(i=1;1<=500;i++)
         {
            stepcount++;
            potential=(i*(i-1))/2*step*size;
            if(potential>hedgeloss) break;
         }
         k=0;
         m=stepcount-1;
         while(k<1)
         {
            k+=0.01;
            m+=0.01;
            potential=(m*(m-1))/2*step*size;
            if(potential>hedgeloss) break;
         }
         beprice=BuyFirstLvl+(m+sellcnt-1)*step;
      }
      else if(buycnt>0) beprice=BuyFirstLvl+buycnt/2*step;
   }

   if(flag==0)
   {  
      if(buycnt>0)
      {
         hedgeloss=(step*(buycnt-1)+midspread)*buycnt*size+spred;
         stepcount=0;
         for(i=1;1<=500;i++)
         {
            stepcount++;
            potential=(i*(i-1))/2*step*size;
            if(potential>hedgeloss) break;
         }
         k=0;
         n=stepcount-1;
         while(k<1)
         {
            k+=0.01;
            n+=0.01;
            potential=(n*(n-1))/2*step*size;
            if(potential>hedgeloss) break;
         }
         beprice=SellFirstLvl-(n+buycnt-1)*step;
      }
      else if(sellcnt>0) beprice=SellFirstLvl-sellcnt/2*step;
   }
   return(beprice);
}
//+------------------------------------------------------------------+
//|  Find Targets                                                |
//+------------------------------------------------------------------+
double CheckTarget(int flag, double Gain, double spd, double bfr, double gsp)
{
   double midspread, step, hedgeloss, size, potential, k, m, n, targetprice, spred;
   double BuyFirstLvl, SellFirstLvl, temp;
   int buycnt=0, sellcnt=0;
   int j, stepcount;
   
   spred=spd*Point;

   midspread=spred+2*bfr*Point;
   
   step=gsp*Point;
   
   size=MarketInfo(Symbol(),MODE_LOTSIZE)*LS;
   
   for (int i=0;i<OrdersTotal();i++) 
   {
      j= OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()==OP_BUY) 
      {
         buycnt++;
         if(buycnt==1) BuyFirstLvl=OrderOpenPrice();
      }
      if(OrderType()==OP_SELL) 
      {
         sellcnt++;
         if(sellcnt==1) SellFirstLvl=OrderOpenPrice();
      }
   }  
   
   if(BuyFirstLvl>0 && SellFirstLvl==0) SellFirstLvl=BuyFirstLvl-midspread;
   if(BuyFirstLvl==0 && SellFirstLvl>0) BuyFirstLvl=SellFirstLvl+midspread;
   
   if(flag==1)
   {  
      if(sellcnt>0) hedgeloss=(step*(sellcnt-1)+midspread)*sellcnt*size+Gain;
      else hedgeloss=Gain;
      stepcount=0;
      for(i=1;1<=500;i++)
      {
         stepcount++;
         potential=(stepcount*(stepcount+1))/2*step*size;
         if(potential>hedgeloss) break;
      }
      k=0;
      m=stepcount-1;
      while(k<1)
      {
         k+=0.01;
         m+=0.01;
         potential=(m*(m-1))/2*step*size;
         if(potential>hedgeloss) break;
      }
      targetprice=BuyFirstLvl+(sellcnt+m-1)*step;
   }

   if(flag==0)
   {  
      if(buycnt>0) hedgeloss=(step*(buycnt-1)+midspread)*buycnt*size+Gain;
      else hedgeloss=Gain;
      stepcount=0;
      for(i=1;1<=500;i++)
      {
         stepcount++;
         potential=(i*(i-1))/2*step*size;
         if(potential>hedgeloss) break;
      }
      k=0;
      if(buycnt==0) n=stepcount-2;
      else n=stepcount-1;
      temp=n;
      while(k<1)
      {
         k+=0.01;
         n+=0.01;
         potential=(n*(n-1))/2*step*size;
         if(potential>hedgeloss) break;
      }
      targetprice=SellFirstLvl-(buycnt+n-1)*step;
   }
   if(buycnt==0 && sellcnt==0) targetprice=0;
   return(targetprice);
}
//+------------------------------------------------------------------+
//|  DrawLines for BEs and Tartets                                   |
//+------------------------------------------------------------------+
void DrawLines(double buy1, double buy2, double sell1, double sell2)
{  
   
   const string B1="BEBuy";
   const string B2="TGTBuy";
   const string S1="BESell";
   const string S2="TGTSell";
   if(ObjectFind(0,B1)!=0)
   {
      ObjectCreate(B1, OBJ_HLINE, 0, Time[1],buy1);         
      ObjectSet(B1, OBJPROP_STYLE, LineStyle);  
      ObjectSet(B1, OBJPROP_WIDTH, LineWidth);                
      ObjectSet(B1, OBJPROP_COLOR, LineColor1);   
   }
   else ObjectMove(0,B1,0,Time[1],buy1);
   
   if(ObjectFind(0,B2)!=0)
   {
      ObjectCreate(B2, OBJ_HLINE, 0, Time[1],buy2);         
      ObjectSet(B2, OBJPROP_STYLE, LineStyle);  
      ObjectSet(B2, OBJPROP_WIDTH, LineWidth);                
      ObjectSet(B2, OBJPROP_COLOR, LineColor1);   
   }
   else ObjectMove(0,B2,0,Time[1],buy2);
   
   if(ObjectFind(0,S1)!=0)
   {
      ObjectCreate(S1, OBJ_HLINE, 0, Time[1],sell1);         
      ObjectSet(S1, OBJPROP_STYLE, LineStyle);  
      ObjectSet(S1, OBJPROP_WIDTH, LineWidth);                
      ObjectSet(S1, OBJPROP_COLOR, LineColor1);   
   }
   else ObjectMove(0,S1,0,Time[1],sell1);
   
   if(ObjectFind(0,S2)!=0)
   {
      ObjectCreate(S2, OBJ_HLINE, 0, Time[1],sell2);         
      ObjectSet(S2, OBJPROP_STYLE, LineStyle);  
      ObjectSet(S2, OBJPROP_WIDTH, LineWidth);                
      ObjectSet(S2, OBJPROP_COLOR, LineColor1);   
   }
   else ObjectMove(0,S2,0,Time[1],sell2);
}
//+------------------------------------------------------------------+
//|  Price Grid Reset Check                                          |
//+------------------------------------------------------------------+
bool PriceGridCheck(double spd)
{
   bool ok;
   
   double BidPrice=Bid;   
   double AskPrice = BidPrice+spd*Point;
   double AskMod=MathMod(AskPrice,PriceGrid);
   double BidMod=MathMod(BidPrice,PriceGrid);
   double sens=Sensitivity/100;   
   double lowerT=PriceGrid-sens;
   double prof=CheckPL();
   if((AskMod<sens || BidMod>lowerT) && prof>MinGridGain) ok=true;
   else ok=false;
   
   return(ok);  
}
//+------------------------------------------------------------------+
//|  Draw vertical grid start line                                   |
//+------------------------------------------------------------------+
void DrawVert(color ColorLine)
{  
   const string VL="VL"+TimeToStr(Time[0],TIME_DATE)+TimeToStr(Time[0],TIME_MINUTES);

   ObjectCreate(VL,OBJ_VLINE,0,Time[0],0);         
   ObjectSet(VL, OBJPROP_STYLE, 3);  
   ObjectSet(VL, OBJPROP_WIDTH, 1);                
   ObjectSet(VL, OBJPROP_COLOR, ColorLine);
   ObjectSet(VL,OBJPROP_BACK,0);   
}
//+------------------------------------------------------------------+
//|  CheckPL function                                                |
//+------------------------------------------------------------------+
void GridExtend(double startprice, double stopprice,double lots)
{
   short Type, steps,i,ticket; 
   double GS;
      
   if(startprice<stopprice) Type=1;
   else Type=2;
   
   if(Digits==3) GS=NormalizeDouble(GridStep*10*Point,2);
   else GS=NormalizeDouble(GridStep*Point,2);
   
   steps=MathAbs(MathFloor(stopprice-startprice)/GS);
 
   for(i=0;i<steps;i++)
   {
      switch(Type)
      {
         case 1: ticket= OrderSend(Symbol(),OP_BUYSTOP,lots,startprice+GS*i,0,0,0,"Gold Mine Buy",unique); break;
         case 2: ticket= OrderSend(Symbol(),OP_SELLSTOP,lots,startprice-GS*i,0,0,0,"Gold Mine Sell",unique); break;
         default: break;
         Sleep(50);
      }
   }
   if(Type==1) GridTop=startprice+GS*steps;
   else GridBottom=startprice-GS*steps;
   return;
}

