public bool IsNewData(CRawDeal rd)
        {
            DateTime rd_date = CUtilTime.NormalizeDay(rd.Moment.Date);

            if (!this.ContainsKey("M1"))
            {
                return(true);
            }

            if (!this["M1"].ContainsKey(rd_date))
            {
                return(true);
            }

            if (this["M1"][rd_date].ListTimeFrameInfo.Count == 0)
            {
                return(true);
            }


            DateTime dt = this["M1"][rd_date].ListTimeFrameInfo.Last().Dt;//.AddMinutes(1);

            if (rd.Moment > dt)
            {
                return(true);
            }


            return(false);
        }
        /*
         * public void WriteTFToFile(CTimeFrameArray tfa, EnmTF TF, DateTime dt)
         * {
         *
         *  CreateDirectoriesIfNeed();
         *  ArrM1.FileName = GetFileName(EnmTF.M1, dt); //TO DO split
         *  CSerializator.Write<CTimeFrameArray>(ref ArrM1);
         *
         * }
         */

        public void CheckTFAnalyzerOnline(CRawDeal rd)
        {
            if (!Plaza2Connector.IsAnalyzerTFOnline)
            {
                if ((Plaza2Connector.ServerTime - rd.Moment).TotalSeconds < 1)
                {
                    Plaza2Connector.IsAnalyzerTFOnline = true;
                }
            }

            //first - the moment when we build candle array
            //and set IsTimeToInitCandle
            if (!Plaza2Connector.IsTimeToInitCandles)
            {
                if ((Plaza2Connector.ServerTime - rd.Moment).TotalHours < 1)
                {
                    Plaza2Connector.IsTimeToInitCandles = true;
                }
            }

            //now we trigger once load tf array
            if (Plaza2Connector.IsTimeToInitCandles && !this._wasStartedCreateTimeArray)
            {
                _wasStartedCreateTimeArray = true;
                CreateCandlesArrays();
            }
        }
        private void ProcessTradeExecute(string inpInstrWithPrefix, JArray jarrTe)
        {
            string instrument = CBfxUtils.RemoveFirstT(inpInstrWithPrefix);


            int decimalVolume = GetDecimalVolume(instrument);

            long    lngMilis      = (long)jarrTe[1];
            decimal dcmlAmountRaw = (decimal)jarrTe[2];
            decimal dcmlAmount    = Math.Abs(dcmlAmountRaw);
            decimal dcmlPrice     = (decimal)jarrTe[3];



            long amount = CUtilConv.GetIntVolume(dcmlAmount, decimalVolume);

            //2018-02-22
            //Amount is too small. Not possible to understand how it
            //could be. Was seen on LTC. For now just ignore these trades
            if (amount == 0)
            {
                return;
            }



            DateTime dt = CUtilTime.DateTimeFromUnixTimestampMillis(lngMilis);


            CRawDeal rd = new CRawDeal {
                Amount = amount,
                Price  = dcmlPrice,
                Moment = dt
            };



            if (dcmlAmountRaw > 0)
            {
                rd.Id_ord_buy = 1;
            }
            else
            {
                rd.Id_ord_sell = 1;
            }

            _client.UpdateDeal(instrument, rd);

            /*  DateTime dtCurr = DateTime.Now;
             * double ddt = (dtCurr - dt).TotalSeconds;
             *
             * if (ddt != 00)
             *    Thread.Sleep(0);
             *
             */
        }
