Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy executing order twice

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Strategy executing order twice

    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:

    protectedoverridevoid Initialize()
    {
    CalculateOnBarClose =
    true;
    }
    ///<summary>
    /// Called on each bar update event (incoming tick)
    ///</summary>
    protectedoverridevoid 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


    #2
    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():
    Code:
    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:
    1. In real-time you might get multiple entry/exits per bar.
    2. 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.
    Last edited by NinjaTrader_JoshP; 08-29-2007, 12:51 AM.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Josh,

      Your code solved the problem.

      Thank you,

      Sami

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      647 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      368 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      108 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      571 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      573 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X