Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to separate entry conditions logic from entry submission logic

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

    How to separate entry conditions logic from entry submission logic

    Hi!

    I want to separate my entry conditions logic from my entry submission logic

    Conditions Logic
    In OnBarUpdate I want to have something like:
    {
    if (condition == true)
    GoTrade("Long");
    }

    Submission Logic
    When calling "GoTrade("Long")" in my conditions logic I want the code to do the following in each state (simplified):
    • In OnBarUpdate: EnterLong();
    • In ExecutionUpdate: Submit a StopLossOrder;
    • In OrderUpdate: Assign an order object;
    How can I achieve this?

    Thanks!

    Kirk

    #2
    Hello KirkHammett,

    You would need to use a variable if you wanted to separate the entry conditions from the entry submission. You would have to form a separate conditions which submits the order, that condition would need to look for a variable becoming true. To submit targets based on the entry fill you can use OnExecutuonUpdate. There is a sample that shows submitting targets from that override here: https://ninjatrader.com/support/help...and_onexec.htm

    Comment


      #3
      Hi Jesse!

      I think you misunderstood my question. I want to have a custom method or a class, which I can call in my main strategy in OnBarUpdate. So I want to avoid to call OnExecutionUpdate and OnOrderUpdate separately in my main strategy. I want to call my custom Method or class in OnOrderUpdate and want that method or class to do everything.

      So again:

      Conditions Logic
      In OnBarUpdate I want to have something like:
      {
      if (condition == true)
      GoTrade("Long"); // custom method or class
      }

      Submission Logic
      When calling "GoTrade("Long")" in my conditions logic I want that method / class to do the following in each state (simplified):
      • In OnBarUpdate: EnterLong();
      • In ExecutionUpdate: Submit a StopLossOrder;
      • In OrderUpdate: Assign an order object;
      So when I call my custom method "Go Trade("Long")" in OnBarUpdate, that method or class should execute EnterLong() in OnBarUpdate, send a StopLossOrder in OnExecutionUpdate and assign an order object in OnOrderUpdate. So I want to clean up my conditions logic code because it gets too messy when having all the code in OnExecutionUpdate and OnOrderUpdate and so on. And I want to use this custom method or class in several strategies. Do I need a partial class here or how can I achieve my goal?

      Thanks in advance!

      Comment


        #4
        Hello KirkHammett,

        OnExecutionUpdate and OnOrderUpdate are called by NinjaTrader based on the order events, you would not call those methods in your own code. The sample I linked shows how to use those methods to find orders and submit targets based on an entry.

        When calling "GoTrade("Long")" in my conditions logic I want that method / class to do the following in each state (simplified):
        In OnBarUpdate: EnterLong();
        In ExecutionUpdate: Submit a StopLossOrder;
        In OrderUpdate: Assign an order object;
        The only part you would control here would be submitting the entry. After that you have to wait to observe the order events for a fill before submitting your targets in ExecutionUpdate. If you want to make custom methods or classes you can do that but you would need to call that from ExecutionUpdate for submitting targets. You can structure your general code as you wish within the bounds of C# language.

        And I want to use this custom method or class in several strategies. Do I need a partial class here or how can I achieve my goal?
        A partial class can be used to make generalized code. You can see an example of a partial class in the following link: https://ninjatrader.com/support/help...hangesOverview

        I would suggest to make your strategy actually work and test it before trying to make custom methods or classes as that would make it much easier to see what kind of structure you may need in the end.

        Comment


          #5
          Thank you. I have already everything in the strategy working already but now I want to clean it up. It would be too complicated to post it completely here thats why I simplified it.

          Do I understand it correctly that I cannot create a custom method or a class which executes code both in OnExecutionupdate and OnOrderUpdate? So I have to create bascially two separate methods and call an execution method in OnExecutionUpdate and an order method in OnOrderUpdate?

          Comment


            #6
            Hello KirkHammett,

            Thats correct, a custom class doesn't get event updates. You would need to still use the strategies overrides, you could call a method in your class and pass it data if you wanted. A custom class wont have the context of your strategy so you would need to pass any objects or variables needed to that class.

            Comment


              #7
              Hi Jesse!

              Sorry but I have to bother you again. I still don't really understand why it is not possible to call one method which executes code in OnBarUpdate, OnExecutionUpdate and OnOrderUpdate.

              Indicators behave also like that: I call the SMA Indicator (= method) in my strategy in OnBarUpdate. Since the SMA Indicator has code in OnStateChange and OnBarUpdate and also creates class level variables this code is being executed in the strategy even though I called it in OnBarUpdate in my strategy. So it should be possible for me to do the same thing like calling the SMA Indicator with the only difference that my code should execute in OnExecutionUpdate and OnOrderUpdate too, right?

              I would be very grateful if you could help me here. Thanks in advance!
              Last edited by KirkHammett; 09-11-2022, 02:08 AM.

              Comment


                #8
                Hello KirkHammett,

                Sorry but I have to bother you again. I still don't really understand why it is not possible to call one method which executes code in OnBarUpdate, OnExecutionUpdate and OnOrderUpdate.
                A single method contains one block of code that is always executed when you call that method. You would be executing the same code if you were calling it from all three overrides. While you can make a parameter for the method and if conditions inside the method to delegate what logic is used that is not really good programming structure. Its just as easy to make 3 custom methods for specific purposes and then call the correct method inside each of the NinjaScript overrides. Here is a simple example of breaking your code into methods:


                Code:
                private MyCustomOBUMethod()
                {
                    //obu specific code such as an entry
                }
                
                private MyCustomOnExecutionMethod(Execution execution)
                {
                    //OnExecutionUpdate specific code such as submitting a target
                }​
                
                private MyCustomOnOrderUpdateMethod(Order order)
                {
                    //OnOrderUpdate specific code such as an entry
                }
                
                protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
                {
                     MyCustomOnOrderUpdateMethod(order);
                }
                
                protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
                {
                    MyCustomOnExecutionMethod(execution);
                }​
                
                protected override void OnBarUpdate()
                {
                    MyCustomOBUMethod();
                }
                Breaking it up this way does not really make a lot of sense if you only have a small amount of code inside each override. You are just duplicating what you already have. This could make sense if you have a lot of code that goes inside each override, that could help to clean it up by making many specific methods and then calling them each from the appropriate override. ​​


                So it should be possible for me to do the same thing like calling the SMA Indicator with the only difference that my code should execute in OnExecutionUpdate and OnOrderUpdate too, right?
                You can call indicators in those overrides because that is within the strategies context.

                For clarity in regard to what we were previously discussing, If you make a custom class that exists outside the strategy so calling indicators or other strategy properties inside that class won't work in that context. If you make custom methods within the strategy class you could call those from OnExecutionUpdate and OnOrderUpdate to execute your code inside the method, that is inside the strategy class so indicators or other strategy properties could be used there as well.

                As LoganJKTrader mentioned its best to learn about the individual overrides before trying to do anything so you are sure which override to use for the given task. OnOrderUpdate is generally only used to collect order variables or observe status changes. OnExecutionUpdate is used specifically to observe fills or executions, OnExecutionUpdate would also be where you need to submit new orders such as targets.

                https://ninjatrader.com/support/help...sub=onexecutio n

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Today, 05:17 AM
                0 responses
                44 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                124 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                65 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                42 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                46 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X