//+------------------------------------------------------------------+
#define SIGNAL_NONE 0
#define SIGNAL_BUY   1
#define SIGNAL_SELL  2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4

#property copyright "Ronald Raygun"
#property link      "http://www.RonaldRaygunForex.com/Support"

extern string Remark1 = "== Main Settings ==";
extern int MagicNumber = 0;
extern bool SignalsOnly = False;
extern bool Alerts = False;
extern bool SignalMail = False;
extern bool PlaySounds = False;
extern bool ECNBroker = False;
extern bool EachTickMode = True;
extern double Lots = 0;
extern bool MoneyManagement = False;
extern int Risk = 0;
extern int Slippage = 5;
 bool UseStopLoss = True;
 int StopLoss = 100;
 bool UseTakeProfit = False;
 int TakeProfit = 60;
extern bool UseTrailingStop = False;
extern int TrailingStop = 30;
extern bool MoveStopOnce = False;
extern int MoveStopWhenPrice = 50;
extern int MoveStopTo = 1;
extern string Remark2 = "";
extern string Remark3 = "== Pattern Match Settings ==";
extern int WindowLength = 20;
extern int PredictionLength = 10;
extern int SearchLength = 100000;
extern int MAPeriod = 1;
extern int MAPrice = PRICE_HIGH;
extern int TopResults = 10;
extern double MinCorr = 0.90;
extern double TPBuffer = 0.20;
extern double SLBuffer = 0.10;
extern double MinRR = 2;

double MasterArray[][2];
double FutureHighArray[];
double FutureLowArray[];

int TradeBar;
int TradesThisBar;

int OpenBarCount;
int CloseBarCount;

int LongMailSignalBarCount;
int ShortMailSignalBarCount;

int LongAlertSignalBarCount;
int ShortAlertSignalBarCount;

int LongSoundSignalBarCount;
int ShortSoundSignalBarCount;

string BrokerType = "4-Digit Broker";
double BrokerMultiplier = 1;

