Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Series of orders using hot keys

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

    Series of orders using hot keys

    I want to use hot keys to send series of orders.
    For example it would be something like this:

    foreach (element in vector){
    Enterlong(element)
    wait(1 second)
    }

    So that if I want to buy 9 contracts in series 4 contracts, one second later 3 contracts and another second later 2 contracts it would be a set vector [4,3,2]. However I need to know how to make this loop wait 1 second after each individual order and how to assign this loop to a given hot key.

    #2
    Hello grzegorzgc3,

    For this type of use you would not want to use a loop because there is no way to pause in NinjaScript, you never want to hold up processing in a script so that it can run in realtime.

    A general way to do this would be to use a condition which triggers a first order and then use a variable to determine how many orders have been submitted. You can increment the variable each time you submit a new order. That variable could be used to control when the condition that submits the orders is allowed to work.

    To do an action 1 second later you would need to use a 1 second series so that each bar close happens at a regular interval. You can use AddDataSeries to add a secondary series for processing logic within that BarsInProgress at that interval. https://ninjatrader.com/support/help...ghtsub=adddata

    For hotkeys that would be a difficult subject, there is currently no means to assign hotkeys in NinjaScript. You can technically use WPF keyboard events but then you would have to handle the existing key events that the chart uses which is not something I have a sample of. If you are well versed in C#/WPF programming you can look into using the preview key events with the ChartControl. A better approach would be to add a custom button to the chart to trigger the action. You can find a sample of creating a button with an action here: https://ninjatrader.com/support/help...ub=usercontrol

    Comment


      #3
      NinjaTrader_Jesse​​​​​​ I am aware that I will not be able to pause this loop however this is not a problem for me. Your answer is well built however can you answer with the assumption that I will not pause the loop?
      Last edited by grzegorzgc3; 10-24-2022, 01:26 PM. Reason: assumption not clear despite being in the first sentence

      Comment


        #4
        Hello grzegorzgc3,

        What assumption are you referring to?

        A loop is not needed for what you described, you can just use a 1 second series and a variable to allow 1 order for each OnBarUpdate call at the frequency of 1 second. Another reason that a loop could not be used is that waiting 1 second would not be possible in historical data, you can pause execution but because the code is all run for the bar the orders would all happen at the same time. Pausing in realtime would affect the scripts ability to process and would cause issue with order submission.

        Comment


          #5
          NinjaTrader_Jesse for the last time, I am not going to pause it so the problems with pausing it don't concern me. And saying that I don't need a loop because I can use a way more complex solution is exactly the opposite of what should be written. Even more so that what you describe is mechanics of a loop. So please give me a simple solution. I promise, if I ever see that I made a mistake by starting a loop, I'll just wait to its end and send the opposite of it (buy - sell, sell - buy). So you don't have to worry about it.

          Comment


            #6
            Hello grzegorzgc3,

            The solution that I provided is already be the most simple and correct solution for what you posted in post 1. A loop is not a good solution for what you described due to how the script operates, the orders would just be submitted all at once.

            To accommodate 1 second spacing you can use how the platform operates to your advantage. The platform executes OnBarUpdate at the frequency of the data series you use so if we assume you add a 1 second secondary series that give you the ability to execute orders with spacing of 1 second. That would work in both historical and realtime data so you could use the platforms tools like backtesting or realtime.

            The reason I suggested using a 1 second secondary series is that gives you the ability to use any primary series you wanted and still have the ability to execute the orders with 1 second spacing between them.

            Using a variable would allow you to control how many orders from your array are submitted. If you had an array of 3 values like you provided [4,3,2] you could increment a variable to know 3 orders were placed to prevent more orders from being submitted after that.



            Comment


              #7
              NinjaTrader_Jesse thank you for the answer, now I know what you mean. Just one more question. At Interactive Brokers API I saw that ticker was wriiten as one of arguments of the function that was submitting orders. Here in Ninja Trader API I don't see it anywhere. Where do you write it?

              Comment


                #8
                Hello grzegorzgc3,

                I am not sure what you are referring to in IB to provide an answer here, do you have more specific details on the feature in IB that you are referring to?

                If you mean that you could supply a instrument name for the order then in NinjaTrader that is not something you can do, you would need to submit the order to the BarsInProgress for that instrument to have it submitted to a specific instrument your script is using. If you script is only using 1 instrument the order methods will submit the orders to that instrument.

                Comment


                  #9
                  NinjaTrader_Jesse well how is algorithm supposed to know for which instrument the orders are submitted? I have to write it somewhere.

                  Sorry, it wasn't in the order placement, nevertheless it is written in the code. https://interactivebrokers.github.io...racts.html#fut
                  Last edited by grzegorzgc3; 10-25-2022, 11:37 AM.

                  Comment


                    #10
                    Hello grzegorzgc3,

                    A strategy is applied to a single instrument, any orders you place go to that instrument. There is also the concept of multi instrument strategy where you add other instruments in code, in that use case you use the BarsInProgress index for the instrument you want to trade.

                    Comment


                      #11
                      NinjaTrader_Jesse great, thank you for repeating that instead of answering my question. Algorithm works for one instrument. Now, where in the code do you write the ticker of that instrument. Surely, the algorithm doesn't read my mind so it has to have the ticker written somewhere in the code.

                      Comment


                        #12
                        Hello grzegorzgc3,

                        That is the answer to the question. You don't hard code the instrument in your code, you select the instrument when you apply the strategy.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        633 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        364 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        105 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        567 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        568 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X