PDA

View Full Version : BarsSinceEntry() in OnBarUpdate Script


beauw
10-09-2008, 08:34 AM
I think I figured out the time portion of my problem, now I have a question about using BarsSinceEntry and BarsSinceExit as my entry and exit conditions.

My understanding of using BarsSinceExit in an onbarupdate script is that it will either return the number of bars since exit, or -1 in the case of no history of an exit. Is this the case? The way I had envisioned using it is in the condition that BarsSinceExit!=0 as one of my entry conditions. The problem that I have is that I am worried the today, as with the BarsSinceEntry()!=0 may mean that today will make BarsSinceEntry()=1. That is, if I do enter today, then will that make BarsSinceEntry=1 in an onbarupdate method? Or will it still be at 0?

The next day I expect that it will return a BarsSinceEntry of 1, since the program is now only running between 9:30 and 4 pm EST. Thus, I use both BarsSinceEntry() and BarsSinceExit()!=0. BarsSinceExit()!=0 is used for the entry, and BarsSinceEntry()>=1 or 2 depending on the answer you give to the question, does the BarsSinceEntry increase by 1 on the entry today? That is, if I buy today, will BarsSinceEntry be 0 or 1? It wasn't clear in the documents which it would be. I'm sure it will work the same for BarsSinceExit().

Thanks,

Beau

NinjaTrader_Josh
10-09-2008, 08:45 AM
Hi Beau,

If the bar has not closed I believe it will not add to the counter.

beauw
10-09-2008, 04:08 PM
Why didn't the order submit today?

if (ToTime(Time[0]) >= 93000 && ToTime(Time[0]) <= 1600000)
{
if (s1b==false
&& BarsSinceExit() != 0
&& (GetCurrentAsk()) * (1 + -0.0025) <= (Bollinger(Low, 1.5, 10).Lower[0]) * (1 + -0.055)
&& LinReg(Close, 10)[0] >= (Bollinger(Low, 1.5, 10).Lower[0]) * (1 + -0.055)
&& StdError(Close, 3).Middle[0] >= 3)
{
EnterLongLimit(DefaultQuantity, (Bollinger(Low, 1.5, 10).Lower[0]) * (1 + -0.055), "S1");

NinjaTrader_Josh
10-09-2008, 04:14 PM
beauw,

Please use TraceOrders = true and debug your order. Also use prints to figure out what your code is doing. Whenever you have difficulties with orders the first thing you want to do is make sure that your if-statement even evaluated to true. Use prints everywhere to determine this. Then you can proceed to debug what went wrong with your order.

Please see these tips: http://www.ninjatrader-support.com/vb/showthread.php?t=3418
http://www.ninjatrader-support.com/vb/showthread.php?t=3627

beauw
10-09-2008, 04:34 PM
First, is that how you would make sure the time is between 9:30 am and 4 pm? Answer that question. The next question was about the BarsSinceExit!=0. That would be the only area that would have prevented this execution. The condition was true on at least 50 stocks today, but I think the BarsSinceExit prevented it, so explain why it wasn't submitted. The s1b==false is set to false at the beginning.

NinjaTrader_Josh
10-09-2008, 05:03 PM
beauw,

Yes, that is how you code the time filter. For information on using time filters please see this reference sample: http://www.ninjatrader-support.com/vb/showthread.php?t=3226

Your strategy behaves exactly as it is told by your code. If an order does not get placed it is because your if-statement was never evaluated to be true. If an order is ignored or rejected you need TraceOrders to determine the reason and adjust your code to accomodate for it.

When debugging you cannot discount any portion of your code. You need to debug your condition bit by bit. You cannot just assume because it was true on 50 stocks that it will be true in this instance. You need to print out values for each and every single one of your conditions and run through them manually. It is time consuming, yes, but that is how you need to proceed while debugging your code.

BarsSinceExit() is -1 when there is no exit to be found. After that it will take on a value representative of how many bars since the exit.