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?
Announcement
Collapse
No announcement yet.
Backtesting problems
Collapse
X
-
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?
-
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.LIMIT, Strategy.THISBAR , 200, nLimit);
Comment
-
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
-
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
Comment