The _Cross function in the global function array in the API documentation is used to calculate the cross state of two pointer lines
It's important to note thatarr1
It is defined as a fast-track indicator array.arr2
When defined as a slow-line indicator array,_Cross
The function returns a positive value, i.e. according to the document.正数为上穿周期, 负数表示下穿的周期, 0指当前价格一样
I know, right now.arr1
Get up there.arr2
It's been n cycles, at which point the fast line crosses the slow line to represent the golden fork.
The same._Cross
If the function returns a negative number, it is a dead fork.
If definedarr1
For the slow-line indicator array,arr2
For the fast-line indicator array, the opposite is true._Cross
The function returns a positive value for the dead fork._Cross
The 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即指标值相等
};
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))
}
You can see that the result is 3, 3, 3.
As can be seen in the diagram, the position of the intersection occurs before the three K-line columns.
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.