Script Bot

From Haasonline Software Wiki
Jump to: navigation, search

Haasbot's Script Bot is an custom trade bot for which you can define (by C# programming) the behaviour of the bot yourself. The script will receive price updates and can retrieve information like the available balance. The script can then create or cancel orders on the exchanges.


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


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


All script bot scripts must implement the IScriptBotScript-interface:

    public interface IScriptBotScript
        void Init();
        void Update(ScriptBotContext context);
        List<DataSerie> ChartDataSeries { get; }
        List<double> GetChartData(ScriptBotContext context);
        List<ScriptParameter> GetParameters();
        void SetParameters(Dictionary<string,object> parameters);

You can either to this by implementing all methods and properties of this interface, or by inheriting of ScriptBotScriptBase. When inheriting of ScriptBotScriptBase, you only have to override the methods and properties you want to be different than the default behavior.

This property returns the name of the bot. We advise to always override this property so you can give your bot a meaningful name.
This method is executed after creating the script-object. 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.
This method will be executed every time there is a price update. In this method you can decide what, if any, action should be taken. An ScriptBotContext-object is passed, containing current price details, open order details,... and a reference to the ScriptBotAPI, giving access to historical price data and bot-operations to create and cancel orders. Of course, overriding this method is a minimum requirement to have a meaningful script bot.
ChartDataSeries is a property defining the chart lines which will be shown on the indicator chart. To change this, this property has to return a list of DataSerie-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 (1 or 2) 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. When the ChartDataSeries returns a non-empty list, the GetChartData-method should be overriden too.
This method returns the data points which have to be shown in the chart(s). It should return a list of double's, in the same order as the DataSeries returned by the ChartDataSeries-property. The length of the list returned by the ChartDataSeries-property should be the same length as the list returned by GetChartData. An ScriptBotContext object is passed to this method. This method is called after calling Update(), so if any chart data is generated in the Update-method, you can store it in a class member, and return it in GetChartData to be shown on the indicator chart.
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.
This method is called when the script parameters are saved 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.


In script bot 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. The PriceHistory-property in the ScriptBotContext and the result of the method GetPriceInstrument in the ScriptBotAPI 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 IScriptBot-interface is the first class in the file.


Compile errors

Compile errors are logged to the Haasbot log. Open the Log Viewer, disable all severities and enable 'SCRIPTBOT'. The error message will contain the compiler error and the line number in the script file. This information is also available on the web interface (open the scriptbot details).

Writing to log-file

You can write debug-info to a log file, by calling Logger.LogToFile(message) on the ScriptBotContext-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.

Example script

This examplescript will log a line to the log file. As long as there are open orders on the selected exchange, that's all the bot does. When there are no open orders, the script will check if the current ask price is lower than the configured threshold price (now 300). If it is, and we haven't bought anything in the last ten minutes, the script will create a market order for 10% of the maximum amount we can buy.

  1. using System;
  2. using System.Drawing;
  3. using System.Collections.Generic;
  4. using HaasbotScripting;
  5. using HaasbotScripting.DataObjects;
  7. public class ExampleScript : ScriptBotScriptBase
  8. {
  9. 	private const decimal BUYTHRESHOLD = 300;
  11. 	private DateTime lastBuy = DateTime.MinValue;
  13. 	public override void Update(ScriptBotContext context)
  14. 	{
  15. 		context.Logger.LogToFile("Updating bot");
  16. 		if(context.OpenOrders.Count > 0) {
  17. 			return; //When there are still open orders on the exchange, do nothing
  18. 		}
  21. 		if(currentAsk < BUYTHRESHOLD && (DateTime.Now - lastBuy).TotalMinutes > 10)
  22. 		{
  23. 			lastBuy = DateTime.Now;
  24. 			decimal availableMoney = context.API.GetAvailableBalance(context.ExchangePlatform, context.SecondaryCurrencyCode);
  25. 			decimal maxBuyAmount = availableMoney / (decimal)currentAsk;
  26. 			context.API.CreateMarketOrder(context.ExchangePlatform, context.PrimaryCurrencyCode, context.SecondaryCurrencyCode, OrderType.Buy, maxBuyAmount * 0.1M);
  27. 		}
  28. 	}
  30. 	public override void Init() 
  31. 	{
  32. 		//Do any initialization of the bot here
  33. 	}
  35. }