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

2 entry orders help!

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

    #91
    Hello PaulMohn,

    "How to store all live orders for the instrument count into a variable Xa?"

    You could add the orders to a list.

    In the scope of the class:
    private List<Order> myOrders;

    In State.DataLoaded (or where ever you want the list to be reset)
    myOrders = new List<Orders>();

    In OnOrderUpdate():
    myOrders.Add(order);

    Then the list count would be the number of elements in the list.
    Print(myOrders.Count);
    for (int index = 0; index < myOrders.Count; index++)
    {
    }

    If you wanted to add up the quantities of each order:
    Print(myOrders.Sum(item => item.Quantity));

    You could also choose to increment an integer each time an order is filled.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #92
      Hello Chelsea and thanks for the counter suggestion.

      I added a counter and the counter works (after the 2nd order it prints 4 contracts vs the 2 with the list):
      Buy
      2
      multiOrdersTargets : 1
      multiOrdersTargets : 2
      Buy
      2
      multiOrdersTargets : 3
      multiOrdersTargets : 4

      I tested using it in the loop but it's not working, despite the counter working.

      Class level variable:
      private int multiOrdersTargets;

      In the initial Target loop action block:
      ++multiOrdersTargets;

      Move target loop:
      for (int index = 0; index < (multiOrdersTargets + quantitySelector.Value); index++)


      OnBarUpdate reset:
      protected override void OnBarUpdate()
      {
      if (myAccount != null && myAccount.Positions.Where(o => o.Instrument == Instrument).Count() > 0)
      accountPosition = myAccount.Positions.Where(o => o.Instrument == Instrument).Last();
      else
      accountPosition = null;

      if (accountPosition == null)
      {
      multiOrdersTargets = 0;



      it throws an index error.


      But it works when move target loop is
      Move target loop:
      for (int index = 0; index < quantitySelector.Value; index++)

      Why doesn't it work with the counter variable? Thanks!






      Last edited by PaulMohn; 06-27-2022, 12:36 PM.

      Comment


        #93
        Hello PaulMohn,

        quantitySelector is likely a UI item right? Are you using a dispatcher invokeasync to access this?

        What is the invalid index? What collection is it used on? What is the size of the collection?

        Why is multiOrdersTargets being added to quantitySelector.Value?

        This would not be a counter. You are doing some kind of math I am not understanding.

        How is the counter integer not working?


        Are you printing this after incrementing it and its not incrementing?

        Chelsea B.NinjaTrader Customer Service

        Comment


          #94
          please see below 50 seconds:
          Demo
          The demo shows that when I press NumPad7 (i.e. the Move order hotkey) after 1st order is submitted the Xa = 2, then after 2nd order Xa = 4, as intended.

          here my prints:
          Buy
          2
          multiOrdersTargets : 1
          multiOrdersTargets : 2
          Xa : 2
          Buy
          2
          multiOrdersTargets : 1
          multiOrdersTargets : 2
          Xa : 4
          Error on triggering custom event for NinjaScript 'ProfitSnipers' on bar 5420: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
          As you can see in the demo, the incrementer (multiOrdersTargets) variable does work.
          Xa shows 2 on the 1st order, then 4 on the 2nd order.
          I used doOncemultiOrdersTargets as bool flag to only set multiOrdersTargets to zero upon 1st order. It works.

          Please, pay attention to the bold and Red Highlighted variable in the snippets below

          Here the relevant class level variable : multiOrdersTargets / doOncemultiOrdersTargets
          PHP Code:
          public class ProfitSnipers Indicator
          {
             private 
          int multiOrdersTargets;
             private 
          bool doOncemultiOrdersTargets

          Here the OnStateChange() new bool flag : doOncemultiOrdersTargets
          PHP Code:
          doOncemultiOrdersTargets     true

          Here the Initial targets snippet / multiOrdersTargets
          PHP Code:
             private async void Account_OrderUpdate(object senderOrderEventArgs orderUpdateArgs)
             {
              
          #region LONG Orders Initial Target and StopLoss OCO orders

               
          if (entryBuyMar****rder != null && entryBuyMar****rder == orderUpdateArgs.Order && orderUpdateArgs.Order.OrderState == OrderState.Filled)
               {
                
          string oco   Guid.NewGuid().ToString("N");

                
          submitTargetsOrdersList  = new List<Order>();

                 
          submitStopLossesOrdersList = new List<Order>();


                
          await ChartControl.Dispatcher.InvokeAsync((Action) (() =>
                {
                 if (
          UseProfitTarget)
                 {
                  
          currentPTPrice orderUpdateArgs.AverageFillPrice ProfitTargetDistance TickSize;

                   for (
          int index 0index quantitySelector.Valueindex++)
                   {
                    
          profitTargetOrder myAccount.CreateOrder(
                      
          orderUpdateArgs.Order.Instrument,
                      
          OrderAction.Sell,
                      
          OrderType.Limit,
                      
          OrderEntry.Automated,
                      
          TimeInForce.Day,
                      
          orderUpdateArgs.Quantity,
                      
          currentPTPrice,
                      
          0,
                      
          oco+index,
                      
          "Profit Target",
                      
          Core.Globals.MaxDate,
                      
          null);

                    
          submitTargetsOrdersList.Add(profitTarget Order);

                    ++
          multiOrdersTargets;
                    Print(
          " multiOrdersTargets : " multiOrdersTargets);

                   }
                 } 

          Here my loop action block / Xa / multiOrdersTargets / doOncemultiOrdersTargets
          PHP Code:
          {
           
          currentPTPrice profitTargetOrder.LimitPrice + (ProfitTargetMove TickSize);

           if (
          doOncemultiOrdersTargets)
           {
            
          multiOrdersTargets    0;
            
          doOncemultiOrdersTargets false;
           }

           
          int Xa = (multiOrdersTargets +quantitySelector.Value);
           Print(
          "Xa : " Xa);

           for (
          int index 0index Xaindex++)
           {
            
          submitTargetsOrdersList[index].LimitPriceChanged currentPTPrice;
           }

           
          myAccount.Change(submitTargetsOrdersList);


          Here the OnBarUpdate incrementer variable reset / multiOrdersTargets
          PHP Code:
             protected override void OnBarUpdate()
             {
              if (
          myAccount != null && myAccount.Positions.Where(=> o.Instrument == Instrument).Count() > 0)
               
          accountPosition myAccount.Positions.Where(=> o.Instrument == Instrument).Last();
              else
               
          accountPosition null;

              if (
          accountPosition == null)
              {
               
          multiOrdersTargets 0;

              } 

          How to fix the error? Thanks!
          Last edited by PaulMohn; 06-27-2022, 01:34 PM.

          Comment


            #95
            Hello PaulMohn,

            What is the invalid index?

            Print this. Print the index.

            Print the size of the collection.

            What is submitTargetsOrdersList.Count?

            Is Xa less than submitTargetsOrdersList.Count?

            Why is multiOrdersTargets being added to quantitySelector.Value? This doesn't make sense for an index.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #96
              Demo

              index reaches 1 max

              submitTargetsOrdersList.Count : 2 (after 1st order executed)

              Xa is more than submitTargetsOrdersList.Count

              Buy
              2
              multiOrdersTargets : 1
              multiOrdersTargets : 2
              Xa : 2
              index : 0
              index : 1
              submitTargetsOrdersList.Count : 2
              Buy
              2
              multiOrdersTargets : 1
              multiOrdersTargets : 2
              Xa : 4
              index : 0
              index : 1
              Error on triggering custom event for NinjaScript 'ProfitSnipers' on bar 5444: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
              multiOrdersTargets is the added value I need to make the Targets orders loop submit the total number of existing orders (4) instead of the resetting to 0 one at each new order (2).
              submitTargetsOrdersList[index].LimitPriceChanged = currentPTPrice;

              as

              1st order
              submitTargetsOrdersList[2].LimitPriceChanged = currentPTPrice;

              2nd order
              submitTargetsOrdersList[4].LimitPriceChanged = currentPTPrice;

              isn't it right? Thanks!

              Comment


                #97
                Hello PaulMohn,

                submitTargetsOrdersList has a count of 2. Meaning there are only two elements (orders) added to the list. You cannot ask for more elements than are in the list.

                Either add 4 elements to the list, or don't ask for more than 2.

                PaulMohn an index cannot be larger than the size of the collection.

                Why is multiOrdersTargets being added to quantitySelector.Value? This is what is causing your error.

                What are you trying to do?

                If you are using a counter then why are you using a list?

                If you have a list, you don't need counter. To loop through the list, loop from 0 to the list.Count.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #98
                  Thanks Chelsea for the list count vs counter observation.
                  I think the counter doesn't help as it's the list that defines the targets available to be moved, regardless of the loop upper limit.
                  So I think I'll abandon the counter use idea.

                  My problem is that the list RESETS itself when this is true
                  PHP Code:
                  else if ((profitTargetOrder != null && (profitTargetOrder.OrderState == OrderState.Filled
                                
                  || profitTargetOrder.OrderState == OrderState.Rejected
                                
                  || profitTargetOrder.OrderState == OrderState.Cancelled))
                        || (
                  stopLossOrder != null && (stopLossOrder.OrderState == OrderState.Filled
                                
                  || stopLossOrder.OrderState == OrderState.Rejected
                                
                  || stopLossOrder.OrderState == OrderState.Cancelled)))
                  {
                       
                  entryBuyMar****rder  null;
                       
                  entrySellMar****rder null;

                         
                  profitTargetOrder  null;
                         
                  stopLossOrder   null;
                      } 

                  I think because of
                  profitTargetOrder = null;
                  stopLossOrder = null;

                  because profitTargetOrder and stopLossOrder are the orders variables for the submitTargetsOrdersList in the Targets Snippet below

                  submitTargetsOrdersList.Add(profitTargetOrder);


                  PHP Code:
                    #region Initial Target & StopLoss Snippets

                     
                  private async void Account_OrderUpdate(object senderOrderEventArgs orderUpdateArgs)
                     {
                      
                  #region LONG Orders Initial Target and StopLoss OCO orders

                       
                  if (entryBuyMar****rder != null && entryBuyMar****rder == orderUpdateArgs.Order && orderUpdateArgs.Order.OrderState == OrderState.Filled)
                       {
                        
                  string oco   Guid.NewGuid().ToString("N");

                        
                  submitTargetsOrdersList  = new List<Order>();

                         
                  submitStopLossesOrdersList = new List<Order>();

                        
                  await ChartControl.Dispatcher.InvokeAsync((Action) (() =>
                        {
                         if (
                  UseProfitTarget)
                         {
                          
                  currentPTPrice orderUpdateArgs.AverageFillPrice ProfitTargetDistance TickSize;

                           for (
                  int index 0index quantitySelector.Valueindex++)
                           {
                            
                  profitTargetOrder myAccount.CreateOrder(
                              
                  orderUpdateArgs.Order.Instrument,
                              
                  OrderAction.Sell,
                              
                  OrderType.Limit,
                              
                  OrderEntry.Automated,
                              
                  TimeInForce.Day,
                              
                  orderUpdateArgs.Quantity,
                              
                  currentPTPrice,
                              
                  0,
                              
                  oco+index,
                              
                  "Profit Target",
                              
                  Core.Globals.MaxDate,
                              
                  null);

                            
                  submitTargetsOrdersList.Add(profitTargetOrder);

                           }
                         } 

                  What I need is a way to get the TOTAL running count of the profitTargetOrder value upon multiple simultaneous orders.

                  I just tried to reset the profitTargetOrder and stopLossOrder variables in the OnBarUpdate instead as
                  PHP Code:
                     #region OnBarUpdate Prints Snippets

                     
                  protected override void OnBarUpdate()
                     {
                      if (
                  myAccount != null && myAccount.Positions.Where(=> o.Instrument == Instrument).Count() > 0)
                       
                  accountPosition myAccount.Positions.Where(=> o.Instrument == Instrument).Last();
                      else
                       
                  accountPosition null;

                      if (
                  accountPosition == null)
                      {
                       
                  profitTargetOrder  null;
                       
                  stopLossOrder   null;

                      } 

                  And commenting them out in the previous snippet as
                  PHP Code:
                  else if ((profitTargetOrder != null && (profitTargetOrder.OrderState == OrderState.Filled
                                
                  || profitTargetOrder.OrderState == OrderState.Rejected
                                
                  || profitTargetOrder.OrderState == OrderState.Cancelled))
                        || (
                  stopLossOrder != null && (stopLossOrder.OrderState == OrderState.Filled
                                
                  || stopLossOrder.OrderState == OrderState.Rejected
                                
                  || stopLossOrder.OrderState == OrderState.Cancelled)))
                  {
                       
                  entryBuyMar****rder  null;
                       
                  entrySellMar****rder null;

                  //       profitTargetOrder  = null;
                  //       stopLossOrder   = null;
                      


                  To only reset the list to zero once the position account is null (not as soon as the profitTargetOrder is Filled or Rejected or Cancelled)

                  but it's still only printing 2 for the submitTargetsOrdersList.Count value after 2nd order.
                  PHP Code:
                  accountPosition == null True
                  accountPosition 
                  == null True
                  Buy
                  2
                  initial
                  submitTargetsOrdersList.Count 1
                  initial
                  submitTargetsOrdersList.Count 2
                  Move 
                  submitTargetsOrdersList.Count 2
                  Buy
                  2
                  initial
                  submitTargetsOrdersList.Count 1
                  initial
                  submitTargetsOrdersList.Count 2
                  Move 
                  submitTargetsOrdersList.Count 2
                  Move 
                  submitTargetsOrdersList.Count 2
                  Move 
                  submitTargetsOrdersList.Count 

                  How can I get ALL the running profitTargetOrder orders added to the submitTargetsOrdersList list without RESSETING TO ZERO after each new order submission (i.e. upon new numPad7 presses) ?

                  My end goal it to be able to move ALL profitTargetOrder orders upon multiple simultaneous orders are live,
                  NOT just the most recent profitTargetOrder orders of the last LONG order sumbitted (what the submitTargetsOrdersList currently does). Thanks!
                  Last edited by PaulMohn; 06-27-2022, 02:57 PM.

                  Comment


                    #99
                    Hello PaulMohn,

                    You can use list.Clear() to clear a list.
                    https://docs.microsoft.com/en-us/dot...r?view=net-6.0

                    Your code appears to be making a new list object every time entryBuyMar****rder is the order updating.

                    If you want multiple orders in here, don't make a new list object here. Continue to add to the existing list object.

                    Move submitTargetsOrdersList = new List<Order>(); to State.DataLoaded.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      Thanks Chelsea, I'll test that next.
                      In the meantime I found this working way

                      Class level variable
                      PHP Code:
                      private List<Order>  testr

                      Code in the NumPad7 Hotkey Action Block
                      PHP Code:
                      testr = new List <Order>();

                       foreach(
                      Account acct in Account.All)
                       {
                        if (
                      acct.Positions != null)
                        {
                         foreach(
                      Position pos in acct.Positions)
                         {
                          if (
                      pos.MarketPosition == MarketPosition.Long)
                          {
                           
                      lock(myAccount.Orders) {
                            foreach(
                      Order moveTPOrders in myAccount.Orders)
                            {
                             
                      testr.Add(moveTPOrders);
                            }
                            for (
                      int index 0index testr.Countindex++)
                            {
                             
                      testr[index].LimitPriceChanged currentPTPrice;
                            }

                           }
                          }
                         }
                        }
                       }
                       
                      myAccount.Change(testr);


                      From previous solution

                      Any tip on simplifying the multiple loops above?

                      Any tip on how to get the orders by the groups they were entered in? Thanks!
                      Last edited by PaulMohn; 06-27-2022, 05:03 PM.

                      Comment


                        Originally posted by NinjaTrader_ChelseaB View Post
                        Hello PaulMohn,

                        You can use list.Clear() to clear a list.
                        https://docs.microsoft.com/en-us/dot...r?view=net-6.0

                        Your code appears to be making a new list object every time entryBuyMar****rder is the order updating.

                        If you want multiple orders in here, don't make a new list object here. Continue to add to the existing list object.

                        Move submitTargetsOrdersList = new List<Order>(); to State.DataLoaded.
                        Hello Chelsea and thanks for the suggestion.

                        Why would I need to clear the list? and where?

                        I've moved the 3 lists to OnDataLoaded action block (and commented them out in the OnPreviewKeyDown() and Account_OrderUpdate() methods)

                        PHP Code:
                        multiOrders = new List<Order>();

                        submitTargetsOrdersList = new List<Order>();

                        submitStopLossesOrdersList = new List<Order>(); 

                        But it's not working. Please would you clarify what you wanted me to infer? Thanks!


                        Also, I noticed you use a kind of array from a list, line 126
                        submissionAccount.Submit(new[] { entryBuyMar****rder });

                        in your ProfitChaseStopTrailIndicatorExample script.

                        Why did you transform the list into an array and didn't just use a list? Are there benefits? What are they if any? Thanks!


                        Also, how would you restrict simplify and restrict the multi-loops solution to the instrument the indicator is loaded on?
                        I'd privilege you solution as it seems to be simpler and less resource intensive if I can make it work. Thanks!
                        Last edited by PaulMohn; 06-28-2022, 05:34 AM.

                        Comment


                          Hello PaulMohn,

                          You were previously creating a new list in the Account_OrderUpdate method.
                          submitTargetsOrdersList = new List<Order>();
                          Why are you creating new list here?
                          If you don't need to reset the list, then don't make a new list or clear it. If you need to reset it, clear it.

                          When should you clear it? When you want to reset it.
                          Don't clear it if you don't want to reset it.


                          "OnDataLoaded" is this your own custom method? Where is this method triggered from?
                          Is this custom method triggered from OnStateChange when State is State.DataLoaded?

                          submissionAccount.Submit() requires a collection. entryBuyMar****rder is not a collection. This is a single Order variable. I did not transform any list into any array.


                          What isn't working?
                          You have declared a new list and have you added an element to the list and then printed the list count and the element is not there?


                          You can check the instrument an order is placed to. <Order>.Instrument.
                          https://ninjatrader.com/support/help.../nt8/order.htm
                          Last edited by NinjaTrader_ChelseaB; 06-28-2022, 07:50 AM.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            What's the use of "new[] {}" in
                            submissionAccount.Submit(new[] { entryBuyMar****rder });
                            ?
                            Isn't that an empty array? If not what is it? Thanks!


                            I added the list clearing methods to the OnBarUpdate method
                            (the idea is the list should be reset only when no open orders remain)
                            PHP Code:
                               protected override void OnBarUpdate()
                               {
                                if (
                            myAccount != null && myAccount.Positions.Where(=> o.Instrument == Instrument).Count() > 0)
                                 
                            accountPosition myAccount.Positions.Where(=> o.Instrument == Instrument).Last();
                                else
                                 
                            accountPosition null;

                                if (
                            accountPosition == null)
                                {
                                 
                            multiOrders.Clear();

                                 
                            submitTargetsOrdersList.Clear();

                                 
                            submitStopLossesOrdersList.Clear();

                                } 

                            And prints statement in the NumPad7 Hotkeys Move Target loop method
                            PHP Code:
                            {
                               
                            currentPTPrice profitTargetOrder.LimitPrice + (ProfitTargetMove TickSize);

                               for (
                            int index 0index quantitySelector.Valueindex++)
                               {
                                  
                            submitTargetsOrdersList[index].LimitPriceChanged currentPTPrice;
                                  Print(
                            "submitTargetsOrdersList.Count : " submitTargetsOrdersList.Count);
                               }

                               
                            myAccount.Change(submitTargetsOrdersList);


                            and it prints the right value
                            Buy
                            submitTargetsOrdersList.Count : 2
                            submitTargetsOrdersList.Count : 2
                            Buy
                            submitTargetsOrdersList.Count : 4
                            submitTargetsOrdersList.Count : 4
                            But it's not working. It still only moves 1 order. And now it moves it only once upon multiple NumPad7 key presses
                            (whereas before it moved it as many times as there were key presses, as it was correct that way).

                            Do you need a demo?

                            What else is not working? Thanks!
                            Last edited by PaulMohn; 06-28-2022, 08:54 AM.

                            Comment


                              Hello PaulMohn,

                              This is an array with one Order element.

                              Account.Submit() requires a collection. This means if you have one order to send, you need to wrap it in a collection like an array.

                              See the help guide.
                              https://ninjatrader.com/support/help...nt8/submit.htm

                              You will need to debug your code.

                              Previously you were asking about adding orders to a list.

                              What was moved? What line of code does the moving? Is this line of being triggered? Is this being triggered with the expected values?

                              Use prints to understand behavior.


                              You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our NinjaTrader Ecosystem team follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.
                              Last edited by NinjaTrader_ChelseaB; 06-28-2022, 09:09 AM.
                              Chelsea B.NinjaTrader Customer Service

                              Comment


                                I used those prints but it's not helping
                                PHP Code:
                                       {
                                        
                                currentPTPrice profitTargetOrder.LimitPrice + (ProfitTargetMove TickSize);

                                         Print(
                                "1. currentPTPrice : " currentPTPrice);
                                        for (
                                int index 0index quantitySelector.Valueindex++)
                                        {
                                         
                                submitTargetsOrdersList[index].LimitPriceChanged currentPTPrice;
                                         Print(
                                "1. submitTargetsOrdersList.Count : " submitTargetsOrdersList.Count);
                                         Print(
                                "2. currentPTPrice : " currentPTPrice);

                                        }

                                         Print(
                                "2. submitTargetsOrdersList.Count : " submitTargetsOrdersList.Count);
                                        
                                myAccount.Change(submitTargetsOrdersList);
                                         Print(
                                "3. submitTargetsOrdersList.Count : " submitTargetsOrdersList.Count);
                                       } 

                                Buy
                                1. currentPTPrice : 110.91
                                1. submitTargetsOrdersList.Count : 2
                                2. currentPTPrice : 110.91
                                1. submitTargetsOrdersList.Count : 2
                                2. currentPTPrice : 110.91
                                2. submitTargetsOrdersList.Count : 2
                                3. submitTargetsOrdersList.Count : 2
                                Buy
                                1. currentPTPrice : 110.9
                                1. submitTargetsOrdersList.Count : 4
                                2. currentPTPrice : 110.9
                                1. submitTargetsOrdersList.Count : 4
                                2. currentPTPrice : 110.9
                                2. submitTargetsOrdersList.Count : 4
                                3. submitTargetsOrdersList.Count : 4

                                What was moved?
                                The Targets OCO orders moved.


                                What line of code does the moving?
                                myAccount.Change(submitTargetsOrdersList);

                                Is this line of being triggered?
                                Yes (please see prints above).

                                PHP Code:
                                Is this being triggered with the expected values

                                Yes (please see prints above).


                                Did you have other prints in mind? Thanks!

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by JoMoon2024, Today, 06:56 AM
                                0 responses
                                6 views
                                0 likes
                                Last Post JoMoon2024  
                                Started by Haiasi, 04-25-2024, 06:53 PM
                                2 responses
                                19 views
                                0 likes
                                Last Post Massinisa  
                                Started by Creamers, Today, 05:32 AM
                                0 responses
                                6 views
                                0 likes
                                Last Post Creamers  
                                Started by Segwin, 05-07-2018, 02:15 PM
                                12 responses
                                1,786 views
                                0 likes
                                Last Post Leafcutter  
                                Started by poplagelu, Today, 05:00 AM
                                0 responses
                                3 views
                                0 likes
                                Last Post poplagelu  
                                Working...
                                X