Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Closing Manual Position in Script

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

    Closing Manual Position in Script

    Hi

    I open my positions manually on NinjaTrader, however I would like to close them automatically (e.g. when price crosses a moving average). Is it possible to do this in ninjascript, and if so are there any examples? I need something more advanced than what is offered by the ATM options

    #2
    Hello robertT2019,

    Thank you for your post.

    The simplest way to do this wouldn't involve scripting at all. In NinjaTrader 8, you can attach an order, for example, a Stop Limit or Stop Market order, to an indicator. So, you could manually place an order, then place an exit order which you then attach to the desired indicator, like an SMA. When the price crosses the indicator, it would hit the exit order and exit the trade for you.

    Here's a publicly available link to our YouTube channel that goes over this:



    This becomes much more complicated to do with NinjaScript. Strategies would generally only be able to see orders that were placed by the strategy and wouldn't have any information on a manual order. It would be possible to accomplish something like this by using the Addon framework to "listen" for account events like a fill, and then execute other logic. This help guide page has a great example of listening for account updates. Although the page says this is for an "Addon", you could also use this inside of an indicator or strategy.

    https://ninjatrader.com/support/help...ount_class.htm

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hello

      It's not clear how can we flatten the position/account from the code of the strategy without stopping a strategy?

      Comment


        #4
        Hello ren37,

        Thank you for your reply.

        You would have to check the current position, submit the appropriate orders to close the position, and explicitly cancel any working entry orders using CancelOrder():



        This would require you to be tracking entry orders as is seen in this example from our help guide:



        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Hello Kate

          Thank you.

          The issue is that OnOrderUpdate() is not triggered when you place your order manually from Chart Trader.
          Hence, we don't have orders objects to use in Account.Cancel()
          Even OnOrderUpdate() would trigger we don't understand from order properties if it is created on the chart the strategy is attached to.
          Only 'one strategy per instrument' will work correctly.

          I used PositionAccount.MarketPosition to check Long/Short states, Account.CancelAllOrders() and Account.CreateOrder/Sumbit()
          It works fine.

          Last edited by ren37; 05-07-2022, 12:03 PM.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by algospoke, Yesterday, 06:40 PM
          2 responses
          19 views
          0 likes
          Last Post algospoke  
          Started by ghoul, Today, 06:02 PM
          3 responses
          14 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by jeronymite, 04-12-2024, 04:26 PM
          3 responses
          45 views
          0 likes
          Last Post jeronymite  
          Started by Barry Milan, Yesterday, 10:35 PM
          7 responses
          20 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by AttiM, 02-14-2024, 05:20 PM
          10 responses
          181 views
          0 likes
          Last Post jeronymite  
          Working...
          X