Exemplo n.º 1
0
        /// <summary>
        /// Получить объект InsStore
        /// Объект InsStore используется для хранения исторических данных по фин. инструменту.
        /// Исторические данные храняться в указанном таймфрейме.
        /// </summary>
        /// <param name="ticker">Инструмент</param>
        /// <param name="tf">ТФ</param>
        /// <returns>InsStore, null - тикер не найден либо InsStore не найден</returns>
        public CommonData.InsStore GetInsStore(string ticker, Timeframes tf)
        {
            var instrum = _instrumBL.GetInstrum(ticker);

            if (instrum == null)
            {
                return(null);
            }

            return(_insStoreDA.GetInsStore(instrum.InsID, tf));
        }
Exemplo n.º 2
0
        public bool AddInstrum(string ticker)
        {
            var instrum = _instrumBL.GetInstrum(ticker);

            if (instrum == null)
            {
                return(false);
            }

            if (_instrums.Any(r => r.InsID == instrum.InsID))
            {
                return(false);
            }

            _instrums.Add(instrum);

            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Вывод исторических данных
        /// </summary>
        /// <param name="args">Tf, Ticker, Date1, Date2</param>
        public void GetBars(List <string> args)
        {
            if (args.Count < 4)
            {
                _console.WriteError("Неверное число аргументов");
                return;
            }

            Timeframes tf;

            if (!Timeframes.TryParse <Timeframes>(args[0], out tf))
            {
                _console.WriteError("Неверный агрумент: Timeframe");
                return;
            }

            string ticker  = args[1];
            var    instrum = _instrumBL.GetInstrum(ticker);

            if (instrum == null)
            {
                _console.WriteError("Тикер не найден");
                return;
            }

            DateTime date1, date2;
            DateTime d;

            if (DateTime.TryParse(args[2].Trim(), out d))
            {
                date1 = d;
            }
            else
            {
                _console.WriteError("Неверно указана дата начала");
                return;
            }
            if (DateTime.TryParse(args[3].Trim(), out d))
            {
                date2 = d;
            }
            else
            {
                _console.WriteError("Неверно указана дата окончания");
                return;
            }

            BarRow bars = new BarRow(tf, instrum.InsID);

            _insStoreBL.LoadHistoryAsync(bars, instrum.InsID, date1, date2).Wait();

            _console.WriteLine("Time\t\t\tOpen\tHigh\tLow\tClose\tVolume");
            _console.WriteSeparator();
            string format = "0." + (new string('0', instrum.Decimals));

            foreach (var bar in bars.Bars)
            {
                _console.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}",
                                                 bar.Time.ToString("dd.MM.yyyy HH:mm:ss"),
                                                 bar.Open.ToString(format),
                                                 bar.High.ToString(format),
                                                 bar.Low.ToString(format),
                                                 bar.Close.ToString(format),
                                                 bar.Volume));
            }
            _console.WriteSeparator();
            _console.WriteLine("Count: " + bars.Count.ToString());
        }
Exemplo n.º 4
0
 public IInstrum GetInstrum(string ticker)
 {
     return _instrumBL.GetInstrum(ticker);
 }
