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 Barry Milan, Today, 10:35 PM
          1 response
          8 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by WeyldFalcon, 12-10-2020, 06:48 PM
          14 responses
          1,428 views
          0 likes
          Last Post Handclap0241  
          Started by DJ888, Yesterday, 06:09 PM
          2 responses
          9 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by jeronymite, 04-12-2024, 04:26 PM
          3 responses
          40 views
          0 likes
          Last Post jeronymite  
          Started by bill2023, Today, 08:51 AM
          2 responses
          16 views
          0 likes
          Last Post bill2023  
          Working...
          X