Exemplo n.º 1
0
 /// <summary>
 /// Инициализация перед началом торговой сессии
 /// </summary>
 public void Initialize()
 {
     _logger.AddInfo("TickDispatcher", "Initialize ...");
     UnsubscribeAll();
     _insID_ticks.Clear();
     _logger.AddInfo("TickDispatcher", "Initialized");
 }
Exemplo n.º 2
0
        /// <summary>
        /// Сохранение данных по всем сделкам для всех инструментов
        /// </summary>
        /// <param name="tickDispatcher">Диспетчер тиковых данных</param>
        /// <param name="sessionDbPath">Каталог данных текущей сессии (определяется датой)</param>
        public void SaveData(ITickDispatcher tickDispatcher, string sessionDbPath)
        {
            if (tickDispatcher == null)
            {
                throw new ArgumentNullException("tickDispatcher");
            }
            if (string.IsNullOrWhiteSpace(sessionDbPath))
            {
                throw new ArgumentException("SessionDbPath is empty, session not opened.");
            }

            _logger.AddInfo("AllTradesData", "Save data ...");
            try
            {
                var allTradesDir = Path.Combine(sessionDbPath, "AllTrades");

                if (!Directory.Exists(allTradesDir))
                {
                    Directory.CreateDirectory(allTradesDir);
                }

                var insIDs = tickDispatcher.GetInstrumIDs();
                foreach (var insID in insIDs)
                {
                    Instrum ins = _instrumTable.GetInstrum(insID);
                    if (ins == null)
                    {
                        continue;
                    }
                    var ticks = tickDispatcher.GetTicks(insID);
                    if (ticks == null || !ticks.Any())
                    {
                        continue;
                    }

                    var encoder = new AllTradesEncoder(ins.Decimals);
                    var persist = new AllTradesPersist();
                    persist.Initialize(allTradesDir, ins.Ticker);
                    _insStoreData.InitInsStores(insID);

                    foreach (Tick tick in ticks)
                    {
                        uint   seconds = (uint)(tick.Time.Hour * 60 * 60 + tick.Time.Minute * 60 + tick.Time.Second);
                        byte[] buf     = encoder.AddTick(seconds, tick.Price, tick.Lots);
                        persist.Write(buf);
                        _insStoreData.AddTick(insID, tick.Time, tick.Price, tick.Lots);
                    }
                    persist.Close();
                }
                _insStoreData.SaveData();
            }
            catch (Exception ex)
            {
                _logger.AddException("AllTradesData", ex);
            }
            _logger.AddInfo("AllTradesData", "Data saved");
        }
Exemplo n.º 3
0
 protected override void OnStart(string[] args)
 {
     _logger.AddInfo("Service", "OnStart");
     try
     {
         _app.Initialize();
     }
     catch (Exception ex)
     {
         _logger.AddException("Service", ex);
     }
 }
Exemplo n.º 4
0
        public void OpenTerminal()
        {
            Type type = Type.GetTypeFromProgID("TEClient.SlotFace");

            _sf = (SlotFace)Activator.CreateInstance(type);
            _sf.Synchronized += OnSynchronized;
            _sf.Open(0);
            _logger.AddInfo("AlorTradeWrapper", "App opened");
        }
Exemplo n.º 5
0
        public void Initialize()
        {
            _logger.AddInfo("BotManager", "Initialize ...");

            _key_bots.Clear();
            _bot_platform.Clear();
            var keys = _botsConfig.GetBotKeys();

            foreach (var key in keys)
            {
                var conf = _botsConfig.GetBotConfig(key);
                if (conf == null)
                {
                    continue;
                }

                try
                {
                    var asm = Assembly.LoadFrom(conf.Assembly);
                    if (asm == null)
                    {
                        continue;
                    }

                    var type = asm.GetType(conf.Class);
                    if (type == null)
                    {
                        continue;
                    }

                    var platform = IoC.Resolve <ILeechPlatform>();
                    var bot      = Activator.CreateInstance(type, platform) as IBot;
                    if (bot == null)
                    {
                        continue;
                    }

                    bot.Initialize(conf.InitData);

                    // после успешной инициализации бота
                    _bot_platform.Add(bot, platform);
                    _key_bots.Add(key, bot);
                    _logger.AddInfo("BotManager", "Bot '" + key + "' initialized");
                }
                catch (Exception ex)
                {
                    _logger.AddException("BotManager", ex);
                }
            }
            _logger.AddInfo("BotManager", "Initialized");
        }