int Current;
bool TickCheck = False;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init() {
   OpenBarCount = Bars;
   CloseBarCount = Bars;
   
   LongMailSignalBarCount = Bars;
   ShortMailSignalBarCount = Bars;
   
   LongAlertSignalBarCount = Bars;
   ShortAlertSignalBarCount = Bars;
   
   LongAlertSignalBarCount = Bars;
   ShortAlertSignalBarCount = Bars;
   

   
   if(Digits == 3 || Digits == 5)
      {
      BrokerType = "5-Digit Broker";
      BrokerMultiplier = 10;
      }


   if (EachTickMode) Current = 0; else Current = 1;

   start();
   return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit() {

for(int DeOT = ObjectsTotal(); DeOT >= 0; DeOT--)
   {
   if(StringFind(ObjectName(DeOT), WindowExpertName(), 0) != -1)
      {
      ObjectDelete(ObjectName(DeOT));
      }
   }
Comment("");

   return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() 


{
   int Order = SIGNAL_NONE;
   int Total, Ticket;
   double StopLossLevel, TakeProfitLevel;
   double PotentialStopLoss;
   double BEven; 
   double TrailStop;



   if (EachTickMode && Bars != CloseBarCount) TickCheck = False;
   Total = OrdersTotal();
   Order = SIGNAL_NONE;

//Limit Trades Per Bar
if(TradeBar != Bars)
   {
   TradeBar = Bars;
   TradesThisBar = 0;
   }


//Money Management sequence
 if (MoneyManagement)
   {
      if (Risk<1 || Risk>100)
      {
         Comment("Invalid Risk Value.");
         return(0);
      }
      else
      {
         Lots=MathFloor((AccountFreeMargin()*AccountLeverage()*Risk*Point*BrokerMultiplier*100)/(Ask*MarketInfo(Symbol(),MODE_LOTSIZE)*MarketInfo(Symbol(),MODE_MINLOT)))*MarketInfo(Symbol(),MODE_MINLOT);
      }
   }

   //+------------------------------------------------------------------+
   //| Variable Begin                                                   |
   //+------------------------------------------------------------------+
   
   static int BarCount;
   
   
   if(BarCount != Bars)
      {
      BarCount = Bars;
      RunAnalysis();
      
      
   
      //Determine the highest high and lowest low
      double HighestHigh = FutureHighArray[ArrayMaximum(FutureHighArray, WHOLE_ARRAY, 0)];
      double LowestLow = FutureLowArray[ArrayMinimum(FutureLowArray, WHOLE_ARRAY, 0)];
      
      //Calculate amount to discount the take profit and stoploss
      double TPPipBuffer = (HighestHigh - LowestLow) * TPBuffer;
      double SLPipBuffer = (HighestHigh - LowestLow) * SLBuffer;
      
      double LongRR = (HighestHigh - TPPipBuffer - iClose(NULL, 0, 1)) / (iClose(NULL, 0, 1) - LowestLow + SLPipBuffer);
      double LongTPPips = HighestHigh - TPPipBuffer - iClose(NULL, 0, 1);
      double LongSLPips = iClose(NULL, 0, 1) - LowestLow + SLPipBuffer;
      
      double ShortRR = (iClose(NULL, 0, 1) - LowestLow + TPPipBuffer) / (HighestHigh + SLPipBuffer - iClose(NULL, 0, 1));
      double ShortTPPips = iClose(NULL, 0, 1) - LowestLow + TPPipBuffer;
      double ShortSLPips = HighestHigh + SLPipBuffer - iClose(NULL, 0, 1);
      
      double TPPips = 0.0;
      double SLPips = 0.0;
      
      double MinimumPips = MarketInfo(Symbol(), MODE_STOPLEVEL) * Point;
      
      //Higher risk / reward ratio gets the trade
      string TradeTrigger = "None";
      if(LongRR > ShortRR && LongRR >= MinRR && LongSLPips > MinimumPips) 
         {
         TradeTrigger = "Open Long";
         TPPips = LongTPPips;
         SLPips = LongSLPips;
         }
      if(ShortRR > LongRR && ShortRR >= MinRR && ShortSLPips > MinimumPips) 
         {
         TradeTrigger = "Open Short";
         TPPips = ShortTPPips;
         SLPips = ShortSLPips;
         }
      }
   //+------------------------------------------------------------------+
   //| Variable End                                                     |
   //+------------------------------------------------------------------+

   //Check position
   bool IsTrade = False;

   for (int i = 0; i < Total; i ++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderType() <= OP_SELL &&  OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         IsTrade = True;
         if(OrderType() == OP_BUY) {
         
            
            //Close

            //+------------------------------------------------------------------+
            //| Signal Begin(Exit Buy)                                           |
            //+------------------------------------------------------------------+
            
            //if(iBarShift(NULL, 0, OrderOpenTime(), false) > PredictionLength) Order = SIGNAL_CLOSEBUY;

            //+------------------------------------------------------------------+
            //| Signal End(Exit Buy)                                             |
            //+------------------------------------------------------------------+

            if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != CloseBarCount)))) {
               OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
               if (!EachTickMode) CloseBarCount = Bars;
               IsTrade = False;
               continue;
            }
            
            PotentialStopLoss = OrderStopLoss();
            BEven = BreakEvenValue(MoveStopOnce, OrderTicket(), MoveStopTo, MoveStopWhenPrice);
            TrailStop = TrailingStopValue(UseTrailingStop, OrderTicket(), TrailingStop);
            
            if(BEven > PotentialStopLoss && BEven != 0) PotentialStopLoss = BEven;
            if(TrailStop > PotentialStopLoss && TrailStop != 0) PotentialStopLoss = TrailStop;
             
            if(PotentialStopLoss != OrderStopLoss()) OrderModify(OrderTicket(),OrderOpenPrice(), PotentialStopLoss, OrderTakeProfit(), 0, MediumSeaGreen); 
         
         } else {
        
            //Close

            //+------------------------------------------------------------------+
            //| Signal Begin(Exit Sell)                                          |
            //+------------------------------------------------------------------+

            //if(iBarShift(NULL, 0, OrderOpenTime(), false) > PredictionLength) Order = SIGNAL_CLOSESELL;

            //+------------------------------------------------------------------+
            //| Signal End(Exit Sell)                                            |
            //+------------------------------------------------------------------+

            if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != CloseBarCount)))) {
               OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
               if (!EachTickMode) CloseBarCount = Bars;
               IsTrade = False;
               continue;
            }
            
            PotentialStopLoss = OrderStopLoss();
            BEven = BreakEvenValue(MoveStopOnce, OrderTicket(), MoveStopTo, MoveStopWhenPrice);
            TrailStop = TrailingStopValue(UseTrailingStop, OrderTicket(), TrailingStop);
            
            if((BEven < PotentialStopLoss && BEven != 0) || (PotentialStopLoss == 0)) PotentialStopLoss = BEven;
            if((TrailStop < PotentialStopLoss && TrailStop != 0) || (PotentialStopLoss == 0)) PotentialStopLoss = TrailStop;
            
            if(PotentialStopLoss != OrderStopLoss() || OrderStopLoss() == 0) OrderModify(OrderTicket(),OrderOpenPrice(), PotentialStopLoss, OrderTakeProfit(), 0, DarkOrange);
              
         }
      }
   }

   //+------------------------------------------------------------------+
   //| Signal Begin(Entry)                                              |
   //+------------------------------------------------------------------+

