Support Board
Date/Time: Wed, 05 Feb 2025 19:06:06 +0000
[Programming Help] - Does anyone know how to format the file pathway for exporting to csv file
View Count: 216
[2025-01-10 23:42:15] |
User373245 - Posts: 47 |
I can not get anything method to work. I am using paralles in Mac and have tried a bunch of methods does the file pathway have to be written in a particular manor. I have tried putting it in the data folder/the desktop/ I have given everything permission in all spots as well as used different methods to establish the pathway. As well as writing it a bunch of ways. With the drive letter \\ vs \ I have tried everything and can not get it to work?? As well as different logic in the if statement to trigger it? #include "sierrachart.h" // Define the name of the DLL SCDLLName("Version Export"); SCSFExport scsf_VersionExport(SCStudyInterfaceRef sc) { if (sc.SetDefaults) { // Name of the study as it appears in Sierra Chart sc.GraphName = "Version Export"; sc.StudyDescription = "Exports OHLC data from the chart to a file on the desktop."; sc.AutoLoop = 0; sc.Input[1].SetPathAndFileName("SierraChart\Data\Test.csv"); return; } SCString FilePath = sc.Input[1].GetPathAndFileName(); // Get the file path if (sc.Index == sc.ArraySize - 1) // Check if it's the last bar { int FileHandle; // Declare FileHandle for file operations // Open the file at the specified path in append mode sc.OpenFile(FilePath, n_ACSIL::FILE_MODE_OPEN_TO_APPEND, FileHandle); unsigned int BytesWritten = 0; // Write data to the file sc.WriteFile(FileHandle, "Test Line\r\n", 11, &BytesWritten); // Close the file after writing sc.CloseFile(FileHandle); } } |
[2025-01-31 03:28:47] |
drinkcodejava - Posts: 5 |
I'm running VMWare Fusion. I tried just running this part of your code and it worked: SCString FilePath = "Test.csv"; // Get the file path if (sc.Index == sc.ArraySize - 1) // Check if it's the last bar { int FileHandle; // Declare FileHandle for file operations // Open the file at the specified path in append mode sc.OpenFile(FilePath, n_ACSIL::FILE_MODE_OPEN_TO_APPEND, FileHandle); unsigned int BytesWritten = 0; // Write data to the file sc.WriteFile(FileHandle, "Test Line\r\n", 11, &BytesWritten); // Close the file after writing sc.CloseFile(FileHandle); } ...so my suggestion is to start from there (w/o the if (sc.SetDefaults) block). For me, this code writes the file into the \Data folder. When I changed "Test.csv" to "Tmp\Test.csv" or "\Tmp2\Test.csv", I just ended up with a file named TmpText.csv and Tmp2Test.csv, so I don't think you can specify a directory path. The compiler finishes, but it complains about the backslashes. I think you are limited to just having the file written into the \SierraChart\Data folder. Hope this helps! |
[2025-01-31 07:37:23] |
seandunaway - Posts: 316 |
it works for me too, including writing to files outside of the Data directory or with absolute filenames most likely your sierrachart instance is not being run as an administrator so you don't have write access, to c:\ for example add a call to sc.GetLastFileErrorMessage() if the open returns false, to know exactly why (e.g. if (!sc.OpenFile(FilePath, n_ACSIL::FILE_MODE_OPEN_TO_APPEND, FileHandle)) sc.AddMessageToLog(sc.GetLastFileErrorMessage(FileHandle), 1);) 2025-01-30 23:34:14.601 | Chart: ESH25-CME [C][M] 5 Min #1 | Study: bars2csv | File error: Windows error code 5: Access is denied. * also, please remember that string literals in c++ use \ to escape special characters like the \n for newline in your WriteFile arguments. use \\ to insert literal \ (e.g. \\.\C:\Tmp\ is "\\\\.\\C:\\Tmp\\") use win32 documentation for CreateFile() for how to format paths correctly, especially if you're wanting to place on a network share or other device like your mac https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea anyway you probably want sc.WriteBarAndStudyDataToFileEx() Date Time Of Last Edit: 2025-01-31 10:39:55
|
[2025-01-31 09:15:40] |
seandunaway - Posts: 316 |
#include <sierrachart.h>
SCDLLName("write bar data to file") SCSFExport scsf_write_bar_data_to_file (SCStudyInterfaceRef sc) { SCInputRef file = sc.Input[0]; if (sc.SetDefaults) { sc.DisplayStudyName = 0; sc.GraphName = "write bar data to file"; sc.GraphRegion = 0; char user_directory[MAX_PATH]; ExpandEnvironmentStrings("%USERPROFILE%", user_directory, MAX_PATH); char default_file[MAX_PATH]; snprintf(default_file, MAX_PATH, "%s\\Desktop\\%s.csv", user_directory, sc.Symbol.GetChars()); file.Name = "file"; file.SetPathAndFileName(default_file); return; } n_ACSIL::s_WriteBarAndStudyDataToFile WriteBarAndStudyDataToFileParams; WriteBarAndStudyDataToFileParams.StartingIndex = sc.CurrentIndex; WriteBarAndStudyDataToFileParams.OutputPathAndFileName = file.GetPathAndFileName(); sc.WriteBarAndStudyDataToFileEx(WriteBarAndStudyDataToFileParams); } 2025-01-31 01:11:36.187 | ESH25-CME [C][M] 5 Min #1 | Beginning export of bar and study data to file C:\Users\sean\Desktop\ESH25-CME.csv Date, Time, Open, High, Low, Last, Volume, # of Trades, OHLC Avg, HLC Avg, HL Avg, Bid Volume, Ask Volume 2025-1-21, 13:15:00.000000, 6088.00, 6089.25, 6087.00, 6088.50, 2104, 1238, 6088.19, 6088.25, 6088.13, 1038, 1066 edit: i just saw there is already an included study which does exactly this 🤣 Write Bar and Study Data To File Date Time Of Last Edit: 2025-01-31 10:20:22
|
To post a message in this thread, you need to log in with your Sierra Chart account: