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

Partial Exit

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

    Partial Exit

    Hello,

    I have a little problem when I try to close a position partially. In my program below, I enter a long position with position size= entryPosSize*maxLot, and I try to exit the position partially with position size= entryPosSize.
    The first time, the partial exit works good, but after, the line ExitLong(entryPosSize) is executed because I see the message "FirstStrategyLong Exit long Resistance” but I don't see any order generated by NinjaTrader, neither any exception or confirmation on Log Tab.
    Could you advise please, what should I do let NinjaTrader execute the partial exit for the second, third time etc.?

    Best regards



    try{
    //Enter Long Cond
    if ((isEnterLongCondTouchSupport() || isEnterLongCondBreakOut())
    && Position.MarketPosition==MarketPosition.Flat)
    {
    entryPosSize=(int)getEntryPosSize(Close[1],MIN(Low,perFlorCap)[0]);
    EnterLong(entryPosSize*maxLot);
    double stop=MIN(Low,3)[0];
    SetStopLoss(CalculationMode.Price,stop);

    String msg="FirstStrategyLong Entry long: Instrument="+Instrument
    +" Price="+Open[0]
    +"EntrySize="+entryPosSize*maxLot+" Stop="+stop;

    Print(msg);
    SendMail(mail_from,mail_to,"FirstStrategyLong Entry long"+Instrument,msg);

    }

    //Exit Long Cond
    if(isExitLongCondResistance() && Position.MarketPosition==MarketPosition.Long)
    {
    ExitLong(entryPosSize);

    String msg="FirstStrategyLong Exit long Resistance: Instrument="+Instrument +" Price="+Open[0]+" Size="+entryPosSize;

    Print(msg);
    SendMail(mail_from,mail_to,"FirstStrategyLong Exit long"+Instrument,msg);
    }
    }catch (Exception e){
    Print(Time[0] + " " + e.ToString());
    Log("General Strategy Error: Please check your strategy for errors.", LogLevel.Error);
    }

    #2
    Hello fallendog,

    To be able to scale out of a Position using the Managed Approach, you will have to create multiple entry orders so that you can specify an entry order to scale out from.

    Here is a thread that has an example of this that you may view.


    Let us know if you have any questions.
    JCNinjaTrader Customer Service

    Comment


      #3
      Partial Exit

      Hi,


      Thank you for your advise to open multiple entry orders that allow to apply different profit targets and stop losses. But my need is a little bit different. I want entry once, with general stop and scale out position with ExitLong(). When I look at ninjascript help about ExitLong(), I see the folowing:


      Closing a Partial Position using an Exit Method
      You can close out a partial position by specifying the exit quantity. The following example goes long 3 contracts. Each subsequent bar update will submit a market order to exit one contract until the position is completely closed. ExitLong(1) will be ignored if a long market position does not exist.
      protected override void OnBarUpdate()
      {
      if (CrossAbove(SMA(10), SMA(20), 1))
      EnterLong(3);

      ExitLong(1);
      }

      If I understand well, ExitLong(quantity) is designed to close the position partially. But I don’t understand, why it works once but not twice in my program?


      Best regards

      Comment


        #4
        Hello fallendog,

        Your understanding of the help guide is correct, but there is a bug when closing a partial position like that when using real-time data (Historical data will process as the help guide states). With real-time data it will exit the first order but then NinjaTrader will consider the entry order completed so NinjaTrader will ignore the rest of the exit orders unless a Unique Signal Name is defined for each one.

        If you would like to enter with one order and exit out with multiple orders using real-time data you may want to use the Unmanaged Approach that will be able to do this.

        Let me know if you have any questions or if I can be of further assistance.
        JCNinjaTrader Customer Service

        Comment


          #5
          Thank you for your answer. I’ll try the Unmanaged Approach. Do you have any example showing how to scale out a position with the Unmanaged Approach?

          Comment


            #6
            Hello fallendog,

            Here is a simple example of it.

            Code:
            protected override void Initialize()
            {
                    CalculateOnBarClose = true;
            	Unmanaged = true;
            }
            protected override void OnBarUpdate()
            {			
            	if (CrossAbove(SMA(10), SMA(20), 1))
            			SubmitOrder(0, OrderAction.Buy, OrderType.Market, 3, 0, 0, "", "");
            			
            	if (Position.MarketPosition == MarketPosition.Long)
            			SubmitOrder(0, OrderAction.Sell, OrderType.Market, 1, 0, 0, "", "");
            }
            JCNinjaTrader Customer Service

            Comment


              #7
              Ok, thank you! I'll try it.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by futtrader, 04-21-2024, 01:50 AM
              4 responses
              41 views
              0 likes
              Last Post futtrader  
              Started by Option Whisperer, Today, 09:55 AM
              1 response
              11 views
              0 likes
              Last Post bltdavid  
              Started by port119, Today, 02:43 PM
              0 responses
              1 view
              0 likes
              Last Post port119
              by port119
               
              Started by Philippe56140, Today, 02:35 PM
              0 responses
              3 views
              0 likes
              Last Post Philippe56140  
              Started by 00nevest, Today, 02:27 PM
              0 responses
              2 views
              0 likes
              Last Post 00nevest  
              Working...
              X