Exemplo n.º 1
0
        public List <Candle> GetCandles(string specification, int count)
        {
            CandleSeriesSaveInfo mySaveInfo = _candleSeriesSaveInfos.Find(s => s.Specification == specification);

            if (mySaveInfo == null)
            {
                mySaveInfo = TryLoadCandle(specification);

                if (mySaveInfo == null)
                {
                    mySaveInfo = new CandleSeriesSaveInfo();
                    mySaveInfo.Specification = specification;
                }

                _candleSeriesSaveInfos.Add(mySaveInfo);
            }

            List <Candle> candles = mySaveInfo.AllCandlesInFile;

            if (candles != null &&
                candles.Count != 0 &&
                candles.Count - 1 - count > 0)
            {
                candles = candles.GetRange(candles.Count - 1 - count, count);
            }

            return(candles);
        }
Exemplo n.º 2
0
        private void SaveSeries(CandleSeries series)
        {
            CandleSeriesSaveInfo mySaveInfo = _candleSeriesSaveInfos.Find(s => s.Specification == series.Specification);

            if (mySaveInfo == null)
            {
                mySaveInfo = TryLoadCandle(series.Specification);

                if (mySaveInfo == null)
                {
                    mySaveInfo = new CandleSeriesSaveInfo();
                    mySaveInfo.Specification    = series.Specification;
                    mySaveInfo.AllCandlesInFile = series.CandlesAll;
                }

                _candleSeriesSaveInfos.Add(mySaveInfo);
            }

            if (series.CandlesAll == null ||
                series.CandlesAll.Count == 0)
            {
                return;
            }

            Candle firstCandle = series.CandlesAll[0];
            Candle lastCandle  = series.CandlesAll[series.CandlesAll.Count - 1];

            if (firstCandle.TimeStart == mySaveInfo.LastCandleTime &&
                lastCandle.TimeStart == mySaveInfo.StartCandleTime &&
                lastCandle.Close == mySaveInfo.LastCandlePrice)
            {
                return;
            }

            mySaveInfo.InsertCandles(series.CandlesAll);

            if (Directory.Exists(_pathName) == false)
            {
                Directory.CreateDirectory(_pathName);
            }

            StreamWriter writer = new StreamWriter(_pathName + "\\" + series.Specification + ".txt");

            for (int i = 0; i < mySaveInfo.AllCandlesInFile.Count; i++)
            {
                writer.WriteLine(mySaveInfo.AllCandlesInFile[i].StringToSave);
            }

            writer.Close();
        }
Exemplo n.º 3
0
        public List <Candle> GetCandles(string specification, int count)
        {
            CandleSeriesSaveInfo mySaveInfo = GetSpecInfo(specification);

            List <Candle> candles = mySaveInfo.AllCandlesInFile;

            if (candles != null &&
                candles.Count != 0 &&
                candles.Count - 1 - count > 0)
            {
                candles = candles.GetRange(candles.Count - 1 - count, count);
            }

            return(candles);
        }
Exemplo n.º 4
0
        public CandleSeriesSaveInfo GetSpecInfo(string specification)
        {
            lock (_lockerSpec)
            {
                CandleSeriesSaveInfo mySaveInfo = _candleSeriesSaveInfos.Find(s => s.Specification == specification);

                if (mySaveInfo == null)
                {
                    mySaveInfo = TryLoadCandle(specification);

                    if (mySaveInfo == null)
                    {
                        mySaveInfo = new CandleSeriesSaveInfo();
                        mySaveInfo.Specification = specification;
                    }

                    _candleSeriesSaveInfos.Add(mySaveInfo);
                }

                return(mySaveInfo);
            }
        }
Exemplo n.º 5
0
        public CandleSeriesSaveInfo TryLoadCandle(string specification)
        {
            List <Candle> candlesFromServer = new List <Candle>();

            if (File.Exists(_pathName + "\\" + specification + ".txt"))
            {
                try
                {
                    using (StreamReader reader = new StreamReader(_pathName + "\\" + specification + ".txt"))
                    {
                        while (reader.EndOfStream == false)
                        {
                            string str       = reader.ReadLine();
                            Candle newCandle = new Candle();
                            newCandle.SetCandleFromString(str);
                            candlesFromServer.Add(newCandle);
                        }

                        reader.Close();
                    }
                }
                catch (Exception e)
                {
                    // ignore
                }
            }

            // далее смотрим есть ли сохранение в глобальном хранилище


            List <Candle> candlesFromOsData = new List <Candle>();

            string path = "Data\\ServersCandleTempData\\" + specification + ".txt";

            if (Directory.Exists("Data\\ServersCandleTempData") &&
                File.Exists(path))
            {
                try
                {
                    using (StreamReader reader = new StreamReader(path))
                    {
                        while (reader.EndOfStream == false)
                        {
                            string str       = reader.ReadLine();
                            Candle newCandle = new Candle();
                            newCandle.SetCandleFromString(str);
                            candlesFromOsData.Add(newCandle);
                        }

                        reader.Close();
                    }
                }
                catch (Exception e)
                {
                    // ignore
                }
            }

            if (candlesFromOsData.Count == 0 &&
                candlesFromServer.Count == 0)
            {
                return(null);
            }

            List <Candle> resultCandles = new List <Candle>();

            if (candlesFromOsData.Count != 0 &&
                candlesFromServer.Count != 0)
            {
                resultCandles = candlesFromServer;
                resultCandles.Merge(candlesFromOsData);
            }
            else if (candlesFromServer.Count != 0)
            {
                resultCandles = candlesFromServer;
            }
            else if (candlesFromOsData.Count != 0)
            {
                resultCandles = candlesFromOsData;
            }

            CandleSeriesSaveInfo myInfo = new CandleSeriesSaveInfo();

            myInfo.Specification = specification;
            myInfo.InsertCandles(resultCandles);

            return(myInfo);
        }