Create the automation component
This commit is contained in:
@@ -1,22 +1,26 @@
|
|||||||
using Controllers.DataBase;
|
using Controllers.DataBase;
|
||||||
|
using Controllers.Payment;
|
||||||
|
using Controllers.PythonInterop;
|
||||||
using Entities;
|
using Entities;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace Controllers.PythonInterop {
|
namespace Controllers.Automation {
|
||||||
|
|
||||||
public class Automation {
|
public class AutomationController {
|
||||||
|
|
||||||
AIModule _aiModule;
|
AIModule _aiModule;
|
||||||
DbDriver _dbDriver;
|
DbDriver _dbDriver;
|
||||||
public Automation(AIModule aiModule, DbDriver dbDriver) {
|
IPayment _paymentProcessor;
|
||||||
|
public AutomationController(AIModule aiModule, DbDriver dbDriver, IPayment PaymentProcessor) {
|
||||||
_aiModule = aiModule;
|
_aiModule = aiModule;
|
||||||
_dbDriver = dbDriver;
|
_dbDriver = dbDriver;
|
||||||
|
_paymentProcessor = PaymentProcessor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void GlobalPredictAI() {
|
public void GlobalPredictAI() {
|
||||||
|
|
||||||
// Start this process on a background thread so its non-blocking
|
// Start this process on a background thread so its non-blocking
|
||||||
Task thread = new Task(async () => {
|
Task thread = new Task(() => {
|
||||||
|
|
||||||
// Load the userlist
|
// Load the userlist
|
||||||
List<string>? UserList = JsonConvert.DeserializeObject<List<string>>(_dbDriver.Get("Users"));
|
List<string>? UserList = JsonConvert.DeserializeObject<List<string>>(_dbDriver.Get("Users"));
|
||||||
@@ -54,6 +58,7 @@ namespace Controllers.PythonInterop {
|
|||||||
await Task.WhenAll(threadpool);
|
await Task.WhenAll(threadpool);
|
||||||
|
|
||||||
// Process Stocks -> Buy Sell Hold / Based on the global data from earlier
|
// Process Stocks -> Buy Sell Hold / Based on the global data from earlier
|
||||||
|
sellStock(username);
|
||||||
|
|
||||||
// Save the new scores to the database for reference from the UI
|
// Save the new scores to the database for reference from the UI
|
||||||
_dbDriver.Set( dbPrefix + "watched", JsonConvert.SerializeObject( VerifiedTrackedStocks ) );
|
_dbDriver.Set( dbPrefix + "watched", JsonConvert.SerializeObject( VerifiedTrackedStocks ) );
|
||||||
@@ -64,82 +69,89 @@ namespace Controllers.PythonInterop {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void GlobalTrainAI(){
|
public void GlobalTrainAI(){
|
||||||
Task thread = new Task(async () => {
|
Task thread = new Task(() => {
|
||||||
|
_aiModule.PullAI();
|
||||||
_aiModule.TrainAI();
|
_aiModule.TrainAI();
|
||||||
});
|
});
|
||||||
thread.Start();
|
thread.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
void sellStock(PurchasedStock stock){
|
void sellStock(string username){
|
||||||
string dbPrefix = $"[{userName.ToLower()}]:";
|
string dbPrefix = $"[{username.ToLower()}]:";
|
||||||
if (Session != null){
|
|
||||||
|
// Get all stock history
|
||||||
|
List<PurchasedStock>? PurchaseHistory = JsonConvert.DeserializeObject<List<PurchasedStock>>( _dbDriver.Get( dbPrefix + "history" ) );
|
||||||
|
List<PurchasedStock> VerifiedPurchaseHistory = PurchaseHistory != null ? PurchaseHistory : new List<PurchasedStock>();
|
||||||
|
|
||||||
|
// Find the stocks that need to be sold
|
||||||
|
foreach(PurchasedStock cur in VerifiedPurchaseHistory) {
|
||||||
|
if (cur.Sold == false) {
|
||||||
|
|
||||||
// Get sell price
|
// Get sell price
|
||||||
float sellPrice = stock.Quantity * aiModule.GetCurrentPrice( stock.Symbol );
|
float sellPrice = cur.Quantity * _aiModule.GetCurrentPrice( cur.Symbol );
|
||||||
|
|
||||||
|
// Try create payment session
|
||||||
|
(bool, string) createResult = _paymentProcessor.CreatePayment(username);
|
||||||
|
if (!createResult.Item1) {
|
||||||
|
Console.WriteLine("Create Payment Failure: " + createResult.Item2);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Try to sell the stock
|
// Try to sell the stock
|
||||||
(bool, string) result = PaymentProcessor.TrySell(PaymentKey, sellPrice);
|
(bool, string) paymentResult = _paymentProcessor.TrySell(createResult.Item2, sellPrice);
|
||||||
if (!result.Item1){
|
if (!paymentResult.Item1){
|
||||||
resultError = result.Item2;
|
Console.WriteLine("Process Payment Failure: " + paymentResult.Item2);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove Stock
|
|
||||||
Session.TradeHistory.Remove(stock);
|
|
||||||
dbDriver.Set( dbPrefix + "Stocks", Newtonsoft.Json.JsonConvert.SerializeObject(Session.TradeHistory) );
|
|
||||||
|
|
||||||
// Reload the users money
|
|
||||||
bool moneyLoaded = float.TryParse(dbDriver.Get( dbPrefix + "money" ), out float moneyResult);
|
|
||||||
Session.Money = moneyLoaded ? moneyResult : 1000;
|
|
||||||
|
|
||||||
// Reset the Impodent Key
|
|
||||||
result = PaymentProcessor.CreatePayment(Session.UserName);
|
|
||||||
if (!result.Item1){
|
|
||||||
resultError = "[New payment session failed] : " + result.Item2;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
PaymentKey = result.Item2;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void buyStock(string StockSymbol){
|
// Save the stock history
|
||||||
if (Session != null){
|
_dbDriver.Set( dbPrefix + "Stocks", JsonConvert.SerializeObject(VerifiedPurchaseHistory) );
|
||||||
string dbPrefix = $"[{userName.ToLower()}]:";
|
}
|
||||||
|
|
||||||
// Try Parse the quantitiy input
|
void buyStock(string username, string stockSymbol){
|
||||||
// Get the max quantity that I can Afford
|
string dbPrefix = $"[{username.ToLower()}]:";
|
||||||
float QuantityResult = 0;
|
|
||||||
|
// Get all stock history
|
||||||
|
List<PurchasedStock>? PurchaseHistory = JsonConvert.DeserializeObject<List<PurchasedStock>>( _dbDriver.Get( dbPrefix + "history" ) );
|
||||||
|
List<PurchasedStock> VerifiedPurchaseHistory = PurchaseHistory != null ? PurchaseHistory : new List<PurchasedStock>();
|
||||||
|
|
||||||
|
// Get users money
|
||||||
|
string MoneyStr = _dbDriver.Get( dbPrefix + "money");
|
||||||
|
bool Money = float.TryParse( MoneyStr, out float VerifiedMoney );
|
||||||
|
if (!Money) {
|
||||||
|
Console.WriteLine( "Unable to load users money" );
|
||||||
|
VerifiedMoney = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Get Stock Price
|
// Get Stock Price
|
||||||
float stockPrice = aiModule.GetCurrentPrice( StockSymbol );
|
float stockPrice = _aiModule.GetCurrentPrice( stockSymbol );
|
||||||
|
|
||||||
|
// Get max stocks user can purchase [ int cast truncates the decimal ]
|
||||||
|
int MaxQty = (int)( VerifiedMoney / stockPrice );
|
||||||
|
|
||||||
|
// Try create payment session
|
||||||
|
(bool, string) createResult = _paymentProcessor.CreatePayment(username);
|
||||||
|
if (!createResult.Item1) {
|
||||||
|
Console.WriteLine("Create Payment Failure: " + createResult.Item2);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Try Pay for the stock
|
// Try Pay for the stock
|
||||||
(bool, string) result = PaymentProcessor.TryPayment(PaymentKey, QuantityResult * stockPrice);
|
(bool, string) result = _paymentProcessor.TryPayment(createResult.Item2, stockPrice * MaxQty);
|
||||||
if (!result.Item1){
|
if (!result.Item1){
|
||||||
resultError = result.Item2;
|
Console.WriteLine("Process Payment Failure: " + result.Item2);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the stock
|
// Add the stock
|
||||||
Session.TradeHistory.Add( new PurchasedStock(){
|
VerifiedPurchaseHistory.Add( new PurchasedStock(){
|
||||||
Symbol = StockSymbol.ToUpper(),
|
Symbol = stockSymbol.ToUpper(),
|
||||||
PurchasePrice = stockPrice,
|
PurchasePrice = stockPrice,
|
||||||
Quantity = QuantityResult,
|
Quantity = MaxQty,
|
||||||
} );
|
} );
|
||||||
dbDriver.Set( dbPrefix + "Stocks", Newtonsoft.Json.JsonConvert.SerializeObject(Session.TradeHistory) );
|
_dbDriver.Set( dbPrefix + "Stocks", JsonConvert.SerializeObject(VerifiedPurchaseHistory) );
|
||||||
|
|
||||||
// Reload the users money
|
|
||||||
bool moneyLoaded = float.TryParse(dbDriver.Get( dbPrefix + "money" ), out float moneyResult);
|
|
||||||
Session.Money = moneyLoaded ? moneyResult : 1000;
|
|
||||||
|
|
||||||
// Reset the Impodent Key
|
|
||||||
result = PaymentProcessor.CreatePayment(Session.UserName);
|
|
||||||
if (!result.Item1){
|
|
||||||
resultError = "[New payment session failed] : " + result.Item2;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
PaymentKey = result.Item2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,7 +8,7 @@ namespace Controllers.Payment {
|
|||||||
// [ Success, ErrorMessage ]
|
// [ Success, ErrorMessage ]
|
||||||
public (bool, string) TryPayment(string ImpodentKey, float Price);
|
public (bool, string) TryPayment(string ImpodentKey, float Price);
|
||||||
|
|
||||||
//
|
// [ Success, ErrorMessage ]
|
||||||
public (bool, string) TrySell(string ImpodentKey, float Price);
|
public (bool, string) TrySell(string ImpodentKey, float Price);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ namespace Controllers.PythonInterop {
|
|||||||
string _PyPath = "";
|
string _PyPath = "";
|
||||||
string _ExecPath = "";
|
string _ExecPath = "";
|
||||||
|
|
||||||
public AIModule() {
|
public AIModule(string pypath = "/usr/bin/python3.11") {
|
||||||
_PyPath = "/usr/bin/python3.11";
|
_PyPath = pypath;
|
||||||
_ExecPath = $"{Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AIPython")}";
|
_ExecPath = $"{Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AIPython")}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace Entities {
|
|||||||
public float Quantity { get; set; } = 0;
|
public float Quantity { get; set; } = 0;
|
||||||
public float PurchasePrice { get; set; } = 0;
|
public float PurchasePrice { get; set; } = 0;
|
||||||
public float SellPrice { get; set; } = 0;
|
public float SellPrice { get; set; } = 0;
|
||||||
|
public bool Sold = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user