Esempio n. 4
0
        protected override void ProcessRecord()
        {
            int hr, mn, s;

            try
            {
                string instrument = (string)_currentRecord.values["SECCODE"];

                if (_instruments.IsContainsInstrument(instrument))
                {
                    decimal price   = Convert.ToDecimal(_currentRecord.values["PRICE"]);
                    string  buySell = _currentRecord.values["BUYSELL"].ToString();
                    long    amount  = Convert.ToInt32(_currentRecord.values["QUANTITY"].ToString());



                    string tradeTime = (_currentRecord.values["TRADETIME"]).ToString();

                    CASTSConv.ASTSTimeToHrMinSec(tradeTime, out hr, out mn, out s);
                    double dmilisec = 0;


                    if (_currentRecord.values["MICROSECONDS"] != null)
                    {
                        dmilisec = Math.Ceiling(Convert.ToDouble(_currentRecord.values["MICROSECONDS"].ToString()) / 1000);
                        dmilisec = Math.Min(999, dmilisec);
                    }


                    int milisec = Convert.ToInt16(dmilisec);
                    //TODO use server time !
                    DateTime dt     = DateTime.Now;
                    DateTime moment = new DateTime(dt.Year, dt.Month, dt.Day, hr, mn, s, milisec);
                    //  moment = moment.AddHours(-2);

                    CRawDeal rd = new CRawDeal(amount, price, buySell, moment);

                    _dealBox.Update(instrument, rd);

                    if (!_client.IsDealsOnline)
                    {
                        _client.IsDealsOnline = true;
                        _client.EvDealsOnline.Set();
                    }
                }
            }
            catch (Exception e)
            {
                Error("CTableAll_Trades.ProcessRecord", e);
            }

            finally
            {
                base.ProcessRecord();
            }
        }
 public void CheckDataOnline(CRawDeal currRD)
 {
     if (!this.IsOnlineData && Plaza2Connector.IsDealsOnline)
     {
         if (currRD.ReplID == Plaza2Connector.DealBox.DealsStruct[m_isin].LastRcvRD.ReplID)
         {
             this.IsOnlineData = true;
         }
     }
 }
        public void AnalyzeOnlineData(CRawDeal rd)
        {
            try
            {
                if (m_timerWriteFiles == null)
                {
                    m_timerWriteFiles = new CTimer(5000, new Action(m_dictTFArray.WriteAllDataToDisk), true);
                }

                CheckTFAnalyzerOnline(rd);

                if (m_prevDeal == null && m_currDeal == null)
                {
                    m_currDeal = rd;

                    CTimeFrameInfo tfi = m_dictTFArray.GetLatestTFI();
                    if (tfi != null)
                    {
                        m_prevDeal = new CRawDeal(tfi);
                    }
                    else
                    {
                        m_prevDeal = rd;
                        return;
                    }
                }

                m_prevDeal = m_currDeal;
                m_currDeal = rd;

                //if data old do not analyze it
                if (!m_dictTFArray.IsNewData(rd))
                {
                    return;
                }


                AnalyzeOnlineM1((CRawDeal)rd.Copy(), m_prevDeal);


                /*
                 * if (m_dictTFArray.IsTimeToWrite(EnmTF.M1))
                 * {
                 *
                 *  (new System.Threading.Tasks.Task(() => m_dictTFArray.WriteAllDataToDisk())).Start();
                 * }
                 *
                 */
            }

            catch (Exception e)
            {
                Error("Error AnalyzeData", e);
            }
        }
Esempio n. 7
0
        public void SimUpdateDeals(CRawDeal dealInp)
        {
            try
            {
                int    isin_id = (int)dealInp.Isin_id;
                string isin    = _client.Instruments.GetInstrumentByIsinId(isin_id);

                //TODO normal
                //	if (m_dealsStructList.ContainsKey(isin))
                m_dealsStructList[isin].Update(dealInp);
            }
            catch (Exception e)
            {
                Error("CDealsBox.SimUpdateDeals", e);
            }
        }
        private void ThreadAnalyzeOnline()
        {
            const int MAX_COUNT = 10000;

            // foreach (CRawDeal rd in m_blkColRowDeals.GetConsumingEnumerable())
            while (true)
            {
                CRawDeal rd = m_bqRawDeals.GetElementBlocking();

                /*  if (m_blkColRowDeals.Count > MAX_COUNT)
                 *    Plaza2Connector.Error("ThreadAnalyzeOnline > MAX_COUNT. "+m_isin+
                 *        " count="+m_blkColRowDeals.Count);
                 */

                AnalyzeOnlineData(rd);
            }
        }
