Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cant send single orders - only multiples beyond single lot

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

    Cant send single orders - only multiples beyond single lot

    HI
    Using some original code from ProfitSniper which I have customised.
    I am trying to get this code to work. It works fine with 2 lots or more but gives errors on single lots.
    Anyone spot my error?
    Edit - mmm... so it appears to be the very first order which will not do single lots. If I do 2 lots, close and then do 1 lot it works

    Code:
            #region Initial Target & StopLoss Snippets.
                
                private void Account_OrderUpdate(object sender, OrderEventArgs orderUpdateArgs)
                {
                    /*
                    Print("2A. Account_OrderUpdate() with KEYDOWN " + Time[0]);
                    Print("entryBuyMar****rder != null : " + (entryBuyMar****rder != null));
                    Print("entryBuyMar****rder == orderUpdateArgs.Order : " + (entryBuyMar****rder == orderUpdateArgs.Order));
                    Print("orderUpdateArgs.Order.OrderState == OrderState.Filled : " + (orderUpdateArgs.Order.OrderState == OrderState.Filled));
                    Print("orderUpdateArgs.Order.OrderState == OrderState.Working : " + (orderUpdateArgs.Order.OrderState == OrderState.Working));
                    Print(" ");
                    Print("accountPosition == null : " + (accountPosition == null));
                    Print("profitTargetOrder == nul : " + (profitTargetOrder == null));
                    Print("stopLossOrder == nul : " + (stopLossOrder == null));
                    Print(" ");
                    */
    
                    // LONG Order Target and StopLoss OCO orders
                    if (entryBuyMar****rder != null && entryBuyMar****rder == orderUpdateArgs.Order && orderUpdateArgs.Order.OrderState == OrderState.Filled)
                    {
                        
                        string oco            = Guid.NewGuid().ToString("N");
                        string oco1         = Guid.NewGuid().ToString("N");
                        submitOrdersArray    = new List<Order>();
                        liveq = orderUpdateArgs.Quantity;
                            currentPtPrice = orderUpdateArgs.AverageFillPrice + ProfitTargetDistance * TickSize;
                            currentPtPrice1 = orderUpdateArgs.AverageFillPrice + (ProfitTargetDistance * TickSize*2);
                            currentSlPrice = orderUpdateArgs.AverageFillPrice - StopLossDistance * TickSize;
                            currentSlPrice1 = orderUpdateArgs.AverageFillPrice - (StopLossDistance * TickSize)+TickSize;
    
                                if(liveq >=2)
        {
            if(liveq % 2 ==1)
            
             Q0 = (int)(liveq * 0.5 + 0.5);        
            else
            Q0 = (int)(liveq* 0.5);
        
            Q1 = liveq - (int)Q0;
            
        }
    
                            if (PrintDetails)
                                Print(string.Format("{0} | Account_OrderUpdate | placing profit target | currentPtPrice: {1}", orderUpdateArgs.Time, currentPtPrice));
            if(liveq >=2)
            {
                            profitTargetOrder1 = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Sell, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day, Q1, currentPtPrice1, 0, oco1, "Profit Target", Core.Globals.MaxDate, null);                                      
                            submitOrdersArray.Add(profitTargetOrder1);
                            
                            profitTargetOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Sell, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day, Q0, currentPtPrice, 0, oco, "Profit Target 1", Core.Globals.MaxDate, null);
                            submitOrdersArray.Add(profitTargetOrder);
    
                                        stopLossOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, Q0, 0, currentSlPrice, oco, "Stop Loss", Core.Globals.MaxDate, null);
                            submitOrdersArray.Add(stopLossOrder);
                                stopLossOrder1 = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, Q1, 0, currentSlPrice1, oco1, "Stop Loss", Core.Globals.MaxDate, null);
                            submitOrdersArray.Add(stopLossOrder1);
    
            }
            else
            {
                            profitTargetOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Sell, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day, Q0, currentPtPrice, 0, oco, "Profit Target 1", Core.Globals.MaxDate, null);
                            submitOrdersArray.Add(profitTargetOrder);
                            stopLossOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, Q0, 0, currentSlPrice, oco, "Stop Loss", Core.Globals.MaxDate, null);
                            submitOrdersArray.Add(stopLossOrder);
    
        
            }
    
    
                            if (PrintDetails)
                                Print(string.Format("{0} | Account_OrderUpdate | placing stop loss | currentSlPrice: {1}", orderUpdateArgs.Time, currentSlPrice));
    
    
                        myAccount.Submit(submitOrdersArray);
                        
    
                        
                    }// end buyorder
    
                    
                    
                //     SHORT Order Target and StopLoss OCO orders
                    if (entrySellMar****rder != null && entrySellMar****rder == orderUpdateArgs.Order && orderUpdateArgs.Order.OrderState == OrderState.Filled)
                    {
                        string oco            = Guid.NewGuid().ToString("N");
                        string oco1         = Guid.NewGuid().ToString("N");
                        submitOrdersArray    = new List<Order>();
                        liveq = orderUpdateArgs.Quantity;
    
                        
                        currentPtPrice = orderUpdateArgs.AverageFillPrice - ProfitTargetDistance * TickSize;
                            currentPtPrice1 = orderUpdateArgs.AverageFillPrice - (ProfitTargetDistance * TickSize*2);
                            currentSlPrice = orderUpdateArgs.AverageFillPrice + StopLossDistance * TickSize;
                            currentSlPrice1 = orderUpdateArgs.AverageFillPrice + (StopLossDistance * TickSize)+TickSize;
    
                                if(liveq >=2)
        {
            if(liveq % 2 ==1)
            
             Q0 = (int)(liveq * 0.5 + 0.5);    
            else
            Q0 = (int)(liveq* 0.5);
        
            Q1 = liveq - (int)Q0;
            
        }
    
    
                            if (PrintDetails)
                                Print(string.Format("{0} | Account_OrderUpdate | placing profit target | currentPtPrice: {1}", orderUpdateArgs.Time, currentPtPrice));
    if(liveq >=2)
            {
                            profitTargetOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Buy, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day, Q0, currentPtPrice, 0, oco, "Profit Target", Core.Globals.MaxDate, null);
                            submitOrdersArray.Add(profitTargetOrder);
                        
                            profitTargetOrder1 = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Buy, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day, Q1, currentPtPrice1, 0, oco1, "Profit Target", Core.Globals.MaxDate, null);
                            submitOrdersArray.Add(profitTargetOrder1);
                
                                        stopLossOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Buy, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, Q0, 0, currentSlPrice, oco, "Stop Loss", Core.Globals.MaxDate, null);
                            submitOrdersArray.Add(stopLossOrder);
                
                                        stopLossOrder1 = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Buy, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, Q1, 0, currentSlPrice1, oco1, "Stop Loss", Core.Globals.MaxDate, null);
                            submitOrdersArray.Add(stopLossOrder1);
    
            }    
            else
            {
                            profitTargetOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Buy, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day, Q0, currentPtPrice, 0, oco, "Profit Target", Core.Globals.MaxDate, null);
                            submitOrdersArray.Add(profitTargetOrder);
                            stopLossOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Buy, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, Q0, 0, currentSlPrice, oco, "Stop Loss", Core.Globals.MaxDate, null);
                            submitOrdersArray.Add(stopLossOrder);
            }
    
                            if (PrintDetails)
                                Print(string.Format("{0} | Account_OrderUpdate | placing stop loss | currentSlPrice: {1}", orderUpdateArgs.Time, currentSlPrice));
                            
                        
                        myAccount.Submit(submitOrdersArray);
                    }
    
                    // once the exit orders are closed, reset for a new entry.
                    else if ((profitTargetOrder != null && (profitTargetOrder.OrderState == OrderState.Filled
                                                            || profitTargetOrder.OrderState == OrderState.Rejected
                                                            || profitTargetOrder.OrderState == OrderState.Cancelled))
                        ||(profitTargetOrder1 != null && (profitTargetOrder1.OrderState == OrderState.Filled
                                                            || profitTargetOrder1.OrderState == OrderState.Rejected
                                                            || profitTargetOrder1.OrderState == OrderState.Cancelled))
                          || (stopLossOrder != null && (stopLossOrder.OrderState == OrderState.Filled
                                                            || stopLossOrder.OrderState == OrderState.Rejected
                                                            || stopLossOrder.OrderState == OrderState.Cancelled))
                        || (stopLossOrder1 != null && (stopLossOrder1.OrderState == OrderState.Filled
                                                            || stopLossOrder1.OrderState == OrderState.Rejected
                                                            || stopLossOrder1.OrderState == OrderState.Cancelled)))
                        
                    {
                        entryBuyMar****rder        = null;
                        entrySellMar****rder    = null;
                        
                        profitTargetOrder =profitTargetOrder1= null;
                            
                
                        stopLossOrder=stopLossOrder1= null;
                        
                        #region DIRECTION LOCK (LONG StopLoss Order Rejection Prevention Move Lock)
                        
                            currentSlPriceUpOnly     = 0;
                            currentSlPriceDownOnly    = 999999;
                        
                        #endregion
                    }
                }
            
            #endregion
    ​
    Last edited by Mindset; 01-01-2025, 05:58 AM.

    #2
    Welll thank you ChatGPT.
    The parameter problem was quantity.
    I tried moving this to before the Entryorders were even looked at but that made no difference.

    I have got round the problem by changing the following lines - replacing Q0 with 1 as it can only be 1 in a single lot order.

    else
    {
    profitTargetOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instru ment, OrderAction.Buy, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day,1, currentPtPrice, 0, oco, "Profit Target", Core.Globals.MaxDate, null);
    submitOrdersArray.Add(profitTargetOrder);
    stopLossOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instru ment, OrderAction.Buy, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, 1, 0, currentSlPrice, oco, "Stop Loss", Core.Globals.MaxDate, null);
    submitOrdersArray.Add(stopLossOrder);
    }

    Comment


      #3
      Hello Mindset,

      I'm glad you were able to work out a solution.

      Moving forward in the future, if you would like assistance with an error message please provide the full error message.

      The issue may have been with the logic assigning the Q0 or Q1 variables a value.
      It would be advised to print the variable value with a label and the bar datetime, with TraceOrders enabled to diagnose the issue.
      Chelsea B.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      558 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      324 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      545 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      547 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X