Login Page - Create Account

Support Board


Date/Time: Wed, 27 Nov 2024 20:26:46 +0000



Post From: Executing external programs on various events

[2023-07-12 20:12:52]
User166108 - Posts: 73
JohnR, I wrote this to solve my problem, maybe you can use it as a starting point.

I also have a trade copier that I built.


#include "boost/format.hpp"
#include "boost/lexical_cast.hpp"
#include "boost/process.hpp"
#include "boost/process/windows.hpp"
#include "sierrachart.h"
#include <limits>

namespace bp = boost::process;

SCDLLName("Chart events plugin");

class ChartEvents
{
private:
double m_lastPosition = 0;

static void executeProgram(SCStudyInterfaceRef sc, std::string command,
std::vector<std::string> args)
{
SCString logMessage;
logMessage.Append("Executing command: ");
logMessage.Append(command.c_str());
logMessage.Append(" ");
for (auto const &arg : args)
{
logMessage.Append(arg.c_str());
logMessage.Append(" ");
}
sc.AddMessageToLog(logMessage, 0);
boost::process::spawn(command, boost::process::args(args),
bp::std_out > "output.log", bp::std_err > "error.log",
boost::process::windows::hide);
}

public:
void processPosition(SCStudyInterfaceRef sc, std::string const &pythonPath,
std::string const &onPositionChanged,
double currentPosition)
{
if (m_lastPosition == currentPosition)
return;

m_lastPosition = currentPosition;

if (onPositionChanged == "Unset" || onPositionChanged.empty())
return;

executeProgram(sc, pythonPath,
{onPositionChanged, sc.Symbol.GetChars(),
boost::lexical_cast<std::string>(currentPosition)});
}
};

SCSFExport scsf_ChartEvents(SCStudyInterfaceRef sc)
{
SCInputRef Input_PythonPath = sc.Input[0];
SCInputRef Input_OnPositionChange = sc.Input[1];

try
{
if (sc.SetDefaults)
{
sc.GraphName = "Chart events";
sc.StudyDescription = "Execute programs on chart events";
sc.GraphRegion = 0;
sc.FreeDLL = 1;
sc.AutoLoop = 0;

Input_PythonPath.Name = "Path to Python executable";
Input_PythonPath.SetString("");

Input_OnPositionChange.Name = "onPositionChangeCommand";
Input_OnPositionChange.SetDescription(
"Execute on position change: <your input> <current position>");
Input_OnPositionChange.SetString("");
}
else
{
auto ptr = (ChartEvents *)sc.GetPersistentPointer(1);
if (!ptr)
{
delete ptr;
ptr = new ChartEvents();
sc.SetPersistentPointer(1, ptr);
}

s_SCPositionData position;
sc.GetTradePosition(position);
ptr->processPosition(sc, Input_PythonPath.GetString(),
Input_OnPositionChange.GetString(),
position.PositionQuantity);

if (sc.LastCallToFunction)
{
auto ptr = (ChartEvents *)sc.GetPersistentPointer(1);
delete ptr;
sc.SetPersistentPointer(1, nullptr);
}
}
}
catch (std::exception const &e)
{
sc.AddMessageToLog(e.what(), 1);
}
}