akumidv / tradingview-assistant-chrome-extension

An assistant for backtesting trading strategies and checking (showing) external signals in Tradingview implemented as a Chrome browser extension.
GNU General Public License v3.0
159 stars 56 forks source link

Feature Request: Adding 5-fold Cross-Validation Functionality to Your TradingView Backtesting Chrome Extension #106

Open g001613001 opened 1 year ago

g001613001 commented 1 year ago

I hope this message finds you well. I am an active user of your TradingView backtesting Chrome extension and have found it to be a valuable tool for my trading strategy analysis. I appreciate the hard work and dedication you have put into developing this useful extension.

I am writing to suggest a new feature that I believe would greatly improve the functionality and effectiveness of your extension - the integration of 5-fold cross-validation for backtesting.

Currently, the extension allows users to input a start date and performs a single backtest from that date to the most recent data available. While this is useful, incorporating 5-fold cross-validation would provide a more robust assessment of a trading strategy's performance by evaluating its performance across multiple non-overlapping time periods.

5-fold cross-validation works by dividing the data into five equal subsets, using four of the subsets for training and the remaining subset for testing. This process is repeated five times, each time using a different subset for testing. This method can help reduce the risk of overfitting and provide a more accurate assessment of a strategy's performance.

To implement this feature, the extension could allow users to input both a start date and an end date for the backtesting period, then automatically divide this period into five equal time intervals. The extension would then perform the 5-fold cross-validation by running the backtest on each combination of training and testing subsets.

I believe that the addition of this feature would greatly benefit users of your extension, enabling more accurate and reliable backtesting results. Thank you for considering my suggestion, and I look forward to any updates you might make to the extension.

Best regards

akumidv commented 1 year ago

Thanks for you suggestion. This method and also Forward test I added as plan to improving, but not sure about date when I have time to implement it.

Quetzalquatl commented 1 year ago

@akumidv i hope it helps you

function crossValidation(data, k) { const foldSize = Math.ceil(data.length / k); const shuffledData = shuffle(data.slice()); const folds = [];

for (let i = 0; i < k; i++) { const start = i foldSize; const end = (i + 1) foldSize; const fold = shuffledData.slice(start, end); folds.push(fold); }

const results = [];

for (let i = 0; i < k; i++) { const trainingData = []; for (let j = 0; j < k; j++) { if (i !== j) { trainingData.push(...folds[j]); } } const validationData = folds[i]; const model = trainModel(trainingData); const accuracy = testModel(model, validationData); results.push(accuracy); }

const averageAccuracy = results.reduce((a, b) => a + b, 0) / results.length; return averageAccuracy; }

function shuffle(array) { let currentIndex = array.length; let temporaryValue, randomIndex;

while (0 !== currentIndex) { randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1;

temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;

}

return array; }

function trainModel(data) { // Model training code here }

function testModel(model, data) { // Model testing code here }