Beispiel #1
0
 public Trader(Connection connection,
               Statistics statistics,
               TradersSettings sets,
               FairPriceMaker priceMaker,
               TraderModusOperandi modusOperandi,
               int seqNum,
               int tradersTotal)
 {
     this.connection    = connection;
     this.statistics    = statistics;
     this.priceMaker    = priceMaker;
     this.modusOperandi = modusOperandi;
     this.seqNum        = seqNum;
     this.tradersTotal  = tradersTotal;
     settings           = sets;
     sinusPayloadCalc   = new SinusoidPayloadCalculator(sets);
     tradeThread        = new Thread(ThreadRoutine);
 }
Beispiel #2
0
        /// <param name="onStop">action called when the pool stops</param>
        public void Start(Action onStop)
        {
            this.onStop = onStop;
            ReadSettings();
            logger = new Logger(logMessageAction, settings.LoggingLevel);
            logger.LogMessage("Starting test", LoggingLevel.Critical);
            statistics       = new Statistics(settings);
            serverStatPicker = new ServerStatPicker(settings, logger);
            var fairPriceMaker = new FairPriceMaker(settings);

            try
            {
                for (var i = 0; i < settings.PayloadSets.TradersCount; i++)
                {
                    var conn = new Connection(settings.Uri, logger, i);
                    conn.onResponse += OnResponse;
                    conn.Open();
                    connectionPool.Add(conn);
                }
            }
            catch (Exception ex)
            {
                logger.LogMessage($"Error opening connection: {ex.GetType().Name}: {ex.Message}", LoggingLevel.Error);
                onStop();
                return;
            }

            var connIndex = 0;
            var rolePool  = Enumerable.Range(0, settings.TradeSets.MakersCount).Select(i => TraderModusOperandi.Maker).ToList();

            rolePool.AddRange(Enumerable.Range(0, settings.TradeSets.OneShotTradersCount).Select(i => TraderModusOperandi.OneShotTaker));
            rolePool.AddRange(Enumerable.Range(0, settings.PayloadSets.TradersCount).Select(i => TraderModusOperandi.Taker));

            traders = Enumerable.Range(0, settings.PayloadSets.TradersCount).Select(i =>
            {
                var trader = new Trader(connectionPool[connIndex++], statistics, settings, fairPriceMaker, rolePool[i], i, settings.PayloadSets.TradersCount)
                {
                    AccountId         = $"Trader_{i}",
                    ClearingAccountId = $"Qurrex_{connIndex}"
                };
                if (connIndex == connectionPool.Count)
                {
                    connIndex = 0;
                }
                return(trader);
            }).ToList();

            // the first trader watches for the test to stop on timeout
            traders.First().onTimer = () =>
            {
                ThreadPool.QueueUserWorkItem(o =>
                {
                    logger.LogMessage("Test is stopping by timeout...", LoggingLevel.Critical);
                    Stop();
                });
            };
            traders.First().timeoutTime = DateTime.Now.AddSeconds(settings.TestDurationSeconds);

            statistics.tradersByRole = traders.GroupBy(t => t.modusOperandi).ToDictionary(t => t.Key, t => t.Count());
            statistics.Start();
            traders.ForEach(t => t.Start());
            serverStatPicker.Start();
            logger.LogMessage("Test has been started", LoggingLevel.Critical);
        }