Example #1
0
        internal static void Update(TradingDay tradingDay, ISecurityRepository securityRepository)
        {
            CheckInitialized(securityRepository);

            if (Prices.Count == 0)
            {
                foreach (var symbol in Symbols)
                {
                    Prices.Add(symbol, 0);
                }
            }

            if (LastChanges.Count == 0)
            {
                foreach (var symbol in Symbols)
                {
                    LastChanges.Add(symbol, 0);
                }
            }

            foreach (var symbol in Symbols)
            {
                var effect = tradingDay.Effects[symbol];

                Prices[symbol] += effect;

                if (tradingDay.Day == 0)
                {
                    continue;
                }

                LastChanges[symbol] = effect;
            }
        }
Example #2
0
        /// <summary>
        /// Method to close the market.
        /// </summary>
        /// <param name="tradingDay"></param>
        /// <param name="simulationState"></param>
        /// <returns></returns>
        async Task CloseMarketAsync(TradingDay tradingDay, SimulationState simulationState)
        {
            _timer.Stop();
            SimulationState = simulationState;

            AppSettings.UpdateReportsEnabled(true);

            await UpdateMarketAsync(tradingDay, true);
        }
        public List <TradingDay> GetPriceHistory(string symbol, DateTime startdate, DateTime endDate)
        {
            if (startdate > DateTime.Now ||
                endDate > DateTime.Now)
            {
                throw new InvalidDateException();
            }

            string      url        = string.Format("https://api.tdameritrade.com/v1/marketdata/{0}/pricehistory", symbol.ToUpper());
            var         client     = new RestClient(url);
            RestRequest getRequest = new RestRequest(Method.GET);

            getRequest.AddHeader("cache-control", "no-cache");
            getRequest.AddHeader("content-type", "application/x-www-form-urlencoded");
            getRequest.AddParameter("apikey", ConfigurationManager.AppSettings.Get("apikey"));
            getRequest.AddParameter("periodType", "month");
            getRequest.AddParameter("period", "1");
            getRequest.AddParameter("frequencyType", "daily");
            getRequest.AddParameter("frequency", "1");
            getRequest.AddParameter("startDate", getEpochTime(startdate).ToString().PadRight(13, '0'));
            getRequest.AddParameter("endDate", getEpochTime(endDate).ToString().PadRight(13, '0'));

            var response = client.Get(getRequest);

            JObject        priceHistorySearch = JObject.Parse(response.Content);
            IList <JToken> candles            = new List <JToken>();

            try
            {
                candles = priceHistorySearch["candles"].Children().ToList();
            }
            catch (Exception)
            {
                return(new List <TradingDay>());
            }

            List <TradingDay> priceHistory = new List <TradingDay>();

            foreach (var candle in candles)
            {
                TradingDay tradingDay = new TradingDay()
                {
                    Volume     = (int)candle["volume"],
                    OpenPrice  = (decimal)candle["open"],
                    HighPrice  = (decimal)candle["high"],
                    LowPrice   = (decimal)candle["low"],
                    ClosePrice = (decimal)candle["close"],
                    Date       = convertFromEpoch((string)candle["datetime"])
                };

                priceHistory.Add(tradingDay);
            }

            return(priceHistory);
        }
Example #4
0
        public TradingDayDto Get(DateTime date)
        {
            TradingDay tradingDay = _repository.FirstOrDefault(x => x.Day.Year == date.Year && x.Day.Month == date.Month && x.Day.Day == date.Day);

            if (tradingDay == null)
            {
                tradingDay     = new TradingDay();
                tradingDay.Day = date.Date;
                this._repository.Insert(tradingDay);
            }

            return(tradingDay.MapTo <TradingDayDto>());
        }
Example #5
0
        public void Add(MarketLogEntryDto dto)
        {
            TradingDay tradingDay = _tradingDayRepository.FirstOrDefault(x => x.Day.Year == dto.TimeStamp.Year && x.Day.Month == dto.TimeStamp.Month && x.Day.Day == dto.TimeStamp.Day);

            if (tradingDay == null)
            {
                tradingDay     = new TradingDay();
                tradingDay.Day = dto.TimeStamp.Date;
                this._tradingDayRepository.Insert(tradingDay);
            }
            dto.TradingDayId = tradingDay.Id;

            this._marketLogEntryRepository.Insert(dto.MapTo <MarketLogEntry>());
        }
