Esempio n. 1
0
        /// <summary>
        /// Initializes the specified assembly.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <param name="fundinfo">The fundinfo.</param>
        public void Initialize(Assembly assembly, AddFundMessage fundinfo)
        {
            try
            {
                //Set state
                State = FundState.Initializing;

                //Set fund universe
                Universe = Universe.Create(fundinfo.UniverseName, Portfolio.BrokerAccount.Securities, fundinfo.Universe);

                //Set fund modules
                foreach (var modulename in fundinfo.ModuleNames)
                {
                    //Try and get the module instance
                    if (!DynamicLoader.TryGetInstance(assembly, modulename, out IModule instance))
                    {
                        throw new Exception($"Could not find module {modulename} in provided assembly. Did you add the export attribute?");
                    }

                    //Set quantfund
                    instance.SetQuantFund(this);

                    //Set parameters
                    fundinfo.Parameters.Where(x => x.ModuleName == modulename)
                    .ForEach(parm => instance.SetParameter(parm.Name, parm.Value));

                    //Add to modules
                    _modules.Add(instance);
                }

                //Set universe to position tracker
                Positions.SetUniverse(Universe.Securities.Select(x => x.Ticker).ToArray());

                //Set benchmark
                Benchmark.OnCalc(x => Universe.Sum(s => s.Price * Universe.GetWeight(s)));

                //Subscribe to all ticker symbols by default
                Universe.ForEach(x => Portfolio.Subscription.AddSubscription(this, x, new TickQuoteBarAggregator(TimeSpan.FromMinutes(1)), fundinfo.ForceTick));

                //Initialize all modules
                _modules.ForEach(m => m.Initialize());
            }
            catch (Exception exc)
            {
                _log.Error(exc, $"Could not initialize quant fund with name {Name} due to error: {exc.Message}");
                Portfolio.ExceptionHandler.HandleException(exc, FundId);
                State = FundState.DeployError;
            }
        }