Files
AI-Stock-Trader/WebServer/Components/Pages/Home.razor
T

144 lines
4.4 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>
}
<!-- AI Frame -->
<div class="gridFrame">
<div>
<button @onclick="pullandtrain">@button1Text</button>
<button @onclick="predict">@button2Text</button>
</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
Session = new loginSession(){
UserName = userName.ToLower()
};
}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()
};
}else{
loginError = "account is taken";
}
}
// AI Stuff
string button1Text = "Train AI";
string button2Text = "Predict AI";
async Task pullandtrain(){
button1Text = "Do not refresh the page. The data is refreshing.";
await Task.Delay(1);
aiModule.TrainAI();
button1Text = "Refresh Completed";
}
async Task predict(){
button2Text = "Do not refresh the page. The data is refreshing.";
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);
button2Text = "Refresh Completed";
StateHasChanged();
await Task.Delay(1);
}
// Data Types
class stockPredictionPair {
public string Symbol { get; set; } = "";
public int Movement { get; set; } = 0;
}
class loginSession {
public string UserName { get; set; } = "";
}
}