if(TradeTrigger == "Open Long") Order = SIGNAL_BUY;
if(TradeTrigger == "Open Short") Order = SIGNAL_SELL;

   //+------------------------------------------------------------------+
   //| Signal End                                                       |
   //+------------------------------------------------------------------+

   //Buy
   if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != OpenBarCount)))) {
      if(SignalsOnly) {
         if (SignalMail && LongMailSignalBarCount != Bars) 
            {
            SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
            LongMailSignalBarCount = Bars;
            }
         if (Alerts && LongAlertSignalBarCount != Bars)
            {
            Alert("[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
            LongAlertSignalBarCount = Bars;
            }
         if (PlaySounds && LongSoundSignalBarCount != Bars) 
            {
            PlaySound("alert.wav");
            LongSoundSignalBarCount = Bars;
            }
      }
      
      if(!IsTrade && !SignalsOnly && TradesThisBar < 1) {
         //Check free margin
         if (AccountFreeMarginCheck(Symbol(), OP_BUY, Lots) < 0) {
            Print("We have no money. Free Margin = ", AccountFreeMargin());
            return(0);
         }

         if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;
         if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;
         
         StopLossLevel = Ask - SLPips;
         TakeProfitLevel = Ask + TPPips;
         
         Ticket = SendOrder(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);
            if(Ticket > 0) {
               if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
				  Print("BUY order opened : ", OrderOpenPrice());
                  if (SignalMail && LongMailSignalBarCount != Bars) 
                     {
                     SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
                     LongMailSignalBarCount = Bars;
                     }
                  if (Alerts && LongAlertSignalBarCount != Bars)
                     {
                     Alert("[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
                     LongAlertSignalBarCount = Bars;
                     }
                  if (PlaySounds && LongSoundSignalBarCount != Bars) 
                     {
                     PlaySound("alert.wav");
                     LongSoundSignalBarCount = Bars;
                     }
                  TradesThisBar++;
			   } else {
   				Print("Error opening BUY order : ", GetLastError());
			   }
            }
            
         if (EachTickMode) TickCheck = True;
         if (!EachTickMode) OpenBarCount = Bars;
         return(0);
      }
   }

   //Sell
   if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != OpenBarCount)))) {
      if(SignalsOnly) {
          if (SignalMail && ShortMailSignalBarCount != Bars) 
            {
            SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
            ShortMailSignalBarCount = Bars;
            }
          if (Alerts && ShortAlertSignalBarCount != Bars)
            {
            Alert("[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
            ShortAlertSignalBarCount = Bars;
            }
          if (PlaySounds && ShortSoundSignalBarCount != Bars) 
            {
            PlaySound("alert.wav");
            ShortSoundSignalBarCount = Bars;
            }
         }
      if(!IsTrade && !SignalsOnly && TradesThisBar < 1) {
         //Check free margin
         if (AccountFreeMarginCheck(Symbol(), OP_SELL, Lots) < 0) {
            Print("We have no money. Free Margin = ", AccountFreeMargin());
            return(0);
         }

         if (UseStopLoss) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;
         if (UseTakeProfit) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;
         
         StopLossLevel = Bid + SLPips;
         TakeProfitLevel = Bid - TPPips;
         
         Ticket = SendOrder(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);
         if(Ticket > 0) {
            if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
				Print("SELL order opened : ", OrderOpenPrice());
                if (SignalMail && ShortMailSignalBarCount != Bars) 
                  {
                  SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
                  ShortMailSignalBarCount = Bars;
                  }
               if (Alerts && ShortAlertSignalBarCount != Bars)
                  {
                  Alert("[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
                  ShortAlertSignalBarCount = Bars;
                  }
               if (PlaySounds && ShortSoundSignalBarCount != Bars) 
                  {
                  PlaySound("alert.wav");
                  ShortSoundSignalBarCount = Bars;
                  }
                TradesThisBar++;
			} else {
				Print("Error opening SELL order : ", GetLastError());
			}
         }
         if (EachTickMode) TickCheck = True;
         if (!EachTickMode) OpenBarCount = Bars;
         return(0);
      }
   }

   if (!EachTickMode) CloseBarCount = Bars;

   return(0);
}

