Use threading for python. and make singals from python
This commit is contained in:
@@ -8,18 +8,20 @@ import features
|
|||||||
import matplotlib
|
import matplotlib
|
||||||
matplotlib.use("Agg")
|
matplotlib.use("Agg")
|
||||||
|
|
||||||
def Predict():
|
def Predict(Symbol):
|
||||||
# Define paths (consistent with your previous script)
|
# Define paths (consistent with your previous script)
|
||||||
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
DATA_DIR = os.path.join(SCRIPT_DIR, "data")
|
DATA_DIR = os.path.join(SCRIPT_DIR, "data")
|
||||||
MODEL_PATH = os.path.join(DATA_DIR, "model.keras")
|
MODEL_PATH = os.path.join(DATA_DIR, "model.keras")
|
||||||
|
|
||||||
# Pull 1 month of current data to make prediction against | for volatility 20
|
# Pull 1 month of current data to make prediction against | for volatility 20
|
||||||
df = yf.download("NVDA", period="2mo", auto_adjust=True)
|
df = yf.download(Symbol, period="2mo", auto_adjust=True)
|
||||||
if not df.empty:
|
if not df.empty:
|
||||||
df = features.MakeFeatures(df, 1)
|
df = features.MakeFeatures(df, 1)
|
||||||
df = features.CleanDF(df)
|
df = features.CleanDF(df)
|
||||||
|
|
||||||
|
print(Symbol)
|
||||||
|
|
||||||
# Drop our predictor
|
# Drop our predictor
|
||||||
df.drop('Target_Close', axis=1, inplace=True)
|
df.drop('Target_Close', axis=1, inplace=True)
|
||||||
|
|
||||||
@@ -50,7 +52,15 @@ def Predict():
|
|||||||
|
|
||||||
print(f"Predicted Target_Close: {flat_predictions}")
|
print(f"Predicted Target_Close: {flat_predictions}")
|
||||||
|
|
||||||
return json.dumps(flat_predictions)
|
movement_indicator = 0
|
||||||
|
if (np.mean(flat_predictions) > 0.01):
|
||||||
|
movement_indicator = 1
|
||||||
|
elif (np.mean(flat_predictions) < -0.01):
|
||||||
|
movement_indicator = -1
|
||||||
|
else:
|
||||||
|
movement_indicator = 0
|
||||||
|
|
||||||
|
return movement_indicator
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
Predict()
|
Predict()
|
||||||
@@ -22,8 +22,17 @@
|
|||||||
<button @onclick="pullandtrain">@button1Text</button>
|
<button @onclick="pullandtrain">@button1Text</button>
|
||||||
<button @onclick="predict">@button2Text</button>
|
<button @onclick="predict">@button2Text</button>
|
||||||
|
|
||||||
@foreach(double cur in predictions){
|
@foreach (stockPredictionPair cur in predictions){
|
||||||
<p>@cur</p><br/>
|
<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>
|
||||||
@@ -34,7 +43,13 @@
|
|||||||
string button1Text = "Train AI";
|
string button1Text = "Train AI";
|
||||||
string button2Text = "Predict AI";
|
string button2Text = "Predict AI";
|
||||||
|
|
||||||
double[] predictions = {};
|
|
||||||
|
List<stockPredictionPair> predictions = new List<stockPredictionPair>(){
|
||||||
|
new stockPredictionPair(){ Symbol = "AAPL" },
|
||||||
|
new stockPredictionPair(){ Symbol = "AAPL" },
|
||||||
|
new stockPredictionPair(){ Symbol = "TTD" },
|
||||||
|
new stockPredictionPair(){ Symbol = "FUN" }
|
||||||
|
};
|
||||||
|
|
||||||
async Task pullandtrain(){
|
async Task pullandtrain(){
|
||||||
button1Text = "Do not refresh the page. The data is refreshing.";
|
button1Text = "Do not refresh the page. The data is refreshing.";
|
||||||
@@ -46,8 +61,24 @@
|
|||||||
async Task predict(){
|
async Task predict(){
|
||||||
button2Text = "Do not refresh the page. The data is refreshing.";
|
button2Text = "Do not refresh the page. The data is refreshing.";
|
||||||
await Task.Delay(1);
|
await Task.Delay(1);
|
||||||
predictions = aiModule.PredictAI();
|
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";
|
button2Text = "Refresh Completed";
|
||||||
|
StateHasChanged();
|
||||||
|
await Task.Delay(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
class stockPredictionPair{
|
||||||
|
public string Symbol = "";
|
||||||
|
public int Movement = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -30,13 +30,11 @@ namespace PythonInterop {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public double[] PredictAI() {
|
public int PredictAI(string StockSymbol) {
|
||||||
using (Py.GIL()) {
|
using (Py.GIL()) {
|
||||||
dynamic main = Py.Import("ai-predictor");
|
dynamic main = Py.Import("ai-predictor");
|
||||||
string result = main.Predict();
|
int result = main.Predict(StockSymbol);
|
||||||
double[]? predictions = JsonSerializer.Deserialize<double[]>(result);
|
return result;
|
||||||
double[] nullCasted = predictions != null ? predictions : [];
|
|
||||||
return nullCasted;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user