Get script parameters

In the script, you can get the parameters by referring to the script fields (OptimProperty). Or through the ctx.Runtime.GetParameters () method, it returns the entire list of parameters. But it works correctly with a normal script run. During optimization, there will be parameters with irrelevant data. Value is always equal to the initial value of the parameter.

If you need to get script parameters from the code of another indicator, you can proceed as follows: The context has a script object (ctx.ScriptObject through the ContextExecutor class), so you can get the parameters through reflection.

Sample code:

using System.ComponentModel;
using TSLab.Script;
using TSLab.Script.Handlers;
using TSLab.Script.Handlers.Options;
using TSLab.Script.Optimization;

namespace MyLib.Handlers
{
    [HandlerCategory("MyLib")]
    [HelperName("TraceScriptParameters", Language = Constants.En)]
    [HelperName("TraceScriptParameters", Language = Constants.Ru)]
    [InputsCount(1)]
    [Input(0, TemplateTypes.SECURITY, Name = "SECURITYSource")]
    [OutputsCount(0)]
    [OutputType(TemplateTypes.DOUBLE)]
    [Description("Lists parameters")]
    public class TraceScriptParameters : IStreamHandler, ISecurityInputs, IContextUses
    {
        public IContext Context { get; set; }

        public void Execute(ISecurity source)
        {
            var ctx = Context as TSLab.ScriptEngine.ContextExecutor;
            if (ctx == null)
                return;

            Context.Log("=== Parameters ===", MessageType.Info, true);
            var fields = ctx.ScriptObject.GetType().GetFields();
            foreach (var field in fields)
            {
                var obj = field.GetValue(ctx.ScriptObject);
                var value = GetValue(obj);
                Context.Log($"{field.Name}={value}", MessageType.Info, true);
            }
        }

        private object GetValue(object obj)
        {
            switch (obj)
            {
                case OptimProperty pDouble:
                    return pDouble.Value;
                case IntOptimProperty pInt:
                    return pInt.Value;
                case BoolOptimProperty pBool:
                    return pBool.Value;
                default:
                    return null;
            }
        }
    }
}

Last updated