Announcement

Collapse
No announcement yet.

Tick counter

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Tick counter

    I am having trouble with the code below. I am trying to just pass a symbol to a funtion udTick and have it return a 1, -1 or 0 which is whether the symbol is on uptick, downtick or unchanged. It works fine as it is below, but if i uncomment out the line for nq #f, the number returned is always the same, but if i check it in the function itself, it works properly. For some reseaon when i run the function more than once, the return numbers dont change. Any ideas? Another question, using the dde command winros in excel, what is the variable to see whether a stock is on up or down tick?




    var ntick_1 = null;
    var ntick = null;

    function udTick(symbol) {
    var tc = 0;
    if (getCurrentBarIndex() < 0) return;


    ntick_1 = ntick;
    ntick = close(0,symbol);

    if (ntick > ntick_1) {
    //uptick
    tc = 1;
    } else if (ntick < ntick_1) {
    //downtick
    tc = -1;
    }
    // debugPrint(tc);
    return tc;
    }



    function main(){
    var counter = 0;
    var counter1 = 1;
    var counter2 = 2;
    var counter3 = 0;
    //counter = udTick("nq #f");
    counter1 = udTick("es #f");
    counter3 = counter + counter1 + counter2;

    debugPrintln("counter---->"+ counter);
    debugPrintln(" count #1---> " + counter1 );
    debugPrintln( " count #2---> " + counter2);
    debugPrintln( " count #3---->" + counter3);

  • #2
    Hello jarrodb,

    The key logic problem that your dealing with is how you are using the ntick and ntick_1 global variables. In main(), you are passing two different symbols tothe udTick() function in succession. Therefore, when you pass nq #f, ntick gets assigned to the current value of nq. Immediately after, you are passing es #f. At this point ntick_1 gets assigned to the value of ntick, which is storing nq #f. When your conditions in udTick() are evaluated you are comparing the previous tick of nq to the current tick of es.

    You need to add some logic to udTick() that checks for the value of the symbol and assign the tick values to a set of unique global variables for each symbol you plan to pass to the function. The easiest way to do this is by turning ntick and ntick_1 into arrays and use your symbol variable in the array references to make use of named arrays. This will keep the tick values of different symbols you pass to it separated so that your conditions in udTick() compare the proper values and not mix es and nq values. Try the following. I disabled the debug statements in main() and added a new one in udTick() so that you can see the values used for each symbol within the execution of udTick().

    PHP Code:
    //var ntick_1 = null;
    var ntick_1 = new Array();
    //var ntick = null;
    var ntick = new Array();

    function 
    udTick(symbol) {
        var 
    tc 0;
        if (
    getCurrentBarIndex() < 0) return;


        
    //ntick_1 = ntick;
        //ntick = close(0, sym(symbol));
        
    ntick_1[symbol] = ntick[symbol];
        
    ntick[symbol] = close(0sym(symbol));

        if (
    ntick[symbol] > ntick_1[symbol]) {
            
    //uptick
            
    tc 1;
        } else if (
    ntick[symbol] < ntick_1[symbol]) {
            
    //downtick
            
    tc = -1;
        }
        
    // debugPrint(tc);
        
    debugPrintln(symbol " _0: " ntick[symbol] + "  _1: " ntick_1[symbol] + " tc: " tc);

        return 
    tc;
    }



    function 
    main(){
        var 
    counter 0;
        var 
    counter1 1;
        var 
    counter2 2;
        var 
    counter3 0;
        
    counter  udTick("nq #f");
        
    counter1 udTick("es #f");
        
    counter3 counter counter1 counter2;

        
    //debugPrintln("counter---->"+ counter);
        //debugPrintln(" count #1---> " + counter1 );
        //debugPrintln( " count #2---> " + counter2);
        //debugPrintln( " count #3---->" + counter3);
        
        
    return;

    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment

    Working...
    X