Introduction to Scripter

Author: Mark Fric

May 14th, 2015

Rating:
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Note! Scripter functionality is experimental for now, and the class hierarchy and its names could be changing!
Also, Scripter and Program interface documentation is in progress, and will be extended in the following weeks.

 

What is Scripter

Generally, Scripter offers Quant Analyzer user a way to use program features without using user interface. It is something like Macro functionality in Excel.

Scripter is for advanced users, because you’ll need to do some programing there. The script is a simple Java code that calls various features of the program.

 Do you want to make a Monte Carlo simulation? You can do it using Scripter, defining all the parameters and storing the result into databank or displaying the key values in the output field.

 

Why to use Scripter

Scriper allows automation. It enables you to automatize things that you’d normally have to do in user interface by hand

For example, let’s say  you want to:

  • load some report
  • use What-If to createvariants of the system – with reports, where only Monday trades are removed, or where only Tuesday, trades are removed, etc.
  • run monte carlo simulation on each variation and get its results at given confidence levels
  • choose the best MC simulation and run money management simulation on it with various methds

You can do all this by hand, by clicking back and forth with the program, but if you plan to do this process often then it could make sense to write a script for it.

 

Example

We’ll make a sample script that will load the report, displays its Net Profit, then run Monte Carlo simulation and display MC Net Profit in 90% confidence level.

 

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.strategyquant.lib.results.SQResultsGroup;
import com.strategyquant.lib.scripter.Program;
import com.strategyquant.lib.snippets.MonteCarloSim.ConfidenceLevelResults;
import com.strategyquant.lib.snippets.MonteCarloSim.MonteCarloSimulationResults;
import com.strategyquant.lib.snippets.MonteCarloSim.MonteCarloStatValues;
import com.strategyquant.lib.utils.ExtendableStorage;
import com.strategyquant.lib.utils.SQConst;
import com.strategyquant.lib.results.SQResult;
import com.strategyquant.lib.results.SQStats;
import com.strategyquant.lib.results.StatsConst;

public class MonteCarloSample2 implements Runnable {
public static final Logger Log = LoggerFactory.getLogger("MonteCarloSample");

    @Override
    public void run() {
        try {
            SQResultsGroup strategyResults = (SQResultsGroup) Program.get("Loader").call("loadFile", "./tests/data/loaders/SQ_Portfolio1.stp");

            // display original net profit
            SQResult result = strategyResults.getResult(SQConst.SYMBOL_PORTFOLIO);
            SQStats stats = result.getStats(SQConst.DIRECTION_ALL, SQConst.PL_IN_MONEY, SQConst.KEY_SAMPLE_ALL);
            SQStats statsPct = result.getStats(SQConst.DIRECTION_ALL, SQConst.PL_IN_PCT, SQConst.KEY_SAMPLE_ALL);

            System.out.println("Original NetProfit="+stats.getDouble(StatsConst.NET_PROFIT));
            System.out.println("Original PctNetProfit="+statsPct.getDouble(StatsConst.NET_PROFIT));

            // run Monte Carlo and display Net Profit at 90% confidence level
            ExtendableStorage settings = (ExtendableStorage) Program.get("MonteCarlo").call("getSettings", null);
            settings.set("NumberOfSimulations", 10);
            settings.set("Result", strategyResults);
            settings.set("Methods.SkipTradesRandomly.Use", true);

            MonteCarloSimulationResults simulationResults = (MonteCarloSimulationResults) Program.get("MonteCarlo").call("run", settings);

            System.out.println("# simulations="+simulationResults.simulationsList.size());

            //compute confidence levels
            settings = (ExtendableStorage) Program.get("MonteCarloConfidenceLevels").call("getSettings", null);
            settings.set("SimulationResults", simulationResults);
            settings.set("Result", strategyResults);

            ConfidenceLevelResults confidenceLevelResults = (ConfidenceLevelResults) Program.get("MonteCarloConfidenceLevels").call("run", settings);
            MonteCarloStatValues mcResult = confidenceLevelResults.get(90);

            System.out.println("MC 90% NetProfit="+mcResult.netProfit);
            System.out.println("MC 90% PctNetProfit="+mcResult.pctNetProfit);
        } catch(Exception e) {
            e.printStackTrace();
            Log.error("Exception :", e);
        }
    }
}

 

Leave a Reply

Please Login to comment