//+------------------------------------------------------------------+
//|                                                   HedgeRocks.mq4 |
//|                                                    Public Domain |
//|         Optimized and tested with http://www.tradeviewforex.com  |
//| 
//+------------------------------------------------------------------+

#property copyright "Public Domain"
#property link      "http://www.tradeviewforex.com"
#include <stdlib.mqh>
#include <stderror.mqh>
#include <WinUser32.mqh>

double ver = 1.7;
//---- input parameters
extern double     MarginPercentage  = 0.15;     //Margin Percentage
extern datetime   _____HighGroup_Pick_1_or_2_____;        //Space holder, informational
extern bool       TradeEURUSD       = true;     
extern bool       TradeGBPUSD       = true;
extern datetime   _____LowGroup_Pick_Only_1_____;
extern bool       TradeUSDCHF       = true;
extern bool       TradeUSDJPY       = false;
extern datetime   _____LimitsOnOrders______;       
extern double     StopLoss          = 0.033;    //Percent, Stop Loss, Pair
extern double     TakeProfit        = 0.0166;    //Percent, Take Profit,Pair
extern datetime   _____Misc_Stuff_____;
extern double     Limit_order_reduction_factor     = 1.5;
extern int        GrabProfitFactor                 = 4;
int os,o1,o2 =0;                         //Globals

 
int init()
{
if (TradeUSDCHF && TradeUSDJPY)
   {
   PlaySound ("alert.wav");
   MessageBox("Can Not Trade Both Low-Group pairs(yet):  UDSJPY group is now disabled","WARNING",MB_YESNO|MB_ICONWARNING);
   TradeUSDJPY = false;
   }
if (!TradeEURUSD && !TradeGBPUSD)
   {
   PlaySound("alert.wav");
   MessageBox("You must Pick at least One High-Group Pair, EURUSD Selected","WARNING",MB_YESNO|MB_ICONWARNING);
   TradeEURUSD = true;
   }   
if (!TradeUSDCHF && !TradeUSDJPY)
   {
   PlaySound("alert.wav");
   MessageBox("You must Pick One Low-Group Pair, USDCHF Selected","WARNING",MB_YESNO|MB_ICONWARNING);
   TradeUSDCHF = true;
   }
if (MarginPercentage < 0.05 || MarginPercentage > 0.30)
   {
   PlaySound("alert.wav");
   MessageBox("Margin Percentage range .05 - .30 (5%-30%), Selecting .09 Default","WARNING",MB_YESNO|MB_ICONWARNING);
   MarginPercentage = 0.09;
   }
if (GrabProfitFactor < 1 || GrabProfitFactor > 15)
   {
   PlaySound("alert.wav");
   MessageBox("The Profit Factor may have value: 0 - 15, Selecting Default (4)","WARNING",MB_YESNO|MB_ICONWARNING);
   GrabProfitFactor = 4;
   }

if (Limit_order_reduction_factor < 1 || Limit_order_reduction_factor > 3)
   {
   PlaySound("alert.wav");
   MessageBox("Limit order reduction may have value: 0 - 3, Selecting Default (1.5)","WARNING",MB_YESNO|MB_ICONWARNING);
   Limit_order_reduction_factor = 1.5;
   }
if (AccountCompany() != "Rosenthal Collins Group, LLC")
   {
   PlaySound("alert.wav");
   MessageBox("EA test with tradeviewforex.com DEMO at 200:1: Using other brokers requires modifications","WARNING",MB_YESNO|MB_ICONWARNING);
   Limit_order_reduction_factor = 1.5;
   }


Comment("");
ObjectsDeleteAll(0,OBJ_LABEL);
Comment("Waiting for tick ");
return(0);
}

int deinit()
{
Comment("");
ObjectsDeleteAll(0,OBJ_LABEL);
return(0);
}

