Move away from Python Interop for better stability

This commit is contained in:
2026-03-09 17:11:59 -07:00
parent 52c5062d6e
commit ac67be137c
8 changed files with 134 additions and 60 deletions
@@ -1,4 +1,5 @@
import os
import sys
import joblib
import numpy as np
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
@@ -7,7 +8,11 @@ import features
import matplotlib
matplotlib.use("Agg")
def Predict(Symbol):
def Predict():
# Get the Symbol from ARGV
Symbol = sys.argv[1]
# Define paths (consistent with your previous script)
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
DATA_DIR = os.path.join(SCRIPT_DIR, "data")
@@ -21,8 +26,6 @@ def Predict(Symbol):
# Make the feature set
df = features.MakeFeatures(df)
print(Symbol)
# Drop our predictor
df.drop('Target_Close', axis=1, inplace=True)
@@ -51,8 +54,7 @@ def Predict(Symbol):
# 'predictions' will be a 2D array, flatten it if you want a simple list
flat_predictions = actual_prediction.flatten().tolist()
print(f"Predicted Target_Close: {flat_predictions}")
# Set the movement indicator
movement_indicator = 0
if (np.mean(flat_predictions) > 0.01):
movement_indicator = 1
@@ -61,7 +63,10 @@ def Predict(Symbol):
else:
movement_indicator = 0
return movement_indicator
# Return to C# via stdout
print(f"---RESULT_START---")
print(movement_indicator)
print(f"---RESULT_END---")
if __name__ == "__main__":
Predict("AAPL")
Predict()
@@ -75,12 +75,9 @@ def TrainAI():
test_results = dnn_model.evaluate(
test_features, test_labels, verbose=0
)
print(f"Test Results: {test_results}")
# Save the model
dnn_model.save(os.path.join(DATA_DIR, "model.keras"))
if __name__ == "__main__":
TrainAI()
# Last train Predicted Target_Close: [0.0022113274317234755, 0.0021446370519697666, 0.0022628342267125845, 0.002175702480599284, 0.0021452796645462513, 0.0020838389173150063, 0.0017336219316348433, 0.002210840117186308, 0.0021144403144717216, 0.0021278387866914272, 0.0021266420371830463, 0.002261851681396365, 0.002108299173414707, 0.002121902070939541, 0.0022294146474450827]
TrainAI()
+14 -2
View File
@@ -1,7 +1,19 @@
import sys
import yfinance as yf
def getCurrentPrice(symbol):
def getCurrentPrice():
# Get the Symbol from ARGV
symbol = sys.argv[1]
ticker = yf.Ticker(symbol)
data = ticker.history(period="1d", interval="1m")
current_price = data['Close'].iloc[-1]
return current_price
# Return to C# via stdout
print(f"---RESULT_START---")
print(current_price)
print(f"---RESULT_END---")
if __name__ == "__main__":
getCurrentPrice()
+4 -4
View File
@@ -15,7 +15,6 @@ def pull():
# Scrape the data
all_data = []
for i, symbol in enumerate(tickers):
print(f"Processing: {i} of {len(tickers)}")
df = yf.download(symbol, period="max", auto_adjust=True)
if not df.empty:
# Remove the ticker column
@@ -24,10 +23,11 @@ def pull():
all_data.append(df)
# Concatinate into a combined list and cache
print("Processing data")
final_df = pd.concat(all_data)
# Save to file
print("Writing data to file")
final_df.to_parquet(os.path.join(DATA_DIR, "stocks.parquet"))
final_df.head(200).to_csv(os.path.join(DATA_DIR, "stocks.preview.csv"))
final_df.head(200).to_csv(os.path.join(DATA_DIR, "stocks.preview.csv"))
if __name__ == "__main__":
pull()