Esempio n. 9
0
        private void ParseDealsLine(string st)
        {
            if (!st.Contains("replID"))
            {
                return;
            }



            DateTime dt      = GetDateTimeDoub(st);
            CRawDeal rawDeal = GetRawDeal(st);

            _listData.Add(new CDataElement()
            {
                Dt      = dt,
                RawDeal = rawDeal
            }
                          );
        }
 public void UpdateDeal(string instrument, CRawDeal rd)
 {
     _dealBoxCrypto.Update(instrument, rd);
 }
Esempio n. 11
0
        private CRawDeal GetRawDeal(string st)
        {
            string [] res = st.Split(' ');



            CRawDeal rd = new CRawDeal();

            rd.Moment = GetDateTimeP2(st);


            foreach (var s in res.ToList())
            {
                string [] r = s.Split('=');

                if (r.Length <= 1)
                {
                    continue;
                }

                if (r[0].Contains("replID"))
                {
                    rd.ReplID = Convert.ToInt64(r[1]);
                }
                else if (r[0].Contains("replRev"))
                {
                    rd.ReplRev = Convert.ToInt64(r[1]);
                }
                else if (r[0].Contains("id_deal"))
                {
                    rd.Id_deal = Convert.ToInt64(r[1]);
                }
                else if (r[0].Contains("isin_id"))
                {
                    rd.Isin_id = Convert.ToInt64(r[1]);
                }
                else if (r[0].Contains("amount"))
                {
                    rd.Amount = Convert.ToInt64(r[1]);
                }
                else if (r[0].Contains("id_ord_buy"))
                {
                    rd.Id_ord_buy = Convert.ToInt64(r[1]);
                }
                else if (r[0].Contains("ord_sell"))                 //note: incorrect in log
                {
                    rd.Id_ord_sell = Convert.ToInt64(r[1]);
                }
                else if (r[0].Contains("pos"))                 //note: incorrect in log
                {
                    rd.Pos = Convert.ToInt32(r[1]);
                }
                else if (r[0].Contains("price"))                 //note: incorrect in log
                {
                    rd.Price = Convert.ToDecimal(r[1]);
                }
            }



            /*	Regex newReg = new Regex(@"replID=([0-9]{11})\sid_deal=([0-9])");
             *      Match m = newReg.Match(st);
             *
             *      if (m.Groups.Count <= 1)
             *              throw new ApplicationException("GetRawDeal");
             *
             */
            return(rd);
        }
Esempio n. 12
0
 public void UpdateDeal(string instrument, CRawDeal rd)
 {
 }
