Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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, 04: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()


          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.

              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, 08:46 AM.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by warreng86, 11-10-2020, 02:04 PM
                6 responses
                1,360 views
                0 likes
                Last Post mathewlo  
                Started by Perr0Grande, Today, 08:16 PM
                0 responses
                5 views
                0 likes
                Last Post Perr0Grande  
                Started by elderan, Today, 08:03 PM
                0 responses
                9 views
                0 likes
                Last Post elderan
                by elderan
                 
                Started by algospoke, Today, 06:40 PM
                0 responses
                10 views
                0 likes
                Last Post algospoke  
                Started by maybeimnotrader, Today, 05:46 PM
                0 responses
                14 views
                0 likes
                Last Post maybeimnotrader  
                Working...
                X