int start()
{

//program variables
RefreshRates ();

int gpf = (GrabProfitFactor * 2) + 500;  //Calibrate this variable to suit your Broker's software

double cf;
double Rate[10],lLots[15],jLots[15],gLots[15],gjLots[15];
double eClose[50],cClose[50],gClose[50],jClose[50];
double pa,lev,ls;
double blr1,blr2,slr1,slr2,slr5,blr5,slr7,blr7;
double EUSL1, EUTP1, UCSL1, UCTP1;
double EUSL2, EUTP2, UJSL2, UJTP2;
double GUSL3, GUTP3, UCSL3, UCTP3;
double GJSL4, GJTP4, JGSL4, JGTP4;
double ECCor, EJCor, GCCor, GJCor;
double Ol[100],Op[100],Oop[100];
string Ocmt[100];
int Ot[100],Oty[100],Omn[100];
datetime Oot[100];
int EUBuy,UCBuy,EU2Buy,UJ2Buy,GU3Buy,UC3Buy,GJ4Buy,JG4Buy;
int eb1,es1,ub1,us1,eb2,es2,jb2,js2,gb3,gs3,ub3,us3,gb4,gs4,jb4,js4;
int EUBl,EUBls[10],EUSl,EUSls[10],UCBl,UCBls[10],UCSl,UCSls[10];
int EU2Bl,EU2Bls[10],EU2Sl,EU2Sls[10],UJ2Bl,UJ2Bls[10],UJ2Sl,UJ2Sls[10];
int GU3Bl,GU3Bls[10],GU3Sl,GU3Sls[10],UC3Bl,UC3Bls[10],UC3Sl,UC3Sls[10];
int GJ4Bl,GJ4Bls[10],GJ4Sl,GJ4Sls[10],JG4Bl,JG4Bls[10],JG4Sl,JG4Sls[10];
bool f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15, f16, f21, f22, f23, f24, f25, f26;  //flags
int dEU,dUC,dUJ,dGU,T1,T2,T3,T4,T5;
int ec,ce,ej,je,gc,cg,gj,jg,xx,x5 = 0;
double SysProfit,SysSwap = 0;
int attempt =0; 
double s1,s2 = 0;
int magic = 4875;
double lsf = 0.85; 

double blp1 = 0.0079/Limit_order_reduction_factor;
double blp2 = 0.0094/Limit_order_reduction_factor;
double slp1 = 0.0082/Limit_order_reduction_factor;
double slp2 = 0.0098/Limit_order_reduction_factor;
double blp5 = 0.0078/Limit_order_reduction_factor;
double slp5 = 0.0082/Limit_order_reduction_factor;
double blp7 = 0.0111/Limit_order_reduction_factor;
double slp7 = 0.0116/Limit_order_reduction_factor;



if (AccountFreeMargin() < 1000)
   {
   MessageBox ("EA requires a Free Margin < $1000.00","WARNING",MB_ICONSTOP|MB_OK);
   return (0);
   }

double sEUl = MarketInfo("EURUSD",MODE_SWAPLONG);
double sEUs = MarketInfo("EURUSD",MODE_SWAPSHORT);
double sUCl = MarketInfo("USDCHF",MODE_SWAPLONG);
double sUCs = MarketInfo("USDCHF",MODE_SWAPSHORT);
double sGUl = MarketInfo("GBPUSD",MODE_SWAPLONG);
double sGUs = MarketInfo("GBPUSD",MODE_SWAPSHORT);
double sUJs = MarketInfo("USDJPY",MODE_SWAPSHORT);
double sUJl = MarketInfo("USDJPY",MODE_SWAPLONG);
Rate[1] = MarketInfo ("EURUSD",MODE_ASK);
Rate[2] = MarketInfo ("USDCHF",MODE_ASK);
Rate[3] = MarketInfo ("EURUSD",MODE_BID);
Rate[4] = MarketInfo ("USDCHF",MODE_BID);
Rate[5] = MarketInfo ("USDJPY",MODE_ASK);
Rate[6] = MarketInfo ("USDJPY",MODE_BID);
Rate[7] = MarketInfo ("GBPUSD",MODE_ASK);
Rate[8] = MarketInfo ("GBPUSD",MODE_BID);
dGU = MarketInfo("GBPUSD",MODE_DIGITS);
dEU = MarketInfo("EURUSD",MODE_DIGITS);
dUC = MarketInfo("USDCHF",MODE_DIGITS);
dUJ = MarketInfo("USDJPY",MODE_DIGITS);
ls = MarketInfo("EURUSD",MODE_LOTSIZE);
double eP = MarketInfo ("EURUSD",MODE_POINT);
double cP = MarketInfo ("USDCHF",MODE_POINT);
double gP = MarketInfo ("GBPUSD",MODE_POINT);
double jP = MarketInfo ("USDJPY",MODE_POINT);
pa = AccountBalance ();
lev = AccountLeverage ();
string ac = AccountCompany ();
double eSpread = MarketInfo ("EURUSD",MODE_SPREAD) * MarketInfo ("EURUSD",MODE_POINT);
double cSpread = MarketInfo ("USDCHF",MODE_SPREAD) * MarketInfo ("USDCHF",MODE_POINT);
double gSpread = MarketInfo ("GBPUSD",MODE_SPREAD) * MarketInfo ("GBPUSD",MODE_POINT);
double jSpread = MarketInfo ("USDJPY",MODE_SPREAD) * MarketInfo ("USDJPY",MODE_POINT);

//Find Averages of Last Few Hours

ArrayInitialize (eClose,0);
ArrayInitialize (cClose,0);
ArrayInitialize (gClose,0);
ArrayInitialize (jClose,0);
attempt = 0;
while (iClose ("EURUSD",PERIOD_M1,0) == 0 || iClose ("USDCHF",PERIOD_M1,0) == 0 || iClose ("GBPUSD",PERIOD_M1,0) == 0 || iClose ("USDJPY",PERIOD_M1,0) == 0 && attempt <= 10)
      {
      iClose ("EURUSD",PERIOD_M1,0);
      iClose ("USDCHF",PERIOD_M1,0);
      iClose ("GBPUSD",PERIOD_M1,0); 
      iClose ("USDJPY",PERIOD_M1,0);
      Sleep (15);
      attempt ++;
      }
//average 5 min intervals times 36 intervals = 3 hours           
for (int x1 = 0; x1 <= 36; x1++)
   {
   for (int x2 =1; x2 <=5; x2++)
      {
      eClose[x1] = eClose[x1] + iClose ("EURUSD",PERIOD_M1,x1*5+x2);
      cClose[x1] = cClose[x1] + iClose ("USDCHF",PERIOD_M1,x1*5+x2);
      gClose[x1] = gClose[x1] + iClose ("GBPUSD",PERIOD_M1,x1*5+x2);
      jClose[x1] = jClose[x1] + iClose ("USDJPY",PERIOD_M1,x1*5+x2); 
      }
   } 
//find the high and low in order to set the boundries
double eHigh = eClose[ArrayMaximum (eClose,36,1)]/5;
double eLow = eClose[ArrayMinimum (eClose,36,1)]/5;
double cHigh = cClose[ArrayMaximum (cClose,36,1)]/5;
double cLow = cClose[ArrayMinimum (cClose,36,1)]/5;
double gHigh = gClose[ArrayMaximum (gClose,36,1)]/5;
double gLow = gClose[ArrayMinimum (gClose,36,1)]/5;
double jHigh = jClose[ArrayMaximum (jClose,36,1)]/5;
double jLow = jClose[ArrayMinimum (jClose,36,1)]/5;

// The Math

//Conversion factor 
cf = (pa/10000)*(100000/ls)*(MarginPercentage/0.05)*(lev/400);

//EC ONLY

lLots[1] = (1.08257/Rate[1])*cf;
lLots[2] = -0.4587 * (0.001/Rate[1]) * ((Rate[1]-Rate[2])/0.001) + 1;
lLots[3] = (lLots[1]/lLots[2]);
lLots[4] = 2*cf-(lLots[3]*Rate[1]);
lLots[5] = lLots[3]*0.019976;
lLots[6] = lLots[4]*0.02;

//EC - GC
lLots[7] = (0.5336/Rate[1])*cf;
lLots[8] = -0.5439 * (0.001/Rate[1]) * ((Rate[1]-Rate[2])/0.001) + 1;
lLots[9] = (lLots[7]/lLots[8]);
lLots[10] = 1*cf-(lLots[9]*Rate[1]);
lLots[11] = lLots[9]*0.019976;
lLots[12] = lLots[10]*0.02;

//EJ ONLY
jLots[1] = (1.98/Rate[1])*cf;
jLots[2] = -0.01 * (0.001/Rate[1]) * ((Rate[1]-Rate[5])/0.001) + 1;
jLots[3] = (jLots[1]/jLots[2]);
jLots[4] = 2*cf-(jLots[3]*Rate[1]);
jLots[5] = jLots[3]*0.018;
jLots[6] = jLots[4]*0.015;

//EU - EJ 
jLots[7] = (1.1504/Rate[1])*cf;
jLots[8] = -0.01394 * (0.001/Rate[1]) * ((Rate[1]-Rate[5])/0.001) + 1;
jLots[9] = (jLots[7]/jLots[8]);
jLots[10] = 1*cf-(jLots[9]*Rate[1]);
jLots[11] = jLots[9]*0.018;
jLots[12] = jLots[10]*0.015;

//GC ONLY
gLots[1] = (0.91473/Rate[7])*cf;
gLots[2] = -0.54262 * (0.001/Rate[7]) * ((Rate[7]-Rate[2])/0.001) + 1;
gLots[3] = (gLots[1]/gLots[2]);
gLots[4] = 2*cf-(gLots[3]*Rate[7]);
gLots[5] = gLots[3]*0.016;
gLots[6] = gLots[4]*0.02;

//GC - UEU
gLots[7] = (0.38114/Rate[7])*cf;
gLots[8] = -0.5439 * (0.001/Rate[7]) * ((Rate[7]-Rate[2])/0.001) + 1;
gLots[9] = (gLots[7]/gLots[8]);
gLots[10] = 1*cf-(gLots[9]*Rate[7]);
gLots[11] = gLots[9]*0.016;
gLots[12] = gLots[10]*0.02;

//GJ ONLY
gjLots[1] = (1.97212/Rate[7])*cf;
gjLots[2] = -0.013941 * (0.001/Rate[7]) * ((Rate[7]-Rate[5])/0.001) + 1;
gjLots[3] = (gjLots[1]/gjLots[2]);
gjLots[4] = 2*cf-(gjLots[3]*Rate[7]);
gjLots[5] = gjLots[3]*0.016;
gjLots[6] = gjLots[4]*0.0284;

//GJ - GC
gjLots[7] = (0.82171/Rate[7])*cf;
gjLots[8] = -0.01394 * (0.001/Rate[7]) * ((Rate[7]-Rate[5])/0.001) + 1;
gjLots[9] = (gjLots[7]/gjLots[8]);
gjLots[10] = 1*cf-(gjLots[9]*Rate[7]);
gjLots[11] = gjLots[9]*0.016;
gjLots[12] = gjLots[10]*0.0284;
  

 
  
//Scan and parse current orders
   
for (int idx = 1; idx <= OrdersTotal ()+1; idx ++)
{
   if (OrderSelect (idx-1,SELECT_BY_POS,MODE_TRADES) == TRUE)
      {
      Ocmt[idx] = StringSubstr(OrderComment (),0,5);                                            
      Ol[idx] = OrderLots ();
      Op[idx] = OrderProfit ();
      Ot[idx] = OrderTicket ();
      Oty[idx] = OrderType ();
      Omn[idx] = OrderMagicNumber ();
      Oop[idx] = OrderOpenPrice ();
      Oot[idx] = OrderOpenTime ();

      if (Ocmt[idx] == "EUB01" && Oty[idx] == OP_BUY)
         {
         if (ec == 0)
            EUBuy = idx;
         ec++;
         }
      if (Ocmt[idx] == "UCB01" && Oty[idx] == OP_BUY)
         {
         if (ce == 0)
            UCBuy = idx;
         ce++;
         }      
      if (Ocmt[idx] == "EUB02" && Oty[idx] == OP_BUY)  
         {
         if (ej == 0)
            EU2Buy = idx;
         ej++;
         }     
      if (Ocmt[idx] == "UJB02" && Oty[idx] == OP_BUY)  
         {
         if (je == 0)
            UJ2Buy = idx;
         je++;
         }     
      if (Ocmt[idx] == "GUB03" && Oty[idx] == OP_BUY)  
         {
         if (gc == 0)
            GU3Buy = idx;
         gc++;
         }
      if (Ocmt[idx] == "UCB03" && Oty[idx] == OP_BUY)  
         {
         if (cg == 0)
            UC3Buy = idx;
         cg++;
         }
      if (Ocmt[idx] == "GJB04" && Oty[idx] == OP_BUY)  
         {
         if (gj == 0)
            GJ4Buy = idx;
         gj++;
         }
      if (Ocmt[idx] == "JGB04" && Oty[idx] == OP_BUY)  
         {
         if (jg == 0)
            JG4Buy = idx;
         jg++;
         }
            
      if (Ocmt[idx] == "EUBL1" && Oty[idx] == OP_BUYLIMIT)   
         EUBl = idx; 
         
      if (Ocmt[idx] == "EUBL1" && Oty[idx] == OP_BUY)
         {
         EUBls[eb1] = idx;
         eb1++;
         }   
              
      if (Ocmt[idx] == "EUSL1" && Oty[idx] == OP_SELLLIMIT)
         EUSl = idx;  
             
      if (Ocmt[idx] == "EUSL1" && Oty[idx] == OP_SELL)
         {
         EUSls[es1] = idx;         
         es1++;
         }
         
      if (Ocmt[idx] == "UCBL1" && Oty[idx] == OP_BUYLIMIT)
         UCBl = idx;     
             
      if (Ocmt[idx] == "UCBL1" && Oty[idx] == OP_BUY)
         {
         UCBls[ub1] = idx;         
         ub1++;
         }

      if (Ocmt[idx] == "UCSL1" && Oty[idx] == OP_SELLLIMIT)
         UCSl = idx;    
         
      if (Ocmt[idx] == "UCSL1" && Oty[idx] == OP_SELL)
         {
         UCSls[us1] = idx;
         us1++;
         }
         
      if (Ocmt[idx] == "EU2BL" && Oty[idx] == OP_BUYLIMIT)   
         EU2Bl = idx; 
           
      if (Ocmt[idx] == "EU2BL" && Oty[idx] == OP_BUY)   
         {
         EU2Bls[eb2] = idx;               
         eb2++;
         }
         
      if (Ocmt[idx] == "EU2SL" && Oty[idx] == OP_SELLLIMIT)
         EU2Sl = idx; 
         
      if (Ocmt[idx] == "EU2SL" && Oty[idx] == OP_SELL)
         {
         EU2Sls[es2] = idx; 
         es2++;
         }
         
      if (Ocmt[idx] == "UJ2BL" && Oty[idx] == OP_BUYLIMIT)
         UJ2Bl = idx; 
         
      if (Ocmt[idx] == "UJ2BL" && Oty[idx] == OP_BUY)
         {
         UJ2Bls[jb2] = idx;              
         jb2++;
         }
         
      if (Ocmt[idx] == "UJ2SL" && Oty[idx] == OP_SELLLIMIT)
         UJ2Sl = idx;  
         
      if (Ocmt[idx] == "UJ2SL" && Oty[idx] == OP_SELL)
         {
         UJ2Sls[js2] = idx;  
         js2++;
         }
                  
      if (Ocmt[idx] == "GU3BL" && Oty[idx] == OP_BUYLIMIT)   
         GU3Bl = idx;
                  
      if (Ocmt[idx] == "GU3BL" && Oty[idx] == OP_BUY)  
         { 
         GU3Bls[gb3] = idx; 
         gb3++;
         }

      if (Ocmt[idx] == "GU3SL" && Oty[idx] == OP_SELLLIMIT)
         GU3Sl = idx;  
                
      if (Ocmt[idx] == "GU3SL" && Oty[idx] == OP_SELL)
         {
         GU3Sls[gs3] = idx;         
         gs3++;
         }

      if (Ocmt[idx] == "UC3BL" && Oty[idx] == OP_BUYLIMIT)
         UC3Bl = idx;  
                
      if (Ocmt[idx] == "UC3BL" && Oty[idx] == OP_BUY)
         {
         UC3Bls[ub3] = idx;        
         ub3++;
         }

      if (Ocmt[idx] == "UC3SL" && Oty[idx] == OP_SELLLIMIT)
         UC3Sl = idx; 
              
      if (Ocmt[idx] == "UC3SL" && Oty[idx] == OP_SELL)
         {
         UC3Sls[us3] = idx;      
         us3++;
         }

      if (Ocmt[idx] == "GJ4BL" && Oty[idx] == OP_BUYLIMIT)   
         GJ4Bl = idx; 
                 
      if (Ocmt[idx] == "GJ4BL" && Oty[idx] == OP_BUY)  
         {       
         GJ4Bls[gb4] = idx;         
         gb4++;
         }

      if (Ocmt[idx] == "GJ4SL" && Oty[idx] == OP_SELLLIMIT)
         GJ4Sl = idx;   
               
      if (Ocmt[idx] == "GJ4SL" && Oty[idx] == OP_SELL)
         {
         GJ4Sls[gs4] = idx; 
         gs4++;
         }
         
      if (Ocmt[idx] == "JG4BL" && Oty[idx] == OP_BUYLIMIT)
         JG4Bl = idx;  
           
      if (Ocmt[idx] == "JG4BL" && Oty[idx] == OP_BUY)
         {
         JG4Bls[jb4] = idx; 
         jb4++;
         }
              
      if (Ocmt[idx] == "JG4SL" && Oty[idx] == OP_SELLLIMIT)
         JG4Sl = idx;
         
      if (Ocmt[idx] == "JG4SL" && Oty[idx] == OP_SELL)
         {
         JG4Sls[js4] = idx;
         js4++;
         }
      if (Omn[idx] == magic)
         if (Oty[idx] == OP_SELL || Oty[idx] == OP_BUY)
            {
            SysProfit = SysProfit + Op[idx];
            SysSwap = SysSwap + OrderSwap();
            }                        
      }
}

// Checks to make sure there aren't outstanding orders
if (ec > 1 || ce > 1 || ej > 1 || je > 1 || gc > 1 || cg > 1 || gj > 1 || jg > 1)
   {
   PlaySound("alert.wav");
   if (MessageBox("There are existing system buy orders present and it may skew the ratios, consider closing!!! (OK to continue)","WARNING",MB_OKCANCEL) == IDCANCEL)
      return(0);
   }
if (je != 0 || ej != 0 || jg != 0 || gj != 0 && !TradeUSDJPY)
   {
   PlaySound("alert.wav");
   MessageBox("There are USDJPY orders present and TradeUSDJPY is disabled, check settings!!! (EA HALTED)","WARNING",MB_OK);
   return(0);
   }
if (ec != 0 || ce != 0 || gc != 0 || cg != 0 && !TradeUSDCHF)
   {
   PlaySound("alert.wav");
   MessageBox("There are USDCHF orders present and TradeUSDCHF is disabled, check settings!!! (EA HALTED)","WARNING",MB_OK);
   return(0);
   }
if (ec != 0 || ej != 0 && !TradeEURUSD)
   {
   PlaySound("alert.wav");
   MessageBox("There are EURUSD orders present and TradeEURUSD is disabled, check settings!!! (EA HALTED)","WARNING",MB_OK);
   return(0);
   }    
if (gc != 0 || gj != 0 && !TradeGBPUSD)
   {
   PlaySound("alert.wav");
   MessageBox("There are GBPUSD orders present and TradeGBPUSD is disabled, check settings!!! (EA HALTED)","WARNING",MB_OK);
   return(0);
   }

//Set time in minutes
T1 = TimeYear(TimeCurrent()) *24*365*60 + TimeDayOfYear(TimeCurrent()) *24*60 + TimeHour (TimeCurrent())*60 + TimeMinute (TimeCurrent());


//EURUSD-USDCHF Do checks, set flags
if (TradeEURUSD && TradeUSDCHF )
   {
   double cEC = Correl ("EURUSD","USDCHF",PERIOD_H4,TimeDayOfYear(TimeCurrent())*6);
   T2 = TimeYear (Oot[EUBuy])*24*365*60 + TimeDayOfYear (Oot[EUBuy])*24*60 + TimeHour (Oot[EUBuy])*60 + TimeMinute(Oot[EUBuy]);

   if (cEC > -0.01 && o1 < 1)
      if (MessageBox ("EURUSD-USDCHF is poorly correlated (+):  High Risk, consider other pairs.","WARNING",MB_OKCANCEL) == IDCANCEL)
         return (0);
      else
         o1 ++;
         
   for (xx = 3; xx <= 6; xx++)
      if (lLots[xx] < 0.01)
         lLots[xx] = 0.01;
   for (xx = 9; xx <= 12; xx++)
      if (lLots[xx] < 0.01)
         lLots[xx] = 0.01;
         
   if (EUBuy != 0 && UCBuy != 0)
      if (T1-T2 < 120)
         ECCor = Correl ("EURUSD","USDCHF",PERIOD_M1,T1-T2);
      else
         ECCor = Correl ("EURUSD","USDCHF",PERIOD_H1,(T1-T2)/60);   
         
   if (EUBuy == 0 || UCBuy == 0)                                    
      f1 = true;
   if (EUSl == 0 || UCSl == 0 || EUBl == 0 || UCBl == 0)           
      f2 = true; 
   if (!f1 && Op[EUBuy] + Op[UCBuy] >= gpf * eP * ((Ol[EUBuy]+Ol[UCBuy])/2) * ls || Op[EUBuy] + Op[UCBuy] <= gpf * eP * ((Ol[EUBuy]+Ol[UCBuy])/2) * ls * (-3))   
      {
      Alert ("EU - UC Limit Reached :  ",Op[EUBuy] + Op[UCBuy],"  Reallocating Group");
      f3 = true;
      f9 = true;
      }
//Limit Grabs/max down   
   attempt = 0;
   //if Limit Order met, and has met either +50.00/-75.00 and the Rate has gone above or below the 3 hour max/min plus spreads
   if (!f2)
      {
      for (x5 = 0;x5 < es1;x5++)
         if (Op[EUSls[x5]] > gpf * lsf * eP * Ol[EUSls[x5]] * ls || Op[EUSls[x5]] < gpf * lsf * eP * Ol[EUSls[x5]] * ls * (-1.5) && Rate[1] < eLow - eSpread || Rate[1] > eHigh + eSpread ) // ) // && TestLimits(gpf * lsf * eP * Ol[EUSls[x5]] * ls ))
            while (!CloseLimits(Ot[EUSls[x5]],Ol[EUSls[x5]],MarketInfo ("EURUSD",MODE_ASK),dEU) && attempt <= 10)
               attempt ++;
      for (x5 = 0;x5 < us1;x5++)
         if (Op[UCSls[x5]] > gpf * lsf * cP * Ol[UCSls[x5]] * ls || Op[UCSls[x5]] < gpf * lsf * cP * Ol[UCSls[x5]] * ls * (-1.5) && Rate[2] < cLow - cSpread || Rate[2] > cHigh + cSpread ) // && TestLimits(gpf * lsf * eP * Ol[UCSls[x5]] * ls ))
            while (!CloseLimits(Ot[UCSls[x5]],Ol[UCSls[x5]],MarketInfo ("USDCHF",MODE_ASK),dUC) && attempt <= 10)
               attempt ++;
      for (x5 = 0;x5 < eb1;x5++)
         if (Op[EUBls[x5]] > gpf * lsf * eP * Ol[EUBls[x5]] * ls || Op[EUBls[x5]] < gpf * lsf * cP * Ol[EUBls[x5]] * ls * (-1.5) && Rate[1] > eHigh + eSpread || Rate[1] < eLow - eSpread ) // && TestLimits(gpf * lsf * eP * Ol[EUBls[x5]] * ls ))
            while (!CloseLimits(Ot[EUBls[x5]],Ol[EUBls[x5]],MarketInfo ("EURUSD",MODE_BID),dEU) && attempt <= 10)
               attempt ++;
      for (x5 = 0;x5 < ub1;x5++)
         if (Op[UCBls[x5]] > gpf * lsf * cP * Ol[UCBls[x5]] * ls || Op[UCBls[x5]] < gpf * lsf * cP * Ol[UCBls[x5]] * ls * (-1.5) && Rate[2] > cHigh + cSpread || Rate[2] < cLow - cSpread ) // && TestLimits(gpf * lsf * eP * Ol[UCBls[x5]] * ls ))
            while (!CloseLimits(Ot[UCBls[x5]],Ol[UCBls[x5]],MarketInfo ("USDCHF",MODE_BID),dUC) && attempt <= 10)
               attempt ++;
      }
   }

if (TradeEURUSD && TradeUSDJPY)
   {
   double cEJ = Correl ("EURUSD","USDJPY",PERIOD_H4,TimeDayOfYear(TimeCurrent())*6);
   T3 = TimeYear(Oot[EU2Buy])*24*365*60 + TimeDayOfYear (Oot[EU2Buy])*24*60 + TimeHour (Oot[EU2Buy])*60 + TimeMinute(Oot[EU2Buy]);
   if (cEJ > -0.01 && o1 < 1)
      if (MessageBox ("EURUSD-USDJPY is poorly correlated (+):  High Risk, consider other pairs.","WARNING",MB_OKCANCEL) == IDCANCEL)
         return (0);
      else
         o1 ++;   
   for (xx = 3; xx <= 6; xx++)
      if (jLots[xx] < 0.01)
         jLots[xx] = 0.01;
   for (xx = 9; xx <= 12; xx++)
      if (jLots[xx] < 0.01)
         jLots[xx] = 0.01;
   if (EU2Buy != 0 && UJ2Buy != 0)
      if (T1-T3 < 120)     
         EJCor = Correl ("EURUSD","USDJPY",PERIOD_M1,T1-T3);
      else
         EJCor = Correl ("EURUSD","USDJPY",PERIOD_H1,(T1-T3)/60);
   if (EU2Buy == 0 || UJ2Buy == 0)                                  
      f5 = true;
   if (EU2Sl == 0 || UJ2Sl == 0 || EU2Bl == 0 || UJ2Bl == 0)
      f6 = true;
   if (!f5 && Op[EU2Buy] + Op[UJ2Buy] >= gpf * eP * ((Ol[EU2Buy]+Ol[UJ2Buy])/1.5) * ls || Op[EU2Buy] + Op[UJ2Buy] <= gpf *eP * ((Ol[EU2Buy]+Ol[UJ2Buy])/2) * ls * (-3))  
      {
      Alert ("EU - UJ Limit Reached :  ",Op[EU2Buy] + Op[UJ2Buy],"  Reallocating Group");
      f7 = true;
      f10 = true;
      }
   //Limit Grabs/max down   
   attempt = 0;
   if (!f6)
      {
      for (x5 = 0;x5 < es2;x5++)
         if (Op[EU2Sls[x5]] > gpf * lsf * eP * Ol[EU2Sls[x5]] * ls || Op[EU2Sls[x5]] < gpf * lsf * eP * Ol[EU2Sls[x5]] * ls * (-1.5) && Rate[1] < eLow - eSpread || Rate[1] > eHigh + eSpread ) // && TestLimits(gpf * lsf * eP * Ol[EU2Sls[x5]] * ls ))
            while (!CloseLimits(Ot[EU2Sls[x5]],Ol[EU2Sls[x5]],MarketInfo ("EURUSD",MODE_ASK),dEU) && attempt <= 10)
               attempt ++;
      for (x5 = 0;x5 < js2;x5++)
         if (Op[UJ2Sls[x5]] > gpf * lsf * eP * Ol[UJ2Sls[x5]] * ls || Op[UJ2Sls[x5]] < gpf * lsf * eP * Ol[UJ2Sls[x5]] * ls * (-1.5) && Rate[5] < jLow - jSpread || Rate[5] > jHigh + jSpread ) // && TestLimits(gpf * lsf * eP * Ol[UJ2Sls[x5]] * ls ))
            while (!CloseLimits(Ot[UJ2Sls[x5]],Ol[UJ2Sls[x5]],MarketInfo ("USDJPY",MODE_ASK),dUJ) && attempt <= 10)
               attempt ++;
      for (x5 = 0;x5 < eb2;x5++)
         if (Op[EU2Bls[x5]] > gpf * lsf * eP * Ol[EU2Bls[x5]] * ls || Op[EU2Bls[x5]] < gpf * lsf * eP * Ol[EU2Bls[x5]] * ls * (-1.5) && Rate[1] > eHigh + eSpread || Rate[1] < eLow - eSpread ) // && TestLimits(gpf * lsf * eP * Ol[EU2Bls[x5]] * ls ))
            while (!CloseLimits(Ot[EU2Bls[x5]],Ol[EU2Bls[x5]],MarketInfo ("EURUSD",MODE_BID),dEU) && attempt <= 10)
               attempt ++;
      for (x5 = 0;x5 < jb2;x5++)
         if (Op[UJ2Bls[x5]] > gpf * lsf * eP * Ol[UJ2Bls[x5]] * ls || Op[UJ2Bls[x5]] < gpf * lsf * eP * Ol[UJ2Bls[x5]] * ls * (-1.5) && Rate[5] > jHigh + jSpread || Rate[5] < jLow - jSpread ) // && TestLimits(gpf * lsf * eP * Ol[UJ2Bls[x5]] * ls ))
            while (!CloseLimits(Ot[UJ2Bls[x5]],Ol[UJ2Bls[x5]],MarketInfo ("USDJPY",MODE_BID),dUJ) && attempt <= 10)
               attempt ++;
      }         
   }

if (TradeGBPUSD && TradeUSDCHF)
   {  
   double cGC = Correl ("GBPUSD","USDCHF",PERIOD_H4,TimeDayOfYear(TimeCurrent())*6);
   T4 = TimeYear(Oot[GU3Buy])*24*365*60+TimeDayOfYear(Oot[GU3Buy])*24*60 + TimeHour (Oot[GU3Buy])*60 + TimeMinute (Oot[GU3Buy]);
   if (cGC > -0.01 && o1 < 1)
      if (MessageBox ("GBPUSD-USDCHF is poorly correlated (+):  High Risk, consider other pairs.","WARNING",MB_OKCANCEL) == IDCANCEL)
         return (0);
      else
         o1 ++;  

   for (xx = 3; xx <= 6; xx++)
      if (gLots[xx] < 0.01)
         gLots[xx] = 0.01;
   for (xx = 9; xx <= 12; xx++)
      if (gLots[xx] < 0.01)
         gLots[xx] = 0.01;
   if (GU3Buy != 0 && UC3Buy != 0)
      if (T1-T4 < 120)    
         GCCor = Correl ("GBPUSD","USDCHF",PERIOD_M1,T1-T4);
      else
         GCCor = Correl ("GBPUSD","USDCHF",PERIOD_H1,(T1-T4)/60);
   if (GU3Buy == 0 || UC3Buy == 0)                               
      f11 = true;
   if (GU3Sl == 0 || UC3Sl == 0 || GU3Bl == 0 || UC3Bl == 0)
      f12 = true;
   if (!f11 && Op[GU3Buy] + Op[UC3Buy] >= gpf * gP * ((Ol[GU3Buy]+Ol[UC3Buy])/2) * ls || Op[GU3Buy] + Op[UC3Buy] <= gpf * gP * ((Ol[GU3Buy]+Ol[UC3Buy])/2) * ls * (-3))  
      {
      Alert ("GU - UC Limit Reached :  ",Op[GU3Buy] + Op[UC3Buy],"  Reallocating Group");
      f13 = true;
      f15 = true;
      }
   //Limit Grabs/max down   
   attempt = 0;
   if (!f12)
      {
      for (x5 = 0;x5 < gs3;x5++)
         if (Op[GU3Sls[x5]] > gpf * lsf * gP * Ol[GU3Sls[x5]] * ls  || Op[GU3Sls[x5]] < gpf * lsf * gP * Ol[GU3Sls[x5]] * ls * (-1.5) && Rate[7] < gLow - gSpread || Rate[7] > gHigh + gSpread ) // && TestLimits(gpf * lsf * eP * Ol[GU3Sls[x5]] * ls ))
            while (!CloseLimits(Ot[GU3Sls[x5]],Ol[GU3Sls[x5]],MarketInfo ("GBPUSD",MODE_ASK),dGU) && attempt <= 10)
               attempt ++;
      for (x5 = 0;x5 < us3;x5++)
         if (Op[UC3Sls[x5]] > gpf * lsf * cP * Ol[UC3Sls[x5]] * ls  || Op[UC3Sls[x5]] < gpf * lsf * cP * Ol[UC3Sls[x5]] * ls * (-1.5) && Rate[2] < cLow - cSpread || Rate[2] > cHigh + cSpread ) // && TestLimits(gpf * lsf * eP * Ol[UC3Sls[x5]] * ls ))
            while (!CloseLimits(Ot[UC3Sls[x5]],Ol[UC3Sls[x5]],MarketInfo ("USDCHF",MODE_ASK),dUC) && attempt <= 10)
               attempt ++;
      for (x5 = 0;x5 < gb3;x5++)
         if (Op[GU3Bls[x5]] > gpf * lsf * gP * Ol[GU3Bls[x5]] * ls  || Op[GU3Bls[x5]] < gpf * lsf * gP * Ol[GU3Bls[x5]] * ls * (-1.5) && Rate[7] > gHigh + gSpread || Rate[7] < gLow - gSpread ) // && TestLimits(gpf * lsf * eP * Ol[GU3Bls[x5]] * ls ))
            while (!CloseLimits(Ot[GU3Bls[x5]],Ol[GU3Bls[x5]],MarketInfo ("GBPUSD",MODE_BID),dGU) && attempt <= 10)
               attempt ++;
      for (x5 = 0;x5 < ub3;x5++)
         if (Op[UC3Bls[x5]] > gpf * lsf * cP * Ol[UC3Bls[x5]] * ls  || Op[UC3Bls[x5]] < gpf * lsf * cP * Ol[UC3Bls[x5]] * ls * (-1.5) && Rate[2] > cHigh + cSpread || Rate[2] < cLow - cSpread ) // && TestLimits(gpf * lsf * eP * Ol[UC3Bls[x5]] * ls ))
            while (!CloseLimits(Ot[UC3Bls[x5]],Ol[UC3Bls[x5]],MarketInfo ("USDCHF",MODE_BID),dUC) && attempt <= 10)
               attempt ++;
      }
   }
    
if (TradeGBPUSD && TradeUSDJPY)
   { 
   double cGJ = Correl ("GBPUSD","USDJPY",PERIOD_H4,TimeDayOfYear(TimeCurrent())*6);
   T5 = TimeYear(Oot[GJ4Buy])*24*365*60+TimeDayOfYear(Oot[GJ4Buy])*24*60 + TimeHour (Oot[GJ4Buy])*60 + TimeMinute(Oot[GJ4Buy]);
   if (cGJ > -0.01 && o1 < 1)
      if (MessageBox ("GBPUSD-USDJPY is poorly correlated (+):  High Risk, consider other pairs.","WARNING",MB_OKCANCEL) == IDCANCEL)
         return (0);
      else
         o1 ++; 

   for (xx = 3; xx <= 6; xx++)
      if (gjLots[xx] < 0.01)
         gjLots[xx] = 0.01;
   for (xx = 9; xx <= 12; xx++)
      if (gjLots[xx] < 0.01)
         gjLots[xx] = 0.01;
   if (GJ4Buy != 0 && JG4Buy != 0)
      if (T1-T5 < 120)
         GJCor = Correl ("GBPUSD","USDJPY",PERIOD_M1,T1-T5);
      else  
         GJCor = Correl ("GBPUSD","USDJPY",PERIOD_H1,(T1-T5)/60);
   if (GJ4Buy == 0 || JG4Buy == 0)                               
      f21 = true;
   if (GJ4Sl == 0 || JG4Sl == 0 || GJ4Bl == 0 || JG4Bl == 0)
      f22 = true;
   if (!f21 && Op[GJ4Buy] + Op[JG4Buy] >= gpf * gP * ((Ol[GJ4Buy]+Ol[JG4Buy])/1.5) * ls || Op[GJ4Buy] + Op[JG4Buy] <= gpf *gP * ((Ol[GJ4Buy]+Ol[JG4Buy])/2) * ls * (-3))  
      {
      Alert ("GU - UJ Limit Reached :  ",Op[GJ4Buy] + Op[JG4Buy],"  Reallocating Group");
      f23 = true;
      f25 = true;
      }
   //Limit Grabs/max down   
   attempt = 0;
   if (!f22)
      {
      for (x5 = 0;x5 < gs4;x5++)
         if (Op[GJ4Sls[x5]] > gpf * lsf * gP * Ol[GJ4Sls[x5]] * ls || Op[GJ4Sls[x5]] < gpf * lsf * gP * Ol[GJ4Sls[x5]] * ls * (-1.5) && Rate[7] < gLow - gSpread || Rate[7] > gHigh + gSpread ) // && TestLimits(gpf * lsf * eP * Ol[GJ4Sls[x5]] * ls ))
            while (!CloseLimits(Ot[GJ4Sls[x5]],Ol[GJ4Sls[x5]],MarketInfo ("GBPUSD",MODE_ASK),dGU) && attempt <= 10)
               attempt ++;
      for (x5 = 0;x5 < js4;x5++)
         if (Op[JG4Sls[x5]] > gpf * lsf * gP * Ol[JG4Sls[x5]] * ls || Op[JG4Sls[x5]] < gpf * lsf * gP * Ol[JG4Sls[x5]] * ls * (-1.5) && Rate[5] < jLow - jSpread || Rate[5] > jHigh + jSpread ) // && TestLimits(gpf * lsf * eP * Ol[JG4Sls[x5]] * ls ))
            while (!CloseLimits(Ot[JG4Sls[x5]],Ol[JG4Sls[x5]],MarketInfo ("USDJPY",MODE_ASK),dUJ) && attempt <= 10)
               attempt ++;
      for (x5 = 0;x5 < gb4;x5++)
         if (Op[GJ4Bls[x5]] > gpf * lsf * gP * Ol[GJ4Bls[x5]] * ls || Op[GJ4Bls[x5]] < gpf * lsf * gP * Ol[GJ4Bls[x5]] * ls * (-1.5) && Rate[7] > gHigh + gSpread || Rate[7] < gLow - jSpread ) // && TestLimits(gpf * lsf * eP * Ol[GJ4Bls[x5]] * ls ))
            while (!CloseLimits(Ot[GJ4Bls[x5]],Ol[GJ4Bls[x5]],MarketInfo ("GBPUSD",MODE_BID),dGU) && attempt <= 10)
               attempt ++;
      for (x5 = 0;x5 < jb4;x5++)
         if (Op[JG4Bls[x5]] > gpf * lsf * gP * Ol[JG4Bls[x5]] * ls || Op[JG4Bls[x5]] < gpf * lsf * gP * Ol[JG4Bls[x5]] * ls * (-1.5) && Rate[5] > jHigh + jSpread || Rate[5] < jLow - jSpread ) // && TestLimits(gpf * lsf * eP * Ol[JG4Bls[x5]] * ls ))
            while (!CloseLimits(Ot[JG4Bls[x5]],Ol[JG4Bls[x5]],MarketInfo ("USDJPY",MODE_BID),dUJ) && attempt <= 10)
               attempt ++;
      }
   }

       
if (f1 == true || f3 == true)
   {
   EUSL1 = Rate[1]-(Rate[1]*StopLoss);                                        //set t/p s/l values
   EUTP1 = Rate[1]+(Rate[1]*TakeProfit);
   UCSL1 = Rate[2]-(Rate[2]*StopLoss);
   UCTP1 = Rate[2]+(Rate[2]*TakeProfit);
   
   if (TradeEURUSD && TradeGBPUSD)
      {
      s1 = lLots[9];
      s2 = lLots[10];
      }
   else
      {  
      s1 = lLots[3];
      s2 = lLots[4];
      }
      
   if (os >1)
      if (EUBuy + UCBuy != 0 || f3 == true)
         {                             //if at least one ticket is present
         if (EUBuy == 0 || f9 == true)
            while (!OrderClose (Ot[UCBuy],Ol[UCBuy],NormalizeDouble (Rate[4],dEU),0,0) && attempt < 10)
               {
               Alert ("CLOSE|USDCHF|Error: ",ErrorDescription(GetLastError()),"  Trying Again, attempt# ",attempt);
               RefreshRates ();      
               Rate[4] = MarketInfo ("USDCHF",MODE_BID);
               attempt ++;
               }
         attempt =0;      
         if (UCBuy == 0 || f9 == true)
            while (!OrderClose (Ot[EUBuy],Ol[EUBuy],NormalizeDouble (Rate[3],dEU),0,0) && attempt < 10)
               {
               Alert ("CLOSE|EURUSD|Error: ",ErrorDescription(GetLastError()),"  Trying Again, attempt# ",attempt);
               RefreshRates ();      
               Rate[3] = MarketInfo ("EURUSD",MODE_BID);
               attempt ++;
               }
         attempt =0;           
         if (OrderSend ("EURUSD",OP_BUY,NormalizeDouble (s1,dEU),NormalizeDouble (Rate[1],dEU),0,NormalizeDouble (EUSL1,dEU),NormalizeDouble (EUTP1,dEU),"EUB01",magic,0) == false)
            Alert ("BUY|EURUSD|Error: ",ErrorDescription(GetLastError()));
         if (OrderSend ("USDCHF",OP_BUY,NormalizeDouble (s2,dEU),NormalizeDouble (Rate[2],dEU),0,NormalizeDouble (UCSL1,dEU),NormalizeDouble (UCTP1,dEU),"UCB01",magic,0) == false)
            Alert ("BUY|USDCHF|Error: ",ErrorDescription(GetLastError()));
         }

   if (EUBuy + UCBuy == 0 && f3 == false)                             //if no tickets are present
      {
      if (OrderSend ("EURUSD",OP_BUY,NormalizeDouble (s1,dEU),NormalizeDouble (Rate[1],dEU),0,NormalizeDouble (EUSL1,dEU),NormalizeDouble (EUTP1,dEU),"EUB01",magic,0) == false)
            Alert ("BUY|EURUSD|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("USDCHF",OP_BUY,NormalizeDouble (s2,dEU),NormalizeDouble (Rate[2],dEU),0,NormalizeDouble (UCSL1,dEU),NormalizeDouble (UCTP1,dEU),"UCB01",magic,0) == false)
            Alert ("BUY|USDCHF|Error: ",ErrorDescription(GetLastError()));
      }
   }

if (f5 == true || f7 == true)
   {
   EUSL2 = Rate[1]-(Rate[1]*StopLoss);                                        //set t/p s/l values
   EUTP2 = Rate[1]+(Rate[1]*TakeProfit);
   UJSL2 = Rate[5]-(Rate[5]*StopLoss);
   UJTP2 = Rate[5]+(Rate[5]*TakeProfit);
   
   if (TradeEURUSD && TradeGBPUSD)
      {
      s1 = jLots[9];
      s2 = jLots[10];
      }
   else
      {  
      s1 = jLots[3];
      s2 = jLots[4];
      }
      
   if (os >=1)
      if (EU2Buy + UJ2Buy != 0 || f7 == true)                             //if at least one ticket is present
         {
         if (EU2Buy == 0 || f10 == true)
            while (!OrderClose (Ot[UJ2Buy],Ol[UJ2Buy],NormalizeDouble (Rate[6],dEU),0,0) && attempt < 10)
               {
               Alert ("CLOSE|USDJPY|Error: ",ErrorDescription(GetLastError()),"  Trying Again, attempt# ",attempt);
               RefreshRates ();      
               Rate[6] = MarketInfo ("USDJPY",MODE_BID);
               attempt ++;
               }
         attempt =0;
         if (UJ2Buy == 0 || f10 == true)
            while (!OrderClose (Ot[EU2Buy],Ol[EU2Buy],NormalizeDouble (Rate[3],dEU),0,0) && attempt < 10)
               {
               Alert ("CLOSE|EURUSD|Error: ",ErrorDescription(GetLastError()),"  Trying Again, attempt# ",attempt);
               RefreshRates ();      
               Rate[3] = MarketInfo ("EURUSD",MODE_BID);
               attempt ++;
               }
         attempt =0;

            
         if (OrderSend ("EURUSD",OP_BUY,NormalizeDouble (s1,dEU),NormalizeDouble (Rate[1],dEU),0,NormalizeDouble (EUSL2,dEU),NormalizeDouble (EUTP2,dEU),"EUB02",magic,0) == false)
            Alert ("OPEN|EURUSD|Error: ",ErrorDescription(GetLastError()));    
         if (OrderSend ("USDJPY",OP_BUY,NormalizeDouble (s2,dUJ),NormalizeDouble (Rate[5],dUJ),0,NormalizeDouble (UJSL2,dUJ),NormalizeDouble (UJTP2,dUJ),"UJB02",magic,0) == false)
            Alert ("OPEN|EURUSD|Error: ",ErrorDescription(GetLastError()));
         }

   if (EU2Buy + UJ2Buy == 0 && f7 == false)                             //if no tickets are present
      {
      if (OrderSend ("EURUSD",OP_BUY,NormalizeDouble (s1,dEU),NormalizeDouble (Rate[1],dEU),0,NormalizeDouble (EUSL2,dEU),NormalizeDouble (EUTP2,dEU),"EUB02",magic,0) == false)
         Alert ("EURUSD|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("USDJPY",OP_BUY,NormalizeDouble (s2,dUJ),NormalizeDouble (Rate[5],dUJ),0,NormalizeDouble (UJSL2,dUJ),NormalizeDouble (UJTP2,dUJ),"UJB02",magic,0) == false)
         Alert ("USDJPY|Error: ",ErrorDescription(GetLastError()));
      }
   }
   
if (f11 == true || f13 == true)
   {
   GUSL3 = Rate[7]-(Rate[7]*StopLoss);                                        //set t/p s/l values
   GUTP3 = Rate[7]+(Rate[7]*TakeProfit);
   UCSL3 = Rate[2]-(Rate[2]*StopLoss);
   UCTP3 = Rate[2]+(Rate[2]*TakeProfit);
   
   if (TradeEURUSD && TradeGBPUSD)
      {
      s1 = gLots[9];
      s2 = gLots[10];
      }
   else
      {  
      s1 = gLots[3];
      s2 = gLots[4];
      }
      
   if (os >= 1)
      if (GU3Buy + UC3Buy != 0 || f13 == true)                             //if at least one ticket is present
         {
         if (GU3Buy == 0 || f15 == true)
            while (!OrderClose (Ot[UC3Buy],Ol[UC3Buy],NormalizeDouble (Rate[4],dUC),0,0) && attempt < 10)
               {
               Alert ("CLOSE|USDCHF|Error: ",ErrorDescription(GetLastError()),"  Trying Again, attempt# ",attempt);
               RefreshRates ();      
               Rate[4] = MarketInfo ("USDCHF",MODE_BID);
               attempt ++;
               }
         attempt =0;
               
         if (UC3Buy == 0 || f15 == true)
            while (!OrderClose (Ot[GU3Buy],Ol[GU3Buy],NormalizeDouble (Rate[8],dEU),0,0) && attempt < 10)
               {
               Alert ("CLOSE|GBPUSD|Error: ",ErrorDescription(GetLastError()),"  Trying Again, attempt# ",attempt);
               RefreshRates ();      
               Rate[8] = MarketInfo ("GBPUSD",MODE_BID);
               attempt ++;
               }
         attempt =0;
         
         if (OrderSend ("GBPUSD",OP_BUY,NormalizeDouble (s1,dGU),NormalizeDouble (Rate[7],dGU),0,NormalizeDouble (GUSL3,dGU),NormalizeDouble (GUTP3,dGU),"GUB03",magic,0) == false)
            Alert ("OPEN|GBPUSD|Error: ",ErrorDescription(GetLastError()));    
         if (OrderSend ("USDCHF",OP_BUY,NormalizeDouble (s2,dUC),NormalizeDouble (Rate[2],dUC),0,NormalizeDouble (UCSL3,dUC),NormalizeDouble (UCTP3,dUC),"UCB03",magic,0) == false)
            Alert ("OPEN|USDCHF|Error: ",ErrorDescription(GetLastError()));
         }

   if (GU3Buy + UC3Buy == 0 && f13 == false)                             //if no tickets are present
      {
      if (OrderSend ("GBPUSD",OP_BUY,NormalizeDouble (s1,dGU),NormalizeDouble (Rate[7],dGU),0,NormalizeDouble (GUSL3,dGU),NormalizeDouble (GUTP3,dGU),"GUB03",magic,0) == false)
            Alert ("OPEN|GBPUSD|Error: ",ErrorDescription(GetLastError()));    
      if (OrderSend ("USDCHF",OP_BUY,NormalizeDouble (s2,dUC),NormalizeDouble (Rate[2],dUC),0,NormalizeDouble (UCSL3,dUC),NormalizeDouble (UCTP3,dUC),"UCB03",magic,0) == false)
            Alert ("OPEN|USDCHF|Error: ",ErrorDescription(GetLastError()));
      }
   }
//----------------------------
if (f21 == true || f23 == true)
   {
   GJSL4 = Rate[7]-(Rate[7]*StopLoss);                                        //set t/p s/l values
   GJTP4 = Rate[7]+(Rate[7]*TakeProfit);
   JGSL4 = Rate[5]-(Rate[5]*StopLoss);
   JGTP4 = Rate[5]+(Rate[5]*TakeProfit);
   if (TradeEURUSD && TradeGBPUSD)
      {
      s1 = gjLots[9];
      s2 = gjLots[10];
      }
   else
      {  
      s1 = gjLots[3];
      s2 = gjLots[4];
      }
    if (os >= 1)
      if (GJ4Buy + JG4Buy != 0 || f23 == true)                             //if at least one ticket is present
         {
         if (GJ4Buy == 0 || f25 == true)
            while (!OrderClose (Ot[JG4Buy],Ol[JG4Buy],NormalizeDouble (Rate[6],dUJ),0,0) && attempt < 10)
               {
               Alert ("CLOSE|USDJPY|Error: ",ErrorDescription(GetLastError()),"  Trying Again, attempt# ",attempt);
               RefreshRates ();      
               Rate[6] = MarketInfo ("USDJPY",MODE_BID);
               attempt ++;
               }
         attempt =0;
               
         if (JG4Buy == 0 || f25 == true)
            while (!OrderClose (Ot[GJ4Buy],Ol[GJ4Buy],NormalizeDouble (Rate[8],dGU),0,0) && attempt < 10)
               {
               Alert ("CLOSE|GBPUSD|Error: ",ErrorDescription(GetLastError()),"  Trying Again, attempt# ",attempt);
               RefreshRates ();      
               Rate[8] = MarketInfo ("GBPUSD",MODE_BID);
               attempt ++;
               }
         attempt =0;
         
         if (OrderSend ("GBPUSD",OP_BUY,NormalizeDouble (s1,dGU),NormalizeDouble (Rate[7],dGU),0,NormalizeDouble (GJSL4,dGU),NormalizeDouble (GJTP4,dGU),"GJB04",magic,0) == false)
            Alert ("OPEN|GBPUSD|Error: ",ErrorDescription(GetLastError()));    
         if (OrderSend ("USDJPY",OP_BUY,NormalizeDouble (s2,dUJ),NormalizeDouble (Rate[5],dUJ),0,NormalizeDouble (JGSL4,dUJ),NormalizeDouble (JGTP4,dUJ),"JGB04",magic,0) == false)
            Alert ("OPEN|USDJPY|Error: ",ErrorDescription(GetLastError()));
         }

   if (GJ4Buy + JG4Buy == 0 && f23 == false)                             //if no tickets are present
      {
      if (OrderSend ("GBPUSD",OP_BUY,NormalizeDouble (s1,dGU),NormalizeDouble (Rate[7],dGU),0,NormalizeDouble (GJSL4,dGU),NormalizeDouble (GJTP4,dGU),"GJB04",magic,0) == false)
            Alert ("OPEN|GBPUSD|Error: ",ErrorDescription(GetLastError()));    
      if (OrderSend ("USDJPY",OP_BUY,NormalizeDouble (s2,dUJ),NormalizeDouble (Rate[5],dUJ),0,NormalizeDouble (JGSL4,dUJ),NormalizeDouble (JGTP4,dUJ),"JGB04",magic,0) == false)
            Alert ("OPEN|USDJPY|Error: ",ErrorDescription(GetLastError()));
      }
   }
   

//----------------------------
if (f2 == true || f4 == true)
   {
   blr1=Rate[1]-(Rate[1]*blp1);                                             //set pending-order rate-offsets based on current rates
   blr2=Rate[2]-(Rate[2]*blp2);
   slr1=Rate[1]+(Rate[1]*slp1);
   slr2=Rate[2]+(Rate[2]*slp2);
   
   if (TradeEURUSD && TradeGBPUSD)
      {
      s1 = lLots[11];
      s2 = lLots[12];
      }
   else
      {  
      s1 = lLots[5];
      s2 = lLots[6];
      }
      
   if (EUBl + EUSl + UCBl + UCSl != 0 || f4 == true)         //if at least one ticket is present
      {
      OrderDelete (Ot[EUBl],0);
      OrderDelete (Ot[EUSl],0);
      OrderDelete (Ot[UCBl],0);
      OrderDelete (Ot[UCSl],0);
      if (OrderSend ("EURUSD",OP_BUYLIMIT,NormalizeDouble (s1,dEU),NormalizeDouble (blr1,dEU),0,NormalizeDouble (blr1-(blr1*StopLoss),dEU),NormalizeDouble (blr1+(blr1*TakeProfit),dEU),"EUBL1",magic,0) == false)
         Alert ("EURUSDBUYLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("USDCHF",OP_BUYLIMIT,NormalizeDouble (s2,dUC),NormalizeDouble (blr2,dUC),0,NormalizeDouble (blr2-(blr2*StopLoss),dUC),NormalizeDouble (blr2+(blr2*TakeProfit),dUC),"UCBL1",magic,0) == false)
         Alert ("USDCHFBUYLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("EURUSD",OP_SELLLIMIT,NormalizeDouble (s1,dEU),NormalizeDouble (slr1,dEU),0,NormalizeDouble (slr1+(slr1*StopLoss),dEU),NormalizeDouble (slr1-(slr1*TakeProfit),dEU),"EUSL1",magic,0) == false)
         Alert ("EURUSDSELLLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("USDCHF",OP_SELLLIMIT,NormalizeDouble (s2,dUC),NormalizeDouble (slr2,dUC),0,NormalizeDouble (slr2+(slr2*StopLoss),dUC),NormalizeDouble (slr2-(slr2*TakeProfit),dUC),"UCSL1",magic,0) == false)
         Alert ("USDCHFSELLLIMIT|Error: ",ErrorDescription(GetLastError()));
      }  

   if (EUBl + EUSl + UCBl + UCSl == 0 && f4 == false)         //if no tickets are present
      {
      if (OrderSend ("EURUSD",OP_BUYLIMIT,NormalizeDouble (s1,dEU),NormalizeDouble (blr1,dEU),0,NormalizeDouble (blr1-(blr1*StopLoss),dEU),NormalizeDouble (blr1+(blr1*TakeProfit),dEU),"EUBL1",magic,0) == false)
         Alert ("EURUSDBUYLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("USDCHF",OP_BUYLIMIT,NormalizeDouble (s2,dUC),NormalizeDouble (blr2,dUC),0,NormalizeDouble (blr2-(blr2*StopLoss),dUC),NormalizeDouble (blr2+(blr2*TakeProfit),dUC),"UCBL1",magic,0) == false)
         Alert ("USDCHFBUYLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("EURUSD",OP_SELLLIMIT,NormalizeDouble (s1,dEU),NormalizeDouble (slr1,dEU),0,NormalizeDouble (slr1+(slr1*StopLoss),dEU),NormalizeDouble (slr1-(slr1*TakeProfit),dEU),"EUSL1",magic,0) == false)
         Alert ("EURUSDSELLLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("USDCHF",OP_SELLLIMIT,NormalizeDouble (s2,dUC),NormalizeDouble (slr2,dUC),0,NormalizeDouble (slr2+(slr2*StopLoss),dUC),NormalizeDouble (slr2-(slr2*TakeProfit),dUC),"UCSL1",magic,0) == false)
         Alert ("USDCHFSELLLIMIT|Error: ",ErrorDescription(GetLastError()));
      }

   }

if (f6 == true || f8 == true)
   {
   blr1=Rate[1]-(Rate[1]*blp1);                                             //set pending-order rate-offsets based on current rates
   blr5=Rate[5]-(Rate[5]*blp5);
   slr1=Rate[1]+(Rate[1]*slp1);
   slr5=Rate[5]+(Rate[5]*slp5);
   
   if (TradeEURUSD && TradeGBPUSD)
      {
      s1 = jLots[11];
      s2 = jLots[12];
      }
   else
      {  
      s1 = jLots[5];
      s2 = jLots[6];
      }

   if (EU2Bl + EU2Sl + UJ2Bl + UJ2Sl != 0 || f8 == true)         //if at least one ticket is present
      {
      OrderDelete (Ot[EU2Bl],0);
      OrderDelete (Ot[EU2Sl],0);
      OrderDelete (Ot[UJ2Bl],0);
      OrderDelete (Ot[UJ2Sl],0);
      if (OrderSend ("EURUSD",OP_BUYLIMIT,NormalizeDouble (s1,dEU),NormalizeDouble (blr1,dEU),0,NormalizeDouble (blr1-(blr1*StopLoss),dEU),NormalizeDouble (blr1+(blr1*TakeProfit),dEU),"EU2BL",magic,0) == false)
         Alert ("EURUSDBUYLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("USDJPY",OP_BUYLIMIT,NormalizeDouble (s2,dUJ),NormalizeDouble (blr5,dUJ),0,NormalizeDouble (blr5-(blr5*StopLoss),dUJ),NormalizeDouble (blr5+(blr5*TakeProfit),dUJ),"UJ2BL",magic,0) == false)
         Alert ("USDJPYBUYLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("EURUSD",OP_SELLLIMIT,NormalizeDouble (s1,dEU),NormalizeDouble (slr1,dEU),0,NormalizeDouble (slr1+(slr1*StopLoss),dEU),NormalizeDouble (slr1-(slr1*TakeProfit),dEU),"EU2SL",magic,0) == false)
         Alert ("EURUSDSELLLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("USDJPY",OP_SELLLIMIT,NormalizeDouble (s2,dUJ),NormalizeDouble (slr5,dUJ),0,NormalizeDouble (slr5+(slr5*StopLoss),dUJ),NormalizeDouble (slr5-(slr5*TakeProfit),dUJ),"UJ2SL",magic,0) == false)
         Alert ("USDJPYSELLLIMIT|Error: ",ErrorDescription(GetLastError()));
      }  

   if (EU2Bl + EU2Sl + UJ2Bl + UJ2Sl == 0 && f8 == false)         //if no tickets are present
      {
      if (OrderSend ("EURUSD",OP_BUYLIMIT,NormalizeDouble (s1,dEU),NormalizeDouble (blr1,dEU),0,NormalizeDouble (blr1-(blr1*StopLoss),dEU),NormalizeDouble (blr1+(blr1*TakeProfit),dEU),"EU2BL",magic,0) == false)
         Alert ("EURUSDBUYLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("USDJPY",OP_BUYLIMIT,NormalizeDouble (s2,dUJ),NormalizeDouble (blr5,dUJ),0,NormalizeDouble (blr5-(blr5*StopLoss),dUJ),NormalizeDouble (blr5+(blr5*TakeProfit),dUJ),"UJ2BL",magic,0) == false)
         Alert ("USDJPYBUYLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("EURUSD",OP_SELLLIMIT,NormalizeDouble (s1,dEU),NormalizeDouble (slr1,dEU),0,NormalizeDouble (slr1+(slr1*StopLoss),dEU),NormalizeDouble (slr1-(slr1*TakeProfit),dEU),"EU2SL",magic,0) == false)
         Alert ("EURUSDSELLLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("USDJPY",OP_SELLLIMIT,NormalizeDouble (s2,dUJ),NormalizeDouble (slr5,dUJ),0,NormalizeDouble (slr5+(slr5*StopLoss),dUJ),NormalizeDouble (slr5-(slr5*TakeProfit),dUJ),"UJ2SL",magic,0) == false)
         Alert ("USDJPYSELLLIMIT|Error: ",ErrorDescription(GetLastError()));
       }

   }  


if (f12 == true || f16 == true)
   {
   blr2=Rate[2]-(Rate[2]*blp2);                                       //set pending-order rate-offsets based on current rates
   blr7=Rate[7]-(Rate[7]*blp7);
   slr2=Rate[2]+(Rate[2]*slp2);
   slr7=Rate[7]+(Rate[7]*slp7);
   
   if (TradeEURUSD && TradeGBPUSD)
      {
      s1 = gLots[11];
      s2 = gLots[12];
      }
   else
      {  
      s1 = gLots[5];
      s2 = gLots[6];
      }
      

   if (GU3Bl + GU3Sl + UC3Bl + UC3Sl != 0 || f16 == true)         //if at least one ticket is present
      {
      OrderDelete (Ot[GU3Bl],0);
      OrderDelete (Ot[GU3Sl],0);
      OrderDelete (Ot[UC3Bl],0);
      OrderDelete (Ot[UC3Sl],0);
      if (OrderSend ("GBPUSD",OP_BUYLIMIT,NormalizeDouble (s1,dGU),NormalizeDouble (blr7,dGU),0,NormalizeDouble (blr7-(blr7*StopLoss),dGU),NormalizeDouble (blr7+(blr7*TakeProfit),dGU),"GU3BL",magic,0) == false)
         Alert ("GBPUSDBUYLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("USDCHF",OP_BUYLIMIT,NormalizeDouble (s2,dUC),NormalizeDouble (blr2,dUC),0,NormalizeDouble (blr2-(blr2*StopLoss),dUC),NormalizeDouble (blr2+(blr2*TakeProfit),dUC),"UC3BL",magic,0) == false)
         Alert ("USDCHFBUYLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("GBPUSD",OP_SELLLIMIT,NormalizeDouble (s1,dGU),NormalizeDouble (slr7,dGU),0,NormalizeDouble (slr7+(slr7*StopLoss),dGU),NormalizeDouble (slr7-(slr7*TakeProfit),dGU),"GU3SL",magic,0) == false)
         Alert ("GBPUSDSELLLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("USDCHF",OP_SELLLIMIT,NormalizeDouble (s2,dUC),NormalizeDouble (slr2,dUC),0,NormalizeDouble (slr2+(slr2*StopLoss),dUC),NormalizeDouble (slr2-(slr2*TakeProfit),dUC),"UC3SL",magic,0) == false)
         Alert ("USDCHFSELLLIMIT|Error: ",ErrorDescription(GetLastError()));
      }  

   if (GU3Bl + GU3Sl + UC3Bl + UC3Sl == 0 && f16 == false)         //if no tickets are present
      {
      if (OrderSend ("GBPUSD",OP_BUYLIMIT,NormalizeDouble (s1,dGU),NormalizeDouble (blr7,dGU),0,NormalizeDouble (blr7-(blr7*StopLoss),dGU),NormalizeDouble (blr7+(blr7*TakeProfit),dGU),"GU3BL",magic,0) == false)
         Alert ("GBPUSDBUYLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("USDCHF",OP_BUYLIMIT,NormalizeDouble (s2,dUC),NormalizeDouble (blr2,dUC),0,NormalizeDouble (blr2-(blr2*StopLoss),dUC),NormalizeDouble (blr2+(blr2*TakeProfit),dUC),"UC3BL",magic,0) == false)
         Alert ("USDCHFBUYLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("GBPUSD",OP_SELLLIMIT,NormalizeDouble (s1,dGU),NormalizeDouble (slr7,dGU),0,NormalizeDouble (slr7+(slr7*StopLoss),dGU),NormalizeDouble (slr7-(slr7*TakeProfit),dGU),"GU3SL",magic,0) == false)
         Alert ("GBPUSDSELLLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("USDCHF",OP_SELLLIMIT,NormalizeDouble (s2,dUC),NormalizeDouble (slr2,dUC),0,NormalizeDouble (slr2+(slr2*StopLoss),dUC),NormalizeDouble (slr2-(slr2*TakeProfit),dUC),"UC3SL",magic,0) == false)
         Alert ("USDCHFSELLLIMIT|Error: ",ErrorDescription(GetLastError()));
       }

   }
//---------------
if (f22 == true || f26 == true)
   {
   blr5=Rate[5]-(Rate[5]*blp5);
   blr7=Rate[7]-(Rate[7]*blp7);
   slr5=Rate[5]+(Rate[5]*slp5);
   slr7=Rate[7]+(Rate[7]*slp7);
  
   if (TradeEURUSD && TradeGBPUSD)
      {
      s1 = gjLots[11];
      s2 = gjLots[12];
      }
   else
      {  
      s1 = gjLots[5];
      s2 = gjLots[6];
      }

   if (GJ4Bl + GJ4Sl + JG4Bl + JG4Sl != 0 || f26 == true)         //if at least one ticket is present
      {
      //Alert ("GBPUSD","|",NormalizeDouble (s1,dGU),"|",NormalizeDouble (slr7,dGU),"|",NormalizeDouble (slr7+(slr7*StopLoss),dGU),"|",NormalizeDouble (slr7-(slr7*TakeProfit),dGU));
      OrderDelete (Ot[GJ4Bl],0);
      OrderDelete (Ot[GJ4Sl],0);
      OrderDelete (Ot[JG4Bl],0);
      OrderDelete (Ot[JG4Sl],0);
      if (OrderSend ("GBPUSD",OP_BUYLIMIT,NormalizeDouble (s1,dGU),NormalizeDouble (blr7,dGU),0,NormalizeDouble (blr7-(blr7*StopLoss),dGU),NormalizeDouble (blr7+(blr7*TakeProfit),dGU),"GJ4BL",magic,0) == false)
         Alert ("GBPUSDBUYLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("USDJPY",OP_BUYLIMIT,NormalizeDouble (s2,dUJ),NormalizeDouble (blr5,dUJ),0,NormalizeDouble (blr5-(blr5*StopLoss),dUJ),NormalizeDouble (blr5+(blr5*TakeProfit),dUJ),"JG4BL",magic,0) == false)
         Alert ("USDJPYBUYLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("GBPUSD",OP_SELLLIMIT,NormalizeDouble (s1,dGU),NormalizeDouble (slr7,dGU),0,NormalizeDouble (slr7+(slr7*StopLoss),dGU),NormalizeDouble (slr7-(slr7*TakeProfit),dGU),"GJ4SL",magic,0) == false)
         Alert ("GBPUSDSELLLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("USDJPY",OP_SELLLIMIT,NormalizeDouble (s2,dUJ),NormalizeDouble (slr5,dUJ),0,NormalizeDouble (slr5+(slr5*StopLoss),dUJ),NormalizeDouble (slr5-(slr5*TakeProfit),dUJ),"JG4SL",magic,0) == false)
         Alert ("USDJPYSELLLIMIT|Error: ",ErrorDescription(GetLastError()));
      }  

   if (GJ4Bl + GJ4Sl + JG4Bl + JG4Sl == 0 && f16 == false)         //if no tickets are present
      {
      if (OrderSend ("GBPUSD",OP_BUYLIMIT,NormalizeDouble (s1,dGU),NormalizeDouble (blr7,dGU),0,NormalizeDouble (blr7-(blr7*StopLoss),dGU),NormalizeDouble (blr7+(blr7*TakeProfit),dGU),"GJ4BL",magic,0) == false)
         Alert ("GBPUSDBUYLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("USDJPY",OP_BUYLIMIT,NormalizeDouble (s2,dUJ),NormalizeDouble (blr5,dUJ),0,NormalizeDouble (blr5-(blr5*StopLoss),dUJ),NormalizeDouble (blr5+(blr5*TakeProfit),dUJ),"JG4BL",magic,0) == false)
         Alert ("USDJPYBUYLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("GBPUSD",OP_SELLLIMIT,NormalizeDouble (s1,dGU),NormalizeDouble (slr7,dGU),0,NormalizeDouble (slr7+(slr7*StopLoss),dGU),NormalizeDouble (slr7-(slr7*TakeProfit),dGU),"GJ4SL",magic,0) == false)
         Alert ("GBPUSDSELLLIMIT|Error: ",ErrorDescription(GetLastError()));
      if (OrderSend ("USDJPY",OP_SELLLIMIT,NormalizeDouble (s2,dUJ),NormalizeDouble (slr5,dUJ),0,NormalizeDouble (slr5+(slr5*StopLoss),dUJ),NormalizeDouble (slr5-(slr5*TakeProfit),dUJ),"JG4SL",magic,0) == false)
         Alert ("USDJPYSELLLIMIT|Error: ",ErrorDescription(GetLastError()));
       }

   }
 
//---------------
Comment ("Hedge Rocks, Expert Advisor: Version ",ver,"  ALPHA   ",ac);  

if (TradeUSDCHF)
   {
   Crawler ("Group GU-UC:      ",3,102,3,Op[GU3Buy] + Op[UC3Buy],2,Yellow);  
   Crawler ("GU-UC Term Correl:   ",3,82,3,GCCor,2,Yellow);
   Crawler ("GU-UC YTD Correl:    ",3,62,3,cGC,2,Yellow);
   Crawler ("O/N Interest GU+UC: ",3,42,3,Ol[GU3Buy]*sGUl + Ol[UC3Buy]*sUCl,2,Yellow);
   Crawler ("GU-UC Grab Profit: ",3,22,3,gpf * gP * ((Ol[GU3Buy]+Ol[UC3Buy])/2) * ls,2,Yellow);
   Crawler ("GU-UC Max Drawdown: ",3,2,3,gpf * gP * ((Ol[GU3Buy]+Ol[UC3Buy])/2) * ls * (-3),2,Yellow);

   Crawler ("Group EU-UC:       ",0,102,2,Op[EUBuy] + Op[UCBuy],2,Yellow);
   Crawler ("EU-UC Term Correl: ",0,82,2,ECCor,2,Yellow);
   Crawler ("EU-UC YTD Correl:  ",0,62,2,cEC,2,Yellow);
   Crawler ("O/N Interest EU+UC: ",0,42,2,Ol[EUBuy]*sEUl + Ol[UCBuy]*sUCl,2,Yellow);
   Crawler ("EU-UC Grab Profit:  ",0,22,2,gpf * eP * ((Ol[EUBuy]+Ol[UCBuy])/2) * ls,2,Yellow);
   Crawler ("EU-UC Max Drawdown: ",0,2,2,gpf * eP * ((Ol[EUBuy]+Ol[UCBuy])/2) * ls * (-3),2,Yellow);
   }
if (TradeUSDJPY)
   {
   Crawler ("Group GU-UJ:      ",3,102,3,Op[GJ4Buy] + Op[JG4Buy],2,Yellow);
   Crawler ("GU-UJ Term Correl:",3,82,3,GJCor,2,Yellow);
   Crawler ("GU-UJ YTD Correl: ",3,62,3,cGJ,2,Yellow);
   Crawler ("O/N Interest GU+UJ: ",3,42,3,Ol[GJ4Buy]*sGUl + Ol[JG4Buy]*sUJl,2,Yellow);
   Crawler ("GU-UJ Grab Profit:   ",3,22,3,gpf * gP * ((Ol[GJ4Buy]+Ol[JG4Buy])/1.5) * ls,2,Yellow);
   Crawler ("GU-UJ Max Drawdown:",3,2,3,gpf *gP * ((Ol[GJ4Buy]+Ol[JG4Buy])/2) * ls * (-3),2,Yellow);


   Crawler ("Group EU-UJ:      ",0,102,2,Op[EU2Buy] + Op[UJ2Buy],2,Yellow);
   Crawler ("EU-UJ Term Correl: ",0,82,2,EJCor,2,Yellow);
   Crawler ("EU-UJ YTD Correl:    ",0,62,2,cEJ,2,Yellow);
   Crawler ("O/N Interest EU+UJ: ",0,42,2,Ol[EU2Buy]*sEUl + Ol[UJ2Buy]*sUJl,2,Yellow);
   Crawler ("EU-UJ Grab Profit:  ",0,22,2,gpf * eP * ((Ol[EU2Buy]+Ol[UJ2Buy])/1.5) * ls,2,Yellow);
   Crawler ("EU-UJ Max Drawdown: ",0,2,2,gpf *eP * ((Ol[EU2Buy]+Ol[UJ2Buy])/2) * ls * (-3),2,Yellow);


   }
Crawler ("Ticks:",1,23,0,os,0,Yellow);
Crawler ("Session Loss/Gain: ",1,42,0,SysProfit,2,Yellow);
Crawler ("Session Interest :  ",1,60,0,SysSwap,2,Yellow);

Crawler ("Margin Percentage: ",14,23,1,MarginPercentage * 100,0,Yellow);
Crawler ("Leverage: (x:1)  ",14,42,1,lev,0,Yellow);
Crawler ("Lot Size:     ",14,60,1,ls,0,Yellow);


os ++;
Sleep (500);
return (0);
}

void Crawler (string Obj, int x, int y,int corner, double Info, int Dts, color Col1)
{
ObjectCreate(Obj,OBJ_LABEL, 0,0,0);
ObjectSetText(Obj,Obj + DoubleToStr(Info, Dts), 9, "Courier New", Col1);
ObjectSet(Obj, OBJPROP_CORNER, corner);
ObjectSet(Obj, OBJPROP_XDISTANCE, x);
ObjectSet(Obj, OBJPROP_YDISTANCE, y);
ObjectSet(Obj, OBJPROP_FONTSIZE, 9);
return;
}


bool CloseLimits (int ticket,double lots,double rate,int digits)
{
if (!OrderClose (ticket,lots,NormalizeDouble (rate,digits),0,0))
   {
   Alert ("Error Closing Limit Order: ",ErrorDescription(GetLastError()),"|",ticket,"|",lots,"|",rate,"|",digits);
   RefreshRates ();
   Sleep (10);
   return (false);
   }

return (true);
}

double Correl (string CurHigh, string CurLow, int Per, int nb)
{
if (nb < 2)
   return (0);
int attempt = 0;
double sweep;
double delta_x,delta_y;
double sum_sq_x = 0;
double sum_sq_y = 0;
double sum_coproduct = 0;
while (iClose (CurHigh,Per,0) == 0 && attempt < 20)
   {
   Print ("Attempt (High)# ",attempt);
   attempt ++;
   Sleep (50);
   }
attempt = 0;
while (iClose (CurLow,Per,0) == 0 && attempt < 20)
   {
   Print ("Attempt (Low)# ",attempt);
   attempt ++;
   Sleep (50);
   }  
double mean_x = iClose (CurHigh,Per,0);
double mean_y = iClose (CurLow,Per,0);
for (int i = 1;i < nb;i++) //iBars (CurHigh,Per); i++)
   {
    sweep = (i - 1.0) / i;
    delta_x = iClose (CurHigh,Per,i) - mean_x;
    delta_y = iClose (CurLow,Per,i) - mean_y;
    sum_sq_x += delta_x * delta_x * sweep;
    sum_sq_y += delta_y * delta_y * sweep;
    sum_coproduct += delta_x * delta_y * sweep;
    mean_x += delta_x / i;
    mean_y += delta_y / i;
    }
double pop_sd_x = MathSqrt( sum_sq_x / nb );//iBars (CurHigh,Per) );
double pop_sd_y = MathSqrt( sum_sq_y / nb );//iBars (CurHigh,Per) );
double cov_x_y = sum_coproduct / nb ;//iBars (CurHigh,Per);
if (pop_sd_x * pop_sd_y == 0) 
   return (0);
double correlation = cov_x_y / (pop_sd_x * pop_sd_y);
return (correlation);
}