Example #1
0
        /// <summary>
        /// Simulate a day in the market.
        /// </summary>
        public void Simulate()
        {
            if (CurrentDate != EndDate)
            {
                CurrentDate = CurrentDate.AddDays(1);
                StockDay    = DataManager.IsStockDay(CurrentDate);

                // before we even bother with any calculations, we need to
                // make sure the stock market was open on this given day
                if (StockDay)
                {
                    UpdateMarketState();
                    StockDays.Add(CurrentDate);
                    Log("Stockday on " + CurrentDate + ", proceeding...");

                    // how has our equity changed over postmarket and premarket
                    for (int i = 0; i < Positions.Count; i++)
                    {
                        List <BarData> history = MarketState.FirstOrDefault(x => x.Key == Positions[i].Symbol).Value;

                        // how much has the stock changed in value overnight?
                        double change = history.Last().Open - history[history.Count - 2].Close;

                        // change our equity!
                        Equity += change * Positions[i].Shares;
                    }

                    // process orders
                    ProcessAllOrders();

                    // with our new positions, how will our equity change today?
                    for (int i = 0; i < Positions.Count; i++)
                    {
                        List <BarData> history = MarketState.FirstOrDefault(x => x.Key == Positions[i].Symbol).Value;

                        // how much has the stock changed in value overnight?
                        double change = history.Last().Close - history.Last().Open;

                        // change our equity!
                        Equity += change * Positions[i].Shares;

                        // update the position value
                        Positions[i].PriceHistory.Add(new Tuple <DateTime, double, double>(CurrentDate, history.Last().Close, change));
                    }

                    // allow the strategy to make calls for the next day
                    Strategy.Update(this);

                    // process all logs for the day
                    ProcessLogs();
                }

                EquityHistory.Add(new Tuple <DateTime, double>(CurrentDate, Equity));
                BuyingPowerHistory.Add(new Tuple <DateTime, double>(CurrentDate, BuyingPower));
            }
            else
            {
                throw new Exception("Simulation is already complete.");
            }
        }
Example #2
0
 public Account(IStrategy strategy, DateTime startDate, DateTime endDate, double equity, bool halfBake)
 {
     Strategy      = strategy;
     StartDate     = startDate;
     CurrentDate   = startDate;
     EndDate       = endDate;
     InitialEquity = equity;
     BuyingPower   = equity;
     Equity        = equity;
     HalfBake      = halfBake;
     EquityHistory.Add(new Tuple <DateTime, double>(startDate, equity));
     BuyingPowerHistory.Add(new Tuple <DateTime, double>(startDate, BuyingPower));
     ID = new Random().Next(1000000, 9999999);
 }
Example #3
0
            internal void Update(DateTime time, double equity)
            {
                if (IsFirstRecord)
                {
                    StartingEquity = equity;
                }

                IsFirstRecord = false;
                EquityHistory.Add(new IndicatorDataPoint(time, equity));
                BalancePeak = equity > BalancePeak ? equity : BalancePeak;
                if (BalancePeak - equity > MaxDrowDown)
                {
                    MaxDrowDown = BalancePeak - equity;
                }

                Profit = equity - StartingEquity;
                if (MaxDrowDown != 0)
                {
                    ProfitOverMaxDrowDown = Profit / MaxDrowDown;
                }
            }