Exemplo n.º 6
0
        public void SaveData()
        {
            DateTime firstDate; DateTime lastDate;

            var lastHistData = _reposBL.GetIntParam(LAST_HISTORY_DATA);

            if (lastHistData == null)
            {
                firstDate = DateTime.Today.AddHours(_config.CorrectHours).Date;
            }
            else
            {
                firstDate = StorageLib.ToDateTime(lastHistData.Value).AddDays(1);
            }
            lastDate = firstDate;

            _logger.AddInfo("InsStoreData", "Bars saving ...");

            bool isNewTran = _storage.BeginTransaction();

            try
            {
                foreach (var insStore in _insStore_barRow.Keys)
                {
                    var bars = _insStore_barRow[insStore].Bars;
                    if (!bars.Any())
                    {
                        continue;
                    }

                    var lastBarDate = bars.Last().Time.Date;
                    if (lastBarDate > lastDate)
                    {
                        lastDate = lastBarDate;
                    }

                    _insStoreBL.InsertData(insStore.InsStoreID, bars, firstDate, lastBarDate, false, new CancellationToken());
                }

                _reposBL.SetIntParam(LAST_HISTORY_DATA, StorageLib.ToDbTime(lastDate));
                _storage.Commit(isNewTran);
            }
            catch (Exception ex)
            {
                _storage.Rollback(isNewTran);
                _logger.AddException("InsStoreBL:SaveData", ex);
            }

            _logger.AddInfo("InsStoreData", string.Format("Bars saved: {0} - {1}", firstDate.ToString("dd.MM.yyyy"), lastDate.ToString("dd.MM.yyyy")));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Initialization database (create if need, convertation)
        /// </summary>
        /// <returns></returns>
        public void Initialize()
        {
            _logger.AddInfo("DataStorage", "Initialize ...");
            int version = GetDBVersion();

            for (int v = 0; v < DbConvert.Scripts.Length; v++)
            {
                if (version < v)
                {
                    Convert(DbConvert.Scripts[v], v);
                }
            }
            _logger.AddInfo("DataStorage", "Initialized");
        }
Exemplo n.º 8
0
        private async Task exportProject(ExportRunOptions runOptions)
        {
            var projectFile = runOptions.ProjectFile;

            if (!FileHelper.FileExists(projectFile))
            {
                throw new OSPSuiteException($"Project file '{projectFile}' does not exist.");
            }

            DirectoryHelper.CreateDirectory(runOptions.OutputFolder);

            _logger.AddInfo($"Starting project export for '{projectFile}'");

            _workspacePersistor.LoadSession(_workspace, projectFile);
            _logger.AddDebug($"Project loaded successfully from '{projectFile}'");

            await ExportSimulationsIn(_workspace.Project, runOptions);

            _logger.AddInfo($"Project export for '{projectFile}' terminated");
        }
Exemplo n.º 9
0
        public async Task Start()
        {
            try
            {
                _logPresenter.ClearLog();
                _view.EnableButtons(false);
                _logger.AddInfo(PKSimConstants.Information.LoadingSnapshot(_loadFromSnapshotDTO.SnapshotFile, typeToLoad));
                await Task.Run(() => PerformLoad(_loadFromSnapshotDTO.SnapshotFile));

                _logger.AddInfo(PKSimConstants.Information.SnapshotLoaded(typeToLoad));
            }
            catch (Exception e)
            {
                _logger.AddException(e);
            }
            finally
            {
                _view.EnableButtons(cancelEnabled: true, okEnabled: ModelIsDefined, startEnabled: CanClose);
            }
        }
Exemplo n.º 10
0
        private void DoWork()
        {
            int lasttime = -1;

            _logger.AddInfo("Scheduler", "Working");
            while (_isWorking)
            {
                var now     = DateTime.Now;
                int curtime = now.Hour * 10000 + now.Minute * 100 + now.Second;
                if (lasttime == curtime)
                {
                    Thread.Sleep(200);
                    continue;
                }
                lasttime = curtime;

                lock (_time_action)
                {
                    foreach (var time in _time_action.Keys)
                    {
                        if (time != curtime)
                        {
                            continue;
                        }
                        Task.Factory.StartNew(() =>
                        {
                            try
                            {
                                _time_action[time].Invoke();
                            }
                            catch (Exception ex)
                            {
                                _logger.AddException("Scheduler", ex);
                            }
                        });
                    }
                }
            }
            _logger.AddInfo("Scheduler", "Stop work");
        }
Exemplo n.º 11
0
 public override void LogException(Exception ex)
 {
     if (ex.IsInfoException())
     {
         var message = ex.ExceptionMessage();
         _dialogCreator.MessageBoxInfo(message);
         _logger.AddInfo(message);
     }
     else
     {
         showException(ex);
     }
 }
Exemplo n.º 12
0
        public async Task RunBatchAsync(JsonRunOptions runOptions)
        {
            _logger.AddInfo($"Starting batch run: {DateTime.Now.ToIsoFormat()}");

            await Task.Run(() => startJsonSimulationRun(runOptions));

            _logger.AddInfo($"Batch run finished: {DateTime.Now.ToIsoFormat()}");
        }
Exemplo n.º 13
0
        /// <summary>
        /// Загрузить таблицу из базы (предварительно ее очистить)
        /// </summary>
        public void Load()
        {
            var list = _da.GetInstrumList();

            _ticker_instrum.Clear();
            _insID_instrum.Clear();
            foreach (var ins in list)
            {
                if (_ticker_instrum.ContainsKey(ins.Ticker))
                {
                    continue;
                }
                _ticker_instrum.Add(ins.Ticker, ins);
                _insID_instrum.Add(ins.InsID, ins);
            }
            _logger.AddInfo("InstrumTable", "Load instrums: " + _ticker_instrum.Keys.Count.ToString());
        }
Exemplo n.º 14
0
        private async Task <bool> ConnectAsync()
        {
            string url; string login; string password;

            _dataProtect.GetPulxerParams(out url, out login, out password);

            var res = await _socket.ConnectAsync(url, login, password);

            if (!res.IsSuccess)
            {
                _logger.AddError("LpClientApp", "Pulxer connection error: " + res.Message);
                return(false);
            }

            _core.Initialize(_sysPipe, "Leech Agent");
            _logger.AddInfo("LpClientApp", "Pulxer connection successful.");

            return(true);
        }
Exemplo n.º 15
0
 public void MessageBoxInfo(string message)
 {
     _logger.AddInfo(message);
 }
Exemplo n.º 16
0
        public async Task RunBatchAsync(QualificationRunOptions runOptions)
        {
            _runOptions = runOptions;

            if (!FileHelper.FileExists(runOptions.ConfigurationFile))
            {
                throw new QualificationRunException(ConfigurationFileNotFound(runOptions.ConfigurationFile));
            }

            setupOutputFolder();

            dynamic qualificationPlan = await _jsonSerializer.Deserialize <dynamic>(runOptions.ConfigurationFile);

            IReadOnlyList <Project> projects = GetListFrom <Project>(qualificationPlan.Projects);

            if (projects == null)
            {
                throw new QualificationRunException(ProjectsNotDefinedInQualificationFile);
            }

            IReadOnlyList <SimulationPlot> allPlots  = retrieveProjectPlots(qualificationPlan);
            IReadOnlyList <Input>          allInputs = retrieveInputs(qualificationPlan);

            var begin = DateTime.UtcNow;

            await updateProjectsFullPath(projects);

            //Configurations only need to be created once!
            var projectConfigurations = await Task.WhenAll(projects.Select(p => createQualifcationConfigurationFor(p, projects, allPlots, allInputs)));

            _logger.AddDebug("Copying static files");
            StaticFiles staticFiles = await copyStaticFiles(qualificationPlan);

            _logger.AddInfo("Starting validation runs...");
            var validations = await Task.WhenAll(projectConfigurations.Select(validateProject));

            var invalidConfigurations = validations.Where(x => !x.Success).ToList();

            if (invalidConfigurations.Any())
            {
                throw new QualificationRunException(errorMessageFrom(invalidConfigurations));
            }

            //Run all qualification projects
            _logger.AddInfo("Starting qualification runs...");
            var runResults = await Task.WhenAll(projectConfigurations.Select(runQualification));

            var invalidRunResults = runResults.Where(x => !x.Success).ToList();

            if (invalidRunResults.Any())
            {
                throw new QualificationRunException(errorMessageFrom(invalidRunResults));
            }

            await createReportConfigurationPlan(runResults, staticFiles, qualificationPlan);

            var end       = DateTime.UtcNow;
            var timeSpent = end - begin;

            _logger.AddInfo($"Qualification scenario finished in {timeSpent.ToDisplay()}");
        }
Exemplo n.º 17
0
        public async Task RunBatchAsync(QualificationRunOptions runOptions)
        {
            _snapshotProjectCache.Clear();
            _logger.AddInfo(runOptions.Validate ? "Starting validation run..." : "Starting qualification run...");

            var config = await readConfigurationFrom(runOptions);

            if (config == null)
            {
                throw new QualificationRunException(UnableToLoadQualificationConfigurationFromFile(runOptions.ConfigurationFile));
            }

            var errorMessage = config.Validate().Message;

            if (!string.IsNullOrEmpty(errorMessage))
            {
                throw new QualificationRunException(errorMessage);
            }

            _logger.AddDebug($"Loading project from snapshot file '{config.SnapshotFile}'...");
            var snapshot = await snapshotProjectFromFile(config.SnapshotFile);

            //Ensures that the name of the snapshot is also the name of the project as defined in the configuration
            snapshot.Name = config.Project;
            _logger.AddDebug($"Project {snapshot.Name} loaded from snapshot file '{config.SnapshotFile}'.");

            await performBuildingBlockSwap(snapshot, config.BuildingBlocks);

            await performSimulationParameterSwap(snapshot, config.SimulationParameters);

            //Retrieve charts and validate inputs before exiting validation to ensure that we can throw error messages if an element is not available
            var plots = retrievePlotDefinitionsFrom(snapshot, config);

            validateInputs(snapshot, config);

            if (runOptions.Validate)
            {
                _logger.AddInfo($"Validation run terminated for {snapshot.Name}");
                return;
            }

            var begin   = DateTime.UtcNow;
            var project = await _snapshotTask.LoadProjectFromSnapshot(snapshot);

            _workspace.Project = project;

            var projectOutputFolder = createProjectOutputFolder(config.OutputFolder, project.Name);

            _logger.AddDebug($"Exporting project {project.Name} to '{projectOutputFolder}'", project.Name);

            var exportRunOptions = new ExportRunOptions
            {
                OutputFolder = projectOutputFolder,
                ExportMode   = SimulationExportMode.Xml | SimulationExportMode.Csv
            };

            var simulationExports = await _exportSimulationRunner.ExportSimulationsIn(project, exportRunOptions);

            var simulationMappings = simulationExports.Select(x => simulationMappingFrom(x, config)).ToArray();

            var observedDataMappings = await exportAllObservedData(project, config);

            var inputMappings = exportInputs(project, config);

            var mapping = new QualificationMapping
            {
                SimulationMappings   = simulationMappings,
                ObservedDataMappings = observedDataMappings,
                Plots  = plots,
                Inputs = inputMappings
            };

            await _jsonSerializer.Serialize(mapping, config.MappingFile);

            _logger.AddDebug($"Project mapping for '{project.Name}' exported to '{config.MappingFile}'", project.Name);

            var projectFile = Path.Combine(config.TempFolder, $"{project.Name}{CoreConstants.Filter.PROJECT_EXTENSION}");

            _workspacePersistor.SaveSession(_workspace, projectFile);
            _logger.AddDebug($"Project saved to '{projectFile}'", project.Name);

            var snapshotFile = Path.Combine(config.TempFolder, $"{project.Name}{Constants.Filter.JSON_EXTENSION}");
            await _snapshotTask.ExportModelToSnapshot(project, snapshotFile);

            _logger.AddDebug($"Project snapshot saved to '{snapshotFile}'", project.Name);

            var end       = DateTime.UtcNow;
            var timeSpent = end - begin;

            _logger.AddInfo($"Project '{project.Name}' exported for qualification in {timeSpent.ToDisplay()}", project.Name);
        }
Exemplo n.º 18
0
 public void AddLog(string source, string text)
 {
     _logger.AddInfo("Bot:" + source, text);
 }
Exemplo n.º 19
0
        public void Load()
        {
            _logger.AddInfo("BotsConfiguration", "Loading bots configuration ...");
            _key_botconf.Clear();

            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(_config.GetBotsConfigPath());

                foreach (XmlNode xn in xDoc.DocumentElement.ChildNodes)
                {
                    if (xn.Name != "Bot")
                    {
                        _logger.AddInfo("BotsConfiguration", "Skip node: " + xn.Name);
                        continue;
                    }

                    var xa_key = xn.Attributes["Key"];
                    if (xa_key == null)
                    {
                        _logger.AddError("BotsConfiguration", "Key attribute not found.");
                        continue;
                    }
                    string key = xa_key.Value;

                    var xa_assembly = xn.Attributes["Assembly"];
                    if (xa_assembly == null)
                    {
                        _logger.AddError("BotsConfiguration", "Assembly attribute not found.");
                        continue;
                    }
                    string assembly = xa_assembly.Value;
                    if (!Path.IsPathRooted(assembly))
                    {
                        assembly = Path.Combine(_config.GetBotsPath(), assembly);
                    }

                    var xa_class = xn.Attributes["Class"];
                    if (xa_class == null)
                    {
                        _logger.AddError("BotsConfiguration", "Class attribute not found.");
                        continue;
                    }
                    string cls = xa_class.Value;

                    var    xa_initdata = xn.Attributes["InitData"];
                    string initdata    = "";
                    if (xa_initdata != null)
                    {
                        initdata = xa_initdata.Value;
                    }
                    else
                    {
                        _logger.AddInfo("BotsConfiguration", "InitData attribute not found. Empty string used.");
                    }

                    if (_key_botconf.ContainsKey(key))
                    {
                        _logger.AddError("BotsConfiguration", "Duplicate key: " + key);
                        continue;
                    }

                    _key_botconf.Add(key, new BotConfig()
                    {
                        Assembly = assembly,
                        Class    = cls,
                        InitData = initdata,
                        Key      = key
                    });
                    _logger.AddInfo("BotsConfiguration",
                                    string.Format("Bot config load: Key={0}, Assembly={1}, Class={2}", key, assembly, cls));
                }
            }
            catch (FileNotFoundException ex)
            {
                _logger.AddError("BotsConfiguration", "Bots config file not found");
            }
            catch (Exception ex)
            {
                _logger.AddException("BotsConfiguration", ex);
            }
        }
Exemplo n.º 20
0
        /// <summary>
        /// Инициализация перед началом торговой сессии.
        /// </summary>
        public void OpenSession()
        {
            _logger.AddInfo("LeechApp", "OpenSession");

            _instrumTable.Load();
            _accountTable.Load();

            // создание каталогов
            _sessionDbPath = _config.GetSessionDbPath(DateTime.Today);
            if (!Directory.Exists(_sessionDbPath))
            {
                Directory.CreateDirectory(_sessionDbPath);
            }

            _tickDispatcher.Initialize();
            _botsConfig.Load();
            _botManager.Initialize();
            _alorTrade.Initialize();
            _lpClientApp.Initialize();
            _logger.AddInfo("LeechApp", "Session opened");
        }