116 lines
4.5 KiB
C#
116 lines
4.5 KiB
C#
using Controllers.PythonInterop;
|
|
using Entities;
|
|
|
|
namespace Controllers.ProjectTest {
|
|
|
|
public class D683_Project_Test {
|
|
|
|
// TESTING Starting Money
|
|
float Money = 1000;
|
|
|
|
// TESTING STOCK HISTORY
|
|
List<PurchasedStock> StockHistory = new List<PurchasedStock>();
|
|
|
|
AIModule _aiModule;
|
|
public D683_Project_Test( AIModule aiModule ) {
|
|
_aiModule = aiModule;
|
|
}
|
|
|
|
public async Task<(float, List<PurchasedStock>)> Simulate(List<Stock> TrackedStocks) {
|
|
float StartingMoney = Money;
|
|
// Run once for each day 30 days straight
|
|
for (int i=30; i>=0; i--) {
|
|
|
|
// Check if the day is a weekend
|
|
DateTime chosenDate = DateTime.Now.AddDays(-i);
|
|
if (chosenDate.DayOfWeek == DayOfWeek.Saturday || chosenDate.DayOfWeek == DayOfWeek.Sunday) {
|
|
// If its a weekend skip
|
|
continue;
|
|
}
|
|
|
|
// Go through each watched stock and find the highest prediction
|
|
List<Task> threadpool = new List<Task>();
|
|
foreach(Stock cur in TrackedStocks) {
|
|
// Predict the trend on a new thread
|
|
Task thread = new Task(() => {
|
|
(string, float)Result = _aiModule.PredictAI(cur.Symbol, i);
|
|
// If error log it
|
|
if (!string.IsNullOrEmpty(Result.Item1)){
|
|
Console.WriteLine(Result.Item1);
|
|
}
|
|
// Write the score to the users tracked stocks
|
|
cur.Score = Result.Item2;
|
|
});
|
|
thread.Start();
|
|
threadpool.Add(thread);
|
|
}
|
|
// Wait for all processes to finish
|
|
await Task.WhenAll( threadpool );
|
|
|
|
// Get the highest ranked
|
|
Stock HighestRanking = new Stock(){ Symbol="NVDA", Score = -400 }; // Just a placeholder incase an empty list comes through there is a fallback
|
|
foreach(Stock cur in TrackedStocks) {
|
|
if (HighestRanking.Score < cur.Score) {
|
|
HighestRanking = cur;
|
|
}
|
|
}
|
|
|
|
// Sell all stocks
|
|
foreach(PurchasedStock cur in StockHistory) {
|
|
if (cur.Sold == false) {
|
|
// Get sell price
|
|
float sellPrice = cur.Quantity * _aiModule.GetCurrentPrice( cur.Symbol, i );
|
|
if (sellPrice == 0) {
|
|
// Account for holidays
|
|
goto next;
|
|
}
|
|
// Add up the total sale
|
|
Money = Money + sellPrice;
|
|
// Mark as sold
|
|
cur.Sold = true;
|
|
// Set Sell Date
|
|
cur.SellDate = DateTime.Now.AddDays(-i);
|
|
}
|
|
}
|
|
|
|
// Dont buy anything on the last run
|
|
if (i != 0) {
|
|
|
|
// Buy predicted stock
|
|
float stockPrice = _aiModule.GetCurrentPrice( HighestRanking.Symbol, i );
|
|
|
|
// Get max stocks user can purchase [ int cast truncates the decimal ]
|
|
int MaxQty = (int)( Money / stockPrice );
|
|
|
|
// Add the stock
|
|
StockHistory.Add( new PurchasedStock(){
|
|
Symbol = HighestRanking.Symbol.ToUpper(),
|
|
PurchasePrice = stockPrice,
|
|
Quantity = MaxQty,
|
|
BuyDate = DateTime.Now.AddDays(-i)
|
|
} );
|
|
Money = Money - ( stockPrice * MaxQty );
|
|
}
|
|
|
|
next:;
|
|
}
|
|
|
|
// Get price of SPY starting and end
|
|
float SPYbegin = _aiModule.GetCurrentPrice( "SPY", 30 );
|
|
float SPYend = _aiModule.GetCurrentPrice( "SPY" );
|
|
|
|
// Get max quantity for the amount of money given
|
|
int SPYQty = (int)( StartingMoney / SPYbegin );
|
|
|
|
// Calculate how much money you would end with if just stayed in the normal S&P500
|
|
float earned = StartingMoney / SPYbegin * SPYend;
|
|
|
|
// Return the difference between the AI and the SPY -> if value returned is 10. The AI did better than the SPY by $10
|
|
return (Money - earned, StockHistory);
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
} |