This board has been archived, no new registrations are allowed. Please come join us on our discord!

Scripting an Safety

General discussion about the C# Script Indicators, Safeties and Insurances
User avatar
Posts: 1047
Joined: Fri Sep 12, 2014 9:37 am

Scripting an Safety

Postby stephan » Wed Mar 23, 2016 2:32 pm

Design properties
This software is build on the layer model and OOP. It means things are sandboxed and this will also apply to all the scripted abilities. As an result of this the following limitations are set;

- An safety is NOT checked by insurances, if it tells to buy the bot will buy and if it says sell the bot will just sell.
- Safeties are always updated each minute, this is locked.

You could work around this limitations by making an script trading indicator, but a indicator has other properties and effects.

File location
The script files (and log files) of the trading indicators are located in "My documents\HTS\ScriptSafeties". The .cs files are the files containing the script and the .log files are the logbook files.

The files are indexed on-the-fly, so in general no restart if needed if you add a new file. In the worst case you need to refresh your browser with F5 and then it will show up.

Code logic
Every coding is hard if you do not know where to begin or how it works. You could just try to read the example code, but lets have an closer look here.

The principle is to implement a interface inside the .cs file and the interface looks like this:

Code: Select all

    public enum SafetyResult { DoNothing, BuyLong, SellShort }
    public interface ISafetyScript
        string Name { get; }
        int TicksBack { get; }

        void Init();
        SafetyResult GetResult(SafetyContext context);

        List<ScriptParameter> GetParameters();
        void SetParameters(Dictionary<string, object> parameters);

        List<ChartRecord> GetChartLines();
        List<decimal> GetChartData();

The Name property is called to obtain the name of your scripted indicator and the TicksBack is used to tell how much history depth this indicator need to do its calculation.

At very first start the "Init()" function is called. This is so you can maybe prepare a few things inside your code if needed.

During runtime (and backtesting!) the function "GetResult()" is called, it comes with an context and its expected to deliver an result when executed.

The "GetParameters()" function is used to get the input fields you want to have displayed on your screen when setting up this indicator.

These are the basic and most important things to know about how to setup the logic. Now, lets have a closer look to the "GetResult()" function, because it comes with the context of how the calculation need to be done.

Code: Select all

    public class SafetyContext
        public string PriceSourceName { get; set; }
        public string PrimairyCoin { get; set; }
        public string SecondairyCoin { get; set; }
        public string ContractName { get; set; }

        public decimal LastBuyPrice { get; set; }
        public decimal LastSellPrice { get; set; }

        public decimal LastLongPrice { get; set; }
        public decimal LastShortPrice { get; set; }

        public int TicksBack { get; set; }

        public int Offset { get; set; }

        public IPriceInstrument PriceInstrument { get; set; }
        public PriceTick PriceTick { get; set; }

        public IndicatorLog Logger { get; set; }

        public IIndicatorAPI API { get; set; }

So inside the context you can get all the information you might need.

It even comes with an own small API which can be used to get more information.

Code: Select all

    public interface IIndicatorAPI
        List<string> GetAllPriceSources();
        List<string> GetEnabledPriceSources();
        List<string> GetPriceMarkets(string pricesourceName);

        IPriceInstrument GetPriceInstrument(string pricesourceName, string primairyCoin, string secondairyCoin, string contractName, int interval, int ticksBack);
        PriceTick GetPriceTick(string pricesourceName, string primairyCoin, string secondairyCoin, string contractName);
        PriceTick GetMinutePriceTick(string pricesourceName, string primairyCoin, string secondairyCoin, string contractName);

Development Environment
We are working on live code editing, but this might not be the best way to code your script. Instead it might be much easier if you installed an development environment like Visual Studio or Xamarin locally and use that as a base. If you choose this easier way then all you have to do is include the open library named "TradeServer.ScriptingDriver.dll" and then you will get the required interface and you can have a deeper look to the available functionality. For the calculation you add in the libary "TA-Lib-Core.dll" to get its interface.

We will be adding a few examples as new topics to this part of our forum. We hope we can motivate everybody to share there work like we did.
Join the telegram group too:

User avatar
King of Scripts
Posts: 284
Joined: Fri Mar 18, 2016 11:33 am

Re: Scripting an Safety

Postby pshai » Fri Aug 11, 2017 11:26 am

Missing BotPosition field in SafetyContext! :| Please update to match the current version...
~Thou shall profit if thou art patient.~

Check out my kingdom for custom scripts for trade bots here!

Return to “C# Script Indicators, Safeties and Insurances”

Who is online

Users browsing this forum: No registered users and 1 guest