//Desiree' Turner Maurice //CS 307 //Program 3 #include "..\traders\dmaurice.h" #include "..\market\sim_data.h" #include //*************************************** //This function will swap the values of //the array at indexes i and j. // //*************************************** void swap(StockData *A, int i, int j) { StockData tmp; tmp = A[i]; A[i] = A[j]; A[j] = tmp; return; } //*************************************** //This function will return the max value. // //*************************************** int max(StockData *A, int i, int j) { if (A[i].TickerNumber 0;j--){ siftdown(A, j, n); } return; } //*************************************** //This function will step backwards //through the array, and call the siftdown //routine to arrange the array in the form //of a heap. //*************************************** void sortarray(StockData *A, int n) { int i=0; int max=0; int second=0; //Initialize i i = n; //Arrange the array in the form of a heap makeheap(A, i); while (i>1){ //Swap the contents of index 1 with index i swap(A, 1, i); --i; /*Call the sift down rountine to arrange the array in the form of a heap again.*/ siftdown(A, 1, i); } // return; } dmaurice1::dmaurice1( int TraderNum ) : Trader(TraderNum) { MyBroker = GetBroker(); StockOwn = -1; }; void dmaurice1::Think() { int TickerID; double lowestPrice; StockData stock; StockMarketInfo smi; MyBroker->GetStockMarketInfo ( smi ); MyBroker->GetStockInfo( 0, stock); lowestPrice = stock.CurrentPrice ; TickerID = stock.TickerNumber ; for (int i=0; iGetStockInfo(i, stock); if (lowestPrice > stock.CurrentPrice){ TickerID = stock.TickerNumber; lowestPrice = stock.CurrentPrice; } } if (TickerID == StockOwn){ StockToBuy = -1; } else { StockToBuy = TickerID; } }; int dmaurice1::Trade() { int i=0; //PGG double CashBalance=0; double CashToSpend=0; int NumStocks = 0; //AssetData **Assets = NULL; AssetData *Assets[MAX_STOCKS]; // TRADE!!! if (StockOwn > -1 && StockToBuy > -1){ MyBroker->GetWorth((AssetData*(*)[]) &Assets, &CashBalance); while (!Assets[i] && i < MAX_STOCKS) i++; //PGG if (iNumShares; //Sell existing stock. CashBalance = MyBroker->Sell (StockOwn, NumStocks); } MyBroker->ClearAssetData( (AssetData *(*)[]) &Assets); } else { CashBalance = MyBroker->GetBalance (); } if (CashBalance > 100 && StockToBuy > -1){ CashToSpend = CashBalance * .75; StockOwn = StockToBuy; CashBalance = MyBroker->Buy( StockToBuy, CashToSpend ); } return 0; }; dmaurice2::dmaurice2( int TraderNum ) : Trader(TraderNum) { MyBroker = GetBroker(); }; void dmaurice2::Think() { int NumStocksProcessed=0; StockMarketInfo smi; MyBroker->GetStockMarketInfo ( smi ); stocks = NULL; stocks = new StockData[smi.NumberOfStocks]; for (int i=0; iGetStockInfo (i, stocks[i]); cout << stocks[i].TickerNumber << endl; } //Set the nubmer of stocks that we should buy. if (smi.NumberOfStocks <4){ NumStocksToBuy = smi.NumberOfStocks; } else { NumStocksToBuy = 4; } //sort the array and we want to buy the lowest P/E Ratio stocks. sortarray (stocks, smi.NumberOfStocks); }; int dmaurice2::Trade() { double CashBalance=0; double CashToSpend=0; int NumStocks=0; int x=0; //AssetData **Assets = NULL; AssetData *Assets[MAX_STOCKS]; if (BoughtStock == true){ //Get asset information MyBroker->GetWorth((AssetData*(*)[]) &Assets, &CashBalance); //loop through owned stocks. for (x=0; xNumShares; //Sell existing stock. CashBalance = MyBroker->Sell (Assets[x]->Ticker , NumStocks); } } MyBroker->ClearAssetData((AssetData *(*)[]) &Assets); } else { CashBalance = MyBroker->GetBalance(); } BoughtStock = true; //Calculate CashToSpend = ((CashBalance - 150)/4); if (CashToSpend>15) { for (x=0; xBuy(stocks[x].TickerNumber, CashToSpend); } } return 0; }; dmaurice3::dmaurice3( int TraderNum ) : Trader(TraderNum) { MyBroker = GetBroker(); StocksBought = -1; NumStocks=0; AmountToSpendOnEach=0; }; void dmaurice3::Think() { double totalCostToSell; double totalAssets; StockMarketInfo smi; MyBroker->GetStockMarketInfo ( smi ); NumStocks = smi.NumberOfStocks; totalCostToSell = NumStocks * 16.0; totalAssets = MyBroker->GetBalance() ; AmountToSpendOnEach = ((totalAssets - totalCostToSell) / NumStocks); }; int dmaurice3::Trade() { StockData stock; StockMarketInfo smi; double CashBalance; if (StocksBought < 0) { MyBroker->GetStockMarketInfo ( smi ); for (int i=0; iGetStockInfo(i, stock); CashBalance = MyBroker->Buy ( i, AmountToSpendOnEach); } StocksBought = smi.NumberOfStocks; } return 0; };