Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to cycle/select orders in ninjascript

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

    How to cycle/select orders in ninjascript

    Hi everyone,

    So I am trying to code my first strategy in Ninjascript. I have some coding experience, mainly from Java and MQL4 (MT4).

    What I want to do is to create a strategy that cancels a limit order I have manually placed, if price comes within a certain distance of the order, and then reverses a specific distance. Basically remove the pending limit order if price comes close, but doesnt trigger, and then retraces.

    I have a thought of how I would do this. Get the time of order submission. Get the number of bars since this timestamp, cycle through these bars to find the highest point (if a sell limit). If the highest point is closer than x amount of ticks from my order, and price after that reverses z amount of ticks, cancel the order.

    I have a couple of problems tho, the main one (currently), being:

    How to cycle through the orders of the current chart symbol? I have been googling and have found one example, creating a for-loop
    "for (int j=0; j < NinjaTrader.Cbi.Globals.Accounts[i].Positions.Count; j++ )". But this returns the error "the type or namespace name 'Globals' does not exist in the namespace'NinjaTrader.Cbi'.

    Any tips on how to resolve this?

    Thank you
    Mattias

    #2
    Hello Mattias,
    Thanks for your post and welcome to the NInjaTrader forum.

    It would not be possible to create a strategy that manages manually placed orders.

    However, it would be possible to accomplish something like this by using the Addon framework to watch for account events like a fill and then execute other logic. This help guide page has an example of listening for account updates: https://ninjatrader.com/support/help...ount_class.htm

    I think it is important to note that there is a divide between what a strategy does, and what you want. The difference here is that you're doing all the subscribing, choosing accounts, etc. yourself, and in the case of a strategy all this stuff is handled for you. What I believe you want is a tool similar to how a superDOM or ATM works, which is not how strategies are intended to work.

    A strategy is specifically meant to be isolated so when you collect its performance, its for that script, so you can differentiate that data. You're not using a strategy with the managed approach, but instead just making your own tool that behaves how you want it to. Then you are just interfacing with the standard C# objects in the platform.

    If you wanted to loop through the Accounts orders collection, that would be one way to find the current charts orders: https://ninjatrader.com/support/help...rs_account.htm
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Thank you Josh, although I am not sure that I get what you mean with the first part of your message. Do you mean that I can't even cancel an order with a strategy if it's placed manually?

      The loop works wonders, thank you!

      Comment


        #4
        Alright, I understand that i can't cancel a manually placed order directly by using a strategy.

        I have downloaded the add-on framework and have looked into it a bit. I am not very skilled at programming, how would one go about programming what I want?

        In the default framework I can enter market orders, I guess I want to add a button for a limit order, but where would I then add the logic for the order cancellation? Is it possible to just add it in the "onBarUpdate()" part of the code like I did in the strategy? It doesnt seem to work when I do it.

        Thank you,
        Mattias
        Last edited by Krossaren96; 05-19-2019, 03:59 PM. Reason: I understood your first post

        Comment


          #5
          Mattias,

          Using an addon you could loop through the Orders collection and use the Account class to find the specific orders you want. You could also place an order with the Account class by using CreateOrder()
          https://ninjatrader.com/support/help...reateorder.htm

          There is no concept of OnBarUpdate() inside an Addon, so the logic would need to be based on Executions, account events, or your own custom event like a button press.

          For more information on the Add On development process please see the following link: https://ninjatrader.com/support/help...t_overview.htm
          Josh G.NinjaTrader Customer Service

          Comment


            #6
            Listening for an order change is not what I need, is there not way to listen to continuous data in an Add-on?

            Thank you,
            Mattias

            Comment


              #7
              Mattias,

              Sure, just use OnMarketData() for level 1 data and OnMarketDepth for level 2 data.

              https://ninjatrader.com/support/help...us/?add_on.htm
              Josh G.NinjaTrader Customer Service

              Comment


                #8
                Thank you, it starts to make sense now. However, there are a lot of commands that doesnt work in the addon that worked in the strategy, some examples are "CurrentBar", "BarsInProgress" etc? How do I go around this?
                Mattias
                Last edited by Krossaren96; 05-20-2019, 07:46 AM.

                Comment


                  #9
                  Mattias,

                  You would need to use a BarsRequest for anything like that: https://ninjatrader.com/support/help...arsrequest.htm
                  Here is a sample you may find useful: https://ninjatrader.com/support/foru...est-indicators

                  I have already provided some links above, but below are all the resources from our help guide regarding Addons:

                  https://ninjatrader.com/support/help...t_overview.htm
                  https://ninjatrader.com/support/help...ng_add_ons.htm
                  https://ninjatrader.com/support/help...don_window.htm
                  https://ninjatrader.com/support/help...r_an_addon.htm
                  https://ninjatrader.com/support/help...us/?add_on.htm
                  Josh G.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by stafe, Today, 10:43 AM
                  0 responses
                  7 views
                  0 likes
                  Last Post stafe
                  by stafe
                   
                  Started by UltraNIX, Today, 09:13 AM
                  1 response
                  10 views
                  0 likes
                  Last Post NinjaTrader_ChrisL  
                  Started by Tuxstuff215, Today, 06:32 AM
                  1 response
                  15 views
                  0 likes
                  Last Post NinjaTrader_ChrisL  
                  Started by carlot, Today, 08:10 AM
                  1 response
                  20 views
                  0 likes
                  Last Post NinjaTrader_ChrisL  
                  Started by KirkHammett, Yesterday, 11:15 PM
                  1 response
                  16 views
                  0 likes
                  Last Post NinjaTrader_ChrisL  
                  Working...
                  X