/// <summary>
 /// To create candles from the tick trades collection.
 /// </summary>
 /// <param name="trades">Tick trades.</param>
 /// <param name="series">Candles series.</param>
 /// <returns>Candles.</returns>
 public static IEnumerableEx <Candle> ToCandles(this IEnumerableEx <Trade> trades, CandleSeries series)
 {
     return(new CandleEnumerable <Trade>(series, trades));
 }
		/// <summary>
		/// To start paper trading.
		/// </summary>
		public void Start(IEnumerableEx<Strategy> strategies)
		{
			if (strategies == null)
				throw new ArgumentNullException(nameof(strategies));

			_progressStep = ((EmulationSettings.StopTime - EmulationSettings.StartTime).Ticks / 100).To<TimeSpan>();
			
			_cancelEmulation = false;
			_totalBatches = (int)((decimal)strategies.Count / EmulationSettings.BatchSize).Ceiling();
			_currentBatch = -1;

			CurrentProgress = 0;
			
			State = EmulationStates.Starting;

			_batches = strategies.Batch(EmulationSettings.BatchSize).GetEnumerator();

			TryStartNextBatch();
		}
Exemple #3
0
        public void Start(Security security, Type dataType, object arg, IEnumerableEx values, object path)
        {
            if (dataType == null)
            {
                throw new ArgumentNullException("dataType");
            }

            if (security == null && dataType != typeof(NewsMessage) && dataType != typeof(SecurityMessage))
            {
                throw new ArgumentNullException("security");
            }

            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            var currProgress = 5;

            var valuesPerPercent = (values.Count / (100 - currProgress)).Max(1);
            var valuesPerCount   = (valuesPerPercent / 10).Max(1);
            var currCount        = 0;
            var valuesCount      = 0;

            Func <int, bool> isCancelled = count =>
            {
                var isCancelling = _worker.CancellationPending;

                if (!isCancelling)
                {
                    valuesCount += count;

                    if (valuesCount / valuesPerPercent > currProgress)
                    {
                        currProgress = valuesCount / valuesPerPercent;
                        _worker.ReportProgress(currProgress);
                    }

                    if (valuesCount > currCount)
                    {
                        currCount = valuesCount + valuesPerCount;
                        this.GuiAsync(() => UpdateCount(valuesCount));
                    }
                }

                return(isCancelling);
            };

            string       fileName;
            BaseExporter exporter;

            switch (_button.ExportType)
            {
            case ExportTypes.Excel:
                fileName = (string)path;
                exporter = new ExcelExporter(security, arg, isCancelled, fileName, () => GuiDispatcher.GlobalDispatcher.AddSyncAction(TooManyValues));
                break;

            case ExportTypes.Xml:
                fileName = (string)path;
                exporter = new XmlExporter(security, arg, isCancelled, fileName);
                break;

            case ExportTypes.Txt:
                var wnd = new ExportTxtPreviewWindow
                {
                    DataType = dataType,
                    Arg      = arg
                };

                var registry = ((HydraEntityRegistry)ConfigManager.GetService <IEntityRegistry>()).Settings.TemplateTxtRegistry;

                if (dataType == typeof(SecurityMessage))
                {
                    wnd.TxtTemplate = registry.TemplateTxtSecurity;
                }
                else if (dataType == typeof(NewsMessage))
                {
                    wnd.TxtTemplate = registry.TemplateTxtNews;
                }
                else if (dataType.IsSubclassOf(typeof(CandleMessage)))
                {
                    wnd.TxtTemplate = registry.TemplateTxtCandle;
                }
                else if (dataType == typeof(Level1ChangeMessage))
                {
                    wnd.TxtTemplate = registry.TemplateTxtLevel1;
                }
                else if (dataType == typeof(QuoteChangeMessage))
                {
                    wnd.TxtTemplate = registry.TemplateTxtDepth;
                }
                else if (dataType == typeof(ExecutionMessage))
                {
                    if (arg == null)
                    {
                        throw new ArgumentNullException("arg");
                    }

                    switch ((ExecutionTypes)arg)
                    {
                    case ExecutionTypes.Tick:
                        wnd.TxtTemplate = registry.TemplateTxtTick;
                        break;

                    case ExecutionTypes.Order:
                    case ExecutionTypes.Trade:
                        wnd.TxtTemplate = registry.TemplateTxtTransaction;
                        break;

                    case ExecutionTypes.OrderLog:
                        wnd.TxtTemplate = registry.TemplateTxtOrderLog;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
                else
                {
                    throw new ArgumentOutOfRangeException("dataType", dataType, LocalizedStrings.Str721);
                }


                if (!wnd.ShowModal(this))
                {
                    return;
                }

                if (dataType == typeof(SecurityMessage))
                {
                    registry.TemplateTxtSecurity = wnd.TxtTemplate;
                }
                else if (dataType == typeof(NewsMessage))
                {
                    registry.TemplateTxtNews = wnd.TxtTemplate;
                }
                else if (dataType.IsSubclassOf(typeof(CandleMessage)))
                {
                    registry.TemplateTxtCandle = wnd.TxtTemplate;
                }
                else if (dataType == typeof(Level1ChangeMessage))
                {
                    registry.TemplateTxtLevel1 = wnd.TxtTemplate;
                }
                else if (dataType == typeof(QuoteChangeMessage))
                {
                    registry.TemplateTxtDepth = wnd.TxtTemplate;
                }
                else if (dataType == typeof(ExecutionMessage))
                {
                    if (arg == null)
                    {
                        throw new ArgumentNullException("arg");
                    }

                    switch ((ExecutionTypes)arg)
                    {
                    case ExecutionTypes.Tick:
                        registry.TemplateTxtTick = wnd.TxtTemplate;
                        break;

                    case ExecutionTypes.Order:
                    case ExecutionTypes.Trade:
                        registry.TemplateTxtTransaction = wnd.TxtTemplate;
                        break;

                    case ExecutionTypes.OrderLog:
                        registry.TemplateTxtOrderLog = wnd.TxtTemplate;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
                else
                {
                    throw new ArgumentOutOfRangeException("dataType", dataType, LocalizedStrings.Str721);
                }

                fileName = (string)path;
                exporter = new TextExporter(security, arg, isCancelled, fileName, wnd.TxtTemplate, wnd.TxtHeader);
                break;

            case ExportTypes.Sql:
                fileName = null;
                exporter = new DatabaseExporter(security, arg, isCancelled, (DatabaseConnectionPair)path)
                {
                    CheckUnique = false
                };
                break;

            case ExportTypes.Bin:
                var drive = (IMarketDataDrive)path;
                fileName = drive is LocalMarketDataDrive ? drive.Path : null;
                exporter = new BinExporter(security, arg, isCancelled, drive);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            CreateWorker(values.Count, fileName);

            _worker.DoWork += (s, e) =>
            {
                _worker.ReportProgress(currProgress);

                exporter.Export(dataType, values);

                _worker.ReportProgress(100);
                Thread.Sleep(500);
                _worker.ReportProgress(0);
            };

            _worker.RunWorkerAsync();
        }
 public CandleEnumerable(CandleSeries series, IEnumerableEx <TValue> values)
     : base(() => new CandleEnumerator(series, values))
 {
     _values = values;
 }
Exemple #5
0
 /// <summary>
 /// Построить тиковые сделки из свечек.
 /// </summary>
 /// <param name="candles">Свечи.</param>
 /// <param name="volumeStep">Шаг объема.</param>
 /// <returns>Тиковые сделки.</returns>
 public static IEnumerableEx <ExecutionMessage> ToTrades(this IEnumerableEx <CandleMessage> candles, decimal volumeStep)
 {
     return(new TradeEnumerable(candles, volumeStep));
 }
Exemple #6
0
 /// <summary>
 /// Построить свечи из коллекции стаканов.
 /// </summary>
 /// <param name="depths">Стаканы.</param>
 /// <param name="series">Серия свечек.</param>
 /// <returns>Свечи.</returns>
 public static IEnumerableEx <Candle> ToCandles(this IEnumerableEx <MarketDepth> depths, CandleSeries series)
 {
     return(new CandleEnumerable <MarketDepth>(series, depths));
 }
 /// <summary>
 /// Построить тиковые сделки из лога заявок.
 /// </summary>
 /// <param name="items">Строчки лога заявок.</param>
 /// <returns>Тиковые сделки.</returns>
 public static IEnumerableEx <ExecutionMessage> ToTicks(this IEnumerableEx <ExecutionMessage> items)
 {
     return(new OrderLogTickEnumerable(items));
 }
 /// <summary>
 /// Построить стаканы из лога заявок.
 /// </summary>
 /// <param name="items">Строчки лога заявок.</param>
 /// <param name="interval">Интервал генерации стакана. По-умолчанаю равен <see cref="TimeSpan.Zero"/>, что означает генерацию стаканов при каждой новой строчке лога заявок.</param>
 /// <param name="maxDepth">Максимальная глубина стакана. По-умолчанию равно <see cref="int.MaxValue"/>, что означает бесконечную глубину.</param>
 /// <returns>Стаканы.</returns>
 public static IEnumerableEx <QuoteChangeMessage> ToMarketDepths(this IEnumerableEx <ExecutionMessage> items, TimeSpan interval = default(TimeSpan), int maxDepth = int.MaxValue)
 {
     return(new DepthEnumerable(items, interval, maxDepth));
 }
        public void Start(Security security, Type dataType, object arg, IEnumerableEx values, object path)
        {
            if (security == null && dataType != typeof(News) && dataType != typeof(Security))
            {
                throw new ArgumentNullException("security");
            }

            if (dataType == null)
            {
                throw new ArgumentNullException("dataType");
            }

            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            var currProgress = 5;

            var valuesPerPercent = (values.Count / (100 - currProgress)).Max(1);
            var valuesPerCount   = (valuesPerPercent / 10).Max(1);
            var currCount        = 0;
            var valuesCount      = 0;

            Func <int, bool> isCancelled = count =>
            {
                var isCancelling = _worker.CancellationPending;

                if (!isCancelling)
                {
                    valuesCount += count;

                    if (valuesCount / valuesPerPercent > currProgress)
                    {
                        currProgress = valuesCount / valuesPerPercent;
                        _worker.ReportProgress(currProgress);
                    }

                    if (valuesCount > currCount)
                    {
                        currCount = valuesCount + valuesPerCount;
                        this.GuiAsync(() => UpdateCount(valuesCount));
                    }
                }

                return(isCancelling);
            };

            string       fileName;
            BaseExporter exporter;

            switch (_button.ExportType)
            {
            case ExportTypes.Excel:
                fileName = (string)path;
                exporter = new ExcelExporter(security, arg, isCancelled, fileName, () => GuiDispatcher.GlobalDispatcher.AddSyncAction(TooManyValues));
                break;

            case ExportTypes.Xml:
                fileName = (string)path;
                exporter = new XmlExporter(security, arg, isCancelled, fileName);
                break;

            case ExportTypes.Txt:
                fileName = (string)path;
                exporter = new TextExporter(security, arg, isCancelled, fileName);
                break;

            case ExportTypes.Sql:
                fileName = null;
                exporter = new DatabaseExporter(security, arg, isCancelled, (DatabaseConnectionPair)path)
                {
                    CheckUnique = false
                };
                break;

            case ExportTypes.Bin:
                var drive = (IMarketDataDrive)path;
                fileName = drive is LocalMarketDataDrive ? drive.Path : null;
                exporter = new BinExporter(security, arg, isCancelled, drive);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            CreateWorker(values.Count, fileName);

            _worker.DoWork += (s, e) =>
            {
                _worker.ReportProgress(currProgress);

                exporter.Export(dataType, values);

                _worker.ReportProgress(100);
                Thread.Sleep(500);
                _worker.ReportProgress(0);
            };

            _worker.RunWorkerAsync();
        }