Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Close position using OnExecutionUpdate()

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

    Close position using OnExecutionUpdate()

    Hi,

    I want to know how can I close a position when indicator do something. For example a SMA(5) cross SMA(34).

    I know that I can do it using OnBarUpdate() method, but I am not sure how can I send a signal or something like that to exit using OnExecutionUpdate(). I just only did it with fixed targets and stop but not by a condition like a SMA crossing.

    In this line of this sample shows a position entering and exit until reach a number of ticks:
    stopOrder = ExitLongStopMarket(0, true, 1, execution.Price - 20 * TickSize, "stop", "long limit entry");
    targetOrder = ExitLongLimit(0, true, 1, execution.Price + 40 * TickSize, "target", "long limit entry");

    but what would happen if I want to close a position when indicator do something?

    Link

    https://ninjatrader.com/support/help...lOrder_NT8.zip

    #2
    Hello luislpez,

    Thanks for your post.

    You could create a class level bool (initially set to false). In the OnBarUpdate section of your script, create a crossover condition that compares the SMA(5) to the SMA(34) and flip the bool to true. Then, you could check if the bool is true in the OnExecutionUpdate() section of your script and call your Exit methods to exit a position.

    See the SampleMACrossover strategy that comes default with NinjaTrader for an example of creating a crossover condition comparing two SMAs. To view the script, open a New > NinjaScript Editor window, open the Strategies folder, and select the SampleMACrossover file.

    See this help guide for more information about OnExecutionUpdate(): https://ninjatrader.com/support/help...tionupdate.htm

    And, see this reference sample: https://ninjatrader.com/support/help...and_onexec.htm

    Let us know if we may assist further.

    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hi Brandon,

      Could you please give me an example?. I tried using variables, public enum, but I think that the problem is that OnExecutionUpdate() only run one time when put a position, but when is running cannot call this method again to exit this position.

      Comment


        #4
        Hello luislpez,

        Thanks for your note.

        That is correct. The OnExecutionUpdate() method will only be called when an execution is placed such as an entry or exit order. This method will not get called for each bar update that occurs like in OnBarUpdate().

        See this reference sample which demonstrates using OnExecutionUpdate() to submit orders: https://ninjatrader.com/support/help...and_onexec.htm

        And, see this help guide documentation: https://ninjatrader.com/support/help...tionupdate.htm

        Let us know if we may assist further.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment

        Latest Posts

        Collapse

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