Support Board
Date/Time: Wed, 05 Feb 2025 17:03:59 +0000
[Programming Help] - ACSIL export to csv
View Count: 209
[2025-01-06 06:02:17] |
User373245 - Posts: 47 |
Does anyone have any experience exporting data to a csv file the log is correct 2025-01-06 06:00:45.102 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | Open Price: 98.285500 2025-01-06 06:00:45.102 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | High Price: 98.292000 2025-01-06 06:00:45.102 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | Low Price: 98.275002 2025-01-06 06:00:45.102 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | Close Price: 1.000000 2025-01-06 06:00:45.102 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | 60 SMA Value: 1.190387 2025-01-06 06:00:45.103 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | CSV File Path: /Users/philiphoy/Desktop/Test.csv but my csv file on my desktop does not populate any data #include "sierrachart.h" #include <fstream> // For file operations #include <iostream> // For input/output operations SCDLLName("FILE ACSIL"); SCSFExport scsf_FunctionName(SCStudyInterfaceRef sc) { // Set subgraph reference to OHLC SCInputRef Input_240StoAngle = sc.Input[1]; // SCInputRef OutputFile = sc.Input[3]; //OutputFile.Name = "Output File"; // Name the input for file path //OutputFile.SetPathAndFileName(""); // Default value for the file path (empty for now) SCInputRef OutputFile = sc.Input[3]; OutputFile.Name = "Output File"; // Name the input for file path OutputFile.SetPathAndFileName("/Users/philiphoy/Desktop/Test.csv"); // Set the correct path to the file here // Initialization code: Runs once when the study is added to the chart if (sc.SetDefaults) { sc.GraphName = "File Export"; // Name of the study sc.StudyDescription = "A basic example of an ACSIL study."; sc.AutoLoop = 1; // Enables auto-loop mode for processing each bar Input_240StoAngle.Name = "240 Rsi Angle"; Input_240StoAngle.SetChartStudySubgraphValues(1, 6, 0); return; } // Prepare the arrays for study data SCFloatArray sma60Array; sc.GetStudyArrayUsingID(Input_240StoAngle.GetStudyID(), Input_240StoAngle.GetSubgraphIndex(), sma60Array); // Main calculation code: Runs for each bar if (sc.BaseData[SC_CLOSE][sc.Index]) { // Get price data for logging float currentPrice = sc.BaseData[SC_LAST][sc.Index]; float openPrice = sc.BaseData[SC_OPEN][sc.Index]; float closePrice = sc.BaseData[SC_CLOSE][sc.Index]; float highPrice = sc.BaseData[SC_HIGH][sc.Index]; float lowPrice = sc.BaseData[SC_LOW][sc.Index]; // Log variables to string for logging data SCString openPriceLog, currentPriceLog, closePriceLog, highPriceLog, lowPriceLog, sma60Log; currentPriceLog.Format("Current price: %2f", currentPrice); sc.AddMessageToLog(currentPriceLog, 0); openPriceLog.Format("Open Price: %2f", openPrice); sc.AddMessageToLog(openPriceLog, 0); highPriceLog.Format("High Price: %2f", highPrice); sc.AddMessageToLog(highPriceLog, 0); lowPriceLog.Format("Low Price: %2f", lowPrice); sc.AddMessageToLog(lowPriceLog, 0); closePriceLog.Format("Close Price: %2f", closePrice); sc.AddMessageToLog(closePriceLog, 0); sma60Log.Format("60 SMA Value: %2f", sma60Array[sc.Index]); sc.AddMessageToLog(sma60Log, 0); // Use the dynamically set file path for CSV output SCString csvFilePath = OutputFile.GetString(); // Get file path from the input // Convert SCString to C-style string const char* filePath = csvFilePath.GetChars(); // Open the file in append mode (to avoid overwriting existing data) std::ofstream file(filePath, std::ios::app); // Log the file path for debugging purposes SCString filePathLog; filePathLog.Format("CSV File Path: %s", filePath); sc.AddMessageToLog(filePathLog, 0); // Check if the file opened successfully if (file.is_open()) { // Write the values to the CSV file file << currentPrice << ", " << openPrice << ", " << highPrice << ", " << lowPrice << ", " << closePrice << ", " << sma60Array[sc.Index] << "\n"; file.close(); // Close the file after writing } else { // If the file didn't open, log an error message sc.AddMessageToLog("Failed to open file for writing.", 0); } } } |
[2025-01-06 10:50:48] |
User431178 - Posts: 579 |
Your path is not being set correctly. Move the OutputFile config into the setdefaults block, otherwise it is reset to the hardcoded value each time you calcaulte the study. If you want to set the default path then I think it should be the full path, including the drive letter. #include "sierrachart.h" #include <fstream> // For file operations #include <iostream> // For input/output operations SCDLLName("FILE ACSIL"); SCSFExport scsf_FunctionName(SCStudyInterfaceRef sc) { SCInputRef Input_240StoAngle = sc.Input[1]; SCInputRef OutputFile = sc.Input[3]; // Initialization code: Runs once when the study is added to the chart if (sc.SetDefaults) { sc.GraphName = "File Export"; // Name of the study sc.StudyDescription = "A basic example of an ACSIL study."; sc.AutoLoop = 1; // Enables auto-loop mode for processing each bar Input_240StoAngle.Name = "240 Rsi Angle"; Input_240StoAngle.SetChartStudySubgraphValues(0, 0, 0); OutputFile.Name = "Output File"; // Name the input for file path OutputFile.SetPathAndFileName(""); // Set the correct path to the file here return; } // .......... more code } I would probably use manual loop instead of auto loop and move the file open/close operation outside of the main loop, but that's just me. Do you mean to only write to file if the bar is closed? If so, you would change this if (sc.BaseData[SC_CLOSE][sc.Index])
for thisif (sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
SC_CLOSE is wrong, should be SC_LAST as below (and close is always latest price in bar, so current price is redundant). float openPrice = sc.BaseData[SC_OPEN][sc.Index]; float closePrice = sc.BaseData[SC_LAST][sc.Index]; float highPrice = sc.BaseData[SC_HIGH][sc.Index]; float lowPrice = sc.BaseData[SC_LOW][sc.Index]; |
[2025-01-07 06:15:08] |
User373245 - Posts: 47 |
Hmmmmmm cant get it to work. It's all logging correctly. I've tried a bunch of things. Placing the open and close outside the loop will not compile. I am on a Mac also using parallels and there is no letter to the pathway. Ive checked the file and all that to make sure changes can occur and all that to the file. I can send information to the file if I code it in R or python and it works. It's a csv file I make with textEdit and it's all configured to accept changes. The fact that I am going from parallels to the Mac and all the administration is set...would it log the file pathway if it was not accessing it ??? 2025-01-07 06:08:59.126 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | Current price: 82.371002 2025-01-07 06:08:59.126 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | Open Price: 82.364998 2025-01-07 06:08:59.126 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | High Price: 82.403503 2025-01-07 06:08:59.126 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | Low Price: 82.347000 2025-01-07 06:08:59.127 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | Close Price: 1.000000 2025-01-07 06:08:59.127 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | 60 SMA Value: 0.000000 2025-01-07 06:08:59.127 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | Attempting to write to file now... 2025-01-07 06:08:59.127 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | Formatted Data to Write: 82.371002, 82.364998, 82.403503, 82.347000, 1.000000, 0.000000 2025-01-07 06:08:59.127 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | CSV File Path: /Users/philiphoy/Desktop/Test.csv 2025-01-07 06:08:59.128 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | Data written to file. 2025-01-07 06:08:59.128 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | File closed after writing. 2025-01-07 06:09:03.425 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | Current price: 82.371002 2025-01-07 06:09:03.425 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | Open Price: 82.364998 2025-01-07 06:09:03.425 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | High Price: 82.403503 2025-01-07 06:09:03.425 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | Low Price: 82.347000 2025-01-07 06:09:03.425 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | Close Price: 1.000000 2025-01-07 06:09:03.425 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | 60 SMA Value: 0.000000 2025-01-07 06:09:03.425 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | Attempting to write to file now... 2025-01-07 06:09:03.425 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | Formatted Data to Write: 82.371002, 82.364998, 82.403503, 82.347000, 1.000000, 0.000000 2025-01-07 06:09:03.425 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | CSV File Path: /Users/philiphoy/Desktop/Test.csv 2025-01-07 06:09:03.425 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | Data written to file. 2025-01-07 06:09:03.425 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | File closed after writing. #include "sierrachart.h" #include <fstream> // For file operations #include <iostream> // For input/output operations SCDLLName("FILE Export11111"); SCSFExport scsf_FunctionName(SCStudyInterfaceRef sc) { // Set subgraph reference to OHLC SCInputRef Input_240StoAngle = sc.Input[1]; SCInputRef OutputFile = sc.Input[3]; // Initialization code: Runs once when the study is added to the chart if (sc.SetDefaults) { sc.GraphName = "File Export"; // Name of the study sc.StudyDescription = "A basic example of an ACSIL study."; sc.AutoLoop = 0; // Enables auto-loop mode for processing each bar Input_240StoAngle.Name = "240 Rsi Angle"; Input_240StoAngle.SetChartStudySubgraphValues(1, 6, 0); OutputFile.Name = "Output File"; // Name the input for file path OutputFile.SetPathAndFileName("/Users/philiphoy/Desktop/Test.csv"); return; } // Prepare the arrays for study data SCFloatArray sma60Array; sc.GetStudyArrayUsingID(Input_240StoAngle.GetStudyID(), Input_240StoAngle.GetSubgraphIndex(), sma60Array); // Main calculation code: Runs for each bar if (sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED){ // Get price data for logging float currentPrice = sc.BaseData[SC_LAST][sc.Index]; float openPrice = sc.BaseData[SC_OPEN][sc.Index]; float closePrice = sc.BaseData[SC_CLOSE][sc.Index]; float highPrice = sc.BaseData[SC_HIGH][sc.Index]; float lowPrice = sc.BaseData[SC_LOW][sc.Index]; // Log variables to string for logging data SCString openPriceLog, currentPriceLog, closePriceLog, highPriceLog, lowPriceLog, sma60Log; currentPriceLog.Format("Current price: %2f", currentPrice); sc.AddMessageToLog(currentPriceLog, 0); openPriceLog.Format("Open Price: %2f", openPrice); sc.AddMessageToLog(openPriceLog, 0); highPriceLog.Format("High Price: %2f", highPrice); sc.AddMessageToLog(highPriceLog, 0); lowPriceLog.Format("Low Price: %2f", lowPrice); sc.AddMessageToLog(lowPriceLog, 0); closePriceLog.Format("Close Price: %2f", closePrice); sc.AddMessageToLog(closePriceLog, 0); sma60Log.Format("60 SMA Value: %2f", sma60Array[sc.Index]); sc.AddMessageToLog(sma60Log, 0); sc.AddMessageToLog("Attempting to write to file now...", 0); // Use the dynamically set file path for CSV output SCString csvFilePath = OutputFile.GetString(); // Get file path from the input // Convert SCString to C-style string const char* filePath = csvFilePath.GetChars(); SCString formattedData; formattedData.Format("%2f, %2f, %2f, %2f, %2f, %2f\n", currentPrice, openPrice, highPrice, lowPrice, closePrice, sma60Array[sc.Index]); // Log the formatted data before writing to the file SCString logMessage; logMessage.Format("Formatted Data to Write: %s", formattedData.GetChars()); sc.AddMessageToLog(logMessage, 0); // Open the file in append mode (to avoid overwriting existing data) std::ofstream file(filePath, std::ios::app); // Log the file path for debugging purposes SCString filePathLog; filePathLog.Format("CSV File Path: %s", filePath); sc.AddMessageToLog(filePathLog, 0); // Check if the file opened successfully if (file.is_open()){ file << "Test Line, 1, 2, 3, 4, 5\n"; // Write the values to the CSV file file << currentPrice << ", " << openPrice << ", " << highPrice << ", " << lowPrice << ", " << closePrice << ", " << sma60Array[sc.Index] << "\n"; sc.AddMessageToLog("Data written to file.", 0); file.close(); // Close the file after writing sc.AddMessageToLog("File closed after writing.", 0); } else { // If the file didn't open, log an error message sc.AddMessageToLog("Failed to open file for writing.", 0); } } } |
[2025-01-07 08:30:57] |
User431178 - Posts: 579 |
I am on a Mac also using parallels and there is no letter to the pathway.
Ok, that makes sense then.Hmmmmmm cant get it to work. It's all logging correctly. I've tried a bunch of things. Placing the open and close outside the loop will not compile.
Yes, you can't just change to manual looping without also updating the code to suit.When you say placing outside the loop, I'm going to assume you mean placing outside the main function, as you don't have a loop set up in your study. Working with ACSIL Arrays and Understanding Looping would it log the file pathway if it was not accessing it
Yes, this line would print always, whether the path was valid or the file written.2025-01-07 06:09:03.425 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | CSV File Path: /Users/philiphoy/Desktop/Test.csv These lines, would only print if the write to file block executes. 2025-01-07 06:09:03.425 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | Data written to file. 2025-01-07 06:09:03.425 | Chart: AUDJPY[M] 5 Min #1 | Study: File Export | File closed after writing. So I would assume that you have some data being written. Are you actually getting any data added to the file? |
[2025-01-07 23:18:01] |
User373245 - Posts: 47 |
Ya this is just for establishing the file pathway...I have a regression model built out in R but in fear of over fitting or getting the Rubik's cube relative to time incorrect, I build it out on the SC front end and export the calculations with subgraphs study overlays to a spreadsheet and deal with the rest of the math on R on my computer. I know how to export to spreadsheet and just copy and paste for training the model but now I want to get it live or at least dynamic. I dont know the DTC so trying for end of bar with an extra condition to populate a csv file. Although any spot out of SC will work to any spot on my computer as long as the timing works(doesnt need to be super fast or at least less than or equal to the past of trading and email on a signal which has always worked...So I have a little time. I have a polling method in sync with the timing of UTC...I just need a way to get it out of SC so this is just to establish the pathway and the time...I have not built out the data that I am exporting yet..trying to get this to work first.
|
[2025-01-07 23:24:27] |
User373245 - Posts: 47 |
I deal in multiple time frames to various calculations and I dont trust myself as an engineer to build that when dealing with Machine learning. Its easy to get that wrong with timing and something can appear a lot better then it is if your engineering is off. So I like to build it from the SC sides that way I know its in sync with a live market and I dont over fit the from the engineering. If there is one thing SC charts has dialed is the replay config equal to live relative to any calculation you add! Hence the export of data. I am not good enough in C++ or ACSIl to build the model on SC side. Ive always built models in R
|
To post a message in this thread, you need to log in with your Sierra Chart account: