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

Weird behavior of this simple Exit Long order

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

    Weird behavior of this simple Exit Long order

    Hi Ninjas,

    I got a situation here, which I've tried to discover the cause but still.

    It's a simple rutine of exit a long position and cancelling its StopLoss. The script uses the unmanaged approach.

    The order of exitLong:
    Code:
    xlEntryOrder = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, Qty, priceEntry, 0, "", "Exit Long");
    The exit long rutine:
    Code:
    protected override void OnExecution(IExecution execution)
    {
    if (xlEntryOrder != null && xlEntryOrder== execution.Order )					
    			{					
    				if ( xlEntryOrder.OrderState == OrderState.Filled)				
    				{				
    					CancelOrder(slEntryOrder);	 // Cancel the stop order
    					
    					xlEntryOrder = null;		
    				}				
    				if ( xlEntryOrder.OrderState == OrderState.PartFilled )				
    				{				
    					ChangeOrder(slEntryOrder, Position.Quantity-xlEntryOrder.Filled, 0, (Position.AvgPrice-stop));	 // Change the Stop order accordingly
    				}				
    				if ( xlEntryOrder.OrderState == OrderState.Cancelled && xlEntryOrder.Filled > 0)				
    				{				
    					ChangeOrder(slEntryOrder, Position.Quantity-xlEntryOrder.Filled, 0, (Position.AvgPrice-stop));			
    					xlEntryOrder = null;			
    				}				
    			}
    }
    If I just set and let the instruction for "Filled" then everything works perfectly, BUT
    if I add the others PartFilled or Part.Filled and Cancelled, it just simple doesn't run, NT give the typical Run time error.

    So any ideas why this?

    #2
    My guess without debugging is that slEntryOrder is null at this point when its first called

    Consider adding && slEntryOrder!= null before every changeorder check.

    Let me know if I can further assist.
    LanceNinjaTrader Customer Service

    Comment


      #3
      Lance, I appreciate very much your help. As soon as I can code it again I´ll let u know.

      Comment


        #4
        Originally posted by NinjaTrader_Lance View Post
        My guess without debugging is that slEntryOrder is null at this point when its first called

        Consider adding && slEntryOrder!= null before every changeorder check.

        Let me know if I can further assist.
        Lance, following your suggestion I've just simply erase all instructions after the statement of Par.Filled and Part.Filled and cancelled, and the same error...so it's not slEntryOrder..it must be something else that fails to read these normal statements.

        What I think is that I must set these instructions filtering conditions first with the typical: Filled, PartFilled and PartFilled-Cancelled statement and then in a logic breakdown trying not to repeat or interrupt the logic flow. When solved, I'll post it
        Last edited by pstrusi; 12-20-2013, 02:21 PM.

        Comment


          #5
          In this case you'll need to add print statements before and after the different if checks to see where the code is breaking.

          Tips on debugging: http://www.ninjatrader.com/support/f...ead.php?t=3418

          Let me know if I can further assist.
          LanceNinjaTrader Customer Service

          Comment


            #6
            The snippet that solved

            Code:
            if (xlEntryOrder != null && xlEntryOrder== execution.Order )					
            			{					
            				if ( xlEntryOrder.OrderState == OrderState.Filled )				
            				{				
            					CancelOrder(slEntryOrder);			
            					sEntryOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.Limit, quant, priceEntry, 0, "", "Enter Short");			
            				}				
            				else				
            				{				
            					ChangeOrder(slEntryOrder, Position.Quantity-xlEntryOrder.Filled, 0, (Position.AvgPrice-stop));			
            				}				
            				if (xlEntryOrder.OrderState != OrderState.PartFilled )				
            				{				
            					xlEntryOrder = null;			
            				}				
            			}
            Thanks

            Comment


              #7
              Originally posted by NinjaTrader_Lance View Post
              In this case you'll need to add print statements before and after the different if checks to see where the code is breaking.

              Tips on debugging: http://www.ninjatrader.com/support/f...ead.php?t=3418

              Let me know if I can further assist.
              Almost writing at the same time. As you can see in my earlier post, I redesigned the routine, and it was that when you set the main order = null, it shouldn't do more comparison, that's why it did that run time error

              Thanks pal

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by bill2023, Yesterday, 08:51 AM
              8 responses
              43 views
              0 likes
              Last Post bill2023  
              Started by yertle, Today, 08:38 AM
              6 responses
              25 views
              0 likes
              Last Post ryjoga
              by ryjoga
               
              Started by algospoke, Yesterday, 06:40 PM
              2 responses
              24 views
              0 likes
              Last Post algospoke  
              Started by ghoul, Today, 06:02 PM
              3 responses
              16 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by jeronymite, 04-12-2024, 04:26 PM
              3 responses
              46 views
              0 likes
              Last Post jeronymite  
              Working...
              X