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 fx.practic, 10-15-2013, 12:53 AM
        5 responses
        5,406 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Started by Shai Samuel, 07-02-2022, 02:46 PM
        4 responses
        98 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Started by DJ888, Yesterday, 10:57 PM
        0 responses
        8 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by MacDad, 02-25-2024, 11:48 PM
        7 responses
        160 views
        0 likes
        Last Post loganjarosz123  
        Started by Belfortbucks, Yesterday, 09:29 PM
        0 responses
        9 views
        0 likes
        Last Post Belfortbucks  
        Working...
        X