#property copyright "Copyright 2016, www.forexminerpro.com"
#property link      "scoobidoobi"



extern double lots = 0.1;
extern double incr = 0.2;
extern double    TakeProfit=150; // TP of 15pips
extern double    StopLoss=300; // SL of 30pips
extern bool      MonitorTakeProfit=true;
extern bool      MonitorStopLoss=true;
extern int magic=1;
extern double startHour = 2;
extern double endHour = 22;
extern double hedge = 150; // hedge 15pips
string pending="null";
extern string C0 = "#Do not touch values under this!!#";
extern string C99 = "Logics";
extern int xTime = 0;
bool runnable=true;
bool init=true;
bool result;
double pBid, pAsk, pp;
int i=0;
datetime timeprev=0;
 double  b_close,s_close;


int init() {



  return (0);
}

int deinit() {
  return (0);
}

int start() {
     double lda_0[][6];
     
     ArrayCopyRates(lda_0, Symbol(), PERIOD_D1);
         if (xTime != TimeDay(lda_0[0][0]) && DayOfWeek() != 1 ) {
            xTime = TimeDay(lda_0[0][0]);
            b_close = iClose(Symbol(),PERIOD_D1,0);
            s_close = iClose(Symbol(),PERIOD_D1,1);
            }
    
 
Strategy_1();



AutoSetTPSL();

return(0);
 }          
             
 



int Strategy_1() {
  string l_dbl2str_12="Scalp Sure";
 

  if (lots > 0.1 && (CountOrdersThisPairBUY() == 0 && CountOrdersThisPairSELL() == 0 && OrderProfit() > 0)   ) { lots =0.1; pending="null";}

  
 


   if (CountOrdersThisPairBUY() == 0 && CountOrdersThisPairSELL() == 0   && Hour() >=startHour  && Hour() < endHour   &&  b_close > s_close  && AccountFreeMargin() > 0  ) {
 
   
    OrderSend(Symbol(), OP_BUY,  lots , Ask, 3, 0,0, l_dbl2str_12, magic, 0, RoyalBlue);
   lots=lots + incr;
   
 
   if ( CountOrdersThisPairBUY() > 0 ) {
    OrderSend(Symbol(),OP_SELLSTOP, lots ,Bid-hedge*Point,3,0,0,l_dbl2str_12,magic,0,Red);
    pending="SELLSTOP";
 }
   
   
   
  }
   if (CountOrdersThisPairBUY() == 0 && CountOrdersThisPairSELL() == 0  && Hour() >=startHour  && Hour() < endHour  && b_close < s_close && AccountFreeMargin() > 0 ) {
   
   
     OrderSend(Symbol(), OP_SELL,  lots , Bid, 3, 0, 0, l_dbl2str_12, magic, 0, Red);
      lots=lots + incr;
   
             lots=(lots + incr)*(CountOrdersThisPairBUY()+CountOrdersThisPairSELL());
             if ( CountOrdersThisPairSELL() > 0    ){
       OrderSend(Symbol(),OP_BUYSTOP, lots ,Ask+hedge*Point,3,0,0,l_dbl2str_12,magic,0,RoyalBlue);
       pending="BUYSTOP";
     }
  }
 
   
 
  if ((CountOrdersThisPairBUYSTOP() == CountOrdersThisPairSELLSTOP()) && pending=="SELLSTOP"  && AccountFreeMargin() > 0 && (CountOrdersThisPairBUY() > 0 || CountOrdersThisPairSELL() > 0    ) ) {
    lots=(lots)*(CountOrdersThisPairBUY());
   
            OrderSend(Symbol(),OP_BUYSTOP, lots ,Ask+hedge*Point,3,0,0,l_dbl2str_12,magic,0,RoyalBlue);
    pending="BUYSTOP";
       


   }
 
     
   if ((CountOrdersThisPairBUYSTOP() == CountOrdersThisPairSELLSTOP()) && pending=="BUYSTOP"  && AccountFreeMargin() > 0 && (CountOrdersThisPairBUY() > 0 || CountOrdersThisPairSELL() > 0    ) ) {
        lots=(lots)*(CountOrdersThisPairSELL());
       
     OrderSend(Symbol(),OP_SELLSTOP, lots ,Bid-hedge*Point,3,0,0,l_dbl2str_12,magic,0,Red);
     pending="SELLSTOP" ;
   
   }
   
 
     
   return (0);
}

