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

Delayed order

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

    Delayed order

    I'm buying a contract after MACD crossover. I want to buy after 1 bar from MACD crossover but my code is buying after 2 bars from MACD crossover.

    Can someone help me if I'm missing anything? Attachment shows order placement for buy and close.

    Code:
    protected override void OnStateChange()
    {
      if (State == State.SetDefaults)
        {
          TimeInForce = TimeInForce.Gtc;
          IsExitOnSessionCloseStrategy = true;
          Calculate = Calculate.OnBarClose;
          ExitOnSessionCloseSeconds = 30;
          OrderFillResolution = OrderFillResolution.Standard;
          RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    
          // This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
          // See the Help Guide for additional information
          IsInstantiatedOnEachOptimizationIteration = false;
        }
    }
    
    protected override void OnBarUpdate()
    {
      if (CrossAbove(MACD(12, 26, 9).Diff, 0, 1))
        {
            EnterLong(1, "Buy");
        }
    
      if ((CrossBelow(MACD(Close, 12, 26, 9).Diff, 0, 1))
        && (Position.MarketPosition == MarketPosition.Long))
        {
            ExitLong(1);
        }
    }
    Attached Files

    #2
    Hello visvabalaji,

    Was this a historical test? If so then what you are seeing is likely correct, you would see the execution on the following bar based on the historical fill engine. You can use a Print inside the condition to output the specific time when the condition became true to help identify if that is the fill engine.

    Code:
    if (CrossAbove(MACD(12, 26, 9).Diff, 0, 1))
    {
        Print(Time[0]);
        EnterLong(1, "Buy");
    }
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks for your reply. It is not only historical but for real-time trade also. I added time and checked it triggers order after 2nd bar MACD crossing.

      Attached is the latest screenshot from today's trade.

      Comment


        #4
        Hello visvabalaji,

        You can try to debug this further by using a print. Had you tried the print I previously suggested, if so what time did the print say vs the time the execution is listed in the chart?

        I see you are using OnBarClose so the crossing would need to happen for the close of the bar. The execution would be submitted at that time if a cross was detected. You can also print the price data at the time to see what the strategy is seeing:


        Code:
        if (CrossAbove(MACD(12, 26, 9).Diff, 0, 1))
        {
            Print(Time[0] + " DIFF0: " + MACD(12, 26, 9).Diff[0] + " DIFF1: " + MACD(12, 26, 9).Diff[1]);
            EnterLong(1, "Buy");
        }
        That would show the values which caused the cross so you could compare the time and values the strategy sees the event happening at on the chart.

        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by hurleydood, 09-12-2019, 10:45 AM
        14 responses
        1,096 views
        0 likes
        Last Post Board game geek  
        Started by cre8able, Yesterday, 04:16 PM
        1 response
        16 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by cre8able, Yesterday, 04:22 PM
        1 response
        14 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by stafe, 04-15-2024, 08:34 PM
        5 responses
        28 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by StrongLikeBull, Yesterday, 04:05 PM
        1 response
        12 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Working...
        X