Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Portfolio"/> class.
        /// </summary>
        /// <param name="clock">The clock.</param>
        /// <param name="actionscheduler">The actionscheduler.</param>
        /// <param name="brokerconnection">The brokerconnection.</param>
        /// <param name="brokermodel">The brokermodel.</param>
        /// <param name="currency">The currency.</param>
        /// <param name="eventrunner">The eventrunner.</param>
        /// <param name="exceptionhandler">The exceptionhandler.</param>
        /// <param name="orderTicketHandler">The order ticket handler.</param>
        /// <param name="brokeraccount">The brokeraccount.</param>
        /// <param name="cashmanager">The cashmanager.</param>
        /// <param name="runmode">The runmode.</param>
        /// <param name="datafeed">The datafeed.</param>
        /// <param name="benchmark">The benchmark.</param>
        /// <param name="id">The identifier.</param>
        public Portfolio(WorldClock clock, ActionsScheduler actionscheduler, BrokerConnection brokerconnection, BrokerModel brokermodel,
                         Currency currency, EventRunner eventrunner, ExceptionHandler exceptionhandler, OrderTicketHandler orderTicketHandler,
                         BrokerAccount brokeraccount, CashManager cashmanager, RunMode runmode, DataFeed datafeed, Benchmark benchmark, string id = "")
        {
            //Set references
            ActionsScheduler   = actionscheduler;
            BrokerAccount      = brokeraccount;
            BrokerConnection   = brokerconnection;
            BrokerModel        = brokermodel;
            Clock              = clock;
            Currency           = currency;
            EventRunner        = eventrunner;
            ExceptionHandler   = exceptionhandler;
            CashManager        = cashmanager;
            OrderTicketHandler = orderTicketHandler;
            _porfolioBenchmark = benchmark;

            //Set initial items
            Id            = id;
            IsBacktesting = runmode == RunMode.Backtester;
            OrderFactory  = new OrderFactory(this, BrokerModel);
            OrderTracker  = new OrderTracker(this);
            Subscription  = new DataSubscriptionManager(datafeed, CashManager);
            Results       = new Result(0, _porfolioBenchmark);

            //Portfolio benchmark is not used
            benchmark.OnCalc(x => 0);
        }
Example #2
0
 /// <summary>
 /// Sets the dependencies.
 /// </summary>
 /// <param name="securities">The securities.</param>
 /// <param name="datasubscriptionmanager">The data subscription manager.</param>
 /// <param name="datafilter"></param>
 public void SetDependencies(SecurityTracker securities, DataSubscriptionManager datasubscriptionmanager, DataFilter datafilter)
 {
     Securities = securities;
     DataSubscriptionManager = datasubscriptionmanager;
     DataFilter = datafilter;
 }
Example #3
0
        /// <summary>
        /// Get available ticks, base implementation
        /// </summary>
        /// <returns></returns>
        public virtual IEnumerable <DataFeedPacket> GetAvailableDataPackets()
        {
            //Default values
            DateTime time     = DateTime.MinValue;
            TimeSpan timespan = TimeSpan.FromMilliseconds(10); //Combine all data from the last 10 milliseconds
            Dictionary <TickerSymbol, DataFeedPacket> datapackets = new Dictionary <TickerSymbol, DataFeedPacket>();

            //Check for new data
            while (CurrenDatapoints.Count > 0)
            {
                if (CurrenDatapoints.TryPeek(out DataPoint item))
                {
                    //Get current moment in time
                    if (time == DateTime.MinValue)
                    {
                        time = item.OccuredUtc + timespan;
                    }
                    else if (time < item.OccuredUtc)
                    {
                        break;                  //Not the same timespan
                    }
                    //Get item
                    if (CurrenDatapoints.TryDequeue(out item))
                    {
                        //Get security
                        Security security;
                        if (Securities != null)
                        {
                            security = Securities[item.Ticker];
                            if (security is UnknownSecurity)
                            {
                                _log.Warn($"Received unknown security with ticker {security.Ticker}");
                                continue;
                            }
                        }
                        else
                        {
                            security = new UnknownSecurity(item.Ticker.Name);
                        }

                        //Check if we are allowed to accept this data point
                        if (!DataFilter.Accept(security, item))
                        {
                            continue;
                        }

                        //Get data
                        LastDataReceivedUtc = item.OccuredUtc;
                        if (datapackets.TryGetValue(item.Ticker, out DataFeedPacket packet))
                        {
                            packet.Add(item);
                        }
                        else
                        {
                            datapackets.Add(item.Ticker, new DataFeedPacket(security, DataSubscriptionManager?.GetDataSubscriptions(item.Ticker, item.DataType), item));
                        }
                    }
                }
                else
                {
                    break;                      //No items
                }
            }

            //Wait for new data if we do not have any
            if (datapackets.Count == 0)
            {
                Thread.Sleep(_waittimeinms);
            }

            //Return what we have
            return(datapackets.Values);
        }