Trouble with timeframe changes within code

 

Hi all, I am trying to optimize my EA with with different timeframes as input. 

Here is how I change the TF within the code, where Timeframe is the input: 

int OnInit(){
   ChartSetSymbolPeriod(0, NULL, Timeframe); 
...

Then in my code I simply use Timeframe wherever I need it (just in iCustom, CopyRates and SeriesInfoInteger) 

The optimization run fine but then I get totally different results when I run simple test with the exact same inputs, but with different timeframes within the settings tabs. 

For example on a pair, the best result is coming with timeframe M6. Then I do two different single test with the exact same inputs (where Timeframe input is M6 in both case) but once the setting timeframe is M1 and the other is M6. I get really different results. I really dont understand where that come from because I use ChartSetSymbolPeriod at the beginning of OnInit. 

Anyone would have an idea why please?

Thanks