int SendOrder(string OpenSymbol, int OpenCommand, double OpenVolume, double OpenPrice, int OpenSlippage, double OpenStopLoss, double OpenTakeProfit, string OpenComment, int OpenMagicNumber, datetime OpenExpiration, color OpenColor)
   {
   int FTicket = -2;
   if(!ECNBroker)
      {
      FTicket = OrderSend(OpenSymbol, OpenCommand, OpenVolume, OpenPrice, OpenSlippage, OpenStopLoss, OpenTakeProfit, OpenComment, OpenMagicNumber, OpenExpiration, OpenColor);
      if(FTicket == -1)
         {
         return(FTicket);
         }
      if(OrderSelect(FTicket, SELECT_BY_TICKET, MODE_TRADES))
         {
         return(FTicket);
         }
         else
         {
         ECNBroker = True;
         }
      }
   if(ECNBroker)
      {
      FTicket = OrderSend(OpenSymbol, OpenCommand, OpenVolume, OpenPrice, OpenSlippage, 0.0, 0.0, OpenComment, OpenMagicNumber, OpenExpiration, OpenColor);
      if(FTicket > 0)
         {
         OrderSelect(FTicket, SELECT_BY_TICKET, MODE_TRADES);
         OrderModify(FTicket, OrderOpenPrice(), OpenStopLoss, OpenTakeProfit, OrderExpiration(), CLR_NONE);
         }
      return(FTicket);   
      }    
   } 

double BreakEvenValue (bool Decision, int OrderTicketNum, int FMoveStopTo, int FMoveStopWhenPrice)
   {
   //Select the appropriate order ticket
   OrderSelect(OrderTicketNum, SELECT_BY_TICKET, MODE_TRADES);
   
   //If the Order is a BUY order...
   if(OrderType() == OP_BUY)
      {
      //Check if the user wants to use the MoveStopOnce function and did it correctly
      if(Decision && FMoveStopWhenPrice > 0) 
         {
         //Check if the trade is above the required profit threshold
         if(Bid - OrderOpenPrice() >= Point * FMoveStopWhenPrice) 
            {
            //Return the value of the stoploss
            return(OrderOpenPrice() + Point * FMoveStopTo);
            }
         }
      }
   
   //If the Order is a SELL order...   
   if(OrderType() == OP_SELL)
      {
      //Check if the user wants to use the MoveStopOnce function and did it correctly
      if(Decision && FMoveStopWhenPrice > 0) 
         {
         //Check if the trade is above the required profit threshold
         if(OrderOpenPrice() - Ask >= Point * FMoveStopWhenPrice) 
            {
            //Return the value of the stoploss
            return(OrderOpenPrice() - Point * FMoveStopTo);
            }
         }
      }   
      
   if(OrderType() != OP_BUY || OrderType() != OP_SELL) return(0);
   }
   
