protected int IndexOf(Stock stock, Stock[] market) { int idx = -1; for (int j = 0; j < market.Length; j++) { if ((stock.CODE == market[j].CODE) && (stock.NAME == market[j].NAME)) { idx = j; break; } } return idx; }
public override void SellStocks(Stock[] marketYesterday, Stock[] marketToday) { bool sell; int idx; for (int i = 0; i < stocks.Count; ++i) { idx = IndexOf(stocks[i], marketYesterday); if (idx < 0) continue; sell = (marketYesterday[idx].PRICE > stocks[i].UPPER_POINT || marketYesterday[idx].PRICE < stocks[i].LOWER_POINT); if (!sell) // keep { idx = IndexOf(stocks[i], marketToday); stocks[i].PRICE = marketToday[idx].PRICE; } else // sell { currency += marketYesterday[idx].PRICE * stocks[i].NUMBER; stocks[i].NUMBER = 0; stocks[i].PRICE = 0.0; } } totalAssets = TotalAssets(); }
static void Main(string[] args) { Investigator[] investigators = new Investigator[2]; List<StockOwnedA> stocksA = new List<StockOwnedA>(); stocksA.Add(new StockOwnedA(2357, "華碩", 326.5, 4, 325.0, 305.0)); stocksA.Add(new StockOwnedA(2891, "中信金", 19.25, 1, 19.4, 19.2)); investigators[0] = new InvestigatorAType("柯玟轍", 50000.0, stocksA); List<StockOwned> stocksB = new List<StockOwned>(); stocksB.Add(new StockOwned(2454, "聯發科", 500.0, 2)); stocksB.Add(new StockOwned(2357, "華碩", 326.5, 2)); stocksB.Add(new StockOwned(2888, "新光金", 9.31, 1)); investigators[1] = new InvestigatorBType("連聖雯", 50000.0, stocksB); double[,] stockPrices = new double[3,4] { {500.0, 326.5, 9.31, 19.25}, {509.0, 325.5, 9.30, 19.25}, {510.0, 325.0, 9.34, 19.45} }; // [3,4] 改成 [60,4] Stock[] marketYesterday = new Stock[4]; Stock[] marketToday = new Stock[4]; Console.WriteLine("各投資人起始資產"); Console.WriteLine(); int i; for(i = 0; i < investigators.Length; i++) { investigators[i].PrintAssets(); Console.WriteLine(); } for(int day = 1; day < 3; day++) { Console.WriteLine("day {0}", day); marketYesterday[0] = new Stock(2454, "聯發科", stockPrices[day-1, 0]); marketYesterday[1] = new Stock(2357, "華碩", stockPrices[day-1, 1]); marketYesterday[2] = new Stock(2888, "新光金", stockPrices[day-1, 2]); marketYesterday[3] = new Stock(2891, "中信金", stockPrices[day-1, 3]); marketToday[0] = new Stock(2454, "聯發科", stockPrices[day, 0]); marketToday[1] = new Stock(2357, "華碩", stockPrices[day, 1]); marketToday[2] = new Stock(2888, "新光金", stockPrices[day, 2]); marketToday[3] = new Stock(2891, "中信金", stockPrices[day, 3]); for(i = 0; i < investigators.Length; i++) { investigators[i].SellStocks(marketYesterday, marketToday); investigators[i].PrintAssets(); Console.WriteLine(); } Console.WriteLine(); } Console.ReadLine(); }
public abstract void SellStocks(Stock[] marketYesterday, Stock[] marketToday);
public override void SellStocks(Stock[] marketYesterday, Stock[] marketToday) { int decision; int idx; for (int i = 0; i < stocks.Count; ++i) { decision = rand.Next() % 2; if (decision == 0) // keep { idx = IndexOf(stocks[i], marketToday); if(idx >= 0) { stocks[i].PRICE = marketToday[idx].PRICE; } } else // sell { idx = IndexOf(stocks[i], marketYesterday); if( idx >= 0 ) { currency += marketYesterday[idx].PRICE * stocks[i].NUMBER; stocks[i].NUMBER = 0; stocks[i].PRICE = 0.0; } } } totalAssets = TotalAssets(); }