//+--------------------------------------------------------------------------------------------------------------------------+
//|                              Buy Script for use SFT Trading Room                                                         |
//|                              Copyright © 2010, Sftmgm.com                                                                  |
//|                                                                                                                          |
//+--------------------------------------------------------------------------------------------------------------------------+
// Script allow enter trade on every traded pair in SFT strategy including individual (manuall or automated ADR) SL/TP
// Properties: MinRange - default value of 100 (PIP) 4digit broker 5digit to be set for 1000!
//             Info     - if True - Alert window to show actuall SL/TP per traded pair
//             Manual   - if True - script accept only manually input SL/TP (not ADR based)
//                  
//                        SL a TP  round to with help of  fce. MathFloor() like this: 14.7 or 14.2 returns value to 14                       
//                      

//+-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
//|                        !!!    Important Reccomendations - Always update daily charts to get as exact ADR as possible                           |
//+-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+

#property copyright "Copyright © 2010, sftmgm.com"
#property link      "www.sftmgm.com"


//#property show_inputs

extern double Lots = 1.00;
extern bool   UseMoneyMgmt = false;//Use true if You wish to use Money Management
extern double RiskPercent  = 1;
extern int    PercentSL    = 10;    // SL as % of ADR
extern int    PercentTP    = 15;    // TP as % of ADR
extern int    MinRange     = 100;   // min ADR (prumerna daily range) pro obchodovani, (5digit broker= "1000")
extern bool   Info         = true;  
extern bool   Manual       = 0;     // "1"manually defined SL/TP
                                    // "0"Automaticlly defined SL/TP by script based on ADR of each currency pair
extern bool   OffSunday    = 0;     // "true"(1) Sunday calculate ADR without bar "false"(0), including Sunday's Bar

// Set EURUSD                                
extern double SL_EURUSD = 20;
extern double TP_EURUSD = 30;
extern bool   UseSL_EURUSD = true;
extern bool   UseTP_EURUSD = true;
// Set GBPUSD                                
extern double SL_GBPUSD = 40;
extern double TP_GBPUSD = 50;
extern bool   UseSL_GBPUSD = true;
extern bool   UseTP_GBPUSD = true;
// Set USDCHF                                
extern double SL_USDCHF = 60;
extern double TP_USDCHF = 70;
extern bool   UseSL_USDCHF = true;
extern bool   UseTP_USDCHF = true;
// Set USDJPY
extern double SL_USDJPY = 80;
extern double TP_USDJPY = 90;
extern bool   UseSL_USDJPY = true;
extern bool   UseTP_USDJPY = true;

extern string Note="0 in Entry field means Market Order Buy";
extern double Entry = 0.0000;


//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
double ADR;