double TrailingStopValue (bool Decision, int OrderTicketNum, int FTrailingStop)
   {
   //Select the appropriate order ticket
   OrderSelect(OrderTicketNum, SELECT_BY_TICKET, MODE_TRADES);
   
   //If the Order is a BUY order...
   if(OrderType() == OP_BUY)
      {
      //Check if the user wants to use teh Trailingstop function and did it correctly
      if(Decision && FTrailingStop > 0) 
         {                 
         //Check to see that the profit threshold is met
         if(Bid - OrderOpenPrice() > Point * FTrailingStop) 
            {
            //Return the value of the potential stoploss
            return(Bid - Point * FTrailingStop);
            }
         }
      }
   //If the Order is a SELL order...
   if(OrderType() == OP_SELL)
      {
      //Check if the user wants to use teh Trailingstop function and did it correctly
      if(Decision && FTrailingStop > 0) 
         {                 
         //Check to see that the profit threshold is met
         if((OrderOpenPrice() - Ask) > (Point * FTrailingStop)) 
            {
            //Return the value of the potential stoploss
            return(Ask + Point * FTrailingStop);
            }
         }
      }     
   //If the trade is not the right order type, give a stoploss of 0   
   if(OrderType() != OP_BUY || OrderType() != OP_SELL) return(0);
   }

void RunAnalysis()
   {
   Comment("");
   
   ArrayResize(MasterArray, 0);
   
   //Generate the raw data
   AnalyzeChart();

   //Rank the correlation
   ArraySort(MasterArray, WHOLE_ARRAY, 0, MODE_DESCEND);

   string TopRanking = "Number of Rows: "+ArrayRange(MasterArray, 0)+" \n[Correlation][WindowShift]\n";
   
   //Delete all the previously generated candle lines
   for(int OB = ObjectsTotal(); OB >= 0; OB--)
      {
      if(StringFind(ObjectName(OB), WindowExpertName()+" Rank:", 0) != -1)
         {
         ObjectDelete(ObjectName(OB));
         }
      }
   
   ArrayResize(FutureHighArray, PredictionLength);
   ArrayInitialize(FutureHighArray, 0.0);
   ArrayResize(FutureLowArray, PredictionLength);
   ArrayInitialize(FutureLowArray, 9999999999.0);
   
   //Paint the top 10 results on the chart
   for(int i = 0; i < TopResults; i++)
      {
      
      if(MinCorr <= MasterArray[i][0])
         {
         for(int i1 = 1; i1 <= PredictionLength; i1++)
            {
            //Project the future bar movements onto the chart current close to future high / low
            double ChangeHigh = iHigh(NULL, 0, MasterArray[i][1] - i1) - iClose(NULL, 0, MasterArray[i][1]);
            double ChangeLow = iLow(NULL, 0, MasterArray[i][1] - i1) - iClose(NULL, 0, MasterArray[i][1]);
            double FutureHigh = iClose(NULL, 0, 1) + ChangeHigh;
            double FutureLow = iClose(NULL, 0, 1) + ChangeLow;
         
            FutureHighArray[i1 - 1] = MathMax(FutureHighArray[i1 - 1], FutureHigh);
            FutureLowArray[i1 - 1] = MathMin(FutureLowArray[i1 - 1], FutureLow);
            
            }
         }
      TopRanking = StringConcatenate(TopRanking, "[", MasterArray[i][0], "]", "[", TimeToStr(iTime(NULL, 0, MasterArray[i][1]), TIME_DATE|TIME_SECONDS), "]\n");
         
      }
      
   //Draw the lines if they are greater than 0
   for(i = 0; i < PredictionLength; i++)
      {
      if(FutureHighArray[i] > 0.0 && FutureLowArray[i] < 9999999999.0)
         {
         datetime FutureTime = iTime(NULL, 0, 1) + (Period() * 60 * (i + 1));
         
         string PredictionName = WindowExpertName()+" Rank:"+(i+1)+" Bar: "+i1;
         ObjectDelete(PredictionName);
         ObjectCreate(PredictionName, OBJ_TREND, 0, FutureTime, FutureHighArray[i], FutureTime, FutureLowArray[i]);
         ObjectSet(PredictionName, OBJPROP_COLOR, Aqua);
         ObjectSet(PredictionName, OBJPROP_BACK, True);
         ObjectSet(PredictionName, OBJPROP_STYLE, STYLE_SOLID);
         ObjectSet(PredictionName, OBJPROP_RAY, False);
         }
         else
         {
         //Print("No Candles "+FutureHighArray[i]+" | "+FutureLowArray[i]);
         }
      }   
   //Comment(TopRanking);
   }
   
