Announcement

Collapse
No announcement yet.

Backtesting problems

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

  • Backtesting problems

    I've written a strategy that enters using limit orders. When I print to the formula output window, I see that indeed the commands doLong and doShort are being hit. Also, I am coloring the bars according to position (isLong() and isShort()), which is coloring the bars on my chart, indicating that I am in a position. However, when I try to backtest the strategy, I show no trades. What could cause this?

  • #2
    Backtesting problems

    I've written a strategy that enters using limit orders. When I print to the formula output window, I see that indeed the commands doLong and doShort are being hit. Also, I am coloring the bars according to position (isLong() and isShort()), which is coloring the bars on my chart, indicating that I am in a position. However, when I try to backtest the strategy, I show no trades. What could cause this?

    Comment


    • #3
      Did you do the following:

      "If the Fill Type of Strategy.LIMIT or Strategy.STOP is specified, the nStopOrLimit price parameter must be specified."

      Comment


      • #4
        Yes I did.

        Comment


        • #5
          I'm not sure what price the Strategy object fills orders at. However, in actual trading on an exchange a Limit order is only filled if at or better than the current corresponding bid or ask. Your backtesting data may not have bid and ask data in it...

          Comment


          • #6
            Are you saying I can't backtest using limit orders???

            Comment


            • #7
              After looking at it further, I don't think Strategy LIMIT orders use the bid or ask. Other's have gotten it to work on the forums, so it might be a bug in your code.

              I'd use close(0) or hl2() for the nStopOrLimit parameter and see if that works. I think nStopOrLimit has to be "reasonable" - between the high and low for the bar. For example:
              PHP Code:
              var nLimit hl2();
              // or...
              // var nLimit = close(0);
              Strategy.doLong"Long Signal"Strategy.LIMITStrategy.THISBAR 200nLimit); 
              You might want to post your code, especially what you use for the nStopOrLimit parameter.

              Comment


              • #8
                Here is my code.

                Code:
                if (takelong && low(0)<=buyprice+getMinTick()){
                            Strategy.doLong("LE",Strategy.LIMIT,Strategy.THISBAR,TradeSize,buyprice+getMinTick());
                            entryprice=Math.min(buyprice+getMinTick(),close(0));
                        }

                Comment


                • #9
                  pro_trader,

                  I suspect getMinTick() doesn't work when backtesting...

                  I'd add the if statement / debugPrintln() below to your code as shown and see what it prints out:
                  PHP Code:
                  // log variables to see what we've got...
                  if (takelong) {
                      
                  debugPrintln"buyprice = " buyprice ", getMinTick() = " getMinTick() );
                  }

                  if (
                  takelong && low(0)<=buyprice+getMinTick()){
                      
                  Strategy.doLong("LE",Strategy.LIMIT,Strategy.THISBAR,TradeSize,buyprice+getMinTick());
                      
                  entryprice=Math.min(buyprice+getMinTick(),close(0));

                  Comment


                  • #10
                    Stop and Limit orders on THISBAR

                    I tried posting a question (and e-mailing support) about Stop and Limit orders in backtesting, but didn't get a reply. For a Buy, my understanding is that I should use a Limit order to buy at "a price lower than the current price" and a Stop order buy at "a price higher than the current order". When backtesting, the script has access to the open, high, low and close prices for the bar, so it isn't clear to me what would count as "the current price" when specifying an order price to be executed in the current bar. I was finding that not all the trades I asked the Strategy object to open were, in fact, opened. I ended up avoiding the problem by detecting whether the object opened the trade the first way, and if it didn't trying again the other way. You may be able to do something similar?

                    Code:
                    function openLong( price, tag ) {
                        var OK = Strategy.doLong( tag, Strategy.LIMIT, Strategy.THISBAR, Strategy.DEFAULT, price );
                        if( ! OK ) {
                            debug.println("@@@@@ openLong() failed with LIMIT order");
                            OK = Strategy.doLong( tag, Strategy.STOP, Strategy.THISBAR, Strategy.DEFAULT, price );
                            if( ! OK ) debug.println("@@@@@ openLong() failed with STOP order");
                            }
                        if( ! Strategy.isLong() ) debug.println( "@@@@@ Strategy not Long after openLong" );
                        return;
                        }

                    Comment

                    Working...
                    X