Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

EnterShortLimit directly following EnterShort

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

    EnterShortLimit directly following EnterShort

    I have the following snippet of code

    EnterShort(orderSize, "");
    ExitShortLimit(orderSize,Close[0]-tickNum*tickMult, "", "");

    I enabled TraceOrders, and I received output messages saying that I had not entered a position, therefore, ExitShortLimit is not doing anything.

    The EnterShort task does not seem to be blocking, however, I would like to create a ExitShortLimit order right after a position is entered.

    Is there a way to create an ExitShortLimit order immediately after a position is entered?
    Also, is there a way for the strategy to know what the execution price is? The EnterShort task will enter a position at an unknown (market) price, but I would like to track this after execution.

    Thanks!

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

    You can use the below code to know the entry price
    Code:
    Position.AvgPrice


    You can enter the exit order in the OnExectuion event. A sample code will be like
    Code:
    protected override void OnExecution(IExecution execution)
    {
    	
    	if (Position.MarketPosition == MarketPosition.Short)
    	{
    		//do something
    	}
    }
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      I will give the events a try. Thanks!

      Comment


        #4
        I'm seeing a case where no orders are placed after execution. Here is a snippet of my code.

        protected override void OnExecution(IExecution execution)
        {
        if(execution.Order.OrderState == OrderState.Filled)
        {
        if(Position.MarketPosition == MarketPosition.Long)
        {
        ExitShortLimit(orderSize,Close[0]-tickNum*tickMult, "", "");
        Print("Going Short");
        Print(Close[0]);
        Print(Time[0]);
        }
        if(Position.MarketPosition == MarketPosition.Long)
        {
        ExitLongLimit(orderSize,Close[0]-tickNum*tickMult, "", "");
        Print("Going Long");
        Print(Close[0]);
        Print(Time[0]);
        }
        }
        }

        In this case, even when I call EnterShort, or EnterLong, the code inside the OrderFilled braces never gets executed. I would like to have a limit order created everytime there is a EnterShort or EnterLong.

        Comment


          #5
          My guess is that the print statements inside the Position.MarketPosition conditions aren't executed because the market position isn't updated fast enough. So when your order is filled it takes a while before the strategy market position is updated.

          I've experienced this behaviour numerous times if a strategy is in a long position, and then an ExitLong ( so 1 sell) and EnterShort (1 sell) command are given. Often this will result in an overfill because NT doesn't update the strategy position information fast enough.

          what happens if the OrderState.filled condition looks like this ?

          Code:
           
          if(execution.Order.OrderState == OrderState.Filled)
          { 
          Print( Position.MarketPosition )
          }

          Comment


            #6
            Hello almez,
            As marcow said, it may be the case that the Positions class has not being updated since they run on separate thread. Please use the Print statement and see if that is really the case or not.

            I would also suggest reviewing this sample code which demonstrates how to place orders via the OnExection events.
            The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()
            JoydeepNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            648 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            369 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            108 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            572 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            573 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X