Apply Chart Template Not Working on Custom Created *.tpl file - File Encoding Issue I think??

 

Hi, I have spent hours trying to fix this.

I have Created a .tpl file which when I apply to a chart doesnt seem to apply correctly. I have narrowed the problem down to the Format Type of the file itself I beleive. Although it is saved as a .tpl file and is clearly so in window explorer, when I apply a different template with the exact same contents (after copying and pasting) the Template loads correctly.

I have attached the 'PairChart.tpl which doesn't work properly and the 'TemplateBLANKtplFile.tpl which does work. When I load the PairChart one, the Inputs (Leg1 and Leg2) do not update to what is in the file. You will see that the two are idetical in contents. I noticed that if I copy and Paste All from TemplateBLANKtplFile to PairChart (using Wordpad), and hit Save As it asks me some weird question about a changing the Text Format??

I have tried a whole variety of things in the code where I actually create the Text file but I cannot seem to get it to worlk and I know almost certainly is to do the way the .tpl is encooded.

Perhaps I need to use a certain set of FLAGS and/or Code Pages maybe??

Any help really is appreciated.

Here is the code that generates my custom tpl file

bool UpdateTemplateFile()
  {
   int counter=1;
   int fileHandle=-1;
   int NO_OF_FIELDS_IN_FILE=0;
   bool flag=false;
   string values[1000];
   bool result=false;

   string fileName="TemplateBLANKtplFile.tpl";


//ESTABLISH HOW MANY FIELDS EXIST IN THE TemplateBLANKtplFile FILE
   ResetLastError();
   fileHandle=FileOpen(fileName,FILE_TXT|FILE_READ,"\t");
   
   //Print("FileHandle: ",fileHandle);
   if(fileHandle==INVALID_HANDLE)
     {
      Print("Error Opening File: ",GetLastError());
     }
   else
     {
      counter=0;
      while((FileIsEnding(fileHandle)==false) && (flag==false))
        {
         string temp=FileReadString(fileHandle);
         StringTrimLeft(temp);
         StringTrimRight(temp);

         values[counter]=temp;
         //Print("TEMP: ",temp);
         if(StringCompare(values[counter],"</chart>",false)==0)
           {
            flag=true;
            NO_OF_FIELDS_IN_FILE=counter;
           }
         else
           {
            counter++;
           }
        }
      FileClose(fileHandle);

      //NOW WRITE THE NEW *.TPL FILE NAMED PairChart.tpl
      //Print("FIELDS:",NO_OF_FIELDS_IN_FILE);
      counter=0;
      fileHandle=FileOpen("PairChart.tpl",FILE_TXT|FILE_WRITE|FILE_READ);
      if(fileHandle!=INVALID_HANDLE)
        {
         while(counter<=NO_OF_FIELDS_IN_FILE)
           {
            StringTrimLeft(values[counter]);
            StringTrimRight(values[counter]);

            //XXXXXX is used as the text to replace in the template file and to identify where the text should be changed
            //This is an Input value of the Custom 'RatioPrice' indicator
            if(StringCompare(values[counter],"leg1=XXXXXX",false)==0)
              {
               string leg1="leg1="+m_lstLeg1.Select();
               StringToUpper(leg1);
               values[counter]=leg1;
               StringReplace(values[counter],"USDJPY",leg1);
              }

            //XXXXXX is used as the text to replace in the template file and to identify where the text should be changed
            //This is an Input value of the Custom 'RatioPrice' indicator
            if(StringCompare(values[counter],"leg2=XXXXXX",false)==0)
              {
               string leg2="leg2="+m_lstLeg2.Select();
               if(leg2=="leg2=")
                 {
                  leg2="leg2=EURUSD";
                 }
               StringToUpper(leg2);
               values[counter]=leg2;
               StringReplace(values[counter],"EURGBP",leg2);
              }

            //Now write the value to the tpl file
            FileWrite(fileHandle,values[counter]);
            //Print("WRITING VALUES:",values[counter]);
            counter++;
           }
        }
      FileClose(fileHandle);

      //result=values[NO_OF_FIELDS_IN_FILE+aHeader-1];
      result=true;
      //Print("Reading: "+string(aFile)+" HEADER: "+string(aHeader)+"  RESULT:"+string(result));
     }

   return result;
  }

The above spits out the Attached PairCharts.tpl file.

This is the main body and I genuinely don't believe the problem is here:

void UpdateForLeg1()
  {
   bool deletedOk = FileDelete("PairChart.tpl");
   if(deletedOk)
   {
     Print("Deleted Fine");
   }
   
   bool makeCopy=FileCopy("TemplateBLANKtplFile.tpl",0,"PairChart.tpl",FILE_WRITE);
   bool readOk=UpdateTemplateFile();

   if(readOk)
     {
      if(makeCopy)
        {
         bool processing=true;

         for(long chartID=ChartFirst(); chartID!=-1; chartID=ChartNext(chartID))
           {
            bool updatCurrentChart=IsChartATradingChart(chartID);

            if(updatCurrentChart)
              {
               ResetLastError();
               bool appliedTemplate=ChartApplyTemplate(chartID,"\\Files\\PairChart.tpl");
               if(appliedTemplate)
                 {
                  Print("Applied Template to chart: ",string(chartID));
                 }
               else
                 {
                  Print("Couldn't apply chart template to chart: ",string(chartID));
                 }
              }

            chartID=ChartNext(chartID);
            if(chartID==-1)
              {
               processing=-1;
              }
           }
         Print("Stopped Processing");

        }
      else
        {
         Print("File Template Not Copied");
        }
     }
   else
     {
      Print("Couldn't read file");
     }

  }
 
  1. You get your "Odd text conversion message" because you are not using notepad or equivalent. You are converting Unicode to ANSI, thus the warning.

  2. Saved templates are ANSI. Your code is creating Unicode. If you open your PairChart and save as ANSI, it probably will work.

  3.    fileHandle=FileOpen(fileName,FILE_TXT|FILE_READ,"\t");
    
    Change your code to output ANSI.