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

Adding a combined objective function #117

Open esiwgnahz opened 1 year ago

esiwgnahz commented 1 year ago

Thank you so much for the contribution of this great extension. If I want to combine two objectives ( such as “Net Profit: All” and “Percent Profitable: All”) by weighting them and set them as the new objective function, how should I do?

Something likes

Weighted_profit = curResult['Net Profit: All'] * weight_net_profit + curResult['Percent Profitable: All'] * weight_percent_profitable;
esiwgnahz commented 1 year ago

Until now, I had tried to add a new option in assistant.html and modify the model.getBestResult function. When executing, the error is "TypeError: Cannot convert undefined or null to object". I guess there lack some definitions or links in my modification.

What's else should do? The modified model.getBestResult function is as following. Is there anything wrong?

model.getBestResult = (testResults) => {
  const perfomanceSummary = testResults.perfomanceSummary
  const checkField = testResults.optParamName || backtest.DEF_MAX_PARAM_NAME
  const isMaximizing = testResults.hasOwnProperty('isMaximizing') ? testResults.isMaximizing : true
  if(!perfomanceSummary || !perfomanceSummary.length) return ''
  const bestResult = perfomanceSummary.reduce((curBestRes, curResult) => {
    if(curResult.hasOwnProperty(checkField)) {
      if (checkField == 'weighted_profit') {
        const weight_net_profit = 0.8 // adjust 
        const weight_percent_profitable = 0.2 // adjust 
        const weighted_profit = curResult['Net Profit: All'] * weight_net_profit + curResult['Percent Profitable: All'] * weight_percent_profitable;
        if (isMaximizing && (!curBestRes || !curBestRes.weighted_profit || curBestRes.weighted_profit < weighted_profit)) return {...curResult, weighted_profit}
        else if (!isMaximizing && (!curBestRes || !curBestRes.weighted_profit || curBestRes.weighted_profit > weighted_profit)) return {...curResult, weighted_profit}
      } // 
      else {

        if(isMaximizing && (!curBestRes || !curBestRes[checkField] || curBestRes[checkField] < curResult[checkField])) return curResult
        else if (!isMaximizing && (!curBestRes || !curBestRes[checkField] || curBestRes[checkField] > curResult[checkField])) return curResult
      }
    }
    return curBestRes
  })
  return bestResult
}
akumidv commented 1 year ago

This improvements can be realized in future. For now for goal of using two parameters you can use filter as some solution