#include "pgg1.h" #include pgg1::pgg1( int TraderNum ) : Trader(TraderNum) { cout << "Creating Trader PGG1\n"; cout.flush(); DayCount = 0; UnitCount = 0; }; void pgg1::Think() { StockMarketInfo Info; StockData std; double Delta[MAX_STOCKS]; int i; Broker *MyBroker = GetBroker(); cout << "PGG1 Thinking ($" << MyBroker->GetNetWorth() << ")\n"; cout.flush(); DayCount++; UnitCount++; if (DayCount == 5) DayCount = 0; else if (DayCount == 1) { // fill in the delta array for (i = 0; iGetStockInfo( i, std ); Delta[i] = std.CurrentPrice; } } else return; // Only think every 5th day! // do some thinking..... MyBroker->GetStockMarketInfo(Info); // clear out the decision array for (i=0; iGetStockInfo( i, std ); Delta[i] = std.CurrentPrice - Delta[i]; if (Delta[i] > BestDeal) { BestDeal = Delta[i]; BestIndex = i; } else if (Delta[i] > SecondBestDeal) { SecondBestDeal = Delta[i]; SecondIndex = i; } else if (Delta[i] > ThirdBestDeal) { ThirdBestDeal = Delta[i]; ThirdIndex = i; } } // Now Buy the 3 best deals.... WhatToDo[BestIndex] = BUY; WhatToDo[SecondIndex] = BUY; WhatToDo[ThirdIndex] = BUY; }; int pgg1::Trade() { StockMarketInfo Info; double NewBal; if (DayCount) { cout << "PGG1...No Trade\n"; cout.flush(); } else { // TRADE!!! cout << "PGG1...Trading\n"; Broker *MyBroker = GetBroker(); MyBroker->GetStockMarketInfo(Info); double CashAvailable = MyBroker->GetBalance(); double CashToSpend = CashAvailable - 100.00; CashToSpend = CashToSpend / 3.0; // Now do a little trading. for (int i=0; iBuy( i, CashToSpend ); if (NewBal <= CashToSpend) break; } }; } return 0; }; pgg2::pgg2( int TraderNum ) : Trader(TraderNum) { cout << "Creating Trader PGG2\n"; cout.flush(); DayCount = 0; UnitCount = 0; }; void pgg2::Think() { StockMarketInfo Info; AssetData ad; AssetData *MyAssets[MAX_STOCKS]; double Balance; int i; Broker *MyBroker = GetBroker(); cout << "PGG2 Thinking ($" << MyBroker->GetNetWorth() << ")\n"; cout.flush(); DayCount++; UnitCount++; if (DayCount == 3) DayCount = 0; else return; // Only think every 5th day! // do some thinking..... MyBroker->GetStockMarketInfo(Info); for (i=0; iGetWorth( (AssetData*(*)[]) &MyAssets, &Balance ); // clear out the decision array for (i=0; iNumShares <= 0) WhatToDo[i] = BUY; } // Now Sell what we own..... for (i=0; iNumShares > 0) { ad = *(MyAssets[i]); // Has it been at least 10 units since the purchase? if ((UnitCount - ad.TimeOfPurchase) > 9) { // OK, it's a candidate....what's the difference in the current // price and the price we bought it at? double PerSharePurchase = ad.PurchasePrice / (double) ad.NumShares; double ProfitOrLoss = ad.CurrentPrice - PerSharePurchase; // if the stock has increased by at least 10% - SELL!! if (ProfitOrLoss > 0 && ProfitOrLoss / PerSharePurchase > 0.05) WhatToDo[i] = SELL; // if the stock is losing money - SELL!!!! else if (ProfitOrLoss < 0 && ProfitOrLoss / PerSharePurchase < 0.03) WhatToDo[i] = SELL; } } } GetBroker()->ClearAssetData((AssetData *(*)[])&MyAssets); // PGG //for (i=0; iGetStockMarketInfo(Info); double CashAvailable = MyBroker->GetBalance(); double CashToSpend = CashAvailable / 3.0; CashToSpend = CashToSpend / Info.NumberOfStocks; // Now do a little trading. for (int i=0; iBuy( i, CashToSpend ); if (NewBal <= CashToSpend) break; } }; // Now SELL for (i=0; iSell( i ); } } } return 0; }; pgg3::pgg3( int TraderNum ) : Trader(TraderNum) { cout << "Creating Trader PGG3\n"; cout.flush(); DayCount = 0; UnitCount = 0; }; void pgg3::Think() { StockMarketInfo Info; StockData std; double BestDeals[20]; int BestDealsIndex[20]; int i; Broker *MyBroker = GetBroker(); cout << "PGG1 Thinking ($" << MyBroker->GetNetWorth() << ")\n"; cout.flush(); DayCount++; UnitCount++; if (DayCount == 25) { ; } else if (DayCount == 1) { // fill in the delta array for (i = 0; iGetStockInfo( i, std ); Delta[i] = std.CurrentPrice; } return; } else return; // Only think on the 25th day! // do some thinking..... MyBroker->GetStockMarketInfo(Info); // clear out the decision array for (i=0; iGetStockInfo( i, std ); Delta[i] = std.CurrentPrice - Delta[i]; for (int j=0; j<20; j++) if (Delta[i] > BestDeals[j]) { tmp = Delta[i]; tmpindex = i; for (int k=j; k<20; k++) { tmp2 = BestDeals[k]; tmpindex2 = BestDealsIndex[k]; BestDeals[k] = tmp; BestDealsIndex[k] = tmpindex; tmp = tmp2; tmpindex = tmpindex2; } break; }; } m_NumBestDeals = 0; // Now Buy the 20 best deals.... for (int j=0; j<20; j++) if (BestDeals[j] > 0.0) { m_NumBestDeals++; WhatToDo[BestDealsIndex[j]] = BUY; } }; int pgg3::Trade() { StockMarketInfo Info; double NewBal; if (DayCount != 25) { cout << "PGG3...No Trade\n"; cout.flush(); } else { // TRADE!!! cout << "PGG3...Trading\n"; Broker *MyBroker = GetBroker(); MyBroker->GetStockMarketInfo(Info); double CashAvailable = MyBroker->GetBalance(); double CashToSpend = CashAvailable / (double) m_NumBestDeals; //CashToSpend = CashToSpend / Info.NumberOfStocks; // Now do a little trading. for (int i=0; iBuy( i, CashToSpend ); if (NewBal <= CashToSpend) break; } }; // Now SELL for (i=0; iSell( i ); } } } return 0; };