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

Checking status (active, closed) of multiple positions

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

    Checking status (active, closed) of multiple positions

    I have a strategy that potentially has multiple exits for each entry, so I subdivide the entry into smaller units, each with a unique signalName, so for example, if the entry conditions for 'Long1' entry are satisfied, I'll subdivide that entry into 4 units:

    Code:
    EnterLong(tradeSize, "Long1_1");
    EnterLong(tradeSize, "Long1_2");
    EnterLong(tradeSize, "Long1_3");
    EnterLong(tradeSize, "Long1_4");
    This allows me to manage each of the 4 positions with a different stoploss and target. How can I check within OnBarUpdate which of these positions is still active (or equivalently, which are closed out)? If one of the positions is closed out, I'd like to know so that I can adjust stops/targets on the active positions.

    Position.MarketPosition is not useful here because it will only tell me if the overall strategy is long, short or flat.

    #2
    Hello,
    You could get the quantity of your position by using Position.Quantity.
    You could also create your exit orders as uniqure IOrder objects for each and get information on when an exit order is filled by checking when an exit order is executed in OnExecution()
    For more information on IOrder objects please see the following link: http://ninjatrader.com/support/helpG...nt7/iorder.htm
    Fore more information on OnExecution() please see the following link: http://ninjatrader.com/support/helpG...nexecution.htm
    I would also recommend to review the following reference sample for more information on how the OnExecution() method works: http://ninjatrader.com/support/forum...ead.php?t=7499
    Cody B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_CodyB View Post
      Hello,
      You could also create your exit orders as uniqure IOrder objects for each and get information on when an exit order is filled by checking when an exit order is executed in OnExecution()
      So to do this, I should use ExitLongStop instead of SetStopLoss and use ExitLongLimit instead of SetProfitTarget, correct?

      So for example, in OnBarUpdate:
      Code:
      stopOrder1 = ExitLongStop(stopPrice, fromEntrySignal)
      targetOrder1 = ExitLongLimit(limitPrice, fromEntrySignal)
      And if the market moves in my favor, I can simply call those lines again and pass in new stop and limit prices, correct?

      Since I omit quantity, but include fromEntrySignal, the orders will be placed for the original quantity associated with EnterLong(quantity, fromEntrySignal), correct?

      Since I simply want to obtain status of these stop and limit orders, I don't have to get into the nuts and bolts of handling the orders, correct? I can just query if the orders have been filled in OnExecution?
      Last edited by dolomite; 03-07-2016, 11:50 AM.

      Comment


        #4
        Hello,
        So to do this, I should use ExitLongStop instead of SetStopLoss and use ExitLongLimit instead of SetProfitTarget, correct?
        You definitely have the option to use ExitLongStop() and ExitLongLimit() instead. You could use SetStopLoss and SetProfitTarget and check when they are filled within OnOrderUpdate instead of OnExecution() if you are using a different FromEntrySignal name for each of them. You can find more information on OnOrderUpdate() at the following link: https://ninjatrader.com/support/help...rderupdate.htm
        For example:
        Code:
        protected override void OnBarUpdate()
        {
        	if(SMA(20)[0] > Close[0])
        	{
        		EnterLong( 1, "myEntrySignalOne");
        		SetStopLoss("myEntrySignalOne", CalculationMode.Ticks, 20, false);
        		SetProfitTarget("myEntrySignalOne" , CalculationMode.Ticks, 20);
        		EnterLong( 1, "myEntrySignalTwo");
        		SetStopLoss("myEntrySignalTwo", CalculationMode.Ticks, 20, false);
        		SetProfitTarget("myEntrySignalTwo" , CalculationMode.Ticks, 20);
        	}
        			
        			
        }
        protected override void OnOrderUpdate(IOrder order)
        {
        	if(order.Name == "Stop Loss" &&  order.FromEntrySignal == "myEntrySignalOne")
        		if(order.OrderState == OrderState.Filled)
        				Print("Stop loss for position 1 filled");
        	if(order.Name == "Stop Loss" &&  order.FromEntrySignal == "myEntrySignalTwo")
        		if(order.OrderState == OrderState.Filled)
        				Print("Stop loss for position 2 filled");
                if(order.Name == "Profit Target" &&  order.FromEntrySignal =="myEntrySignalOne")
        		if(order.OrderState == OrderState.Filled)
        				Print("Stop loss for position 1 filled");
        	if(order.Name == "Profit Target" &&  order.FromEntrySignal =="myEntrySignalTwo")
        		if(order.OrderState == OrderState.Filled)
        				Print("Stop loss for position 2 filled");
        }
        You might find it suits your style better to check when the order is execute instead of goes into the filled state though, and for this you would need to create IOrder objects set to ExitLongStop() and ExitLongLimit().


        And if the market moves in my favor, I can simply call those lines again and pass in new stop and limit prices, correct?
        If you have a check to submit more orders when the market goes in your favor then yes you would be able to repeat the process but you will need to use unique names if these orders are pending still.

        Since I omit quantity, but include fromEntrySignal, the orders will be placed for the original quantity associated with EnterLong(quantity, fromEntrySignal), correct?
        This is correct the orders will submit for the same quantity as the entry signal's order.

        Since I simply want to obtain status of these stop and limit orders, I don't have to get into the nuts and bolts of handling the orders, correct? I can just query if the orders have been filled in OnExecution?
        This is correct, you could simply check if the order has been filled if you decide to go with the SetStopLoss() and SetProfitTartget() process of if you decide to go with checking for the execution you would check for the execution of your IOrder object.
        Cody B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_CodyB View Post
          Hello,

          You definitely have the option to use ExitLongStop() and ExitLongLimit() instead. You could use SetStopLoss and SetProfitTarget and check when they are filled within OnOrderUpdate instead of OnExecution() if you are using a different FromEntrySignal name for each of them. You can find more information on OnOrderUpdate() at the following link: https://ninjatrader.com/support/help...rderupdate.htm
          For example:
          Code:
          protected override void OnBarUpdate()
          {
          	if(SMA(20)[0] > Close[0])
          	{
          		EnterLong( 1, "myEntrySignalOne");
          		SetStopLoss("myEntrySignalOne", CalculationMode.Ticks, 20, false);
          		SetProfitTarget("myEntrySignalOne" , CalculationMode.Ticks, 20);
          		EnterLong( 1, "myEntrySignalTwo");
          		SetStopLoss("myEntrySignalTwo", CalculationMode.Ticks, 20, false);
          		SetProfitTarget("myEntrySignalTwo" , CalculationMode.Ticks, 20);
          	}
          			
          			
          }
          protected override void OnOrderUpdate(IOrder order)
          {
          	if(order.Name == "Stop Loss" &&  order.FromEntrySignal == "myEntrySignalOne")
          		if(order.OrderState == OrderState.Filled)
          				Print("Stop loss for position 1 filled");
          	if(order.Name == "Stop Loss" &&  order.FromEntrySignal == "myEntrySignalTwo")
          		if(order.OrderState == OrderState.Filled)
          				Print("Stop loss for position 2 filled");
                  if(order.Name == "Profit Target" &&  order.FromEntrySignal =="myEntrySignalOne")
          		if(order.OrderState == OrderState.Filled)
          				Print("Stop loss for position 1 filled");
          	if(order.Name == "Profit Target" &&  order.FromEntrySignal =="myEntrySignalTwo")
          		if(order.OrderState == OrderState.Filled)
          				Print("Stop loss for position 2 filled");
          }
          In your example, where do you assign the name "Stop Loss"?

          How does the following code know if an order has a name "Stop Loss"?
          Code:
          if(order.Name == "Stop Loss" &&  order.FromEntrySignal == "myEntrySignalOne")
          If I create an IOrder variable:
          Code:
          private IOrder stopOrder1 = null;
          I cannot assign SetStopLoss to that IOrder variable:
          Code:
          stopOrder1 = SetStopLoss("Long1_1", CalculationMode.Price, stopLossValue, false);
          ^ This yields an error: "Cannot implicitly convert type 'void' to 'NinjaTrader.Cbi.IOrder'

          Originally posted by NinjaTrader_CodyB View Post
          You might find it suits your style better to check when the order is execute instead of goes into the filled state though, and for this you would need to create IOrder objects set to ExitLongStop() and ExitLongLimit().
          Since I'm backtesting this strategy, executed is essentially the same as filled, so if using SetStopLoss() and SetProfitTarget() is easier (and I think it would be, since it stays within the Managed Approach), I'll go with that.


          Originally posted by NinjaTrader_CodyB View Post
          If you have a check to submit more orders when the market goes in your favor then yes you would be able to repeat the process but you will need to use unique names if these orders are pending still.
          So re-executing this code:
          Code:
          stopOrder1 = ExitLongStop(stopPrice, fromEntrySignal)
          targetOrder1 = ExitLongLimit(limitPrice, fromEntrySignal)
          does not overwrite the existing order the way that SetStopLoss() and SetProfitTarget() will overwrite a prior stop loss and profit target?
          Last edited by dolomite; 03-07-2016, 03:38 PM.

          Comment


            #6
            Hello,
            Where do you assign the name "Stop Loss"?
            You do not set an IOrder object to SetStopLoss() and SetProfitTarget() When using these methods it automatically assigns out the names Stop Loss or Profit Target. You can access them in OnOrderUpdate() by the order name as order.Name == "Stop Loss" and order.Name == " Profit Target" as I do in the example I provided.

            QUOTE]So re-executing this code:
            Code:
            stopOrder1 = ExitLongStop(stopPrice, fromEntrySignal)
            targetOrder1 = ExitLongLimit(limitPrice, fromEntrySignal)
            does not overwrite the existing order the way that SetStopLoss() and SetProfitTarget() will overwrite a prior stop loss and profit target?[/QUOTE]

            If you have a pending order that is the IOrder objects for stopOrder1 and targetOrder1 this would overwrite these orders. You would need to submit the orders usign unique names such as stopOrder2 or something else so that it does not change the pending IOrder.
            Cody B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_CodyB View Post
              Hello,

              You do not set an IOrder object to SetStopLoss() and SetProfitTarget() When using these methods it automatically assigns out the names Stop Loss or Profit Target. You can access them in OnOrderUpdate() by the order name as order.Name == "Stop Loss" and order.Name == " Profit Target" as I do in the example I provided.
              Got it, thanks. I was running into a problem where the line
              Code:
              if(order.Name == "Stop Loss")
              was not evaluating as true, hence my confusion over the order name. Turns out in NT, the actual name assigned is "Stop loss" (no capitalization of the "l" in "loss).

              Works great, and thanks for the help!
              Last edited by dolomite; 03-07-2016, 04:59 PM.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by love2code2trade, 04-17-2024, 01:45 PM
              4 responses
              31 views
              0 likes
              Last Post love2code2trade  
              Started by cls71, Today, 04:45 AM
              2 responses
              10 views
              0 likes
              Last Post eDanny
              by eDanny
               
              Started by proptrade13, Today, 11:06 AM
              0 responses
              5 views
              0 likes
              Last Post proptrade13  
              Started by kulwinder73, Today, 10:31 AM
              1 response
              10 views
              0 likes
              Last Post NinjaTrader_Erick  
              Started by RookieTrader, Today, 09:37 AM
              3 responses
              15 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Working...
              X