Beispiel #1
0
        /// <summary>
        /// This only works if openTime is only ever called sequentially
        /// </summary>
        /// <param name="symbol"></param>
        /// <param name="openTime"></param>
        /// <returns></returns>
        public PortfolioBacktestBar GetAssetExposureBar(string symbol, DateTime openTime)
        {
#if DEBUG
            if (symbol == null)
            {
                throw new ArgumentNullException(nameof(symbol));
            }
#endif
            List <PortfolioBacktestBar> list = assetExposureBars.GetOrAdd(symbol, s => new List <PortfolioBacktestBar>());
            PortfolioBacktestBar        last = null;
            if (list.Count > 0)
            {
                last = list.Last();
                if (last.OpenTime == openTime)
                {
                    return(last);
                }
#if DEBUG
                if (openTime < last.OpenTime)
                {
                    throw new InvalidOperationException("Attempt to get older bar via GetAssetExposureBar, which can only return current bar or a new bar");
                }
#endif
            }
            var result = new PortfolioBacktestBar(openTime, last == null ? 0 : last.Close);
            list.Add(result);
            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Call this at start of OpenBar, or after end of simulation
        /// </summary>
        public void CloseBar()
        {
            if (UseEquity)
            {
                lastEquityBar = equityBar;
                if (lastEquityBar != null)
                {
                    equityBars.Add(lastEquityBar);
                }
#if DEBUG
                if (Options.JournalLevel >= 2)
                {
                    Console.WriteLine("Eq: " + lastEquityBar);
                }
#endif
            }

            if (UseBalance)
            {
                lastBalanceBar = balanceBar;
                if (lastBalanceBar != null)
                {
                    balanceBars.Add(lastBalanceBar);
                }
#if DEBUG
                if (Options.JournalLevel >= 2)
                {
                    Console.WriteLine("Bal: " + lastBalanceBar);
                }
#endif
            }

            if (Options.ComponentExposureBars)
            {
                foreach (var component in Sim.Portfolio.Components)
                {
                    component.OpenBar(openTime);
                }
            }
        }
Beispiel #3
0
        public void OpenBar()
        {
            if (openTime == default)
            {
                log.LogWarning("Options.StartTime == default, not running simulation for portfolio " + Sim?.Portfolio?.ToString());
                openTime = Options.EndTime;
                return;
            }

            bool firstBar;

            if (openTime > startDate)
            {
                firstBar = false;
                CloseBar();
            }
            else
            {
                firstBar = true;
            }
            if (UseEquity)
            {
                equityBar = new PortfolioBacktestBar(openTime, firstBar ? Sim.Options.InitialBalance : lastEquityBar.Close);
            }
            if (UseBalance)
            {
                balanceBar = new PortfolioBacktestBar(openTime, firstBar ? Sim.Options.InitialBalance : lastBalanceBar.Close);
            }

            barEndTime = Options.TimeFrame.GetNextOpenTimeForOpen(openTime);

            if (Options.ComponentExposureBars)
            {
                foreach (var component in Sim.Portfolio.Components)
                {
                    component.OpenBar(openTime);
                }
            }
        }