Storing trading strategy logic

Discussion in 'App Development' started by bln, Feb 17, 2015.

  1. bln

    bln

    I am courious how to you guys store the logic (the algorithm) of you strategies.

    Prior I have written the logic directly into the source code of the program. But why not use a common format like JSON to store the logic. That way you can share development with other developers without giving away your valuable secrets and IP.

    The logic can be loaded into backtesting or live trading engine for execution.
    Multiple strategies can be traded at the same time with different allocations.

    What do you think about using JSON or XML for this?

    Example of a EMA crossover system with fixed 6% profit target and fixed 2% SL:

    Code:
    {
        "strategy_name": "EMA 20 Crossover 1",
        "instrument": "SPY",
        "timeframe": "5 minutes",
        "signal": {
            "long": {
                "entry": "price > ema(20)",
                "exit": "price >= entry * 1.06 || price <= entry * (1-0.02)"
            },
            "short": {
                "entry": "price < ema(20)",
                "exit": "price <= entry * (1-0.06) || price >= entry * 1.02"
            }
        }
    }
     
  2. i'm using C#, so XML Serialization works for me to store strategy rules on disk.

    Code:
    [System.Serializable]
        [System.Xml.Serialization.XmlInclude(typeof(List<string>))]
        [System.Xml.Serialization.XmlInclude(typeof(List<TemplateRuleVariableType>))]
        public class TemplateRuleBlock
        {
            public TemplateRuleBlock() { }
            public string ClassName { get; set; }
    ...
    In my program, I simply load in this file as a new object instance.
    I have another XML serialized file just for the strategy inputs.
    So that means I can easily different swap instruments into the same strategy.
     
    Zzoom likes this.
  3. 2rosy

    2rosy

    I consider your json to be trading parameter configuration (not code or logic). You would still need code and logic to use that configuration
     
    Baron likes this.
  4. Well, if your using java, just serialize the object logic class.

    And when the program runs load the class dynamically using reflections.

    I use beanshell for an interactive command line.
     
  5. bln

    bln

    You're right. The logic needs to be expressed, either in one single file or chained files.

    May be something like this may work?

    Code:
    {
        "strategy": "foobar",
        "timeframe": "5m",
        "long": {
            "cond": {
                "expr": "price > ema(20)"
            },
            "and1": {
                "expr": "price > ema(50)",
                "or1": {
                    "expr": "b == c",
                    "andnot1": {
                        "expr": "c > d"
                    }
                }
            },
            "and2": {
                "expr": "sma(200) < ema(50)"
            }
            "true": "true sink, go to next logic block",
            "false": "false sink, go to next block or reevaluate current block until true"
        }
    }
     
  6. 2rosy

    2rosy

    considering that json and xml are not programming languages how would you actually use that code
     
  7. Butterfly

    Butterfly

    what a strange pointless thread,
     
  8. Jerry030

    Jerry030

    I agree with the above comment especially since the trading utility of such simple rule sets is of little long term utility. My trading "algos" are the result of training 100,000 of bars with SVM, DL, or Neural Networks applications. The resulting learned model when exported to C++, JAVA or XML runs several megabytes of code, none of which is human comprehensible.
     
  9. Trader13

    Trader13

    There is an of industry of products for protecting your source code from reverse engineering. Search under "C# obfuscator".
     
  10. Crikey you're brave. I would shudder with terror at the thought of running an 'algo' which wasn't comprehensible to any human being, and with that amount of data needed to describe it I'd be seriously concerned about overfitting.

    On topic, I've tried various ways of doing this in the past. It makes most sense for a large organisation, where you might want to limit the amount of new code being written, all of which needs to be tested and understood all over again. For an individual, I don't mind specifying my strategy entirely in code, which during the development phase is natural anyway. Once you've 'locked down' the design then you don't need the flexibility that the storage option offers you; if anything its a temptation to change or meddle with the strategy. I have a very small number of additional parameters, which can easily live in a small database.
     
    #10     Feb 23, 2015