public void GenAggrPrice(List <CDealClass> sourceLst, List <CDealClass> destLst)
        {
            int i = -1;

            while (i < sourceLst.Count - 1)
            {
                //on first iteration first element,on next =  last processed
                if (i == -1)
                {
                    dtBegin = sourceLst[0].DtTm;
                }
                else
                {
                    dtBegin = dtEnd;
                }

                dtEnd = dtBegin.AddMilliseconds(_parPeriod);

                CDealClass      CDCCurr     = null;
                CAggrDealStruct AggrStrBuy  = null;
                CAggrDealStruct AggrStrSell = null;


                //internal loop
                //list finshed            //   less than interval end
                while (i < sourceLst.Count - 1 && sourceLst[i + 1].DtTm <= dtEnd)
                {
                    i++;
                    CDCCurr = sourceLst[i];
                    if (EnmDealDir.Buy == CDCCurr.DirDeal)
                    {
                        OnIterUpdate(CDCCurr, ref AggrStrBuy);
                    }
                    else
                    if (EnmDealDir.Sell == CDCCurr.DirDeal)
                    {
                        OnIterUpdate(CDCCurr, ref AggrStrSell);
                    }
                }

                //after inrernal loop finished calculate aggr data
                if (AggrStrBuy != null)
                {
                    OnCloseUpdate(AggrStrBuy);
                    destLst.Add(AggrStrBuy.DealClass);
                }
                if (AggrStrSell != null)
                {
                    OnCloseUpdate(AggrStrSell);
                    destLst.Add(AggrStrSell.DealClass);
                }
            }
        }
        private void OnIterUpdate(CDealClass curr, ref CAggrDealStruct upd)
        {
            if (upd == null)
            {
                upd = new CAggrDealStruct();
            }

            upd.DealClass.DirDeal = curr.DirDeal;
            upd.DealClass.Amount += curr.Amount;
            upd.NumDeals++;
            upd.Sum += curr.Amount * curr.Price;

            UpdateMinMax(curr, ref upd);
            //upd.Price += curr.Price; //for averaging on close
        }
        private void OnCloseUpdate(CAggrDealStruct ads)
        {
            //   if (ads.DealClass.Amount == 0)
            //    System.Threading.Thread.Sleep(0);


            //previously used algo
            //ads.DealClass.Price = (ads.MaxPrice + ads.MinPrice)/2;
            decimal price = ads.Sum / ads.DealClass.Amount;


            ads.DealClass.Price = GetNormalizedPrice(price);

            ads.DealClass.DtTm = dtBegin.AddMilliseconds(_parPeriod * 0.5);

            ads.DtEnd = dtBegin;
            ads.DtEnd = dtEnd;
        }
        private void UpdateMinMax(CDealClass curr, ref CAggrDealStruct upd)
        {
            if (upd.MinPrice == 0)
            {
                upd.MinPrice = curr.Price;
            }
            if (upd.MaxPrice == 0)
            {
                upd.MaxPrice = curr.Price;
            }

            if (upd.MinPrice < curr.Price)
            {
                upd.MinPrice = curr.Price;
            }
            if (upd.MaxPrice > curr.Price)
            {
                upd.MaxPrice = curr.Price;
            }
        }