public async void Run(StockTradeList tradeList) { if (tradeList != null && tradeList.Trades.Count > 0) { //find all stocks traded StockList stocks = new StockList("TradeList"); foreach (StockTrade trade in tradeList.Trades) { stocks.Add(trade.Stock); } //find DataRange StockData firstData = await StockDataBase.Get(stocks.GetStock(0), tradeList.Interval); int startIndex = firstData.FindDateIndex(_startDate); int stopIndex = firstData.FindDateIndex(_endDate); int dataCount = firstData.TimeSeries.DataPoints.Count; if (stopIndex == 0) { stopIndex = dataCount - 1; } //Simulate trades double startBalance = _balanceChart[0]; StockPortfolio portfolio = new StockPortfolio(startBalance); List <double> balanceChartList = new List <double>(); balanceChartList.Add(startBalance); for (int i = startIndex; i <= stopIndex; i++) { foreach (StockTrade trade in tradeList.Trades) { if (trade.DataIndex == i) { portfolio.MakeTrade(trade, tradeList.Interval); } } balanceChartList.Add(await portfolio.TotalBalance(i, tradeList.Interval)); } _balanceChart = balanceChartList.ToArray(); } }
public static Stock GetFromRecentList(string stockName) { return(_recentList.GetStock(stockName)); }
public async void Run(StockTradeList tradeList) { if (tradeList != null && tradeList.Trades.Count > 0) { //find all stocks traded StockList stocks = new StockList("TradeList"); foreach (StockTrade trade in tradeList.Trades) { stocks.Add(trade.Stock); } //find DataRange StockData firstData = await StockDataBase.Get(stocks.GetStock(0), tradeList.Interval); int startIndex = firstData.FindDateIndex(_startDate); int stopIndex = firstData.FindDateIndex(_endDate); int dataCount = firstData.TimeSeries.DataPoints.Count; if (stopIndex == 0) { stopIndex = dataCount - 1; } //Simulate trades double timesTraded = 0; double timesRight = 0; StockTrade currentTrade = new StockTrade(); StockData data = firstData; for (int i = startIndex; i <= stopIndex; i++) { foreach (StockTrade trade in tradeList.Trades) { if (trade.DataIndex == i) { if (trade.PortfolioPercentage > 0) { data = await StockDataBase.Get(trade.Stock, tradeList.Interval); currentTrade = trade; } else { timesTraded++; double openPrice = data.TimeSeries.DataPoints[currentTrade.DataIndex].Close; double closePrice = data.TimeSeries.DataPoints[i].Close; if (currentTrade.Type == StockTradeType.LongStock && closePrice > openPrice) { timesRight++; } if (currentTrade.Type == StockTradeType.ShortStock && closePrice < openPrice) { timesRight++; } } } } } //save results _rightPredictPercentage = timesRight / timesTraded; _tradesMade = (int)timesTraded; } }