private void Analyze_D1(DateTime dt)
        {
            try
            {
                CTimeFrameInfo ntfi = m_dictTFArray.GetNewTimeFrameInfo(m_isin, EnmTF.D1, dt);


                CTimeFrameArray M_low_TF_Array = m_dictTFArray[EnmTF.H1.ToString()][dt];


                for (int i = 1; i < M_low_TF_Array.ListTimeFrameInfo.Count; i++) //each TF array
                {
                    ntfi.OpenedPos = M_low_TF_Array.ListTimeFrameInfo[i].OpenedPos;

                    CTimeFrameInfo currtfi = M_low_TF_Array.ListTimeFrameInfo[i];
                    ntfi.numOfDeals += currtfi.numOfDeals;
                    ntfi.Volume     += currtfi.Volume;

                    if (ntfi.HighPrice < currtfi.HighPrice)
                    {
                        ntfi.HighPrice = currtfi.HighPrice;
                    }
                    if (ntfi.LowPrice > currtfi.LowPrice)
                    {
                        ntfi.LowPrice = currtfi.LowPrice;
                    }
                }
            }
            catch (Exception e)
            {
                string st = e.Message;
            }
        }
 //Function to trap error.
 //Must be disabled in production system
 private void TrapError(CTimeFrameArray arr)
 {
     for (int i = 0; i < arr.ListTimeFrameInfo.Count - 1; i++)
     {
         if (arr.ListTimeFrameInfo[i].Dt > arr.ListTimeFrameInfo[i + 1].Dt)
         {
             //         InsertToValidPlace(arr.ListTimeFrameInfo[i+1],i+1, arr);
             Error("TrapError TimeFrameArray.");
             return;
         }
     }
 }
        private void InsertToValidPlace(CTimeFrameInfo tfi, int ind, CTimeFrameArray arr)
        {
            int i = 0;

            for (i = 0; i < arr.ListTimeFrameInfo.Count - 1; i++)
            {
                if (arr.ListTimeFrameInfo[i].Dt > tfi.Dt)
                {
                    break;
                }
            }

            arr.ListTimeFrameInfo.Insert(i, tfi);
            arr.ListTimeFrameInfo.RemoveAt(ind);
        }
        private void AnalyzeDiskTFDays()
        {
            try
            {
                if (!m_dictTFArray.ContainsKey(EnmTF.H1.ToString()))
                {
                    return;
                }



                foreach (KeyValuePair <DateTime, CTimeFrameArray> kvp in m_dictTFArray[EnmTF.H1.ToString()]) //all dates of low TF array
                {
                    try
                    {
                        CTimeFrameArray M_low_TF_Array = kvp.Value;

                        DateTime dt = CUtilTime.NormalizeDay(M_low_TF_Array.ListTimeFrameInfo[0].Dt.Date);
                        if (m_dictTFArray.IsContainTimeFrameInfo(EnmTF.D1, dt))
                        {
                            continue;
                        }


                        CTimeFrameInfo ntfi = m_dictTFArray.GetNewTimeFrameInfo(m_isin, EnmTF.D1, dt);

                        Analyze_D1(dt);
                    }
                    catch (Exception e)
                    {
                    }
                }
            }
            catch (Exception e)
            {
                Error("AnalyzeDiskTFDays", e);
            }
        }
        private void AnalyzeDiskTF(ChangedTF changedTF, List <int> lstScale, EnmTF TF_high, EnmTF TF_low)
        {
            try
            {
                foreach (KeyValuePair <DateTime, CTimeFrameArray> kvp in m_dictTFArray[TF_low.ToString()]) //all dates of low TF array
                {
                    if (CUtilTime.OlderThanTwoWorkDays(kvp.Key))
                    {
                        continue;
                    }

                    CTimeFrameArray M_low_TF_Array = kvp.Value;
                    for (int i = 1; i < M_low_TF_Array.ListTimeFrameInfo.Count; i++) //each TF array
                    {
                        DateTime dtPrev = M_low_TF_Array.ListTimeFrameInfo[i - 1].Dt;
                        DateTime dtCurr = M_low_TF_Array.ListTimeFrameInfo[i].Dt;



                        DateTime dtFrom = new DateTime(0);
                        DateTime dtTo   = new DateTime(0);

                        if (changedTF(dtPrev, dtCurr, lstScale, ref dtFrom, ref dtTo))
                        {
                            //TO DO chek prev day etc

                            if (m_dictTFArray.IsContainTimeFrameInfo(TF_high, dtFrom))
                            {
                                continue; //if  already exists in high TF array than nothing to do and  continue
                            }
                            //exp logics
                            CTimeFrameInfo ntfi = null;
                            if (m_dictTFArray.GetLatestTFI(TF_high.ToString()) != null)
                            {
                                DateTime dtLast = (m_dictTFArray.GetLatestTFI(TF_high.ToString())).Dt;
                                if (dtLast > dtFrom)
                                {
                                    ntfi = m_dictTFArray.GetNewTFIPuttingtoWritePlace(m_isin, TF_high, dtFrom);
                                    //Error("Inserting data. TO DO check and remove");
                                }
                                else
                                {
                                    ntfi = m_dictTFArray.GetNewTimeFrameInfo(m_isin, TF_high, dtFrom);
                                }
                            }
                            else
                            {
                                ntfi = m_dictTFArray.GetNewTimeFrameInfo(m_isin, TF_high, dtFrom);
                            }



                            int j = i - 1;

                            //note: the last pos
                            ntfi.OpenedPos  = M_low_TF_Array.ListTimeFrameInfo[j].OpenedPos;
                            ntfi.ClosePrice = M_low_TF_Array.ListTimeFrameInfo[j].ClosePrice;


                            while (j >= 0 && M_low_TF_Array.ListTimeFrameInfo[j].Dt >= dtFrom)
                            {
                                CTimeFrameInfo currtfi = M_low_TF_Array.ListTimeFrameInfo[j];
                                ntfi.numOfDeals += currtfi.numOfDeals;
                                ntfi.Volume     += currtfi.Volume;

                                if (ntfi.HighPrice < currtfi.HighPrice)
                                {
                                    ntfi.HighPrice = currtfi.HighPrice;
                                }
                                if (ntfi.LowPrice > currtfi.LowPrice)
                                {
                                    ntfi.LowPrice = currtfi.LowPrice;
                                }

                                ntfi.OpenPrice = M_low_TF_Array.ListTimeFrameInfo[j].OpenPrice;

                                j--; //backward
                            }

                            DateTime dtcnd = CUtilTime.NormalizeDay(dtFrom);

                            //no need ? check !
                            CheckTFIConsistent(ntfi);
                        }
                    }
                    //TrapError(kvp.Value);
                }
            }
            catch (Exception e)
            {
                Error("AnalyzeDiskTF", e);
            }
        }
        private void AnalizeDiskData()
        {
            try
            {       //Load data from disk first
                if (Directory.Exists(IsinDir))
                {
                    string[] Files = Directory.GetFiles(IsinDir);
                    foreach (string fn in Files)
                    {
                        FileInfo f = new FileInfo(fn);
                        if (f.Length == 0)
                        {
                            File.Delete(fn);
                            Error("Remove empty file " + fn);
                            continue;
                        }

                        Regex newReg = new Regex(@"[\w\W]*([0-9]{4}_[0-9]{2}_[0-9]{2})_(M1|M5|M15|M30|H1|D1).xml");
                        Match m      = newReg.Match(fn);

                        if (m.Groups.Count > 1)
                        {
                            string          stTF   = m.Groups[2].ToString();
                            string          stDate = m.Groups[1].ToString();
                            DateTime        dt     = CUtilTime.GetDateFromString(stDate);
                            CTimeFrameArray tfa    = new CTimeFrameArray();
                            tfa.FileName = fn;
                            //CSerializator.Read<CTimeFrameArray>(ref tfa);
                            CSerializator.ReadSafe <CTimeFrameArray>(ref tfa);

                            m_dictTFArray.AddTFArray(stTF, dt, tfa);


                            string st = "";
                            if (tfa.ListTimeFrameInfo.Count == 0)
                            {
                                st = "";
                            }


                            //  Plaza2Connector.GUIBox.ExecuteCandlesUpdate
                            //    (new Action(() => Plaza2Connector.GUIBox.GUICandleBox.AddTFArray(tfa.ListTimeFrameInfo[0].Isin, stTF, dt.ToString(), tfa)));
                        }
                    }

                    AnalyzeDiskTF(CUtilTF.IsClosed_M5_M15_M30_TF, m_M5_scale, EnmTF.M5, EnmTF.M1);
                    AnalyzeDiskTF(CUtilTF.IsClosed_M5_M15_M30_TF, m_M15_scale, EnmTF.M15, EnmTF.M5);
                    AnalyzeDiskTF(CUtilTF.IsClosed_M5_M15_M30_TF, m_M30_scale, EnmTF.M30, EnmTF.M15);
                    AnalyzeDiskTF(CUtilTF.IsClosed_H1, m_M30_scale, EnmTF.H1, EnmTF.M30);

                    AnalyzeDiskTFDays();


                    //CreateCandlesArrays();
                }
            }

            catch (Exception e)
            {
                Error("AnalizeDiskData", e);
            }
        }