Announcement

Collapse
No announcement yet.

Stochastic Momentum Index (SMI) formula error

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

  • Stochastic Momentum Index (SMI) formula error

    Stochastic Momentum Index (SMI) formula error

    The SMI Formula is as followed:


    I try to implement it in EFS. However, I'm not quite familiar with efs logic. I keep on get TypeError: xxx is null.

    Following is my code:

    Code:
    ************************************************** ***************
    Provided By:
    Interactive Data Corporation (Copyright © eSignal) 2010.
    All rights reserved. This sample eSignal Formula Script (EFS)
    is for educational purposes only. Interactive Data Corporation
    reserves the right to modify and overwrite this EFS file with
    each new release.
    ************************************************** ****************/
    
    var fpArray = new Array();
    
    function preMain(){
    
    setPriceStudy(false);
    setStudyTitle("stochastic momentum index");
    setCursorLabelName("SMI", 0);
    setCursorLabelName("SIM Signal", 1);
    setDefaultBarFgColor(Color.RGB(0x00,0x94,0xFF), 0);
    setDefaultBarFgColor(Color.RGB(0xFE,0x69,0x00), 1);
    setPlotType(PLOTTYPE_LINE,0);
    setPlotType(PLOTTYPE_LINE,1);
    setDefaultBarThickness(1,0);
    setDefaultBarThickness(1,1);
    
    var x=0;
    fpArray[x] = new FunctionParameter("Klen", FunctionParameter.NUMBER);
    with(fpArray[x++]){
    setName("%K Length");
    setLowerLimit(2);
    setDefault(10);
    }
    fpArray[x] = new FunctionParameter("Dlen", FunctionParameter.NUMBER);
    with(fpArray[x++]){
    setName("%D Length");
    setLowerLimit(1);
    setDefault(3);
    }
    }
    
    var bInit = false;
    var ll = 0;
    var hh = 0
    var diff = 0;
    var rdiff = 0;
    var avgrel = 0;
    var avgdiff = 0;
    var smi = 0;
    var smiSignal = 0;
    var a = 0;
    var avalue = 0
    var b = 0;
    var bvalue = 0
    var BarCntr = 0;
    //var nLength = null;
    
    function main(Klen, Dlen){
    
    if(bInit == false){
    var overBuy = 40;
    var overSell = -40;
    ll = lowest(Klen, low(0));
    hh = highest(Klen, high(0));
    diff = hh -ll;
    rdiff = close - (diff * 0.5);
    a = ema(Dlen, rdiff);
    b = ema(Dlen, diff);
    bInit = true;
    }
    
    /*
    if (getBarState() == BARSTATE_ALLBARS) BarCntr += 1;
    if (BarCntr < Klen || BarCntr < Klen ) {return}
    else{
    a = ema(Dlen, rdiff);
    avgrel = ema(Dlen, a);
    b = ema(Dlen, diff);
    avgdiff = ema(Dlen, b);
    }
    
    
    if (getBarState() == BARSTATE_ALLBARS) {
    ll = lowest(Klen, low);
    hh = highest(Klen, high);
    diff = hh -ll;
    rdiff = close() - (diff * 0.5);
    
    a = ema(Dlen, rdiff);
    
    avgrel = ema(Dlen, a.getValue(0));
    b = ema(Dlen, diff);
    avgdiff = ema(Dlen, b.getValue(0));
    }
    */
    if (getBarState() == BARSTATE_NEWBAR)
    {
    avgrel = ema(Dlen, a.getvalue(0));
    avgdiff = ema(Dlen, b.getvalue(0));
    debugPrintln("a = " + a.getvalue(0));
    debugPrintln("avgrel = " + avgrel);
    debugPrintln("b = " + b.getvalue(0));
    debugPrintln("avgdiff = " + avgdiff);
    
    if (avgdiff != 0) { smi = avgrel/(avgdiff/2)*100; }
    else{ smi = 0;}
    
    debugPrintln("SMI = " + smi);
    
    smiSignal = ema(Dlen, smi);
    
    debugPrintln("smiSignal = " + smiSignal);
    }
    
    return new Array (getSeries(smi),getSeries(smiSignal));
    }
    How can I modify code to avoid this error? What's the standard approach for EFS to init variables to avoid Null error?

  • #2
    Hello Opium,
    You have not created a series object, that's what's causing your errors...you can't use "getSeries" to get a series that hasn't been defined, and you don't need to use
    "getSeries"
    unless you have a series object that returns multiple series. Take a look at the reply that I just wrote to the VMA Study Issue, it will hopefully point you in the direction that you need to go...happy coding!

    Comment

    Working...
    X