PDA

View Full Version : BarsSinceEntry


heyligerb
02-16-2009, 09:40 AM
Hi Guys,

Just playing around with a little code here, to help myself understand how to program in NT.

The strategy works fine on a backtest until I add the BarsSinceEntry command. I added BarsSinceEntry b/c in the back test, it enters a long position just after getting stopped out.

I'm coding something wrong, any idea what it is?

Here is my code:

protected override void OnBarUpdate()
{
if
// Check that the bond pit is open
(ToTime(Time[0]) > ToTime(8, 30, 0)
&& ToTime(Time[0]) < ToTime(14, 45, 0)

// Check that current position in this strategy is flat
&& Position.MarketPosition == MarketPosition.Flat

// Check for four consecutive down bars
&& Open[3] > Close[3]
&& Open[2] > Close[2]
&& Open[1] > Close[1]
&& Open[0] > Close[0])
{
// Check that we haven't entered a trade in the last two bars.
if (BarsSinceEntry(0, "FourBarStrategy", 0) >2)

// Buy!
EnterLongLimit(Contracts, Close[0], "FourBarStrategy");

NinjaTrader_Josh
02-16-2009, 10:17 AM
Hi heyligerb,

The reason your code doesn't work is because on the very first condition it is never > 2. You never entered before. Instead you should use BarsSinceExit().

if (BarsSinceExit() > 2 || BarsSinceExit() == -1)
EnterLong();
You only need to use the syntaxing you chose if you are in a multi-series strategy.

heyligerb
02-16-2009, 11:28 AM
Great! That works.

One other quick question...

The statement "Position.MarketPosition == MarketPosition.Flat"

Will that check the market position for the strategy, or across all positions?

I might have another trade on, for the same instrument, but I only want to hold off entering a new position in this strategy, only if, the same strategy has a current position. Not if a different strategy, or a manual trade has a position.

Brian

NinjaTrader_Josh
02-16-2009, 11:41 AM
It is for the strategy position only. It does not know what other strategies are doing or what manual trades are doing.

heyligerb
02-18-2009, 11:12 AM
What would you use on an indicator since BarsSinceExit/Entry don't apply to indicators?

NinjaTrader_Josh
02-18-2009, 11:29 AM
What are you trying to do? An indicator does not know anything about strategy positions, entries, or exits. If you are trying to do something based off of trades you need to do it in a strategy.

heyligerb
02-18-2009, 11:35 AM
Just trying to make an alert similar to the strategy...

Four consecutive down bars would print/play what I tell it to.

I have that part working, but if I get a fifth bar, it print/plays again. I only want it to to it on the first four bars.

Brian

NinjaTrader_Josh
02-18-2009, 11:43 AM
Brian,

You would have to do it over in the strategy. You can fire off alerts from there.