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

Limit orders change every time a new bar opens

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

    Limit orders change every time a new bar opens

    Hi Everyone

    I have created a strategy through the strategy builder.

    The issue with the strategy is, that the limit orders and drawings are updating every time a new bar opens.
    How can this be avoided?
    The strategy should just plot a limit-order and the drawings when the conditions are met, and not change every time a new bar is opened. The limit order should only be canceled at session close, if the limit order is not hit or filled.
    I have tried to play around with the parameter isLiveUntilCancelled, but with any luck. I am not even sure this is the right way to handle this issue.

    This also causes the backtest to enter trades at totally wrong pricelevels.

    Attached is my conditions and actions from the strategy builder.

    Below is the code from the NinjaScript editor.


    Any help is much appreciated.

    Thanks in advance!



    Attached Files
    Last edited by NicoNiso; 04-22-2024, 07:00 AM.

    #2
    Hello NicoNiso,

    Thanks for your post.

    If you are seeing the Limit order and Draw objects are being changed then the condition to call the Limit order and Draw methods is likely becoming true for multiple bars in a row.

    To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.

    Prints will appear in the NinjaScript Output window (New > NinjaScript Output window)

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.

    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hi NinjaTrader_BrandonH

      Thanks a lot for your quick reply.

      I have now added a lot of print to the strategy, and made a backtest of the strategy.
      It seems like the strategy are submitting an order when the strategy evaluates to true, but then when the order on the next bar isn't filled, the order is cancelled.
      Then within the specified time criteria, the strategy evaluates to true again, and then it adds another limit order, that either are filled or not. This goes on until the strategy is no longer evaluates to true.
      This is not the way I want the strategy to behave, and I need some input to change this behavior.

      I need the strategy to add an limit order only the first time the strategy evaluates to true. Then the order has to stay alive from the first entry that is submitted until it gets filled or until it is cancelled because of market closure. That's it. This is also the way I want the drawings to behave.

      How can I accomplish this?

      Attached is the Ninjascript output and the print conditions.
      If you need anything else to be printed to the output, please let me know.

      I hope you can help me out on this.

      Thanks in advance!

      Kind regards
      NicoNiso
      Attached Files

      Comment


        #4
        Hello NicoNiso,

        Thanks for your notes.

        That is the expected behavior of Entry and Exit orders submitted using the Managed Approach.

        From the Managed Approach help guide documentation:

        "By default, orders submitted via Entry() and Exit() methods automatically cancel at the end of a bar if not re-submitted"

        Managed Approach: https://ninjatrader.com/support/help...d_approach.htm

        To keep order alive, you could unlock the code from the Strategy Builder by clicking the 'Unlock code' button and manually program your strategy to use the Enter/Exit order method overload that let you specify the isLiveUntilCancelled argument as true. When isLiveUntilCancelled is set to true, orders would not be cancelled at the end of a bar but instead remain live until the CancelOrder() method is called or its time in force is reached.

        EnterLongLimit(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double limitPrice, string signalName)

        EnterLongLimit(): https://ninjatrader.com/support/help...rlonglimit.htm

        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Hi NinjaTrader_BrandonH

          Thanks for your input.

          I have now incorporated the isLiveUntilCancelled​ argument in the strategy, and now the limit order is kept alive. First problem solved - Thanks!

          However, I still experience that the strategy is evaluating to true multiple times within the specified timeframe, which is totally correct, but I need the strategy to stop evaluating whenever the strategy has evaluated to true the first time.
          So, when the strategy has evaluated​ to true and submitted the first limit order, the strategy somehow has to evaluate to false, and thereby stop proceeding.
          I have tried to include a condition about, if there should be a working order, then the strategy should evaluate to false, but I haven't had any luck with this approach.

          I hope you can help me out.

          Thanks in advance!

          Comment


            #6
            Hello NicoNiso,

            Thanks for your notes.

            A bool could be used to control when trades are made by the strategy.

            You could create a bool named something like OkToTrade (initially set to true). In your condition to place the entry order you would check if the bool is true, then call your entry order method and set the bool to false. By doing so, the entry method would only be called when the bool is true and once the order is submitted the bool would be false.

            Note that you would need to come up with some sort of logic to flip the bool back to true at some point in the script, otherwise it will remain false.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Hi NinjaTrader_BrandonH

              I was thinking at the same thing, but i'm not sure how to get that information. Could you please provide me with some examples or some specific code I can implement?

              I have incorporated the code below in my script, and I am now able to see when a order is in the OrderState.Working, and I can create a bool around the information, but I can't use this information as a condition in the strategy. Question is also, is this approach even the right way to achieve what I am looking fore?

              protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
              {
              bool TradeOK = (order.OrderState == OrderState.Working);
              Print("The most current order state is: " + order.OrderState); // OrderState.PartFilled
              Print("This particular order update state is: " + orderState); // OrderState.Working
              Print("Is state == Working? " + TradeOK);

              Thanks in advance!

              Comment


                #8
                Hello NicoNiso,

                Thanks for your notes.

                Attached is a very simple example script that demonstrates using a bool to control when trades are placed.

                You could implement a similar concept into your script to control how trades are being placed by the script.

                The bool should be a class-level variable in the script set to true by default. You would check if the bool is true to in your condition to place the Enter order method and the call you Enter order method and flip the bool to false.

                Then, you could come up with custom logic to flip the bool back to true in your script. As one example you could set the bool back to true after calling your Exit order method to exit the position.​
                Attached Files
                Brandon H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NM_eFe, Today, 06:14 AM
                2 responses
                12 views
                0 likes
                Last Post NM_eFe
                by NM_eFe
                 
                Started by realblubb, 04-28-2024, 09:28 AM
                1 response
                19 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by TAJTrades, 04-28-2024, 09:46 AM
                1 response
                14 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by llanqui, 04-28-2024, 10:32 AM
                1 response
                11 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by jpapa, 04-23-2024, 07:22 AM
                3 responses
                27 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Working...
                X