Esempio n. 13
0
 public void Update(string instrument, CRawDeal rd)
 {
     m_dealsStructList[instrument].Update(rd);
 }
        private void AnalyzeOnlineM1(CRawDeal rd, CRawDeal prevDeal)
        {
            DateTime dtTill = rd.Moment.AddSeconds(-rd.Moment.Second);

            dtTill = dtTill.AddMilliseconds(-dtTill.Millisecond);


            if (!m_dictTFArray.IsContainTimeFrameInfo(EnmTF.M1, dtTill))
            {
                m_dictTFArray.AddNewTimeFrameInfo(m_isin, EnmTF.M1, dtTill, this);
            }



            CTimeFrameInfo tfi = m_dictTFArray.GetLastTimeFrameInfo(EnmTF.M1);



            DateTime currMoment = rd.Moment;
            CRawDeal currRd     = rd;

            //changed 2016/08/01
            //only if changed extremum do recalc
            bool bChangedExtr = false;

            if (rd.ReplID > tfi.LastReplId)
            {
                tfi.LastReplId = rd.ReplID;
                tfi.OpenedPos  = rd.Pos;
                tfi.Volume    += rd.Amount;
                tfi.numOfDeals++;
                if (currRd.Price > tfi.HighPrice)
                {
                    bChangedExtr  = true;;
                    tfi.HighPrice = currRd.Price;
                }
                if (currRd.Price < tfi.LowPrice)
                {
                    bChangedExtr = true;
                    tfi.LowPrice = currRd.Price;
                }

                tfi.LastUpdate = rd.Moment;

                if (!tfi.bProcessedData)
                {
                    tfi.OpenPrice      = rd.Price;
                    tfi.bProcessedData = true;
                }
                tfi.ClosePrice = rd.Price;

                DateTime dtcnd = CUtilTime.NormalizeDay(dtTill);
                CheckTFIConsistent(tfi);

                //    sw5.Stop();

                //Plaza2Connector.GUIBox.GUICandleBox.UpdateCandle(this.m_isin, EnmTF.M1.ToString(), dtcnd.ToString(), tfi);
                Plaza2Connector.GUIBox.GUICandleBox.QueueTFinfo(this.m_isin, EnmTF.M1.ToString(), dtcnd.ToString(), tfi);
            }

            // TrapError(m_dictTFArray["M1"][CUtil.NormalizeDay(tfi.Dt)]);



            CheckDataOnline(rd);



            if (Plaza2Connector.IsDealsOnline)
            {
                //changed 2016/08/01
                //only if changed extremum do recalc
                if (bChangedExtr)
                {
                    TriggerRecalcAllBotsUpdateTF(EnmTF.M1, tfi);
                }

                DateTime dtTmp  = Plaza2Connector.ServerTime;
                DateTime dtFrom = new DateTime(0);
                DateTime dtTo   = new DateTime(0);

                CTimeFrameInfo tfiPrev = m_dictTFArray.GetPrevLastTimeFrameInfo(EnmTF.M1);


                if (CUtilTF.WasClose_M1_TF(prevDeal.Moment, rd.Moment))
                {
                    Log("M1 was closed prevDeal.Moment=" + prevDeal.Moment + " rd.Moment=" + rd.Moment);
                    if (Plaza2Connector.IsAnalyzerTFOnline)
                    {
                        TriggerRecalcAllBotsChangedTF(EnmTF.M1, tfiPrev);
                    }
                }
            }



            AnalyzeOnlineTF(CUtilTF./*(IsClosed_M5_M15_M30_TF*/ WasClosed_M5_M15_M30_TF, m_M5_scale, EnmTF.M5, EnmTF.M1, prevDeal, rd);


            AnalyzeOnlineTF(CUtilTF./*IsClosed_M5_M15_M30_TF*/ WasClosed_M5_M15_M30_TF, m_M15_scale, EnmTF.M15, EnmTF.M5, prevDeal, rd);



            AnalyzeOnlineTF(CUtilTF./*IsClosed_M5_M15_M30_TF*/ WasClosed_M5_M15_M30_TF, m_M30_scale, EnmTF.M30, EnmTF.M15, prevDeal, rd);



            AnalyzeOnlineTF(CUtilTF./*IsClosed_H1*/ WasClosed_H1, m_M30_scale, EnmTF.H1, EnmTF.M30, prevDeal, rd);


            AnalyzeOnlineTF(CUtilTF./*IsClosed_D1*/ WasClosed_D1, m_M30_scale, EnmTF.D1, EnmTF.H1, prevDeal, rd);


            CTimeFrameInfo TFinfo_D1 = m_dictTFArray.GetLastTimeFrameInfo(EnmTF.D1);

            LowDayPrice  = TFinfo_D1.LowPrice;
            HighDayPrice = TFinfo_D1.HighPrice;
        }
        private void AnalyzeOnlineTF(ChangedTF changedTF, List <int> lstScale, EnmTF TF_high, EnmTF TF_low, CRawDeal prevDeal, CRawDeal currDeal)
        {
            DateTime dtPrev = prevDeal.Moment;
            DateTime dtCurr = currDeal.Moment;


            //Get  last high TF
            CTimeFrameInfo TFinfo_low = m_dictTFArray.GetLastTimeFrameInfo(TF_low);

            /*  if (!TFinfo_low.bNotProcessedData)
             *    return;
             */


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



            bool b_wasChangedTF = changedTF(prevDeal.Moment, currDeal.Moment, lstScale, ref dtFrom, ref dtTo);



            if (!m_dictTFArray.IsContainTimeFrameInfo(TF_high, /*dtFrom*/ dtTo))
            {
                m_dictTFArray.AddNewTimeFrameInfo(m_isin, TF_high, /*dtFrom*/ dtTo, this);
            }

            CTimeFrameInfo TFinfo_low_prev  = m_dictTFArray.GetPrevLastTimeFrameInfo(TF_low);
            CTimeFrameInfo TFinfo_high_prev = m_dictTFArray.GetPrevLastTimeFrameInfo(TF_high);


            CTimeFrameInfo TFinfo_high = m_dictTFArray.GetLastTimeFrameInfo(TF_high);


            bool bChangedExtr = false;


            if (currDeal.ReplID > TFinfo_high.LastReplId)
            {
                TFinfo_high.numOfDeals++;
                TFinfo_high.Volume   += currDeal.Amount;
                TFinfo_high.OpenedPos = currDeal.Pos;

                //changed 2016/08/01
                //only if changed extremum do recalc
                if (TFinfo_high.HighPrice < TFinfo_low.HighPrice)
                {
                    TFinfo_high.HighPrice = currDeal.Price;
                    bChangedExtr          = true;
                }
                if (TFinfo_high.LowPrice > TFinfo_low.LowPrice)
                {
                    TFinfo_high.LowPrice = currDeal.Price;
                    bChangedExtr         = true;
                }

                TFinfo_high.LastReplId = currDeal.ReplID;

                TFinfo_high.LastUpdate = DateTime.Now;

                if (!TFinfo_high.bProcessedData)
                {
                    TFinfo_high.OpenPrice      = TFinfo_low.OpenPrice;
                    TFinfo_high.bProcessedData = true;
                }
                //KAA 2015-12-23
                //because low TF was already updated
                // if (!b_wasChangedTF)
                TFinfo_high.ClosePrice = TFinfo_low.ClosePrice;
                // else
                // TFinfo_high.ClosePrice = TFinfo_low_prev.ClosePrice; //TFinfo_low.ClosePrice;


                DateTime dtcnd = CUtilTime.NormalizeDay(dtFrom);
                //  Plaza2Connector.GUIBox.GUICandleBox.UpdateCandle(this.m_isin, TF_high.ToString(), dtcnd.ToString(), TFinfo_high);

                Plaza2Connector.GUIBox.GUICandleBox.QueueTFinfo(this.m_isin, TF_high.ToString(), dtcnd.ToString(), TFinfo_high);


                //changed 2016/08/01
                //only if changed extremum do recalc
                if (bChangedExtr)
                {
                    TriggerRecalcAllBotsUpdateTF(TF_high, TFinfo_high);
                }

                if (b_wasChangedTF)
                {
                    Log(TF_high.ToString() + " TFinfo_high.Dt=" + TFinfo_high.Dt.ToString() + " was closed prevDeal.Moment=" + prevDeal.Moment + " currDeal.Moment=" + currDeal.Moment + " TFinfo_high.ClosePrice=" + TFinfo_high.ClosePrice +
                        " TFinfo_low_prev.ClosePrice=" + TFinfo_low_prev.ClosePrice + " TFinfo_low_prev.Dt =" + TFinfo_low_prev.Dt);

                    if (Plaza2Connector.IsDealsOnline && Plaza2Connector.IsAnalyzerTFOnline)
                    {
                        TriggerRecalcAllBotsChangedTF(TF_high, TFinfo_high_prev);
                    }
                }
            }
            else
            {
            }


            //  TrapError(m_dictTFArray[TF_high.ToString()][CUtil.NormalizeDay(dtFrom)]);


            int tmp = 1;
        }
 public void AddNewDeal(CRawDeal rd)
 {
     DelayIfLargMemUsage();
     //m_blkColRowDeals.Add(rd);
     m_bqRawDeals.Add(rd);
 }