Example #6
0
        async Task UpdateMarketAsync(TradingDay tradingDay, bool close = false)
        {
            TickerViewModel.Update(tradingDay, _securityRepository, close);
            MiniTickerPartialViewModel.Update(tradingDay, _securityRepository);

            await _pusher.TriggerAsync(
                "stockimulate",
                "update-market",
                new
            {
                day     = _dayNumber,
                news    = tradingDay.NewsItem,
                effects = _securities.Select(security => tradingDay.Effects[security.Symbol]).ToArray(),
                close
            });
        }
        internal static void Update(TradingDay tradingDay, ISecurityRepository securityRepository, bool close = false)
        {
            CheckInitialized(securityRepository);

            if (tradingDay.Day == 0)
            {
                foreach (var symbol in _symbols)
                {
                    Prices[symbol].Add(tradingDay.Effects[symbol]);
                }
            }
            else
            {
                foreach (var symbol in _symbols)
                {
                    Prices[symbol].Add(Prices[symbol].Last() + tradingDay.Effects[symbol]);
                    LastChange[symbol] = tradingDay.Effects[symbol];
                }
            }

            var newsItem = tradingDay.NewsItem;

            if (newsItem != string.Empty)
            {
                News = newsItem;
            }

            if (close)
            {
                MarketStatus = "CLOSED";
            }

            else if (tradingDay.Day != 0)
            {
                ++Day;
            }
        }
Example #8
0
        public static void ShowTradeResults(TradingDay day, TradingDay previousDay, Assets assets)
        {
            var results = EnumerableExtensions.Zip(
                day.Companies,
                previousDay.Companies,
                assets.Portfolio,
                (company, previous, shares) =>
                (
                    stockSymbol: company.StockSymbol,
                    price: company.SharePrice,
                    shares,
                    value: shares * company.SharePrice,
                    change: company.SharePrice - previous.SharePrice
                )).ToList();

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("**********     END OF DAY'S TRADING     **********");
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("STOCK\tPRICE/SHARE\tHOLDINGS\tVALUE\tNET PRICE CHANGE");
            foreach (var result in results)
            {
                Console.WriteLine($"{result.stockSymbol}\t{result.price}\t\t{result.shares}\t\t{result.value:0.00}\t\t{result.change:0.00}");
            }

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            var averagePrice       = day.AverageSharePrice;
            var averagePriceChange = averagePrice - previousDay.AverageSharePrice;

            Console.WriteLine($"NEW YORK STOCK EXCHANGE AVERAGE: {averagePrice:0.00} NET CHANGE {averagePriceChange:0.00}");
            Console.WriteLine();
        }
Example #9
0
        public override int GetHashCode()
        {
            int hash = 1;

            if (TradingDay.Length != 0)
            {
                hash ^= TradingDay.GetHashCode();
            }
            if (LoginTime.Length != 0)
            {
                hash ^= LoginTime.GetHashCode();
            }
            if (BrokerId.Length != 0)
            {
                hash ^= BrokerId.GetHashCode();
            }
            if (UserId.Length != 0)
            {
                hash ^= UserId.GetHashCode();
            }
            if (SystemName.Length != 0)
            {
                hash ^= SystemName.GetHashCode();
            }
            if (FrontId != 0)
            {
                hash ^= FrontId.GetHashCode();
            }
            if (SessionId != 0)
            {
                hash ^= SessionId.GetHashCode();
            }
            if (MaxOrderRef.Length != 0)
            {
                hash ^= MaxOrderRef.GetHashCode();
            }
            if (ShfeTime.Length != 0)
            {
                hash ^= ShfeTime.GetHashCode();
            }
            if (DceTime.Length != 0)
            {
                hash ^= DceTime.GetHashCode();
            }
            if (CzceTime.Length != 0)
            {
                hash ^= CzceTime.GetHashCode();
            }
            if (FfexTime.Length != 0)
            {
                hash ^= FfexTime.GetHashCode();
            }
            if (IneTime.Length != 0)
            {
                hash ^= IneTime.GetHashCode();
            }
            if (_unknownFields != null)
            {
                hash ^= _unknownFields.GetHashCode();
            }
            return(hash);
        }
