Announcement

Collapse
No announcement yet.

Bollinger Band Crossing

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

  • Bollinger Band Crossing

    Hello,

    I try to write a very simple efs file just to learn programming.
    The strategy is the next one:

    * go long if lower bollinger band (2 STD) crossed
    cover this long position as soon as
    basis BB(2STD) crossed or lower BB(3STD) is also crossed

    * go short if Upper BB (2 STD) crossed
    cover this short position as soon as
    basis BB(2STD) crossed or upper BB(3STD) is also crossed

    I wrote the following program:

    var vBollinger2 = new BollingerStudy(20, "Close", 2);
    var vStop3 = new BollingerStudy(20, "Close", 3);


    function preMain() {
    setPriceStudy(true);
    setStudyTitle("Bollinger Bands Piercing");
    }

    function main() {

    if (close() < vBollinger2.getValue(BollingerStudy.LOWER))
    {Strategy.doLong("Crossing Up",Strategy.CLOSE,Strategy.THISBAR);}

    if (close() > vBollinger2.getValue(BollingerStudy.UPPER))
    {Strategy.doShort("Crossing Down",Strategy.CLOSE,Strategy.THISBAR);}


    if (Strategy.isLong()) { if (close() > vBollinger2.getValue(BollingerStudy.BASIS))
    {Strategy.doSell("Close Long",Strategy.MARKET,Strategy.THISBAR);}}

    if (Strategy.isLong()) { if (close() < vStop3.getValue(BollingerStudy.LOWER))
    {Strategy.doSell("Close Long",Strategy.MARKET,Strategy.THISBAR);}}

    if (Strategy.isShort()) { if (close() < vBollinger2.getValue(BollingerStudy.BASIS))
    {Strategy.doCover("Close Short",Strategy.MARKET,Strategy.THISBAR);}}

    if (Strategy.isShort()) { if (close() > vStop3.getValue(BollingerStudy.UPPER))
    {Strategy.doCover("Close Short",Strategy.MARKET,Strategy.THISBAR);}}


    }



    but there is a bug in this program as the result of this strategy is that on each bar there is a Crossing Down & a Close short...

    Do you see where is the problem?

    Thanks

  • #2
    Re: Bollinger Band Crossing

    Vermaut
    From what I am seeing those trades occur at the beginning of the data file while the Bollinger Bands are still priming and returning null. You can easily verify this by plotting the Bollinger Bands that you are using in your strategy.
    To resolve this insert at the beginning of the main function a null check on the values of the Bollinger Bands used in the strategy so as to make sure that this is implemented only when all the studies are returning valid values eg
    PHP Code:
    if(vBollinger2.getValue(BollingerStudy.LOWER)==null || vStop3.getValue(BollingerStudy.LOWER)==null)
    return; 
    Once you do this those trades should no longer be executed by your strategy.
    Also when entering a trade you should add a check if the Strategy is not Long or Short eg
    PHP Code:
     if (!Strategy.isLong() && close() < vBollinger2.getValue(BollingerStudy.LOWER)) {
        
    //rest of your code
    }
    //repeat for the Short trade entry condition 
    else you could end up with multiple trade entries in the same direction
    Alex


    Originally posted by Vermaut
    Hello,

    I try to write a very simple efs file just to learn programming.
    The strategy is the next one:

    * go long if lower bollinger band (2 STD) crossed
    cover this long position as soon as
    basis BB(2STD) crossed or lower BB(3STD) is also crossed

    * go short if Upper BB (2 STD) crossed
    cover this short position as soon as
    basis BB(2STD) crossed or upper BB(3STD) is also crossed

    I wrote the following program:

    var vBollinger2 = new BollingerStudy(20, "Close", 2);
    var vStop3 = new BollingerStudy(20, "Close", 3);


    function preMain() {
    setPriceStudy(true);
    setStudyTitle("Bollinger Bands Piercing");
    }

    function main() {

    if (close() < vBollinger2.getValue(BollingerStudy.LOWER))
    {Strategy.doLong("Crossing Up",Strategy.CLOSE,Strategy.THISBAR);}

    if (close() > vBollinger2.getValue(BollingerStudy.UPPER))
    {Strategy.doShort("Crossing Down",Strategy.CLOSE,Strategy.THISBAR);}


    if (Strategy.isLong()) { if (close() > vBollinger2.getValue(BollingerStudy.BASIS))
    {Strategy.doSell("Close Long",Strategy.MARKET,Strategy.THISBAR);}}

    if (Strategy.isLong()) { if (close() < vStop3.getValue(BollingerStudy.LOWER))
    {Strategy.doSell("Close Long",Strategy.MARKET,Strategy.THISBAR);}}

    if (Strategy.isShort()) { if (close() < vBollinger2.getValue(BollingerStudy.BASIS))
    {Strategy.doCover("Close Short",Strategy.MARKET,Strategy.THISBAR);}}

    if (Strategy.isShort()) { if (close() > vStop3.getValue(BollingerStudy.UPPER))
    {Strategy.doCover("Close Short",Strategy.MARKET,Strategy.THISBAR);}}


    }



    but there is a bug in this program as the result of this strategy is that on each bar there is a Crossing Down & a Close short...

    Do you see where is the problem?

    Thanks

    Comment


    • #3
      Thanks a lot....
      It was indeed the case

      Comment


      • #4
        Vermaut
        You are most welcome
        Alex


        Originally posted by Vermaut
        Thanks a lot....
        It was indeed the case

        Comment


        • #5
          Hey Vermaut,

          I couldn't find any other threads involving BB, so I'm positing my question here.

          Can you direct me to resources anywhere on this site or Esignals Partner site that gives ideas on how to properly use Bollinger Bands?

          Such as how to trade the crossing of the bands, or what other momentum indicators to combine it with?

          I posted this question on the Trading Strategies section a few weeks ago, but the responses involve more of the coding aspect of an EFS.

          Thanks.

          Comment


          • #6
            I was told that this BB crossing formula is available on the EFS script page, but I don't see it.

            Some post a link to it in this thread.

            Thanks.

            Comment

            Working...
            X