Announcement

Collapse
No announcement yet.

Help needed to fix simple code error

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

  • Help needed to fix simple code error

    I realize that this code is really basic, for some reason it doesn't work. The problem seems to be with the v variable, because I've used the other variables and they work, so I rem'd out un-needed code.

    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 studybb = new BollingerStudy(20, "Close", 2.0);
    var studyma = new MAStudy(40, 0, "Close", MAStudy.SIMPLE);

    function main() {
    var vUpper = studybb.getValue(BollingerStudy.UPPER);
    var vMiddle = studybb.getValue(BollingerStudy.BASIS);
    var vLower = studybb.getValue(BollingerStudy.LOWER);
    var v = studyma.getValue(MAStudy.MA);

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

    if(close() > v)
    Strategy.doLong("Going Up", Strategy.LIMIT, Strategy.THISBAR, null, vLower);

    if(close() < v)
    Strategy.doShort("Going Down", Strategy.LIMIT, Strategy.THISBAR, null, vUpper);


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

  • #2
    Mike2
    The code is actually working correctly. For example run a backtest on a daily chart for $INDU with 600 bars loaded and you should get the following trades



    The first thing you can see is that the strategy compounds trades because there is no condition to verify that it is already in a trade.
    That aside the dates shown above are the only ones in which the Close was above or below the moving average (ie. your variable v) AND prices also touched the LIMIT price (ie your variable vUpper or vLower) on the same bar which is required to trigger the trade.
    Given that you are not making use of the Bollinger Basis line try replacing vMiddle with v in the return statement so you can verify the conditions also visually. Once you have done that you may want to review and redefine your conditions for entering a trade.
    Alex

    Comment

    Working...
    X