19. 9. 2022

5 0

Reading a variable value in a rule triggered on Strategy Init from your strategy settings

In a rule triggered on Strategy Init, for each Assign function, StrategyQuant assign a “AssignVrbVleEntMlt” variable to your variable instead of a value.

Then, during initialization, it will give back the AssignVrbVleEntMlt value to your variable

From a strategy produced by the “H1388 Test Variable Template“, we can read in the code :

AssignVrbVleEntMlt = 210

AssignVrbVleEntMlt is receiving the value at the place MALength

Then AssignVrbVleEntMlt give back the value to MALength at Inialization.

1/ This Custom Analysis Code example show you how to get a value of your variable in a rule triggered on strategy Init

We get the value from a function “toInitValue“, from a ResultsGroup, and Rule nameInit“, and a variable name : MALength  (In this example)

Then we are using rg.specialValues() function to set the value in “CA_MALength

 

package SQ.CustomAnalysis;

import com.strategyquant.lib.*;
import java.util.ArrayList;
import com.strategyquant.datalib.*;
import com.strategyquant.tradinglib.*;
import SQ.Utils.Bouillant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*; 

// Created in France by Emmanuel Bouillant-Evrard
public class CA_InitStratsSettings extends CustomAnalysisMethod 
{
    public static final Logger Log = LoggerFactory.getLogger(CA_InitStratsSettings.class);

    public CA_InitStratsSettings() 
    {
        super("CA MALength Settings", TYPE_FILTER_STRATEGY);
    }
    
    //------------------------------------------------------------------------
    
    @Override
    public boolean filterStrategy(String project, String task, String databankName, ResultsGroup rg) throws Exception 
    {
        try 
        {
            Bouillant BouilltParaObj1 = new Bouillant();
            
            String IntTemp = BouilltParaObj1.toInitValue(rg, false,"Init", "MALength", 100); 
            rg.specialValues().set("CA_MALength", Double.parseDouble(IntTemp));

            return true;
      	} catch(Exception e)
        {
            Log.error("Error CA_InitStratsSettings", e);
            return false;																																							       // "Error: " + e.getMessage();
        }
    }
    
    
    //------------------------------------------------------------------------
    
    @Override
    public ArrayList<ResultsGroup> processDatabank(String project, String task, String databankName, ArrayList<ResultsGroup> databankRG) throws Exception {
        return databankRG;
    }
}

 

2 / Then the DatabankColumn code retrieve the value of the Custom Analysis

We are using rg.specialValues to retrive the value from CA_MALength
package SQ.Columns.Databanks;
import com.strategyquant.lib.*;
import com.strategyquant.datalib.*;
import com.strategyquant.tradinglib.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// Created in France by Emmanuel Bouillant-Evrard 
public class StratSettingsMALength extends DatabankColumn 
{
  public static final Logger Log = LoggerFactory.getLogger(StratSettingsMALength.class);
  public StratSettingsMALength()
  {
    super("MALength Settings", DatabankColumn.Decimal2, ValueTypes.Maximize, 0, 0, 100); 
  }
  
  //------------------------------------------------------------------------
  /**
   * This method should return computed value of this new column. You should typically compute it from the list of orders 
   * or from some already computed statistical values (other databank columns). 
   */
  @Override
  public String getValue(ResultsGroup rg, String resultKey, byte direction, byte plType, byte sampleType) throws Exception 
  {   
    try 
    {
      double value = rg.specialValues().getDouble("CA_MALength", -1);

      if(value == -1) 
      {
        // this means value was not set
        return NOT_AVAILABLE;
      } else 
      {
        // return value converted to String
        return java.lang.Double.toString(value);
      }
    } catch(Exception e)
    {
      Log.error("Error StratSettingsMALength", e);
      return "";																																							       // "Error: " + e.getMessage();
    }
  }
}

 

3/ How to install it

  • Import the snippets in the Code Editor
  • Compile All
  • Restart StrategyQuant 
  • then select the Custom Analysis in the Builder :

 

 

Then we add this new column to a Databank and read the value of our Init rules

If the Custom Analysis is not used anymore, the Column Databank must be removed to avoid any error in the log

My name is Emmanuel Bouillant-Evrard, Engineer specialized in Finance. I worked in Research and Development in Finance in USA, Canada and France for 24 years.

 

 

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

Related posts