int start()
{ 
  double Risk = RiskPercent * 0.01;
  int Mode = OP_BUYSTOP;
  if (Ask > Entry && Entry > 0) Mode = OP_BUYLIMIT; 
  if (Entry == 0)  {Entry = Ask; Mode = OP_BUY;}
  
  int Digit=2;  // "2" - for accounts with a step size of positions 0.01, "1 "- for accounts with a step size of 0.1 positions                                
  if ( MarketInfo(Symbol(),MODE_LOTSTEP)==0.1) Digit=1;
  if (!Manual) CalcADR(Symbol(),20);
  
  if (Info) TodaySet();
  else
  if (Symbol()=="EURUSD")
  {
    if (ADR>=MinRange || Manual)  // Enable trade
    { 
      if (!Manual)
      {
        SL_EURUSD=MathFloor(ADR*PercentSL*0.01);
        TP_EURUSD=MathFloor(ADR*PercentTP*0.01);
      }
      if (UseMoneyMgmt)
        Lots = NormalizeDouble( AccountBalance()*Risk/SL_EURUSD/(MarketInfo(Symbol(), MODE_TICKVALUE)),Digit);
      if(Lots > 0) 
        OrderSend(Symbol(),Mode, Lots, Entry, 2, UseSL_EURUSD*(Entry-SL_EURUSD*Point), UseTP_EURUSD*(Entry+TP_EURUSD*Point), "Buy Script",0, NULL, LimeGreen);
    }
  }
  else
  if (Symbol()=="GBPUSD")
  {
    if (ADR>=MinRange || Manual)  // Enable trade
    {
      if (!Manual)
      { 
        SL_GBPUSD=MathFloor(ADR*PercentSL*0.01);
        TP_GBPUSD=MathFloor(ADR*PercentTP*0.01);
      }
      if (UseMoneyMgmt)
        Lots = NormalizeDouble( AccountBalance()*Risk/SL_GBPUSD/(MarketInfo(Symbol(), MODE_TICKVALUE)),Digit); 
      if(Lots > 0) 
        OrderSend(Symbol(),Mode, Lots, Entry, 2, UseSL_GBPUSD*(Entry-SL_GBPUSD*Point), UseTP_GBPUSD*(Entry+TP_GBPUSD*Point), "Buy Script",0, NULL, LimeGreen);
    }
  }
  else
  if (Symbol()=="USDCHF")
  {
    if (ADR>=MinRange || Manual)  // Enable trade
    {
      if (!Manual)
      {  
        SL_USDCHF=MathFloor(ADR*PercentSL*0.01);
        TP_USDCHF=MathFloor(ADR*PercentTP*0.01);
      }
      if (UseMoneyMgmt)
        Lots = NormalizeDouble( AccountBalance()*Risk/SL_USDCHF/(MarketInfo(Symbol(), MODE_TICKVALUE)),Digit); 
      if(Lots > 0) 
        OrderSend(Symbol(),Mode, Lots, Entry, 2, UseSL_USDCHF*(Entry-SL_USDCHF*Point), UseTP_USDCHF*(Entry+TP_USDCHF*Point), "Buy Script",0, NULL, LimeGreen);
    }
  }
  else
  if (Symbol()=="USDJPY")
  { 
    if (ADR>=MinRange || Manual)  // Enable trade
    {
      if (!Manual)
      {  
        SL_USDJPY=MathFloor(ADR*PercentSL*0.01);
        TP_USDJPY=MathFloor(ADR*PercentTP*0.01);
      }
      if (UseMoneyMgmt)
        Lots = NormalizeDouble( AccountBalance()*Risk/SL_USDJPY/(MarketInfo(Symbol(), MODE_TICKVALUE)),Digit); 
      if(Lots > 0) 
        OrderSend(Symbol(),Mode, Lots, Entry, 2, UseSL_USDJPY*(Entry-SL_USDJPY*Point), UseTP_USDJPY*(Entry+TP_USDJPY*Point), "Buy Script",0, NULL, LimeGreen);
    }
  }  
   return(0);
}
//+------------------------------------------------------------------+
// ===========================================================================================================
   void CalcADR(string Currency, int cntADR)
   { int cnt=0;
     int count=0;
     double _adr=0,adr=0;
     
     while(count<cntADR) 
     { cnt++;
       if (TimeDayOfWeek(iTime( Currency, PERIOD_D1, cnt))==0 && OffSunday)
         while( (TimeDayOfWeek(iTime( Currency, PERIOD_D1, cnt))<2 || 
                 TimeDayOfWeek(iTime( Currency, PERIOD_D1, cnt))>5 )  ) {cnt++; }
       
       _adr=_adr+(iHigh( Currency, PERIOD_D1, cnt)-iLow( Currency, PERIOD_D1, cnt))/MarketInfo(Currency,MODE_POINT);
       count++;
       switch (count)
       {
         case  1: {adr=_adr;        break;} 
         case  5: {adr=adr+_adr/5;  break;}
         case 10: {adr=adr+_adr/10; break;}
         case 20: {adr=adr+_adr/20; break;}
       }
     }
     ADR=adr/4;
   }
// ===========================================================================================================
   void TodaySet()
   {  
      CalcADR("EURUSD",20);
      SL_EURUSD=MathFloor(ADR*PercentSL*0.01);
      TP_EURUSD=MathFloor(ADR*PercentTP*0.01);
      Alert("EURUSD:  ADR=",ADR,"   SL=",SL_EURUSD,"   TP=",TP_EURUSD);
      
      CalcADR("GBPUSD",20);
      SL_GBPUSD=MathFloor(ADR*PercentSL*0.01);
      TP_GBPUSD=MathFloor(ADR*PercentTP*0.01);
      Alert("GBPUSD:  ADR=",ADR,"   SL=",SL_GBPUSD,"   TP=",TP_GBPUSD);
      
      CalcADR("USDCHF",20);
      SL_USDCHF=MathFloor(ADR*PercentSL*0.01);
      TP_USDCHF=MathFloor(ADR*PercentTP*0.01);
      Alert("USDCHF:  ADR=",ADR,"   SL=",SL_USDCHF,"   TP=",TP_USDCHF);

      CalcADR("USDJPY",20);
      SL_USDJPY=MathFloor(ADR*PercentSL*0.01);
      TP_USDJPY=MathFloor(ADR*PercentTP*0.01);
      Alert("USDJPY:  ADR=",ADR,"   SL=",SL_USDJPY,"   TP=",TP_USDJPY);
   }