Login Page - Create Account

Support Board


Date/Time: Wed, 24 Apr 2024 22:24:09 +0000



[Programming Help] - Source code

View Count: 1076

[2018-03-30 20:11:54]
User39772 - Posts: 311
Hello Suppport,

is it possible to get the source code of the study "write bar data to file" ?

Regards
[2018-03-30 21:32:15]
Sierra Chart Engineering - Posts: 104368
This is available. Refer to:
How to Build an Advanced Custom Study from Source Code: Searching for Source Code for Sierra Chart Built-In Studies
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
[2018-03-31 00:34:28]
JoseyWales - Posts: 67
Around line 3107 in file "Studies6.cpp"


SCSFExport scsf_WriteBarDataToFile(SCStudyInterfaceRef sc)
{
  SCInputRef Separator = sc.Input[0];
  SCInputRef UseGMTTime = sc.Input[1];
  SCInputRef DateFormat = sc.Input[2];
  SCInputRef OutputMilliseconds = sc.Input[3];

  if (sc.SetDefaults)
  {
    sc.GraphName = "Write Bar Data To File";
    sc.StudyDescription = "Write Bar Data To File";

    sc.GraphRegion = 0;

    Separator.Name = "Separator";
    Separator.SetCustomInputStrings("Space;Comma;Tab");
    Separator.SetCustomInputIndex(1);

    UseGMTTime.Name = "Use GMT Time";
    UseGMTTime.SetYesNo(0);

    DateFormat.Name = "Date Format";
    DateFormat.SetCustomInputStrings("MM/DD/YYYY;YYYY/MM/DD;DD/MM/YYYY");
    DateFormat.SetCustomInputIndex(1);

    OutputMilliseconds.Name = "Output Milliseconds";
    OutputMilliseconds.SetYesNo(false);

    sc.TextInputName = "File Path";

    sc.AutoLoop = 0;//manual looping for efficiency
    return;
  }

  SCString OutputPathAndFileName;
  if (sc.TextInput.GetLength() > 0)
  {
    OutputPathAndFileName = sc.TextInput;
  }
  else
  {
    OutputPathAndFileName.Format("%s-BarData.txt", sc.Symbol.GetChars());
  }

  HANDLE FileHandle = INVALID_HANDLE_VALUE;
  DWORD BytesWritten;
  if (sc.UpdateStartIndex == 0)
  {
    // create new file
    FileHandle = CreateFile(OutputPathAndFileName.GetChars(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    // creating/opening fail
    if (FileHandle == INVALID_HANDLE_VALUE)
    {
      SCString LogMessage;
      LogMessage.Format("Write Bar Data To File study: Error creating output data file: %s. Make sure the path is valid and there are no invalid characters.", OutputPathAndFileName.GetChars());
      sc.AddMessageToLog(LogMessage.GetChars(), 1);
      return;
    }

    // write file header
    SCString FileHeader;
    switch (Separator.GetInt())
    {
      case 0:
        FileHeader = "Date Time Open High Low Close Volume NumberOfTrades BidVolume AskVolume\r\n";
        break;
      case 1:
        FileHeader = "Date, Time, Open, High, Low, Close, Volume, NumberOfTrades, BidVolume, AskVolume\r\n";
        break;
      case 2:
        FileHeader = "Date\tTime\tOpen\tHigh\tLow\tClose\tVolume\tNumberOfTrades\tBidVolume\tAskVolume\r\n";
        break;
    }

    WriteFile(FileHandle, FileHeader.GetChars(), (DWORD)FileHeader.GetLength(), &BytesWritten, NULL);
  }
  else
  {
    // open existing file
    FileHandle = CreateFile(OutputPathAndFileName.GetChars(), GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    // creating/opening fail
    if (FileHandle == INVALID_HANDLE_VALUE)
    {
      SCString LogMessage;
      LogMessage.Format("Write Bar Data To File study: Error creating output data file: %s", OutputPathAndFileName.GetChars());
      sc.AddMessageToLog(LogMessage.GetChars(), 1);
      return;
    }

    SetFilePointer(FileHandle, 0, NULL, FILE_END);
  }

  bool IsRenko = sc.AreRenkoBars() != 0;

  for (int Index = sc.UpdateStartIndex; Index < (sc.ArraySize - 1); Index++)
  {
    if (sc.GetBarHasClosedStatus(Index) != BHCS_BAR_HAS_CLOSED)
      continue;      // write on close

    int Year, Month, Day;
    int hour, minute, second, millisecond;
    SCDateTimeMS BaseDateTimeIn = sc.BaseDateTimeIn[Index];

    if (UseGMTTime.GetBoolean() != 0)
      BaseDateTimeIn = sc.AdjustDateTimeToGMT(BaseDateTimeIn);

    BaseDateTimeIn.GetDateTimeYMDHMS_MS(Year, Month, Day, hour, minute, second, millisecond);

    SCString DateString;
    switch (DateFormat.GetInt())
    {
      case 0:
        DateString.Format("%02d/%02d/%d", Month, Day, Year);
        break;

      case 1:
        DateString.Format("%d/%02d/%02d", Year, Month, Day);
        break;

      case 2:
        DateString.Format("%02d/%02d/%d", Day, Month, Year);
        break;
    }

    // write bar data string to file
    int ValueFormat = sc.BaseGraphValueFormat;

    char MillisecondsText[32] = {0};

    if (OutputMilliseconds.GetYesNo())
      sprintf_s(MillisecondsText, ".%d", millisecond);

    const char *FormatString;
    switch (Separator.GetInt())
    {
      case 0:
        FormatString = "%s %.2d:%.2d:%.2d%s %s %s %s %s %.0f %.0f %.0f %.0f\r\n";
        break;
      case 1:
        FormatString = "%s, %.2d:%.2d:%.2d%s, %s, %s, %s, %s, %.0f, %.0f, %.0f, %.0f\r\n";
        break;

      default:
      case 2:
        FormatString = "%s\t%.2d:%.2d:%.2d%s\t%s\t%s\t%s\t%s\t%.0f\t%.0f\t%.0f\t%.0f\r\n";
        break;
    }

    float OpenValue = IsRenko ? sc.BaseData[SC_RENKO_OPEN][Index] : sc.Open[Index];
    float HighValue = sc.High[Index];
    float LowValue = sc.Low[Index];
    float CloseValue = IsRenko ? sc.BaseData[SC_RENKO_CLOSE][Index] : sc.Close[Index];

    SCString BarDataString;

    BarDataString.Format(FormatString,
      DateString.GetChars(),
      hour,
      minute,
      second,
      MillisecondsText,
      sc.FormatGraphValue(OpenValue, ValueFormat).GetChars(),
      sc.FormatGraphValue(HighValue, ValueFormat).GetChars(),
      sc.FormatGraphValue(LowValue, ValueFormat).GetChars(),
      sc.FormatGraphValue(CloseValue, ValueFormat).GetChars(),
      sc.Volume[Index],
      sc.NumberOfTrades[Index],
      sc.BidVolume[Index],

      sc.AskVolume[Index]);

    WriteFile(FileHandle, BarDataString.GetChars(), (DWORD)BarDataString.GetLength(), &BytesWritten, NULL);
  }
  CloseHandle(FileHandle);

  return;
}

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account