Applying a Trained Scikit-Learn Model to Live Forex Data
Summary
The document asks how to move from evaluating a machine-learning model on historical test data to producing forecasts from newly received broker data. The author describes training several model types on price and fundamental inputs, currently using a random forest, and plans to review predictions in a notebook before manually deciding whether to trade and how much to risk.
The proposed workflow is to retrieve current observations, place them in a pandas dataframe, prepare them in the same way as the training inputs, and pass them to the fitted model. The central practical issue is input shape: a prediction call expects two-dimensional feature rows, with the same columns and preprocessing used during training. The document supplies a sample request and reshape attempt but does not provide a resolved implementation or empirical evidence. It also leaves unspecified how to keep live features aligned with training data, validate data timing, or assess forecasting performance; it explicitly sets backtesting aside.
Key ideas
- A fitted model can generate forecasts from newly retrieved observations when they are represented as feature rows.
- Live inputs must match the feature columns, ordering, and preprocessing used to train the model.
- A single observation generally needs a two-dimensional shape for scikit-learn prediction.
- The proposed notebook workflow leaves live data validation and predictive performance assessment unresolved.
Tags
Full text
# Machine Learning model forecasting on real time data in python
# Machine Learning model forecasting on real time data in python
I’m building a Forex trading system based on machine learning with Python and brokers API. I get price time series data + fundamental data and then i train the model on that. Model means SVM, RF, Ensemble methods, Logistic regression and ANN. The best performer emits a signal forecasting price (classification or regression depends on model). Now i'm using Random Forest.
I'm using Sklearn and i'm stuck on a point: `regressor.predict(X_test)`
After prediction/forecasting on test data, how could i send on live trading the trained model?
How could i predict on real time data from brokers (i know their API but i don't know how to apply the model on updated live data). At the moment i'm not interested in backtesting solutions. My intention is to build a semi automatic strategy completely in Python Jupyter notebook: research, train, test, tuning and estimates in Jupyter notebook with historical data then forecasting every day price on live data, manually executing positions arising from those predictions + manual position sizing. So my workflow is Jupyter notebook + broker platforms.
The point is: i have a model, i have a prediction on test data, then?
My plan was to get real time data in a pandas dataframe (1 row), manipulate it and finally employ the model on it instead of test data. Is it true? I really need to manipulate it (reshaping in 2d like train test split preprocessing in Sklearn) before? Without reshaping i get errors.
For example:
```
URL = "example api live"
params = {'currency' : 'EURUSD','interval' : 'Hourly','api_key':'api_key'}
response = requests.get("example api live", params=params)
df= pd.read_responsejson(response.text)
forecast = df.iloc[:, 0].values.reshape(-1,1)
reg = regressor.predict(forecast)
```
Thank you!Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.