Example #10
0
        public override int GetHashCode()
        {
            int hash = 1;

            if (BrokerId.Length != 0)
            {
                hash ^= BrokerId.GetHashCode();
            }
            if (InvestorId.Length != 0)
            {
                hash ^= InvestorId.GetHashCode();
            }
            if (InstrumentId.Length != 0)
            {
                hash ^= InstrumentId.GetHashCode();
            }
            if (OrderRef.Length != 0)
            {
                hash ^= OrderRef.GetHashCode();
            }
            if (UserId.Length != 0)
            {
                hash ^= UserId.GetHashCode();
            }
            if (TradeId.Length != 0)
            {
                hash ^= TradeId.GetHashCode();
            }
            if (Direction != 0)
            {
                hash ^= Direction.GetHashCode();
            }
            if (OffsetFlag != 0)
            {
                hash ^= OffsetFlag.GetHashCode();
            }
            if (HedgeFlag != 0)
            {
                hash ^= HedgeFlag.GetHashCode();
            }
            if (Price != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(Price);
            }
            if (Volume != 0)
            {
                hash ^= Volume.GetHashCode();
            }
            if (TradeDate.Length != 0)
            {
                hash ^= TradeDate.GetHashCode();
            }
            if (TradeTime.Length != 0)
            {
                hash ^= TradeTime.GetHashCode();
            }
            if (OrderLocalId.Length != 0)
            {
                hash ^= OrderLocalId.GetHashCode();
            }
            if (TradingDay.Length != 0)
            {
                hash ^= TradingDay.GetHashCode();
            }
            if (BrokerOrderSeq != 0)
            {
                hash ^= BrokerOrderSeq.GetHashCode();
            }
            if (_unknownFields != null)
            {
                hash ^= _unknownFields.GetHashCode();
            }
            return(hash);
        }
Example #11
0
        public override int GetHashCode()
        {
            int hash = 1;

            if (TradingDay.Length != 0)
            {
                hash ^= TradingDay.GetHashCode();
            }
            if (InstrumentId.Length != 0)
            {
                hash ^= InstrumentId.GetHashCode();
            }
            if (ExchangeId.Length != 0)
            {
                hash ^= ExchangeId.GetHashCode();
            }
            if (LastPrice != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(LastPrice);
            }
            if (PreSettlementPrice != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(PreSettlementPrice);
            }
            if (PreClosePrice != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(PreClosePrice);
            }
            if (PreOpenInterest != 0)
            {
                hash ^= PreOpenInterest.GetHashCode();
            }
            if (OpenPrice != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(OpenPrice);
            }
            if (HighestPrice != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(HighestPrice);
            }
            if (LowestPrice != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(LowestPrice);
            }
            if (Volume != 0)
            {
                hash ^= Volume.GetHashCode();
            }
            if (TurnOver != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(TurnOver);
            }
            if (OpenInterest != 0)
            {
                hash ^= OpenInterest.GetHashCode();
            }
            if (ClosePrice != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(ClosePrice);
            }
            if (SettlementPrice != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(SettlementPrice);
            }
            if (UpperLimitPrice != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(UpperLimitPrice);
            }
            if (LowerLimitPrice != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(LowerLimitPrice);
            }
            if (UpdateTime.Length != 0)
            {
                hash ^= UpdateTime.GetHashCode();
            }
            if (UpdateMillisec != 0)
            {
                hash ^= UpdateMillisec.GetHashCode();
            }
            if (AveragePrice != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(AveragePrice);
            }
            if (ActionDay.Length != 0)
            {
                hash ^= ActionDay.GetHashCode();
            }
            if (BidPrice != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(BidPrice);
            }
            if (BidVolume != 0)
            {
                hash ^= BidVolume.GetHashCode();
            }
            if (AskPrice != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(AskPrice);
            }
            if (AskVolume != 0)
            {
                hash ^= AskVolume.GetHashCode();
            }
            if (_unknownFields != null)
            {
                hash ^= _unknownFields.GetHashCode();
            }
            return(hash);
        }