PDA

View Full Version : BarsSinceEntry()


neb1998
01-16-2011, 10:34 PM
Alright i looked through a lot of past threads and i cannot find a solution to what seems to be a simple problem.

I have Broken the code down to as simple as possible

" protected override void OnBarUpdate()
{
if (Close[0] > Close[5])
{
EnterLong();

}
if (BarsSinceEntry() > 5 )
{
ExitLong();
}

}"

Why does the code not exit after 5 bars every time? Its erratic at best, sometimes 1 bar, sometimes 3, sometimes 5, sometimes more. I dont understand what is wrong here or if am mis interpreting how this function works. In MC it was simple, if BSE > 2 bars, an exit would happen 2 bars later every time.

NinjaTrader_Bertrand
01-17-2011, 07:06 AM
Hello neb1998, you would need to include proper signal names -

protected override void OnBarUpdate()
{
if (Close[0] > Close[5])
{
EnterLong(DefaultQuantity, "");
}

if (BarsSinceEntry() > 2)
{
ExitLong("", "");
}
}

neb1998
01-17-2011, 11:09 AM
Ok i had tried this before but i went ahead and added them and its still not working. Random exiting.

protected override void OnBarUpdate()
{
if (Close[0] > Close[5])
{
EnterLong(DefaultQuantity,"LE1");

}
if (BarsSinceEntry() > 2 )
{
ExitLong("LE1","LE1");
}

}

neb1998
01-17-2011, 11:10 AM
See Previous Post, i couldnt insert a pic from a reply for some reason.

NinjaTrader_RyanM
01-17-2011, 11:42 AM
Hello neb1998,

Is there any improvement when passing in the name of the entry in your BarsSinceEntry() condition?

if (BarsSinceEntry("LE1") > 2 )
{
ExitLong("LE1","LE1");
}

It looks like every exit takes place 4 bars after the entry. This is consistent with your code.
The condition only evaluates true on the third bar after entry, and the order is submitted on the bar following this.

neb1998
01-17-2011, 01:14 PM
Ok this might be working, thanks! It looks good for now, i have to implement this into my actual strategy but looks good! Thanks.