Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Ninjascript not executing

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

    Ninjascript not executing

    In my strategies, I have the line: if(ToTime(Time[0])) >= 160000 || ToTime(TIme[0])) < 90000) {ExitLong(); ExitShort(); return;}. This is meant to only have the strategy running during certain times. This usually works (the strategy will exit at 160000), but not infrequently, the ninjascript line simply does not execute and the strategy remains in a trade (it appears the strategy does not make any new trades, but does not exit the current trades as it is supposed to do).

    #2
    Hello samish18,

    The logic appears to call both exit long and exit short if the time of the bar is greater than 4 pm or before 9 am. During this time no new orders should be submitted (as calling an exit and entry on the same bar update will cause issues)

    The issue is with the entry not being submitted from 9 am to 4 pm is this correct?

    Use prints to diagnose the issue.

    For the entry conditions logic block you are investigating print the time of the bar and all values compared in the conditions. Include labels for each value and comparison operator (important).
    The output should appear just like the condition but with labels added.

    Enable TraceOrders to show when orders are being submitted, ignored, rejected, or automatically cancelled.

    Print the order.ToString() in OnOrderUpdate() to show when orders are submitted, accepted/working, filled, cancelled, or rejected.

    Below is a link to a support article that includes videos on adding debugging prints to understand behavior.


    Save the output to a text file (right-click the output window and select Save as) and attach this to your next post.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      No, the strategy runs fine from 12 am through 4pm (both before and during the time period in question). The issue is after the tie period (after 4 pm), the strategy does not always exit positions

      Comment


        #4
        Hello samish18,

        Print the branching logic block to see if the branch is evaluating as true.

        If not, print the time of the bar and all values compared in the conditions in the exit branch. Include labels for each value and comparison operator (important).
        The output should appear just like the condition but with labels added.

        Enable TraceOrders to show when orders are being submitted, ignored, rejected, or automatically cancelled.

        Print the order.ToString() in OnOrderUpdate() to show when orders are submitted, accepted/working, filled, cancelled, or rejected.

        Save the output to a text file (right-click the output window and select Save as) and attach this to your next post.​
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Yes, I added prints and it is evaluated as true and the return; executes, but the exits do not

          Comment


            #6
            Hello samish18,

            What return are you referring to?

            A return stops any code below from evaluating.

            Save the output to a text file (right-click the output window and select Save as) and attach this to your next post.​
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I amended it to: if(ToTime(Time[0])) >= 160000 || ToTime(TIme[0])) < 90000) {Print(ToTime(Time[0])); ExitLong(); ExitShort(); return;}. (Just added printing the time of the bar if condition is true. The prints behaved as expected, printing every 5 minutes until 90000 and then resuming again at 160000.

              After 160000, it appears the return is exiting as the later logic in the strategy (EnterLong() and EnterShort()) is not occurring, but the Exits right before the return are not executing. Again, this is not happening all the time, maybe 5-10% of strategies ran this occurs for. How may I fix this?

              Comment


                #8
                Hello samish18,

                To provide direction, I'll need the print output requested in post # 4.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  As described the printed output went as follows:
                  ...
                  85000
                  85500
                  160000
                  160500
                  161000
                  ...

                  Comment


                    #10
                    Hello samish18,

                    As stated in post # 4.

                    "If not, print the time of the bar and all values compared in the conditions in the exit branch. Include labels for each value and comparison operator (important).
                    The output should appear just like the condition but with labels added.​"

                    "Save the output to a text file (right-click the output window and select Save as) and attach this to your next post.​"

                    The output you have provided is not complete, does not appear to include the bar time, shows no comparison, and does not have labels.

                    Unfortunately, these numbers don't tell me what is being compared in the condition.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      I attached the needed output. As seen, when ToTime(Time[0]) is outside of the bounds 90000 and 160000, it executes the print statement correctly. However, it does not always (maybe 10% of the time when a strategy transition across 160000) execute the exit commands.
                      Attached Files

                      Comment


                        #12
                        Hello samish18,

                        I am not seeing any information that any orders have been submitted at all.

                        Are you certain you have enabled TraceOrders and printed the order.ToString() in OnOrderUpdate()?

                        Also, print the Position.MarketPosition one above this to confirm the position is not flat.
                        If the position is flat, calling an exit method will have no effect as there is no position to exit.

                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          How may I amend the code to include these details?

                          Comment


                            #14
                            Additionally, what I see indicates that this may be an execution issue and not a code issue. The strategy never takes a position before 90000 and correctly takes positions between 90000 and 160000. Then, the issue is that most of the time at 160000, it exits any position, but sometimes, it simply does not, but also does not follow the rest of the strategy logic after the line in question. I.e. the issue is that sometimes at 160000 the strategy does not exit the position as it is supposed to do (maybe it for some reason does not recognize the position), but also does not make any more trades if for say there is a moving average cross-over.

                            Comment


                              #15
                              Hello samish18,

                              Below is a link to a support article that includes videos on adding debugging prints to understand behavior.​


                              As noted in the Article assign TraceOrders = true; in State.Configure.

                              You can copy and paste the OnOrderUpdate() override from the documentation linked in the article and in the method logic block Print(order.ToString());.

                              "Then, the issue is that most of the time at 160000, it exits any position, but sometimes, it simply does not, but also does not follow the rest of the strategy logic after the line in question."

                              This could mean the session has ended, could mean the logic is not evaluating as you expect and the logic needs to be corrected, or could mean the order was ignored, rejected, or cancelled.

                              The output will tell us what is occuring.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              79 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              146 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              79 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              52 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              56 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X