backtesting – SMART Trading Strategies https://smarttradingstrategies.com Statistical and Mathematical Approach to Retail Trading Wed, 15 Dec 2021 09:40:33 +0000 en-US hourly 1 https://smarttradingstrategies.com/wp-content/uploads/2021/08/logo-150x150.png backtesting – SMART Trading Strategies https://smarttradingstrategies.com 32 32 How to code a K Nearest Neighbour trading strategy in sklearn https://smarttradingstrategies.com/how-to-code-a-k-nearest-neighbout-trading-strategy/ https://smarttradingstrategies.com/how-to-code-a-k-nearest-neighbout-trading-strategy/#respond Wed, 10 Nov 2021 03:22:55 +0000 https://smarttradingstrategies.com/?p=730 How to do a grid search with forward chaining (using the TimeSeriesSplit class)
How to backtest a strategy that uses KNN to predict if today's close will be greater than 99.5% of yesterday's close
How to use the pyfolio module]]>
This tutorial covers the following:

  • How to use K Nearest Neighbour to predict whether stock prices will close above 99.5%*(today’s close)
  • How to get the confusion matrix, classification report and ROC curve
  • How to do hyperparameter tuning using a grid search
  • How to do forward chaining with time series (using TimeSeriesSplit)
  • How to code a simple trading strategy using K Nearest Neighbour

Understanding the confusion matrix and classification report

Positive = Predicted Positive
Negative = Predicted Negative

TP = True Positive = Instances that are correctly predicted as positive
FP = False Positive = Instances that are incorrectly predicted as positive

TN = True Negative = Instances that are correctly predicted as negative
FN = False Negative = Instances that are incorrectly predicted as negative

Precision = Out of all the instances whose predicted values are positive, how many are true positives
Recall = Out of all the instances whose actual values are positive, how many are true positives

\begin{aligned}
Accuracy &= \frac{TP+TN}{TP+TN+FP+FN}\\\\
Precision &= \frac{TP}{TP+FP}\\\\
Recall &= \frac{TP}{TP+FN}\\\\
F_1 &= \frac{2*recall*precision}{recall+precision}\\\\
\end{aligned}

Link to Code (as HTML file)

KNN.html

]]>
https://smarttradingstrategies.com/how-to-code-a-k-nearest-neighbout-trading-strategy/feed/ 0