Announcement

Collapse
No announcement yet.

Help !. Something simple wrong with this code ?

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

  • Help !. Something simple wrong with this code ?

    Hi, I'm doing a simple calculation which doesn't work with back-testing. If I replace vUpper and vLower with v (in the if ... codes) the code works. The code is as following;

    function preMain() {
    setPriceStudy(true);
    setStudyTitle("Bollinger Bands");
    setCursorLabelName("UpperBB", 0 );
    setCursorLabelName("MiddleBB", 1 );
    setCursorLabelName("LowerBB", 2 );
    setDefaultBarFgColor(Color.red, 0); // upper
    setDefaultBarFgColor(Color.blue, 1); // middle
    setDefaultBarFgColor(Color.green, 2); // lower
    setColorPriceBars(true);
    setDefaultPriceBarColor(Color.black);
    }

    var study = new BollingerStudy(10, "Close", 2.0);
    var study = new MAStudy(40, 0, "Close", MAStudy.SIMPLE);

    function main() {
    var v = study.getValue(MAStudy.MA);
    if(v == null) return;
    var vUpper = study.getValue(BollingerStudy.UPPER);
    var vMiddle = study.getValue(BollingerStudy.BASIS);
    var vLower = study.getValue(BollingerStudy.LOWER);

    // return new Array(vUpper, vMiddle, vLower);

    if(close() <= vLower && !Strategy.isLong())
    Strategy.doLong("Going Up", Strategy.MARKET, Strategy.THISBAR);

    if(close() >= vUpper && !Strategy.isShort())
    Strategy.doShort("Going Down", Strategy.MARKET, Strategy.THISBAR);

    // if(Strategy.isLong() && high() >= vUpper)
    // Strategy.doSell("Sell Up", Strategy.MARKET, Strategy.THISBAR);
    // else if(Strategy.isShort() && low() <= vLower && !Stragegy.doSell)
    // Strategy.doSell("Sell Down", Strategy.MARKET, Strategy.THISBAR);

    if(Strategy.isLong()) setPriceBarColor(Color.lime);
    else if(Strategy.isShort()) setPriceBarColor(Color.red);
    // return new Array(vUpper, vMiddle, vLower);
    return v;
    }

  • #2
    Mike2
    The problem in the script is that you have assigned the same name to the two studies.
    Try the enclosed revision
    Alex

    PHP Code:
    function preMain() {
        
    setPriceStudy(true);
        
    setStudyTitle("Bollinger Bands");
        
    setCursorLabelName("UpperBB");
        
    setCursorLabelName("MiddleBB");
        
    setCursorLabelName("LowerBB");
        
    setDefaultBarFgColor(Color.red0); // upper
        
    setDefaultBarFgColor(Color.blue1); // middle
        
    setDefaultBarFgColor(Color.green2); // lower
        
    setColorPriceBars(true);
        
    setDefaultPriceBarColor(Color.black);
    }

    var 
    study1 = new BollingerStudy(10"Close"2.0);
    var 
    study2 = new MAStudy(400"Close"MAStudy.SIMPLE);

    function 
    main() {
        var 
    study2.getValue(MAStudy.MA);
        if(
    == null) return;
        
        var 
    vUpper study1.getValue(BollingerStudy.UPPER);
        var 
    vMiddle study1.getValue(BollingerStudy.BASIS);
        var 
    vLower study1.getValue(BollingerStudy.LOWER);
            if(
    vUpper==null||vMiddle==null||vLower==null) return;

        if(
    close() <= vLower && !Strategy.isLong())
            
    Strategy.doLong("Going Up"Strategy.MARKETStrategy.THISBAR);

        if(
    close() >= vUpper && !Strategy.isShort())
            
    Strategy.doShort("Going Down"Strategy.MARKETStrategy.THISBAR); 

        
    // if(Strategy.isLong() && high() >= vUpper)
            // Strategy.doSell("Sell Up", Strategy.MARKET, Strategy.THISBAR);
        // else if(Strategy.isShort() && low() <= vLower && !Stragegy.doSell)
            // Strategy.doSell("Sell Down", Strategy.MARKET, Strategy.THISBAR);

        
    if(Strategy.isLong()) setPriceBarColor(Color.lime);
        else if(
    Strategy.isShort()) setPriceBarColor(Color.red);

        return new Array(
    vUppervMiddlevLower);
        
    //return v;

    Comment

    Working...
    X