203 lines
6.7 KiB
Plaintext
203 lines
6.7 KiB
Plaintext
@page "/"
|
|
@rendermode InteractiveServer
|
|
|
|
<PageTitle>Home</PageTitle>
|
|
|
|
<div class="main-area">
|
|
|
|
<!-- Login Frame -->
|
|
@if (Session == null){
|
|
<div class="gridFrame">
|
|
<div><h2>LOGIN</h2></div>
|
|
<div class="loginRow">
|
|
<div><span>username</span></div>
|
|
<input @bind-value="userName" type="text" />
|
|
</div>
|
|
<div class="loginRow">
|
|
<div><span>password</span></div>
|
|
<input @bind-value="passWord" type="password" />
|
|
</div>
|
|
<div class="loginRow">
|
|
<button @onclick="LoginSession">Login</button>
|
|
<button @onclick="registerSession">Register</button>
|
|
</div>
|
|
<div>
|
|
<span style="color: red;">@loginError</span>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- User Frame -->
|
|
}else{
|
|
<div class="gridFrame">
|
|
<span>UserName: @Session.UserName</span><br />
|
|
<div>
|
|
@foreach(PurchasedStock cur in Session.TrackedStocks){
|
|
<div>@cur.Symbol</div>
|
|
<div>@cur.Quantity</div>
|
|
<div>@cur.PurchasePrice</div>
|
|
<div>@cur.PurchaseDate.ToString("M-D-YYYY")</div>
|
|
}
|
|
</div>
|
|
</div>
|
|
<!-- Tool Frame -->
|
|
<div class="gridFrame">
|
|
<div>
|
|
<span>Actions</span>
|
|
<button @onclick="pull">@pullButtonText</button>
|
|
<button @onclick="train">@trainButtonText</button>
|
|
<button @onclick="predict">@predictButtonText</button>
|
|
</div>
|
|
</div>
|
|
}
|
|
|
|
<!-- AI Frame -->
|
|
<div class="gridFrame">
|
|
<div>
|
|
|
|
</div>
|
|
<div>
|
|
@foreach (stockPredictionPair cur in predictions){
|
|
<div>
|
|
<h1>Symbol: @cur.Symbol</h1><br />
|
|
@if (cur.Movement == -1){
|
|
<p>Sell</p>
|
|
} else if (cur.Movement == 1){
|
|
<p>Buy</p>
|
|
} else{
|
|
<p>Hold</p>
|
|
}
|
|
</div>
|
|
}
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
@code {
|
|
|
|
// User Stuff
|
|
loginSession? Session = null;
|
|
|
|
// Login Stuff
|
|
string userName = "";
|
|
string passWord = "";
|
|
string loginError = "";
|
|
|
|
List<stockPredictionPair> predictions = new List<stockPredictionPair>(){
|
|
new stockPredictionPair(){ Symbol = "AAPL" },
|
|
new stockPredictionPair(){ Symbol = "AAPL" },
|
|
new stockPredictionPair(){ Symbol = "TTD" },
|
|
new stockPredictionPair(){ Symbol = "FUN" }
|
|
};
|
|
|
|
async Task LoginSession(){
|
|
string dbPrefix = $"[{userName.ToLower()}]:"; // Set the DB prefix for the get and set
|
|
string passwordhash = dbDriver.Get( dbPrefix + "password" ); // Pull the password hash
|
|
if (BCrypt.Verify( passWord, passwordhash )){ // If the password is valid
|
|
List<PurchasedStock>? stocks = JsonConvert.DeserializeObject<List<PurchasedStock>>( dbDriver.Get( dbPrefix + "stock-data" ) );
|
|
Session = new loginSession(){
|
|
UserName = userName.ToLower(),
|
|
TrackedStocks = stocks != null ? stocks : new List<PurchasedStock>()
|
|
};
|
|
}else{
|
|
loginError = "wrong password";
|
|
}
|
|
}
|
|
|
|
async Task registerSession(){
|
|
string dbPrefix = $"[{userName.ToLower()}]:";
|
|
string passwordhash = dbDriver.Get( dbPrefix + "password" );
|
|
if (string.IsNullOrEmpty(passwordhash)){
|
|
dbDriver.Set( dbPrefix + "password", BCrypt.HashPassword( passWord, BCrypt.GenerateSalt() ) );
|
|
Session = new loginSession(){
|
|
UserName = userName.ToLower(),
|
|
TrackedStocks = new List<PurchasedStock>()
|
|
};
|
|
}else{
|
|
loginError = "account is taken";
|
|
}
|
|
}
|
|
|
|
// AI Stuff
|
|
|
|
string pullButtonText = "Pull Data";
|
|
string trainButtonText = "Train AI";
|
|
string predictButtonText = "Predict AI";
|
|
|
|
bool pullDebounce = true;
|
|
async Task pull(){
|
|
if (pullDebounce){
|
|
pullDebounce = false;
|
|
pullButtonText = "Do not refresh the page. The data is pulling.";
|
|
await Task.Delay(1);
|
|
Task thread = new Task(async () => {
|
|
aiModule.PullAI();
|
|
pullButtonText = "Data pulled";
|
|
await InvokeAsync(StateHasChanged);
|
|
await Task.Delay(2000);
|
|
pullButtonText = "Pull Data";
|
|
await InvokeAsync(StateHasChanged);
|
|
pullDebounce = true;
|
|
});
|
|
thread.Start();
|
|
}
|
|
}
|
|
|
|
bool trainDebounce = true;
|
|
async Task train(){
|
|
if (trainDebounce){
|
|
trainDebounce = false;
|
|
trainButtonText = "Do not refresh the page. The AI is training.";
|
|
await Task.Delay(1);
|
|
Task thread = new Task(async () => {
|
|
aiModule.TrainAI();
|
|
trainButtonText = "AI Trained";
|
|
StateHasChanged();
|
|
await Task.Delay(2000);
|
|
trainButtonText = "Train AI";
|
|
StateHasChanged();
|
|
trainDebounce = true;
|
|
});
|
|
thread.Start();
|
|
}
|
|
}
|
|
|
|
async Task predict(){
|
|
predictButtonText = "Do not refresh the page. The AI is predicting";
|
|
await Task.Delay(1);
|
|
List<Task> threadpool = new List<Task>();
|
|
foreach(stockPredictionPair cur in predictions){
|
|
Task thread = new Task(() => {
|
|
cur.Movement = aiModule.PredictAI(cur.Symbol);
|
|
Console.WriteLine("Received Signal [" + cur.Symbol + "] : " + cur.Movement);
|
|
});
|
|
thread.Start();
|
|
threadpool.Add(thread);
|
|
}
|
|
await Task.WhenAll(threadpool);
|
|
predictButtonText = "Predictions loaded";
|
|
await Task.Delay(2000);
|
|
predictButtonText = "Predict AI";
|
|
}
|
|
|
|
// Data Types
|
|
|
|
class stockPredictionPair {
|
|
public string Symbol { get; set; } = "";
|
|
public int Movement { get; set; } = 0;
|
|
}
|
|
|
|
class loginSession {
|
|
public string UserName { get; set; } = "";
|
|
public List<PurchasedStock> TrackedStocks { get; set; } = new List<PurchasedStock>();
|
|
}
|
|
|
|
class PurchasedStock {
|
|
public string Symbol { get; set; } = "";
|
|
public float PurchasePrice { get; set; } = 0;
|
|
public float Quantity { get; set; } = 0;
|
|
public DateTime PurchaseDate { get; set; } = DateTime.Now;
|
|
}
|
|
|
|
} |