int CountOrdersThisPairBUY() {
  int l_count_0;
  for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
     OrderSelect(l_pos_4, SELECT_BY_POS);
     if (OrderSymbol() == Symbol() && OrderType() == OP_BUY && OrderMagicNumber() == magic) l_count_0++;
  }
  return (l_count_0);
}

int CountOrdersThisPairSELL() {
  int l_count_0 = 0;
  for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
     OrderSelect(l_pos_4, SELECT_BY_POS);
     if (OrderSymbol() == Symbol() && OrderType() == OP_SELL  && OrderMagicNumber() == magic) l_count_0++;
  }
  return (l_count_0);
}

int CountOrdersThisPairBUYSTOP() {
  int l_count_0;
  for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
     OrderSelect(l_pos_4, SELECT_BY_POS);
     if (OrderSymbol() == Symbol() && OrderType() == OP_BUYSTOP && OrderMagicNumber() == magic) l_count_0++;
  }
  return (l_count_0);
}

int CountOrdersThisPairSELLSTOP() {
  int l_count_0 = 0;
  for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
     OrderSelect(l_pos_4, SELECT_BY_POS);
     if (OrderSymbol() == Symbol() && OrderType() == OP_SELLSTOP  && OrderMagicNumber() == magic) l_count_0++;
  }
  return (l_count_0);
}

//+------------------------------------------------------------------+
void AutoSetTPSL() {

  int StopMultd=10;
   double TP=NormalizeDouble(TakeProfit*StopMultd,Digits);
   double SL=NormalizeDouble(StopLoss*StopMultd,Digits);
   
 //-------------------------------------------------------------------+
//Check open orders
//-------------------------------------------------------------------+
if(OrdersTotal()>0){
  for(int i=1; i<=OrdersTotal(); i++)          // Cycle searching in orders
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
        {
          if((MonitorTakeProfit==true)&&(TakeProfit>0)&&(OrderSymbol()==Symbol()) && OrderMagicNumber() == magic){ // monitor tp
          
                // Calculate take profit
                double tpb=NormalizeDouble(OrderOpenPrice()+TP*Point,Digits);
                double tps=NormalizeDouble(OrderOpenPrice()-TP*Point,Digits);
                    
                Comment("Modifying take profit");
                if((OrderType()==OP_BUY)&&(OrderTakeProfit()==0)&&(OrderSymbol()==Symbol()) && OrderMagicNumber() == magic){ OrderModify(OrderTicket(),0,OrderStopLoss(),tpb,0,CLR_NONE); }
                if((OrderType()==OP_SELL)&&(OrderTakeProfit()==0)&&(OrderSymbol()==Symbol()) && OrderMagicNumber() == magic){ OrderModify(OrderTicket(),0,OrderStopLoss(),tps,0,CLR_NONE); }
 
          } // monitor tp
          
          if((MonitorStopLoss==true)&&(StopLoss>0)&&(OrderSymbol()==Symbol()) && OrderMagicNumber() == magic){ // monitor sl
          
                   // Calculate stop loss
                   double slb=NormalizeDouble(OrderOpenPrice()-SL*Point,Digits);
                   double sls=NormalizeDouble(OrderOpenPrice()+SL*Point,Digits);
 
                   Comment("Modifying stop loss");
                   if((OrderType()==OP_BUY)&&(OrderStopLoss()==0)&&(OrderSymbol()==Symbol()) && OrderMagicNumber() == magic){ OrderModify(OrderTicket(),0,slb,OrderTakeProfit(),0,CLR_NONE); }
                   if((OrderType()==OP_SELL)&&(OrderStopLoss()==0)&&(OrderSymbol()==Symbol()) && OrderMagicNumber() == magic){ OrderModify(OrderTicket(),0,sls,OrderTakeProfit(),0,CLR_NONE); }
 
          }// monitor sl
          Comment("");
        }
     }
}

}

