Login Page - Create Account

Support Board


Date/Time: Fri, 26 Apr 2024 08:52:41 +0000



Post From: ACSIL -> Open and write into a text file....

[2019-12-17 03:53:40]
User553714 - Posts: 184
This is a quick example of outputting to a text file; there are a lot of subtleties when using text files escpecially when you set sc.UpdateAlways equal to 1; at that stage you really need to understand the way SC constantly returns into applied studies. I suggest that you keep compiling over and over with differently set paramters to see their effect if you're just starting out and try not to have too much data loaded when testing things.

The study below will only print to file during a live or replay session; if you press stop on the replay the file will be deleting (ie on recalculation) and re-created so press pause if you want to see the results first; this behaviour can off course be changed depending on what you are trying to do.

You might want to download a Notepad++ plugin that updates the textfile in real time so you can see file changes as they occur; I use DocumentMonitor but I am not sure that plugin is still available to Notepad++.

Also don't forget to create the required folder and change the outputfile1 string accordingly for your SC instal location.


SCSFExport scsf_TextFiles(SCStudyInterfaceRef sc)
{

  int& FileTest1 = sc.GetPersistentInt(1);
  
  
  string outputfile1, outputfile2;
  ofstream myfile1, myfile2;
  

  // SetDefaults
  if (sc.SetDefaults)
  {
    sc.GraphName = "1.TextFiles";
    sc.StudyDescription = "Text File use.";
    sc.GraphRegion = 0;
    sc.ValueFormat = VALUEFORMAT_INHERITED;
    sc.AutoLoop = 1;
    //sc.UpdateAlways = 1;
    
    FileTest1 = 0;
  }

  // SPECIAL CASES - often used
  int Index = sc.Index;
  float OpenPrice = sc.BaseData[SC_OPEN][Index];
  float HighPrice = sc.BaseData[SC_HIGH][Index];
  float LowPrice = sc.BaseData[SC_LOW][Index];
  float LastPrice = sc.BaseData[SC_LAST][Index];

  //Declare file location string
  outputfile1 = "C:\\SierraChartB0\\TextFiles\\Debugging.txt";

  // DebuggingLog
  if (Index == 0 && FileTest1 == 0)
  {
    myfile1.open(outputfile1, ios::out | ios::trunc);          // Only create the file if it dosen't exist
    myfile1 << "Start Debug logging..." << endl;
  }

  
  if ( sc.IsFullRecalculation != 1 )                // Only append to file when there is no full-recalculation
  {
    myfile1.open(outputfile1, ios::out | ios::app);
  }    



  myfile1 << "Debug log, " << Index << ", " << OpenPrice << ", " << HighPrice << ", " << LowPrice << ", " << LastPrice << endl;
  

  myfile1.close();
}