Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 BarzTrading, Today, 07:25 AM
      2 responses
      13 views
      1 like
      Last Post BarzTrading  
      Started by devatechnologies, 04-14-2024, 02:58 PM
      3 responses
      19 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Started by tkaboris, Today, 08:01 AM
      0 responses
      3 views
      0 likes
      Last Post tkaboris  
      Started by EB Worx, 04-04-2023, 02:34 AM
      7 responses
      162 views
      0 likes
      Last Post VFI26
      by VFI26
       
      Started by Mizzouman1, Today, 07:35 AM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Working...
      X