public CTimeFrameInfo GetNewTFIPuttingtoWritePlace(string isin, EnmTF TF, DateTime dt)
        {
            CListTimeFrameInfo arr = null;
            int i = 0;

            try
            {
                arr = this[TF.ToString()][dt.Date].ListTimeFrameInfo;

                for (i = 0; i < arr.Count; i++)
                {
                    if (arr[i].Dt > dt)
                    {
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Error("GetNewTFIPuttingtoWritePlace", e);
            }


            mx.WaitOne();
            if (arr != null)
            {
                arr.Insert(i, new CTimeFrameInfo(isin, dt /*, m_timeFrameAnalyzer*/));
            }
            mx.ReleaseMutex();
            return(arr[i]);

            //return null;
        }
        public void SetTF(EnmTF tf)
        {
            TF = tf;
            if (TF == EnmTF.M1)
            {
                DateTF = CUtilTime.NormalizeSeconds(Date);
            }
            else if (TF == EnmTF.M5)
            {
                DateTF = CUtilTime.NormalizeToM5(Date);
            }
            else if (TF == EnmTF.M15)
            {
                DateTF = CUtilTime.NormalizeToM15(Date);
            }
            else if (TF == EnmTF.M30)
            {
                DateTF = CUtilTime.NormalizeToM30(Date);
            }
            else if (TF == EnmTF.H1)
            {
                DateTF = CUtilTime.NormalizeHour(Date);
            }
            else if (TF == EnmTF.D1)
            {
                DateTF = CUtilTime.NormalizeDay(Date);
            }

            else
            {
                DateTF = Date;
            }
        }
        //TO DO check. dangerous
        public CTimeFrameInfo GetPrevLastTimeFrameInfo(EnmTF TF)
        {
            CTimeFrameInfo tfi = null;

            try
            {
                DateTime dtMax = new DateTime(0);
                foreach (KeyValuePair <DateTime, CTimeFrameArray> kvp in this[TF.ToString()])
                {
                    if (kvp.Key > dtMax && kvp.Value.ListTimeFrameInfo.Count > 0)
                    {
                        dtMax = kvp.Key;
                    }
                }

                int cnt = this[TF.ToString()][dtMax].ListTimeFrameInfo.Count;
                if (cnt >= 2)
                {
                    tfi = this[TF.ToString()][dtMax].ListTimeFrameInfo[cnt - 2];
                }
                else
                {
                    tfi = this[TF.ToString()][dtMax].ListTimeFrameInfo.Last();
                }
                return(tfi);
            }
            catch (Exception e)
            {
                Error("GetLastTimeFrameInfo", e);
                return(tfi);
            }
        }
Example #4
0
        public void Bind(CPlaza2Connector plaza2Connector, string inpIsin, string inpTf, string inpDt, Button but, Dispatcher disp)
        {
            _dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;



            _dispatcherParentWindow = disp;
            m_buttonTF = but;

            m_plaza2Connector = plaza2Connector;
            m_isin            = inpIsin;
            tf = inpTf;
            dt = inpDt;
            TF = dictIntervalsTF[tf];
            m_plaza2Connector.GUIBox.GUICandleBox[m_isin][tf][dt].SetSubsribedDispatcher(_dispatcher);

            this.Chart.ItemsSource     = m_plaza2Connector.GUIBox.GUICandleBox[m_isin][tf][dt];
            this.ChartBody.ItemsSource = m_plaza2Connector.GUIBox.GUICandleBox[m_isin][tf][dt];
            m_plaza2Connector.GUIBox.GUICandleBox[m_isin][tf][dt].CandleCollectionUpdate += OnUpdateCandleCollection;

            _dispatcher.UnhandledException += new DispatcherUnhandledExceptionEventHandler(m_plaza2Connector.Alarmer.GUIdispatcher_UnhandledException);


            SubscribeGUIUserDealsOnCreation();
            UpdateAxis();
        }
 public bool IsTimeToWrite(EnmTF TF)
 {
     //TO DO from config
     if (CopyDict.ContainsKey(TF.ToString()) && !bCopyIsBusy)
     {
         DateTime dt = /*this*/ CopyDict[TF.ToString()].Last().Value.DtLastWrite;
         if ((DateTime.Now - dt).TotalSeconds > 20)
         {
             return(true);
         }
     }
     return(false);
 }
        private void TriggerRecalcAllBotsChangedTF(EnmTF TF, CTimeFrameInfo tfi)
        {
            if (!Plaza2Connector.IsDealsOnline || !IsOnlineData)
            {
                return;
            }

            BotEventTF mess = new BotEventTF()
            {
                TFUpdate = TF,
                TFI      = tfi
            };

            Plaza2Connector.TriggerRecalcAllBotsWithInstrument(m_isin, EnmBotEventCode.OnTFChanged, mess);
        }
        public void AddNewTimeFrameInfo(string isin, EnmTF TF, DateTime date, CTimeFrameAnalyzer tfa)
        {
            mx.WaitOne();
            if (!this.ContainsKey(TF.ToString()))
            {
                this[TF.ToString()] = new SortedDictionary <DateTime, CTimeFrameArray>();
            }

            if (!this[TF.ToString()].ContainsKey(date.Date))
            {
                this[TF.ToString()][date.Date] = new CTimeFrameArray();
            }

            this[TF.ToString()][date.Date].ListTimeFrameInfo.Add(new CTimeFrameInfo(isin, date /*, tfa*/));
            mx.ReleaseMutex();
        }
        public bool IsContainTimeFrameInfo(EnmTF TF, DateTime dtFind)
        {
            if (!this.ContainsKey(TF.ToString()))
            {
                return(false);
            }

            if (!this[TF.ToString()].ContainsKey(dtFind.Date))
            {
                return(false);
            }
            foreach (CTimeFrameInfo ctfi in this[TF.ToString()][dtFind.Date].ListTimeFrameInfo)
            {
                if (ctfi.Dt == dtFind)
                {
                    return(true);
                }
            }

            return(false);
        }
        public CTimeFrameInfo GetNewTimeFrameInfo(string isin, EnmTF TF, DateTime dt)
        {
            if (!this.ContainsKey(TF.ToString()))
            {
                this[TF.ToString()] = new SortedDictionary <DateTime, CTimeFrameArray>();
            }
            if (!this[TF.ToString()].ContainsKey(dt.Date))
            {
                this[TF.ToString()][dt.Date] = new CTimeFrameArray();
            }



            mx.WaitOne();
            this[TF.ToString()][dt.Date].ListTimeFrameInfo.Add(new CTimeFrameInfo(isin, dt /*,m_timeFrameAnalyzer*/));
            mx.ReleaseMutex();



            return(this[TF.ToString()][dt.Date].ListTimeFrameInfo.Last());
        }
        public CGUIUserDealViewModel(CRawUserDeal rd, decimal height, string botName, EnmTF tf = EnmTF.M1)
        {
            Id      = rd.Id_Deal;
            Price   = rd.Price;
            Height  = height;
            Date    = rd.Moment;
            Pos     = Price + Height / 2;
            BotName = botName;
            string trngFile = "";

            if (rd.Ext_id_buy > 0)
            {
                trngFile = "triangle_green.png";
                OrdDir   = OrderDirection.Buy;
            }
            else if (rd.Ext_id_sell > 0)
            {
                trngFile = "triangle_red.png";
                OrdDir   = OrderDirection.Sell;
            }



            if (trngFile != "")
            {
                PathFile = CUtil.GetImagesPath() + trngFile;
            }
        }
        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;
        }
        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);
            }
        }