Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to use a Linear Regression model for backtesting? #274

Unanswered
helloworldnoob asked this question in Q&A
Discussion options

I just cant seem to figure it out how to code the Strategy Class right now. There is no need for me to train the model inside the function, as seen here: https://kernc.github.io/backtesting.py/doc/examples/Trading%20with%20Machine%20Learning.html

I want to plug in my already trained model, and use the model against the test dataset, and see all those lovely stats that this module provides :)

What is the correct method for coding the Strategy class so that:

  1. Model is loaded / initialised for the backtest
  2. Grab the current input features
  3. Predict those -> if prediciton >0 BUY, if prediction <0 SELL (super simple for now)
  4. And of course have Backtest() run correctly

Sorry if this question is elsewhere, I couldn't see it.

For reference, I am coming from backtesting my model with borrowed and edited code (without any modules), the way I have been backtesting from start to finish in a flow type of way is as so:

[LOAD data, add data, clean data]
[Select the Features and Target for model]
[Split into train & test]
[Create, fit model]
[Simulate trading positions in the dataframe from the x_test predictions, if prediction >0 ['Position']column =1]
[Function with for loop, if,elif statements, calcuating metrics from trades]
[printout results]

Super confused on how to get up and running correctly with this module, any help is as always appreciated.

You must be logged in to vote

Replies: 1 comment

Comment options

You can still use the example in the documentation. Since you have it trained, pass it to the part it uses the results:

 # Forecast the next movement
 X = get_X(self.data.df.iloc[-1:])
 forecast = self.clf.predict(X)[0]

You can also refractor the example to use linear regression to train a subset of the data to check your understanding.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /