Login Page - Create Account

Support Board


Date/Time: Wed, 05 Feb 2025 20:13:20 +0000



Post From: ACSIL export to csv

[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);
}
}
}