Sumber daya yang dimuat... Pemuatan...

Analisis dan instruksi penggunaan tentang fungsi bawaan _Cross

Penulis:FMZ~Lydia, Dibuat: 2023-10-07 15:35:44, Diperbarui: 2024-01-02 21:17:16

img

Analisis dan instruksi penggunaan tentang fungsi bawaan _Cross

Fungsi _Cross dalam bagian Global Functions dari dokumentasi API digunakan untuk menghitung keadaan crossover dari dua indikator.

Implementasi fungsi mirip dengan kode berikut:

Perhatikan bahwa ketikaarr1didefinisikan sebagai rangkaian indikator cepat danarr2sebagai rangkaian indikator lambat, nilai yang dikembalikan oleh_Crossfungsi positif, yaitu, menurut dokumentasi,a positive number is the period of upward penetration, a negative number indicates the period of downward penetration, and 0 means the same as the current priceAku tidak tahu. Hal ini dapat dilihat, pada saat iniarr1ke atas menembusarr2telah n siklus, pada saat ini, itu garis cepat penetrasi ke atas garis lambat, mewakili crossup. Sama saja._Crossfungsi mengembalikan bilangan negatif, yaitu, crossdown.

Kebalikannya adalah benar jikaarr1didefinisikan sebagai rangkaian indikator lambat danarr2sebagai rangkaian indikator cepat. Nilai positif dikembalikan oleh_CrossFungsi mewakili crossdown. Nilai negatif dikembalikan oleh_CrossFungsi mewakili cross-up.

// Return to the number of periods of upward penetration, the positive number is the number of periods of upward penetration, the negative number indicates the number of periods of downward penetration, 0 means the same as the current price
$.Cross = function(arr1, arr2) {            // The number of parameters is 2, it can be seen from the parameter name, these two parameters should be an array type, the array is like an array in the X-axis for the array index value, Y-axis for the index value of the line in the coordinate system, the function is to determine the intersection of the two lines 
    if (arr1.length !== arr2.length) {      // The first step is to determine if the two arrays being compared are equal in length
        throw "array length not equal";     // Throw an error if they are not equal, it is not possible to determine the intersection of unequal lines.
    }
    var n = 0;                              // Declare the variable n to record the intersection state, initially 0, unintersected 
    for (var i = arr1.length-1; i >= 0; i--) {      // Iterate over the array arr1, from the last element forward.
        if (typeof(arr1[i]) !== 'number' || typeof(arr2[i]) !== 'number') { // When any of the arrays arr1 or arr2 is of a non-numeric type (i.e., an invalid indicator), the traversal loop is broken.
            break;                                  // break out of a loop
        }
        if (arr1[i] < arr2[i]) {                    // If arr1 is smaller than arr2 then n--, will record the relative state of arr1, arr2 at the beginning, (i.e., at the beginning n will adjust itself according to the relative sizes of arr1[i], arr2[i], and once there is another relationship between the sizes of arr1[i], arr2[i] opposite to the state of n, i.e., a crossing of the two lines has occurred.)
            if (n > 0) {
                break;
            }
            n--;
        } else if (arr1[i] > arr2[i]) {             // If arr1 is greater than arr2 then n++
            if (n < 0) {
                break;
            }
            n++;
        } else {                                    // arr1[i] == arr2[i], then immediately break
            break;
        }
    }
    return n;                                       // Returns the value of n, which represents the number of periods that have been crossed, and 0, which means that the indicator values are equal.
};

Mari kita simulasi satu set data yang dilewatkan ke parameter ini dan lihat bagaimana hasilnya

var arr1 = [1,2,3,4,5,6,8,8,9]     // fast line indicator
var arr2 = [2,3,4,5,6,7,7,7,7]     // slow line indicator
function main(){
    Log("_Cross(arr1, arr2) : ", _Cross(arr1, arr2))
    Log("_Cross(arr2, arr1) : ", _Cross(arr2, arr1))
}

img

Kita bisa melihat hasilnya adalah 3, -3.

img

Seperti yang Anda lihat dalam grafik, penyeberangan terjadi di depan tiga garis K-bar.


Lebih banyak