The resource loading... loading...

3.5 Strategy framework templates

Author: Inventors quantify - small dreams, Created: 2017-01-19 16:04:24, Updated: 2017-10-11 10:27:27

3.5 Strategy framework templates


Using the Strategy Framework template, simple trend-type strategies can be built with very little code, and hedge-type strategies can be written if a certain programming foundation exists.

img

  • Use the following steps:

    • 1, If you can find this template in Strategy Square in the picture above, copy it to the control center of your account.

    • 2, Refer to the policy template when writing the policy framework template, as shown below:

      img

  • Paste the policy code of the template template of the policy framework to test the policy framework template ((select the template template template of the policy framework template template already in the policy page):

    var TASK_IDLE = 0;          // 空闲状态命令
    var TASK_OPEN_LONG = 1;     // 建多仓命令
    var TASK_OPEN_SHORT = 2;    // 建空仓命令
    var TASK_ADD = 3;           // 加仓命令
    var TASK_ST = 4;            // 止损命令
    var TASK_COVER = 5;         // 平仓命令
    function onTick1() {        // 趋势系统1: 均线  具体买卖逻辑实现
        // MA 
        var records = _C(exchanges[0].GetRecords);
        if(records.length < 11){
            return $.TaskCmd(TASK_IDLE);
        }
        var ema_fast = TA.MA(records, 7);
        var ema_slow = TA.MA(records, 10);
        var data = "fast[-2]:" + ema_fast[ema_fast.length - 2] + " slow[-2]" + ema_slow[ema_slow.length - 2] + " fast[-1]:" + ema_fast[ema_fast.length - 1] + " slow[-1]:" + ema_slow[ema_slow.length - 1];
        $.AddData(0, "MA", data);
        if (ema_fast[ema_fast.length - 1] < ema_slow[ema_slow.length - 1] && ema_fast[ema_fast.length - 2] > ema_slow[ema_slow.length - 2]) {
            return $.TaskCmd(TASK_COVER);
        }else if(ema_fast[ema_fast.length - 1] > ema_slow[ema_slow.length - 1] && ema_fast[ema_fast.length - 2] < ema_slow[ema_slow.length - 2]){
            return $.TaskCmd(TASK_OPEN_LONG, 0.5);
        }
        return $.TaskCmd(TASK_IDLE);
    }
    function onTick2() {        // 趋势系统2:MACD  具体买卖逻辑实现
        // MACD
        var records = _C(exchanges[1].GetRecords);
        if(records.length < 15){
            return $.TaskCmd(TASK_IDLE);
        }
        var macd = TA.MACD(records);
        var dif = macd[0];
        var dea = macd[1]; 
        var data = "dif[-2]:" + dif[dif.length - 2] + " dea[-2]" + dea[dea.length - 2] + " dif[-1]:" + dif[dif.length - 1] + " dea[-1]:" + dea[dea.length - 1];
        $.AddData(1, "MACD", data);
        if (dif[dif.length - 1] > dea[dea.length - 1] && dif[dif.length - 2] < dea[dea.length - 2]) {
            return $.TaskCmd(TASK_COVER);
        }else if(dif[dif.length - 1] < dea[dea.length - 1] && dif[dif.length - 2] > dea[dea.length - 2]){
            return $.TaskCmd(TASK_OPEN_LONG, 0.8);
        }
        return $.TaskCmd(TASK_IDLE);
    }
    function main() {
        $.Relation_Exchange_onTick(exchanges[0], onTick1);    // 把 添加的第一个交易所  关联  趋势系统1 即 均线MA 
        $.Relation_Exchange_onTick(exchanges[1], onTick2);    // 把 添加的第二个交易所  关联  趋势系统2 即 MACD
        $.Trend();  // 不用传参数。                             // 启动模板
    }
    
  • Export functions are defined as:

    • The order:
    TASK_IDLE = 0;          // 空闲状态命令
    TASK_OPEN_LONG = 1;     // 建多仓命令
    TASK_OPEN_SHORT = 2;    // 建空仓命令
    TASK_ADD = 3;           // 加仓命令
    TASK_ST = 4;            // 止损命令
    TASK_COVER = 5;         // 平仓命令
    

    In the policy, these states must be defined, otherwise the template cannot be recognized.

    • 1, $.Relation_Exchange_onTick ((p1, p2)); Parameter p1: Exchange object, e.g. exchanges[0] i.e. the robot configuration page. The first exchange object added. Parameter p2: Custom transaction logic functions such as the onTick1 function in the example can be passed to the function name.

    • 2, $.TaskCmd ((p1, p2)); Parameters p1 and p2: Send commands to the template to execute, such as: TASK_OPEN_LONG // Build multi-shaft commands Parameter p2: The TASK_IDLE, TASK_COVER commands can be sent without passing parameters. Other commands need to follow a numerical parameter called p2, which indicates the number of operations to be performed. The call requires return $.TaskCmd ((p1, p2); is returned in the onTick function.

    • This is the third time that I've seen this. No parameters

    • 4, $.AddData ((p1, p2, p3)); // Add content to the end of the table in the status bar. Parameter p1 : the index of the table to be added, 0 for the first, 1 for the second ((assuming the second exchange has already been associated with $.Relation_Exchange_onTick) Parameter p2 : attribute name of the added content, in this case the data of the added indicator is displayed in the status bar tables;; ((MA and MACD)

      Parameter p3: String, converts the data to be displayed into a string passed to p3.

  • See the code analysis for the transaction logic function onTick1 in the example:

    function onTick1() {        // 趋势系统1: 均线  具体买卖逻辑实现
        // MA 
        var records = _C(exchanges[0].GetRecords); // 用跟 onTick1 函数 绑定的交易所 exchanges[0] 对象 获取该交易所的K线数据。
        if(records.length < 11){                   // 判断K线数据是否足够长度
            return $.TaskCmd(TASK_IDLE);           // K线数据长度不足时,发送等待命令。程序则不执行下面的代码。
        }
        var ema_fast = TA.MA(records, 7);          // 根据长度足够的K线数据计算 周期为7 的均线数据 即: 快线
        var ema_slow = TA.MA(records, 10);         // 计算 慢线
        var data = "fast[-2]:" + ema_fast[ema_fast.length - 2] + " slow[-2]" + ema_slow[ema_slow.length - 2] + " fast[-1]:" + ema_fast[ema_fast.length - 1] + " slow[-1]:" + ema_slow[ema_slow.length - 1];
        // 处理数据 组合为 字符串 data
        $.AddData(0, "MA", data);                  // 向状态栏表格 添加数据显示
        if (ema_fast[ema_fast.length - 1] < ema_slow[ema_slow.length - 1] && ema_fast[ema_fast.length - 2] > ema_slow[ema_slow.length - 2]) {               // 平仓触发判断
            return $.TaskCmd(TASK_COVER);          // 发送平仓命令
        }else if(ema_fast[ema_fast.length - 1] > ema_slow[ema_slow.length - 1] && ema_fast[ema_fast.length - 2] < ema_slow[ema_slow.length - 2]){           // 开仓触发判断
            return $.TaskCmd(TASK_OPEN_LONG, 0.5); // 发送开多仓命令
        }
        return $.TaskCmd(TASK_IDLE);               // 没有任何 触发,发送等待命令。
    }
    
  • Running shows:

    img

    img

  • Interaction with the Strategy Framework Template

    Since the template does not add interactive controls, the interactive controls can only be added by reference to the policy of the template template.The steps: 1, when adding a control of string type to the policy interaction, the control name is written JS_code as follows:img

    2, then click on the green plus sign, click Save.img

    3, all commands are displayed when the policy is running and can be directly copied and sent to the policy.img

    4, the command format is CMD ((index, CMD_STR, amount) The first parameter: index refers to which exchange the index is operating on, the position of the index is written as 0, that is, it represents operating the first exchange, and so on. The second parameter: the command that appears at the top of the form. The third parameter: the number of operations to be performed.

    For example:img

    Other commands use the same method.

The logic of the transaction is written casually, please do not play it!


More

The rhythm of the sunEvery time I have my doubts, I always find direction in the dream summary.

Inventors quantify - small dreamsI'm not going to say that I'm not happy about it, but I'm not going to say that I'm happy about it.