Playing JavaScript with the old man - creating a partner who will buy and sell 1) The boring life of a front-end middle-aged farmer

Author: The Little Dream, Created: 2017-03-06 09:57:41, Updated: 2017-10-11 10:36:41

Playing JavaScript with the old man, creating a partner who will buy and sell.

The Boring Life of a Frontline Farmer

These must be the common feelings of programmers. As a stray adult programmer, you can no longer be called small white at the level of JavaScript's cucumber, use the name old white! Although it is a cucumber level, busy with daily coding, but it does not prevent you from playing with JS, using your technology to find pleasure in life and work.

  • Discovered

    Old white as a late-comer farmer, I'm a little bit of the concept of trading securities, you can think of me as a programmer who has exploded in the futures market. It's good to learn C, C ++ programming languages when I was in college without skipping classes, these are helpful for my young age to quickly master JavaScript. It's okay to drink with friends (a friend is a privately funded spare commodity manipulator), also chat about the futures market.

    After a search, you can see that JS, Python, C/C++, JAVA, etc. can do programmatic transactions (JS only uses a lot of things, Python is self-learning), there is a lot to learn. But there is always something new to play with, so there is the next series of articles.

    A recently studied robotic program is a well-known foreign trading logic and commodity futures market, bring it to the domestic commodity market (there is a company called simnow that provides a service for simulated accounts).

    img

    img

    When I first started writing in JavaScript, I felt completely helpless, this is a completely different feeling from the front-end JS writing code, for a while the brain did not adapt, before looking at those flowers, green lines, columns on the stock software never considered how it was calculated. This time the common use has been understood, some even studied how to calculate (before I was superstitious about these lines, indicators), I have calculated what to know, these indicators are mostly based on the historical K line calculation, the essence is the same (can not in superstitious indicators!

    There are also a number of references. For example, the STOCH RSI indicator, which has very little information on the Internet, can only be checked by yourself.

    The old white comparison is LOW, please don't mind.

function LLV(array,period){
    if(!array || array.length - period < 0){
        throw "error:" + array;
    }
    var min = array[array.length - period];
    for(var i = array.length - period; i < array.length; i++){
        if( array[i] < min ){
            min = array[i];
        }
    }
    return min;
}

function HHV(array,period){
    if(!array || array.length - period < 0){
        throw "error:" + array;
    }
    var max = array[array.length - period];
    for(var i = array.length - period; i < array.length; i++){
        if( array[i] > max){
            max = array[i];
        }
    }
    return max;
}

function DeleteNullEle(initArr){
    var dealArr = [];
    var initArrLen = initArr.length;
    for(var i = 0,j = 0 ; i < initArrLen ; i++,j++){
        if(initArr[i] === null || isNaN(initArr[i]) ){
            j--;
            continue;
        }
        dealArr[j] = initArr[i];
    }
    return dealArr;
}

/*
LC := REF(CLOSE,1); //REF(C,1) 上一周期的收盘价
RSI:=SMA(MAX(CLOSE-LC,0),N,1)/SMA(ABS(CLOSE-LC),N,1) *100;
%K:     MA(RSI-LLV(RSI,M),P1)/MA(HHV(RSI,M)-LLV(RSI,M),P1)*100;  LLV(l,60)表示:检索60天内的最低价,可适应于检索任何股票
%D:MA(%K,P2);

LC := REF(CLOSE,1);
RSI:=SMA(MAX(CLOSE-LC,0),N,1)/SMA(ABS(CLOSE-LC),N,1) *100;
STOCHRSI:MA(RSI-LLV(RSI,M),P1)/MA(HHV(RSI,M)-LLV(RSI,M),P1)*100;
*/
function FstochRSI(records,n,m,p1,p2){
    var len = records.length;
    //var LC = records[len-2];//上一周期收盘价
    //var rsi = TA.RSI(records,n);// RSI 数组   ,talib
    var rsi = talib.RSI(records,n);
    rsi = DeleteNullEle(rsi);//ceshi

    var arr1 = [];
    var arr2 = [];
    var arr3 = [];
    var arr4 = [];
    var rsi_a = [];
    var rsi_b = [];
    var k = [];
    var d = null;

    /*不包含当前柱
    for(var a = 0 ;a < rsi.length ; a++ ){//改造 不用 LLV
        for(var aa = 0 ; aa <= a; aa++ ){
            rsi_a.push(rsi[aa]);
        }
        arr1.push(rsi[a] - TA.Lowest(rsi_a,m));
    }
    for(var b = 0 ;b < rsi.length ; b++ ){//改造 不用 HHV
        for(var bb = 0 ; bb <= b; bb++ ){
            rsi_b.push(rsi[bb]);
        }
        arr2.push(TA.Highest(rsi_b,m) - TA.Lowest(rsi_b,m));
    }
    */
    for(var a = 0 ;a < rsi.length ; a++ ){//改造 不用 LLV
        if(a < m){
            continue;
        }
        for(var aa = 0 ; aa <= a; aa++ ){
            rsi_a.push(rsi[aa]);
        }
        arr1.push(rsi[a] - LLV(rsi_a,m));
    }
    for(var b = 0 ;b < rsi.length ; b++ ){//改造 不用 HHV
        if(b < m){
            continue;
        }
        for(var bb = 0 ; bb <= b; bb++ ){
            rsi_b.push(rsi[bb]);
        }
        arr2.push(HHV(rsi_b,m) - LLV(rsi_b,m));
    }

    arr1 = DeleteNullEle(arr1);
    arr2 = DeleteNullEle(arr2);
    //Log("arr1:",arr1.length,"-",arr1);//ceshi
    //Log("arr2:",arr2.length,"-",arr2);//ceshi

    arr3 = talib.MA(arr1,p1);
    arr4 = talib.MA(arr2,p1);

    arr3 = DeleteNullEle(arr3);
    arr4 = DeleteNullEle(arr4);

    //Log("ceshi");//ceshi
    var c = 0;
    var diff = 0;
    if(arr3.length !== arr4.length){//实测 长度不相等
        throw "error: !=" + arr3.length + "----" + arr4.length;
        diff = arr4.length - arr3.length; //example   diff  =   10  -   6
    }else{
        //throw "error:" + arr3.length + "----" + arr4.length;
    }

    for( ;c < arr3.length ; c++ ){
        k.push(arr3[c] / arr4[c + diff] * 100);
    }

    d = talib.MA(k,p2);

    return [k,d,rsi];
}

It's like this:

img

I'll write here today and see you next time.https://www.fmz.com/bbs-topic/723

Programmer littleDream originally created


More

little315The cow!