READ-ONLY

The forum is now a read-only archive.

For bug reports & platform questions → [email protected]

Our community lives on Discord and YouTube — come join us!

StrategyQuant Jar Libraries Documentation

1 replies

louloufrenchy

Subscriber, bbp_participant, customer, community, sq-ultimate, 8 replies.

Visit profile

1 year ago #291229

Hi,

 

Am exploring CustomAnalysis for metrics. Am trying to get to relevant libraries.

 

Does the following exist DataManager, IDatabank, IBarsProvider ?

 

How do we get to the full list of libraries available to import in CodeEditor ?

 

Inside CodeEditor when I start typing com. a list comes up but would help if there’s details of each libraries purpose.

 

 

0

louloufrenchy

Subscriber, bbp_participant, customer, community, sq-ultimate, 8 replies.

Visit profile

1 year ago #291239

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

0

Viewing 1 replies (of 1 total)