where do you plan to implement this in MC? The code appears to be a mix of EasyLanguage and the code used for setting custom criteria - these won't work together as far as I am aware.
You can set the value for SetCustomFitnessValue directly with reserved words in the code.
If you take a look at your code below, I am not sure it would give you the result you are looking for.
Assuming you go flat into the time period (with an open position it would be more complex), shouldn't the total_profit during the range be the NetProfit at the end of the range - Netprofit before the range. Which might be what your formula gives you, but it's not clear what value yesterdaynet holds.
I was told that there is no way of calculating total_profit_selected_range (profit from date x to date y) in the custom optimization criteria. The only solution is to use the SetCustomFitnessValue inside my PowerLanguage script. I am trying to get some direction on how to calculate profit from date x to date y inside my strategy script.
in my opinion you have much more control and possibilities when doing it via SetCustomFitnessValue in the code directly. The first step should be to compute the value for the total_profit_selected_range variable and as I wrote in my post above it's not clear if you get the correct value for that.