Exemplo n.º 5
0
        /// <summary>
        /// Синтаксический разбор xml-документа со сделками
        /// </summary>
        /// <param name="xDoc">XML-документ</param>
        /// <returns>Список сделок</returns>
        public IEnumerable <Trade> ParseXml(XmlDocument xDoc)
        {
            if (xDoc == null)
            {
                throw new ArgumentNullException();
            }

            sb.AppendLine("Parsing ...");
            List <Trade> trades = new List <Trade>();

            if (xDoc.DocumentElement == null || xDoc.DocumentElement.Name.ToLower() != "trades")
            {
                sb.AppendLine("Incorrect root element");
                return(trades);
            }

            CultureInfo ci = new CultureInfo("en-US");

            foreach (XmlNode xnTrade in xDoc.DocumentElement.ChildNodes)
            {
                if (xnTrade.Name.ToLower() != "trade")
                {
                    sb.AppendLine("Incorrect trade node: " + xnTrade.OuterXml);
                    continue;
                }

                var attribs = xnTrade.Attributes.Cast <XmlAttribute>().ToList();

                DateTime time;
                var      xa_time = attribs.FirstOrDefault(r => r.Name.ToLower() == "time");
                if (xa_time == null || string.IsNullOrWhiteSpace(xa_time.Value))
                {
                    sb.AppendLine("Time attribute not found or empty: " + xnTrade.OuterXml);
                    continue;
                }
                if (!DateTime.TryParseExact(xa_time.Value, "dd.MM.yyyy HH:mm:ss", ci, DateTimeStyles.None, out time))
                {
                    sb.AppendLine("Incorrect time (dd.mm.yyyy hh:mm:ss): " + xnTrade.OuterXml);
                    continue;
                }

                string ticker    = "";
                var    xa_ticker = attribs.FirstOrDefault(r => r.Name.ToLower() == "ticker");
                if (xa_ticker == null || string.IsNullOrWhiteSpace(xa_ticker.Value))
                {
                    sb.AppendLine("Ticker attribute not found or empty: " + xnTrade.OuterXml);
                    continue;
                }
                ticker = xa_ticker.Value;
                var instrum = _instrumBL.GetInstrum(ticker);
                if (instrum == null)
                {
                    sb.AppendLine("Ticker not found: " + xnTrade.OuterXml);
                    continue;
                }

                decimal price;
                var     xa_price = attribs.FirstOrDefault(r => r.Name.ToLower() == "price");
                if (xa_price == null || string.IsNullOrWhiteSpace(xa_price.Value))
                {
                    sb.AppendLine("Price attribute not found or empty: " + xnTrade.OuterXml);
                    continue;
                }
                string p = xa_price.Value.Replace(",", "."); // в en-us используется точка, но мы понимаем и запятую
                if (!decimal.TryParse(p, NumberStyles.AllowDecimalPoint, ci, out price))
                {
                    sb.AppendLine("Incorrect price: " + xnTrade.OuterXml);
                    continue;
                }

                int lots;
                var xa_lots = attribs.FirstOrDefault(r => r.Name.ToLower() == "lots");
                if (xa_lots == null || string.IsNullOrWhiteSpace(xa_lots.Value))
                {
                    sb.AppendLine("Lots attribute not found or empty: " + xnTrade.OuterXml);
                    continue;
                }
                if (!int.TryParse(xa_lots.Value, NumberStyles.None, ci, out lots))
                {
                    sb.AppendLine("Incorrect lots: " + xnTrade.OuterXml);
                    continue;
                }

                BuySell bs;
                var     xa_bs = attribs.FirstOrDefault(r => r.Name.ToLower() == "bs");
                if (xa_bs == null || string.IsNullOrWhiteSpace(xa_bs.Value))
                {
                    sb.AppendLine("BS attribute not found or empty: " + xnTrade.OuterXml);
                    continue;
                }
                if (xa_bs.Value.ToLower().StartsWith("b"))
                {
                    bs = BuySell.Buy;
                }
                else if (xa_bs.Value.ToLower().StartsWith("s"))
                {
                    bs = BuySell.Sell;
                }
                else
                {
                    sb.AppendLine("Incorrect BS: " + xnTrade.OuterXml);
                    continue;
                }

                Trade trade = new Trade()
                {
                    AccountID = 1,
                    BuySell   = bs,
                    InsID     = instrum.InsID,
                    LotCount  = lots,
                    Comm      = 0,
                    OrderID   = 0,
                    Price     = price,
                    Time      = time,
                    TradeID   = 0,
                    TradeNo   = 0
                };
                trades.Add(trade);
            }
            sb.AppendLine("Parsing complete: " + trades.Count.ToString());

            return(trades);
        }