Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need help with limit orders

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

    Need help with limit orders

    Hi. I am a beginner programmist. I've got the following question: I make a limited order to enter short. After that it comes to a "Working" state. I want to cancel it so that I could enter long having canceled my short order. The commands follow each other one after another but the second order is ignored by my broker. What could you suggest me to do?

    Code:
    ...
    CancelOrder(notFilledOrder);
    o = EnterLongLimit(BarsInProgress, true, DefaultQuantity, GetCurrentBid() -2 * TickSize, "");
    ...

    #2
    Hello kuranushka,
    Welcome to the forum and I am happy to assist you.

    It may be the case that the long order is being placed before the short order could be cancelled, and thus the internal order handling rules is rejecting the long order. Please append the below code in the Initialize section of the code to see the exact reason for the order rejection (the message will appear in the Output Window, File>Output Window)
    Code:
    TraceOrders = true;


    To know more about the internal order handling rules please refer here
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Hi, NinjaTrader_Joydeep.
      I understand why the order is declined
      Could you please give me an advice
      how should I start the strategy in the opposite direction

      Comment


        #4
        and where can we find in the code the list of all orders that were executed at the actual state

        Comment


          #5
          full code:

          Code:
                  protected override void OnBarUpdate()
                  {
          			if (writeLog)
          				fileOut = new StreamWriter(new FileStream(fileName, FileMode.Append, FileAccess.Write));
          			IOrder o = null;
          			IOrder oe = null;
          
          			if (BarsInProgress == 1)
          			{
          				//if(Historical) return;
          				if (writeLog)
          					fileOut.WriteLine(Time[0] + " - " + DateTime.Now.ToString() + " - Close[0][0] = " + Closes[0][0] + ", Open[0][0] = " + Opens[0][0] + ", Close[1][0] = " + Closes[1][0] + ", Open[1][0] = " + Opens[1][0] + ", BarsInProgress = " + BarsInProgress);
          				
          				if (Closes[0][0] > Opens[0][0] && Closes[1][0] > Opens[1][0])
          				{
          					if (writeLog)
          						fileOut.WriteLine(Position.Quantity + " " + Position.MarketPosition + " -> " + "EnterLong: " + Time[0] + " Close[0][0] = " + Closes[0][0] + ", Open[0][0] = " + Opens[0][0] + " Close[1][0] = " + Closes[1][0] + ", Open[1][0] = " + Opens[1][0]);
          					
          					if (hasNotFilled)
          					{
          						if (writeLog)
          							fileOut.WriteLine("OBUCancel " + Position.Quantity + " " + Position.MarketPosition + " -> " + notFilledOrder.ToString());
          						CancelOrder(notFilledOrder);
          					}
          					if (Position.MarketPosition == MarketPosition.Short)
          						ExitShort();
          					o = EnterLongLimit(BarsInProgress, true, DefaultQuantity, GetCurrentBid() -2 * TickSize, "");
          					
          					if (writeLog)
          					{
          						if (o != null)
          							fileOut.WriteLine("OBUEnterL " + Position.Quantity + " " + Position.MarketPosition + " -> " + o.ToString());
          						else
          							fileOut.WriteLine("EnterLong null");
          					}
          	//				EnterLongLimit(DefaultQuantity, Close[0], "");
          				}
          				if (Closes[0][0] < Opens[0][0] && Closes[1][0] < Opens[1][0])
          				{
          					if (writeLog)
          						fileOut.WriteLine(Position.Quantity + " " + Position.MarketPosition + " -> " + "EnterShort: " + Time[0] + " Close[0][0] = " + Closes[0][0] + ", Open[0][0] = " + Opens[0][0] + " Close[1][0] = " + Closes[1][0] + ", Open[1][0] = " + Opens[1][0]);
          					
          					if (hasNotFilled)
          					{
          						if (writeLog)
          							fileOut.WriteLine("OBUCancel " + Position.Quantity + " " + Position.MarketPosition + " -> " + notFilledOrder.ToString());
          						CancelOrder(notFilledOrder);
          					}
          					if (Position.MarketPosition == MarketPosition.Long)
          						ExitLong();
          					o = EnterShortLimit(BarsInProgress, true, DefaultQuantity, GetCurrentAsk() + 2 * TickSize, "");
          					
          					if (writeLog)
          					{
          						if (o != null)
          							fileOut.WriteLine("OBUEnterS " + Position.Quantity + " " + Position.MarketPosition + " -> " + o.ToString());
          						else
          							fileOut.WriteLine("EnterShort null");
          	//				EnterShortLimit(DefaultQuantity, Close[0], "");
          					}
          				}
          			}
          			if (BarsInProgress == 0)
          			{
          				if (writeLog)
          					fileOut.WriteLine(Time[0] + " - " + DateTime.Now.ToString() + " - Close[0][0] = " + Closes[0][0] + ", Open[0][0] = " + Opens[0][0] + ", Close[1][0] = " + Closes[1][0] + ", Open[1][0] = " + Opens[1][0] + ", BarsInProgress = " + BarsInProgress);
          
          				if (Closes[0][0] > Opens[0][0])
          				{
          					oe = ExitShort();
          					if (writeLog)
          					{
          						if (oe != null)
          							fileOut.WriteLine("OBUExitS " + Position.Quantity + " " + Position.MarketPosition + " -> " + oe.ToString());
          						else
          							fileOut.WriteLine("ExitShort null");
          					}
          	//			ExitShortLimit(Close[0], "", "");
          				}
          				if (Closes[0][0] < Opens[0][0])
          				{
          					oe = ExitLong();
          					if (writeLog)
          					{
          						if (oe != null)
          							fileOut.WriteLine("OBUExitL " + Position.Quantity + " " + Position.MarketPosition + " -> " + oe.ToString());
          						else
          							fileOut.WriteLine("ExitLong null");
          					}
          //				ExitLongLimit(Close[0], "", "");
          				}
          			}
          			if (writeLog)
          				fileOut.Close();
          		}
          		
          		protected override void OnOrderUpdate(IOrder order)
          		{
          			hasNotFilled = false;
          			if (order.Name != "Profit target" && order.Filled == 0)
          			{
          				hasNotFilled = true;
          				notFilledOrder = order;
          			}
          			
          			if (writeLog)
          			{
          				fileOut = new StreamWriter(new FileStream(fileName, FileMode.Append, FileAccess.Write));
          				fileOut.WriteLine("OOU " + Position.Quantity + " " + Position.MarketPosition + " -> " + order.ToString());
          				fileOut.Close();
          			}
          		}

          Comment


            #6
            Hello kuranushka,
            To assist you further can you please send a toy NinjaScript code* replicating the behavior to support[AT]ninjatrader[DOT]com

            Please send a *.cs file and not the code snippet.

            Please append Attn:Joydeep in the subject line of the email and give a reference of this thread in the body of the email.

            I look forward to assisting you further.

            *The "toy" just means something that is a stripped down version that isn't necessarily the whole logic. It makes things easier to rout out.
            JoydeepNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by traderqz, Yesterday, 09:06 AM
            2 responses
            15 views
            0 likes
            Last Post traderqz  
            Started by traderqz, Today, 12:06 AM
            3 responses
            6 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by RideMe, 04-07-2024, 04:54 PM
            5 responses
            28 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by f.saeidi, Today, 08:13 AM
            1 response
            8 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by DavidHP, Today, 07:56 AM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_Erick  
            Working...
            X