Announcement

Collapse
No announcement yet.

Double Down!

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

  • Double Down!

    How can I double down on a position that I am already long or short in? I have a long position made when the close goes above one of two moving averages. But then, when the moving averages start positive divergence, I would like to go long again.
    Every thing I have so far, when the second condition is met, the order is continuously filled long.

  • #2
    Hello robertkovari,

    You may add to existing positions with back testing. For example, if you are already long 100 shares, calling Strategy.doLong() again for another 100 shares will add to the position for a total of 200. This works for shorts as well. When you exit the position you may also exit the entire position or only a portion of the position.
    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


    • #3
      Double Up!

      Hello there,

      I tried that, for example:

      if(close()>cSuma && !Strategy.isLong()){
      (Strategy.doLong("Crossing Up", Strategy.MARKET, Strategy.THISBAR));
      }

      if(close()>cSuma && sum>0){
      (Strategy.doLong("Double Up", Strategy.MARKET, Strategy.THISBAR));
      }

      With "cSuma" being one moving average, and "sum" the divergence between "cSuma-cSumb". As long as the sum>0, with each new bar this condition holds true, it fills a new position. In the end when it closes the long position, I have millions of dolors worth of stock. I also tried if(sum>0) && Strategy.isLong()) and other variations for the Double, but they return the same result?

      Thank you for triing to help,
      Robert

      Comment


      • #4
        Hello Robert,

        It sounds like the problem you're having is that the strategy continues to add to the position when you only want add to the position once. Is that correct?

        If so, then you need to add a global flag or a counter to track how many times you have added to the position. Outside main, add the following counter.

        PHP Code:
        var nLong 0
        Then in your first entry code block increment this counter by 1.

        PHP Code:
        if(close()>cSuma && !Strategy.isLong()){
            (
        Strategy.doLong("Crossing Up"Strategy.MARKETStrategy.THISBAR));
            
        nLong++;

        Then in the double up code block check for the value of nLong is less than 2. This will allow only one double up trade and prevent the subsequent unwanted double ups.

        PHP Code:
        if(close()>cSuma && sum>&& nLong 2){
            (
        Strategy.doLong("Double Up"Strategy.MARKETStrategy.THISBAR));
            
        nLong++;

        The other piece of logic that you'll need to add will be to reset the nLong counter back to 0. Reset this to 0 where you perform the exit or reverse trade. You may also want to add another counter to do the same thing for the short side.

        One other suggestion I have for you is to change the entry constants from Strategy.MARKET/Strategy.THISBAR to Strategy.CLOSE/Strategy.THISBAR. The MARKET constant, when used with THISBAR will give you a fill price at the open of the bar. Since your condition is looking at the close of the bar to execute the trade you should use the CLOSE constant. This will give you a more realistic result in the Strategy Analyzer report.
        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


        • #5
          Done!

          Jason,
          Thank you. I tried somthing similar to that, but I wasn't able to make it work. I don't think I had the initial var=0 set out side the main. I'll try it again, thank you!
          Robert

          Comment


          • #6
            It's a beautiful thing!

            pre...{

            var nLong = 0;
            var nShort = 0;

            }

            main...{

            if(close()>cSuma && !Strategy.isLong() && nLong < 1) {
            (Strategy.doLong("Crossing Up",Strategy.CLOSE, Strategy.THISBAR));
            nShort = 0;
            nLong++;
            }

            if(sum > 0 && close()>cSuma && nLong < 2){
            (Strategy.doLong("Double Up", Strategy.MARKET, Strategy.THISBAR));
            nShort = 0;
            nLong++;
            }

            if(close()<cSum && !Strategy.isShort() && nShort < 1){
            (Strategy.doShort("Crossing Down", Strategy.CLOSE, Strategy.THISBAR));
            nLong = 0;
            nShort++;
            }

            if(sum < 0 && close()<cSum && nShort < 2){
            (Strategy.doShort("Double Down", Strategy.MARKET, Strategy.THISBAR));
            nLong = 0;
            nShort++;
            }

            }

            Comment

            Working...
            X