PDA

View Full Version : Strategy executing order twice


SamIam
08-28-2007, 08:17 PM
Greetings,

I am having this problm with all the strategies that I have built in NT. When I start the strategy it right away shows an unrealized gain/loss in the strategy tab. But the strategy does not execute any order. When the opposite condition meets, the strategy not only initiate a new position but also close the unopened position. In this case I ends up with two lots instead of one.

Is there any condition I have to incorporate to avoid this from happening?

Can I force the strategy to right-away place an order if any of the condition in the code is true at the time strategy gets started?

Below is one of the codes that is behaving in this manner:

protected override void Initialize()
{
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition set 1
if (Close[0] > MAX(High,periodOpen)[1])
{
EnterLong(1, "Long");
}
// Condition set 2
if (Close[0] < MIN(Low,periodOpen)[1])
{
EnterShort(1, "Short");
}
// Condition set 3
if (Close[0] < MIN(Low,periodClose)[1])
{
ExitLong("Exit Long", "Long");
}
// Condition set 4
if (Close[0] > MAX(High,periodClose)[1])
{
ExitShort("Exit Short", "Short");
}
}


Thanks,

Sami

NinjaTrader_Josh
08-29-2007, 12:49 AM
Hi Sami,

A possibility of why you are getting unrealized gain/loss immediately after you start a strategy is because when you start a strategy it does a "mini backtest" where it runs through all historical data with the strategy. This occasionally will leave you with open positions hence the unrealized gain/loss. To prevent this you can add this to your code at the start of OnBarUpdate():
if (Historical)
return;Please note that if you add this code segment your strategy will no longer run in the Strategy Analyzer because all data there are historical data.

To force the strategy to execute orders immediately you want change CalculateOnBarClose to false. This will execute your orders in real-time. If you do decide to set it to false please take note of these differences:
In real-time you might get multiple entry/exits per bar.
The market is dynamic. On one tick you might have Close > High and then on the very next tick it might be Close < High. Basically you might get a lot of false signals.

SamIam
08-29-2007, 06:43 PM
Josh,

Your code solved the problem.

Thank you,

Sami