Script Indicators (C#)

HTS's Script Indicator is a custom Indicator for the Trade Bot. This feature allows you to create and share your own indicators.

Check out Pshai's scripts for a set of pre-made custom indicators for use with your custom bot.

Please note that the built-in Script Editor does not work with C# scripts. Please use your preferred text editor or IDE to create your own or edit the ones provided here.

pagePshai Scripts (C#)

Settings

The Script Indicator has only one setting: the full path to the script.

Language

The scripts are C#-scripts, using the CS-Script CLR.

Interface

All indicator scripts must fully implement the IIndicatorScript-interface:

public interface IIndicatorScript
{
        string Name { get; }
        int TicksBack { get; }
        void Init();
        IndicatorResult GetResult(IndicatorContext context);
        List<ChartRecord> GetChartLines();
        List<decimal> GetChartData();
        List<ScriptParameter> GetParameters();
        void SetParameters(Dictionary<string,object> parameters);
}

Name

This property returns the name of the indicator. Haasbot will use this name for the indicator, instead of the general "Script Indicator", so it is clear for the user which script is being used.

string Name { get { return "My Indicator"; } }

TicksBack

With this property you can configure how much price data (Bid, Ask, High, Low, Volume) must be kept in memory by the IndicatorContext. Make sure this is big enough to have enough data to do the necessary calculations in your script, while keeping it small enough to reduce memory usage and unnecessary CPU load when doing these calculations.

using TicTacTec.TA.Library;
// ...
int TicksBack { get { return Core.SmaLookback(sma_length); } }

Init

This method is executed after creating the script-object the first time. You can use this to do any necessary initialization. The created script-object will stay in memory and will be used until the script is recompiled. An indicator-script is recompiled when Haasbot starts and when the Script Indicator settings-window is closed.

GetResult

This method will be executed every time Haasbot checks the indicator. It should return an IndicatorResult (Stay, Buy or Sell). An IndicatorContext-object is passed, containing current price details and a reference to the IndicatorAPI, giving access to historical price data for all supported platforms and currency pairs. The default behavior of this method is to always return Stay.

GetChartLines

GetChartLines is a property defining the chart lines which will be shown on the indicator chart. This property has to return a list of ChartRecord-objects, which define the properties of one chart line.For each line in the chart, you have to define a name, on which chart it has to be shown (0 on main chart - 1,2 and 3 will create their own charts) and optionally a color. When no color is defined, Haasbot assigns a color automatically. For an example, see lines 65 to 76 in the example script.

GetChartData

This method returns the data points which have to be shown in the chart(s). It should return a list of decimal's, in the same order as the ChartRecords returned by the GetChartLines-property. The length of the list returned by the GetChartLines-property should be the same length as the list returned by GetChartData. This method is called after calling GetResult(), so if any chart data is generated in the GetResult-method, you can store it in a class member, and return it in GetChartData to be shown on the indicator chart.

GetParameters

This method returns the parameters the user can configure for this script. It should return a list ScriptParameter objects. These ScriptParameter objects define for each parameter the name, current value, type and optionally some info text shown to the user. The user will be able to configure these parameters in the Haasbot user interface. The current value has to be cast into string.

List<ScriptParameter> GetParameters()
{
        return new List<ScriptParameter>
        {
                new ScriptParameter("SMA Length", ScriptParameterType.Integer, sma_length.ToString()),
                new ScriptParameter("A Useless Decimal", ScriptParameterType.Decimal, (123.456M).ToString()),
                new ScriptParameter("Foo", ScriptParameterType.String, "Bar")
        }
   }

SetParameters

This method is called when the script parameters are modified in the Haasbot user interface. The argument is a dictionary with the parameter names as dictionary-keys, and the parameter values as dictionary-values. The type of the values is object, so you need to cast them to the correct type (the same type as you defined in the ScriptParameter in GetParameters). When only specific values or range of values is valid for a certain parameter, you can do the checks here and revert to the previous value if an invalid value was set by the user.After SetParameters is called, the name of the script (the Name-property) is retrieved again. This way, when parameters are used in the name, the name is updated when the parameters are updated.

void SetParameters(Dictionary<string, object> parameters)
{
        sma_length = Convert.ToInt32( parameters["SMA Length"] );
}

TA-Lib

In an indicator script you have access to TA-Lib for calculating any technical analysis indicators. All functions are static members of the static class Core in the namespace TicTacTec.TA.Library. It is not necessary to include external references to use TA-Lib, but having them allows the of your IDEs intellisense

The PriceInstrument property in the IndicatorContext contains multiple arrays of doubles (Asks, Bids, High, Low, Volume), which can directly be used as input for the TA-Lib-functions.

Creating classes

You can define your own classes and structs in a script. You only have to make sure the class implementing the IIndicatorScript-interface is the first class in the file.

Debugging

Writing to log-file

You can write debug-info to a log file, by calling Logger.LogToFile(message) on the IndicatorContext-object. The message will be written (together with a timestamp) to a log-file. The log-file has the name of the script and is located at the Haasbot execution path.

ScriptParameter

ScriptParameter is the type used to define user-configurable parameters in Script Indicators.

ScriptParameter interface:

    public enum ScriptParameterType { String, Integer, Decimal, Double, DateTime } //Supported parameter types
    public class ScriptParameter
    {
        public ScriptParameter();

        public ScriptParameter(string name, ScriptParameterType type, string defaultValue, string info = "");

        public string Name { get; set; } //Name of the parameter

        public object Value { get; set; } //Current value of the parameter

        public ScriptParameterType Type { get; set; } //Type of the parameter
        
        public string Info { get; set; } //Optional info message shown to the user

    }

Last updated