Skip to content

A Java library to help you create Strategies/Rules/Indicators from Ta4j project in an easy way using a customizable JSON payload.

License

Notifications You must be signed in to change notification settings

thiaguimcavalcanti/ta4j-strategies-factory

Repository files navigation

Test main

Ta4j - Strategies Factory

1. Description

This library was created to convert a customized json payload into org.ta4j.core.Rule object from Ta4j project.

Example of payload

{
    "operator": "AND",
    "rules": [
        {
            "type": "RULE",
            "class": "UnderIndicatorRule",
            "parameters": [
                {
                    "type": "INDICATOR",
                    "class": "CCIIndicator",
                    "parameters": [
                        {
                            "type": "BAR_SERIES"
                        },
                        {
                            "type": "INTEGER",
                            "value": 20
                        }
                    ]
                },
                {
                    "type": "NUMBER",
                    "value": -100
                }
            ]
        },
        {
            "type": "RULE",
            "class": "TimeRangeRule",
            "parameters": [...]
        },
        {
            "type": "RULE",
            "class": "TimeRangeRule",
            "parameters": [...]
        },
        ... and so on
    ]
}

First Rule converted to a org.ta4j.core.Rule object (the other rules in the payload above were added only to exemplify)

img.png

2. Definitions

2.1. Json structure

Root level

Example of payload:

{
    "operator": "AND",
    "rules": [
        ...
    ]
}
Field Available Values
operator AND, OR
rules Array of Rules object

Rule object level

Example of payload:

{
    "type": "RULE",
    "class": "UnderIndicatorRule",
    "parameters": [ 
         {
            "type": "RULE",
            "class": "UnderIndicatorRule",
            "parameters": [...]
         }
         // OR
         {
            "type": "INDICATOR",
            "class": "CCIIndicator",
            "parameters": [...]
         }
         // OR
         {
            "type": "NUMBER",
            "value": -100
         }
         // OR
         {
            "type": "INTEGER",
            "value": 100
         }
         // and so on
     ]
}
Field Available Values
type RULE
class Full path of Ta4j Rule classes. E.g: org.ta4j.core.rules.UnderIndicatorRule
parameters Array with the required parameters in the constructors (at the same defined order) from Rule's classes

Real example:

public UnderIndicatorRule(Indicator<Num> indicator, Number threshold) {
    ...
}

To create an instance of this class, you should send a Rule object in this way:

{
    "type": "RULE",
    "class": "UnderIndicatorRule",
    "parameters": [
        {
            "type": "INDICATOR",
            "class": "CCIIndicator",
            "parameters": [
                {
                    "type": "BAR_SERIES"
                },
                {
                    "type": "INTEGER",
                    "value": 20
                }
            ]
        },
        {
            "type": "NUMBER",
            "value": -100
        }
    ]
}

Indicator object level

Example of payload:

{
    "type": "INDICATOR",
    "class": "CCIIndicator",
    "parameters": [
        {
            "type": "BAR_SERIES"
        },
        // OR
        {
            "type": "INDICATOR",
            "class": "CCIIndicator",
            "parameters": [...]
        }
        // OR
        {
            "type": "NUMBER",
            "value": -100
        }
        // OR
        {
            "type": "INTEGER",
            "value": 100
        }
        // and so on
    ]
}
Field Available Values
type INDICATOR
class Full path of Ta4j Indicator classes. E.g: org.ta4j.core.indicators.MACDIndicator
parameters Array with the required parameters in the constructors (at the same defined order) from Indicator's classes

Real example:

public CCIIndicator(BarSeries series, int barCount) {
    ...
}

To create an instance of this class, you should send an Indicator object in this way:

{
    "type": "INDICATOR",
    "class": "CCIIndicator",
    "parameters": [
        {
            "type": "BAR_SERIES"
        },
        {
            "type": "INTEGER",
            "value": 20
        }
    ]
}

Available Values references

Field Class
type JsonElementType.java
operator JsonOperatorType.java

3. How to use this library

This library can be used as a dependency in your project. So, you will be able to access all classes parsing your json payload in Ta4j classes as demonstrated previously.

3.1. How to parse a json payload into a Rule object

This is the class responsible for: RuleParser.java

You should initialize that class and call the parse method as demonstrated below:

String payload = "{\"operator\":\"AND\",\"rules\":[{\"type\":\"RULE\",\"class\":\"UnderIndicatorRule\",\"parameters\":[{\"type\":\"INDICATOR\",\"class\":\"CCIIndicator\",\"parameters\":[{\"type\":\"TIME_SERIES\"},{\"type\":\"INTEGER\",\"value\":20}]},{\"type\":\"NUMBER\",\"value\":-100}]}]}";

// 1. Initialize the class setting the BarSeries filled accordingly
RuleParser parser = new RuleParser(new BaseBarSeries());

// 2. call the parse method to convert the payload into an org.ta4j.core.Rule class
Rule rule = parser.parse(payload);

4. Customized Rules - Many possibilities

This library provides you to create many Rule's possibilities in a simple way, using JSON structure and with easy integration to your project.

Depending on your implementation, you can store this JSON payload in a database for example, and every time you receive a new ticket, a new call to the RuleParser sending the JSON to convert it into an org.ta4j.core.Rule to run your strategy.

5. Tools

About

A Java library to help you create Strategies/Rules/Indicators from Ta4j project in an easy way using a customizable JSON payload.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages