The resource loading... loading...

Built-in function_Cross analysis and instructions

Author: Inventors quantify - small dreams, Created: 2017-10-11 19:50:44, Updated: 2021-11-05 16:15:56

Built-in function_Cross analysis and instructions

The _Cross function in the global function array in the API documentation is used to calculate the cross state of two pointer lines

  • The function implements code similar to the following:

    It's important to note thatarr1It is defined as a fast-track indicator array.arr2When defined as a slow-line indicator array,_CrossThe function returns a positive value, i.e. according to the document.正数为上穿周期, 负数表示下穿的周期, 0指当前价格一样I know, right now.arr1Get up there.arr2It's been n cycles, at which point the fast line crosses the slow line to represent the golden fork. The same._CrossIf the function returns a negative number, it is a dead fork.

    If definedarr1For the slow-line indicator array,arr2For the fast-line indicator array, the opposite is true._CrossThe function returns a positive value for the dead fork._CrossThe function returns a negative value representing the gold fork.

// 返回上穿的周期数,正数为上穿周数,负数表示下穿的周数,0指当前价格一样
$.Cross = function(arr1, arr2) {            // 参数个数为2个,从参数名可以看出,这两个参数应该都是数组类型,数组就
                                            // 好比是在X轴为数组索引值,Y轴为指标值的坐标系中的线段,该函数就是判断两条线的 交叉情况 
    if (arr1.length !== arr2.length) {      // 首先要判断比较的两个数组长度是否相等
        throw "array length not equal";     // 如果不相等抛出错误,对于不相等的指标线无法判断相交
    }
    var n = 0;                              // 声明变量n用来记录交叉状态,初始0,未相交 
    for (var i = arr1.length-1; i >= 0; i--) {      // 遍历数组arr1,遍历顺序为从最后一个元素向前遍历
        if (typeof(arr1[i]) !== 'number' || typeof(arr2[i]) !== 'number') { // 当arr1或者arr2任何一个数组为非数值类型(即无效指标)时,跳出遍历循环
            break;                                  // 跳出循环
        }
        if (arr1[i] < arr2[i]) {                    // 如果arr1小于arr2则n--,会记录开始时arr1、arr2的相对状态,(即开始时n会根据arr1[i]、arr2[i]相对大小自行调整,一旦出现另一种和n状态相反的arr1[i]、arr2[i]大小关系,即发生了两条线交叉。)
            if (n > 0) {
                break;
            }
            n--;
        } else if (arr1[i] > arr2[i]) {             // 如果arr1大于arr2则n++
            if (n < 0) {
                break;
            }
            n++;
        } else {                                    // arr1[i] == arr2[i],则立即跳出
            break;
        }
    }
    return n;                                       // 返回n值,代表已经交叉了多少周期,0即指标值相等
};
  • So we simulate a set of data to be fed into this parameter and see what happens.

var arr1 = [1,2,3,4,5,6,8,8,9]     // 快线指标
var arr2 = [2,3,4,5,6,7,7,7,7]     // 慢线指标
function main(){
    Log("_Cross(arr1, arr2) : ", _Cross(arr1, arr2))
    Log("_Cross(arr2, arr1) : ", _Cross(arr2, arr1))
}

img

You can see that the result is 3, 3, 3.

img

As can be seen in the diagram, the position of the intersection occurs before the three K-line columns.


More

alphaStrategy00XSo if you don't have a cross, you should return 0.

And the cabbage.What if we cross back and forth?

Inventors quantify - small dreamsWell, let's think about that.

alphaStrategy00XThank you for your reply! I mean, returning 0 is more reasonable, right?

Inventors quantify - small dreamsWhat's up? var arr1 = [1, 2, 3, 4, 5, 6, 8, 8,9] // the fast-line indicator var arr2 = [2, 3, 4, 5, 6, 7, 7, 7] // the indicator of the slow line function main (()) { Log (("_Cross ((arr1, arr2) ": ", _Cross ((arr1, arr2)) Log (("_Cross ((arr2, arr1) ": ", _Cross ((arr2, arr1)) I'm not sure. What's up? You can use this to run a set of non-crossing arrays. I'm not going to return 0.

Inventors quantify - small dreamsOnly the most recent cross-section is detected, and source code analysis can tell.