package SQ.CustomAnalysis;
import com.strategyquant.tradinglib.CustomAnalysisMethod;
import com.strategyquant.tradinglib.CustomAnalysisMethod.SettingType;
import com.strategyquant.tradinglib.ResultsGroup;
import com.strategyquant.lib.app.MainApp;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptContext;
import javax.script.SimpleScriptContext;
import java.io.FileReader;
import java.io.StringWriter;
/**
* CAJythonScript
*
* Runs an external Python script (via Jython) and stores its stdout
* in the ResultsGroup’s specialValues under the key “PythonOutput”.
*/
public class CAJythonScript extends CustomAnalysisMethod {
public CAJythonScript() {
// TYPE_FILTER_STRATEGY means this runs in the “Filter Strategies” step
super(“CAJythonScript”, TYPE_FILTER_STRATEGY);
// Example of adding a setting for the Python script folder (optional)
addSetting(“PythonFolder”, SettingType.String, “python”,
“Relative path under user/ where your .py files live”);
}
@Override
public boolean filterStrategy(
String project,
String task,
String databankName,
ResultsGroup rg
) throws Exception {
// Build path to your Python script
String pythonFolder = getSettingAsString(“PythonFolder”);
String basePath = MainApp.getDataPath() + pythonFolder + MainApp.getFS() ;
String scriptPath = basePath + “sqpython.py”;
// Prepare Jython engine with its own stdout capture
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName(“jython”);
// If your .py imports local modules in user/python/, add it to sys.path
engine.eval(“import sys; sys.path.insert(0, r\”” + basePath + “\”)”);
// Capture everything the Python script prints
StringWriter writer = new StringWriter();
ScriptContext ctx = new SimpleScriptContext();
ctx.setWriter(writer);
// Execute the Python script
engine.eval(new FileReader(scriptPath), ctx);
// Grab and trim the output
String pythonOutput = writer.toString().trim();
// Store it in the ResultsGroup for later use
rg.specialValues().set(“PythonOutput”, pythonOutput);
// Always return true so this CustomAnalysis doesn’t drop strategies
return true;
}
}
Getting libraries errors. That’s the 2nd portion that accesses the other class
package SQ.CustomAnalysis;
import com.strategyquant.tradinglib.CustomAnalysisMethod;
import com.strategyquant.tradinglib.ResultsGroup;
import com.strategyquant.lib.app.MainApp;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptContext;
import javax.script.SimpleScriptContext;
import java.io.FileReader;
import java.io.StringWriter;
public class CAJythonScript extends CustomAnalysisMethod {
public CAJythonScript() {
super(“CAJythonScript”, TYPE_FILTER_STRATEGY);
}
@Override
public boolean filterStrategy(
String project,
String task,
String databankName,
ResultsGroup rg
) throws Exception {
// prepare a writer to capture Python stdout
StringWriter writer = new StringWriter();
ScriptContext ctx = new SimpleScriptContext();
ctx.setWriter(writer);
// get the Jython engine and run your script
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName(“jython”);
// assume sqpython.py lives in your SQ data folder
String script = MainApp.getDataPath() + “sqpython.py”;
engine.eval(new FileReader(script), ctx);
// grab whatever Python printed
String pythonOut = writer.toString().trim();
// stash it in the result’s specialValues map
rg.specialValues().set(“PythonOutput”, pythonOut);
// always return true so filtering doesn’t drop your strategies
return true;
}
}