public void StartServer() { TaskFactory factory = new TaskFactory(cts.Token); factory.StartNew(() => { //запустить обход файла using (StreamReader sr = new StreamReader(FilePath)) { Dictionary<string, Ticker> tickersCache = new Dictionary<string, Ticker>(); DateTime lastMinute = new DateTime(); string line; while ((line = sr.ReadLine()) != null) { if (cts.Token.IsCancellationRequested) //выход из потока по кнопке "Стоп" return; string[] cells = line.Split(Separator); bool isRealTime = true; ConnectorHelper helper = new ConnectorHelper(); if (headTable == null) //Должна быть строка с заголовками { headTable = helper.FillHeadTable(cells); continue; } //try //{ TickEventArgs tick = new TickEventArgs(); tick.Date = helper.ParseDate(cells, headTable, Col_Date, Col_Time, FormatDate, FormatTime); tick.TickerCode = helper.ParseTickerCode(cells, headTable, Col_TickerCode); tick.Price = helper.ParsePrice(cells, headTable, Col_Price, CultureInfo.InvariantCulture); tick.Quantity = helper.ParseQuantity(cells, headTable, Col_Qty); if (tick.Quantity == 0) { if(!tickersCache.ContainsKey(tick.TickerCode)) tickersCache[tick.TickerCode] = tickerRepository.Get(T=>T.Where(t=>t.Code == tick.TickerCode)).FirstOrDefault(); Ticker ticker = tickersCache[tick.TickerCode]; if (ticker != null && ticker.QtyInLot.HasValue) tick.Quantity = helper.ParseQuantity(cells, headTable, Col_Volume, ticker.QtyInLot.Value); } tick.IsRealTime = isRealTime; tick.TradePeriod = helper.ParseTradePeriod(cells, headTable, Col_TradePeriod, Val_PeriodOpening, Val_PeriodTrading, Val_PeriodClosing); if (tick.TradePeriod == TradePeriod.Undefended) tick.TradePeriod = helper.ParseTradePeriod(cells, headTable, Col_Time, FormatTime, Val_SessionStart, Val_SessionFinish); tick.Extended = helper.CreateExtended(cells, headTable); //Включаем задержку if(Delay != 0 && lastMinute < tick.Date) { Thread.Sleep(60000 / Delay); lastMinute = tick.Date.AddMinutes(1); } //вызвать событие IConnector var tickPoked = TickPoked; tickPoked?.Invoke(this, tick); //} //catch (Exception) //{ // throw; //} } } }); }
/// <summary> /// Метод выполняется по подписке при передачи данных из терминала Quik /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnDataPoked(object sender, DataPokeddEventArgs e) { bool isRealTime = e.Cells.Length == 1 ? true : false; //Если пришла одна строка, то скорее всего это реал тайм )). Или проверить время сервера ConnectorHelper helper = new ConnectorHelper(); foreach (object[] cells in e.Cells) { if (headTable == null) //Должна быть строка с заголовками { headTable = helper.FillHeadTable(cells); continue; } try { TickEventArgs tick = new TickEventArgs(); tick.Date = helper.ParseDate(cells, headTable, Col_Date, Col_Time, FormatDate, FormatTime); tick.TickerCode = helper.ParseTickerCode(cells, headTable, Col_TickerCode); tick.Price = helper.ParsePrice(cells, headTable, Col_Price, CultureInfo.InvariantCulture); tick.Quantity = helper.ParseQuantity(cells, headTable, Col_Qty); tick.IsRealTime = isRealTime; tick.TradePeriod = helper.ParseTradePeriod(cells, headTable, Col_TradePeriod, Val_PeriodOpening, Val_PeriodTrading, Val_PeriodClosing); if (tick.TradePeriod == TradePeriod.Undefended) tick.TradePeriod = helper.ParseTradePeriod(cells, headTable, Col_TradePeriod, FormatTime, Val_SessionStart, Val_SessionFinish); tick.Extended = helper.CreateExtended(cells, headTable); //вызвать событие IConnector var tickPoked = TickPoked; tickPoked?.Invoke(this, tick); } catch(Exception) { throw; } } }
public void OnTickPoked(object sender, TickEventArgs e) { }
public void OnTickPoked(object sender, TickEventArgs tick) { if (TickPoked != null) //todo: Сделать ассинхронно TickPoked(this, tick); if (tick.TradePeriod != TradePeriod.Undefended && tick.TradePeriod != TradePeriod.Trading) return; //торгуем только в сессию! //Заполнить "нулевой бар" if (Ticker.Code == tick.TickerCode) { DateTime nextTime; if (ZeroBar == null) nextTime = tick.Date.Date + startTime; else nextTime = ZeroBar.DateOpen.AddMinutes(TimeFrame.ToMinute); if (tick.Date >= nextTime) { //Добавляем свечу в коллекцию if (ZeroBar != null) { Candles.Insert(0, ZeroBar); //вызвать событие формирования свечи OnCreatedCandle(); OnCreatedCandleAsync(); if (tick.IsRealTime) //Стратегию вызываем только в реалтайме! { if (CreatedCandleReal != null) CreatedCandleReal(this, null); } //Strategy.Begin(); } ZeroBar = new Candle(); ZeroBar.DateOpen = nextTime; ZeroBar.Ticker = Ticker; ZeroBar.TimeFrame = TimeFrame; ZeroBar.OpenPrice = ZeroBar.HighPrice = ZeroBar.LowPrice = ZeroBar.ClosePrice = tick.Price; } else if (ZeroBar != null) { ZeroBar.HighPrice = Math.Max(ZeroBar.HighPrice, tick.Price); ZeroBar.LowPrice = Math.Min(ZeroBar.LowPrice, tick.Price); ZeroBar.ClosePrice = tick.Price; } } }