void AnalyzeChart()
   {
   //Scan the current window and add to the master array
   double BasisArray[];
   double MatchArray[];
   ArrayResize(BasisArray, WindowLength);
   ArrayInitialize(BasisArray, 0.0);
   
   ArrayResize(MatchArray, WindowLength);
   ArrayInitialize(MatchArray, 0.0);
   
   //Start with a completely shifted window and compare the correlation.   
   
   int BarsRemaining = MathMin(SearchLength + (WindowLength * 2) + PredictionLength, Bars - MAPeriod);
   
   for(int i = Current; i < WindowLength + Current; i++)
      {
      BasisArray[i] = iMA(NULL, 0, MAPeriod, 0, MODE_SMA, MAPrice, i);
      }
   for(i = WindowLength; i < BarsRemaining; i++)
      {
      //Populate the match array
      for(int i1 = i; i1 < i + WindowLength; i1++)
         {
         MatchArray[i1 - i] = iMA(NULL, 0, MAPeriod, 0, MODE_SMA, MAPrice, i1);
         }
      
      //Calculate correlation
      double Correlation = CalcCorrelation(BasisArray, MatchArray, i);
      
      ArrayResize(MasterArray, ArrayRange(MasterArray, 0) + 1);
      MasterArray[ArrayRange(MasterArray, 0) - 1][0] = Correlation;
      MasterArray[ArrayRange(MasterArray, 0) - 1][1] = i;
      
      //Draw a rectangle Showing the region
      double PriceHigh = iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, WindowLength, i));
      double PriceLow = iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, WindowLength, i));
      datetime TimeStart = iTime(NULL, 0, i);
      datetime TimeStop = iTime(NULL, 0, i + WindowLength);
      
      
      
      }
      
   }

double CalcCorrelation(double& Array1[], double& Array2[], int ShiftUsed)
   {
   if(ArrayRange(Array1, 0) == ArrayRange(Array2, 0))
      {
      double Sum1;
      double Sum2;
      double Sum1sq;
      double Sum2sq;
      double Sum12;
      
      for(int i = 0; i < ArrayRange(Array1, 0); i++)
         {
         Sum1 += Array1[i];
         Sum2 += Array2[i];
         Sum1sq += MathPow(Array1[i], 2);
         Sum2sq += MathPow(Array2[i], 2);
         Sum12 += (Array1[i] * Array2[i]);
         }
      
      //Print("Sum1: ", Sum1, " Sum2: ", Sum2);
      double Numerator = ((ArrayRange(Array1, 0) * Sum12) - (Sum1 * Sum2));
      double Denominator = MathSqrt(((ArrayRange(Array1, 0) * Sum1sq) - MathPow(Sum1, 2)) * ((ArrayRange(Array1, 0) * Sum2sq) - MathPow(Sum2, 2)));
      
      if(Denominator == 0)
         {
         Print("Error: Denominator 0 Shift: ", ShiftUsed, " Sum1: ", Sum1, " Sum2: ", Sum2, " Sum1sq: ", Sum1sq, " Sum2sq: ", Sum2sq, " Sum12: ", Sum12, " Array1Size: ", ArrayRange(Array1, 0), " Array2Size: ", ArrayRange(Array2, 0));
         return(-2);
         }
         else
         {
         return(Numerator / Denominator);
         }
      }
      else
      {
      return(-2);
      }
   }
   