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

Scripting an Indicator

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

Scripting an Indicator

Postby stephan » Mon Mar 21, 2016 9:37 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 trading indicator does not know anything about the bot itself (so no last prices)

You could work around this limitation by making an script safety, but a safety has other properties and effects.


File location
The script files (and log files) of the trading indicators are located in "My documents\HTS\ScriptIndicators". 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 IndicatorResult { StayNoPosition, BuyLong, SellShort }
    public interface IIndicatorScript
    {
        string Name { get; }
        int TicksBack { get; }

        void Init();
        IndicatorResult GetResult(IndicatorContext 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 IndicatorContext
    {
        public string PriceSourceName { get; set; }
        public string PrimairyCoin { get; set; }
        public string SecondairyCoin { get; set; }
        public string ContractName { get; set; }

        public int Interval { 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.

Examples
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: https://t.me/haasonlineofficial

dha
Registered User
Posts: 6
Joined: Tue Dec 08, 2015 1:13 pm

Re: Scripting an Indicator

Postby dha » Sat Jun 11, 2016 10:08 pm

I think it would be a good move to post the c# code for all the indicators already in use in the haasbot platform.
That way it would be more open to user sided tweaking and optimisation.
Would you do that?

DirtbikesMM
Beta Tester
Posts: 22
Joined: Sat Dec 27, 2014 3:58 pm

Re: Scripting an Indicator

Postby DirtbikesMM » Mon May 29, 2017 8:52 pm

I agree some sample scripts for each indicator would be nice. I am looking for an aroon oscillator script. example if aroon oscillator > value then buy, aroon oscillator < value then sell. I am not very good at writing scripts, but if I have an example I could tweak it. Thanks.

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

Re: Scripting an Indicator

Postby pshai » Sat Jul 22, 2017 10:56 am

~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 3 guests