Login Page - Create Account

Support Board


Date/Time: Mon, 25 Nov 2024 17:37:53 +0000



Post From: Help Needed with Passing Data Between Two Studies (educational purposes)

[2024-02-26 12:23:47]
pierre.hh - Posts: 6
Hi Users/Sierra Team,

I'm posting this for educational purposes. I've created two simple studies: Study 1 and Study 2. Study 1 retrieves the volume coming from Time and Sales and displays it in the message log, and it does so correctly. There are other aspects that need to be adapted, but that's not the focus here.

The issue arises when trying to pass this data to another study (Study 2); I can't seem to retrieve the persistent variable from Study 1 and display it in the log. These studies aren't particularly useful in terms of practical application, but this is for educational purposes to understand how to pass data from one study to another for other studies I want to develop that rely on passing data.

Please find Study 1 (the one that works well and displays the data) and Study 2, which shows nothing.

I'm just at the beginning of learning C++ and ACSIL.

Thanks in advance.

PA

study1
#include "sierrachart.h"

SCDLLName("SimpleVolumeLogger")

// Define a unique key for the persistent variable to track the last processed volume
const int PV_LAST_TRADE_VOLUME_KEY = 301;

SCSFExport scsf_SimpleVolumeLogger(SCStudyInterfaceRef sc)
{
SCInputRef Input_DisplayLog = sc.Input[0]; // Define the input reference

if (sc.SetDefaults)
{
sc.GraphName = "Simple Volume Logger";
sc.StudyDescription = "Logs volume from Time and Sales and saves the last volume as a persistent variable.";

// Initialize the input
Input_DisplayLog.Name = "Display Log";
Input_DisplayLog.SetYesNo(1); // Default is YES

sc.AutoLoop = 0; // Manual looping
sc.UpdateAlways = 1; // Update always
return;
}

c_SCTimeAndSalesArray TimeSalesArray;
sc.GetTimeAndSales(TimeSalesArray);

if (TimeSalesArray.GetArraySize() == 0)
return; // No new Time and Sales data

// Loop through Time and Sales data in reverse to process the latest trades first
for (int Index = TimeSalesArray.GetArraySize() - 1; Index >= 0; --Index)
{
const s_TimeAndSales& TSItem = TimeSalesArray[Index];

// Check if logging is enabled
if (Input_DisplayLog.GetYesNo())
{
// Log the volume
SCString LogMessage;
LogMessage.Format("Volume: %d", TSItem.Volume);
sc.AddMessageToLog(LogMessage, 0);
}

// Save the last trade volume as a persistent variable
sc.SetPersistentInt(PV_LAST_TRADE_VOLUME_KEY, TSItem.Volume);
break; // Only process the latest volume and exit loop
}
}


Study2
#include "sierrachart.h"

SCDLLName("VolumeLoggerRetriever")

// Use the same unique key as the first study to access the persistent variable
const int PV_LAST_TRADE_VOLUME_KEY = 301;

SCSFExport scsf_VolumeLoggerRetriever(SCStudyInterfaceRef sc)
{
SCInputRef Input_DisplayLog = sc.Input[0]; // Define the input reference

if (sc.SetDefaults)
{
sc.GraphName = "Volume Logger Retriever";
sc.StudyDescription = "Retrieves and logs the last traded volume saved by the Simple Volume Logger study.";

// Initialize the input
Input_DisplayLog.Name = "Display Log";
Input_DisplayLog.SetYesNo(1); // Default is YES

sc.AutoLoop = 0; // Manual looping
sc.UpdateAlways = 1; // Update always
return;
}

// Retrieve the last trade volume from the persistent variable
int LastVolume = sc.GetPersistentInt(PV_LAST_TRADE_VOLUME_KEY);

// Check if there is any volume to log
if (LastVolume <= 0)
return; // Nothing to log if volume is not positive

// Check if logging is enabled
if (Input_DisplayLog.GetYesNo())
{
// Log the last volume
SCString LogMessage;
LogMessage.Format("Retrieved Volume: %d", LastVolume);
sc.AddMessageToLog(LogMessage, 0);
}
}