Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using EnterLongStop() with existing short position

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

    Using EnterLongStop() with existing short position

    Forgive me if I'm being dense, but how do you do this elegantly?

    Let's say I have a short position with a stop loss and profit target.

    I get a buy signal on the current bar and want to go long if price rises above a certain trigger price.

    However if I use EnterLongStop() the order is ignored because of the internal rules (since I have existing buy orders -- the stop loss and profit target -- in the same direction).

    It works if I buy at market but I don't want to buy at market.

    Clearly, I can submit an order to close the short position, and then submit my EnterLongStop() order on the next bar, but this means there's a delay in submitting it.

    What is the cleanest way of exiting a short position with a stoploss and entering a buy stop order on the same bar?

    #2
    Hello,

    Try submitting an opposing order, like you are doing already, then enter a new order immediately.

    If you post your code I can help you more.
    DenNinjaTrader Customer Service

    Comment


      #3
      OK, here's a very simplified version of the code which exhibits the behavior.

      Let's say the strategy is short, with a stop loss and profit target and I am working on 15m bars.

      On bar N I get a long signal.

      The strategy closes the short position via ExitShort().

      However, EnterLongStop() is ignored because of the existing pending stop/profit target orders to close the short position and which will not be cancelled until the next bar. Of course EnterLong() works fine because it is a market order.

      In order to EnterLongStop() I have to set a flag and wait until the next bar to enter, which delays entry by 15 mins to bar N+1.

      I coded a workaround using ExitShortStop() and ExitShortLimit() orders to manually run the profit target and stop loss routines, but as you guys are no doubt aware it gets complicated as you have to make sure they cancel each other and so on. I would much rather use the built-in code for this.

      What is the cleanest way to submit the EnterLongStop() order and close the existing short position on the same bar?

      Code:
              protected override void Initialize()
              {
                  CalculateOnBarClose = true;
      			
      			Add( AcmeSwing( SwingPercent ));
      			
      			SetProfitTarget( CalculationMode.Percent, profitTarget );
      			SetStopLoss( CalculationMode.Percent, stopLoss );
      			
      			TraceOrders = true;
              }
      
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
      			thisPivotType  = (int) AcmeSwing( SwingPercent ).AcmePivotType[0];
      			thisPivotPrice = AcmeSwing( SwingPercent ).AcmePivotPrice[0];
      						
      			// Entries
      			if( thisPivotType == pivotTypeHigherLow || thisPivotType == pivotTypeLowerLow )
      			{
      				ExitShort();
      				EnterLongStop( High[0], "Bullish reversal" );
      			}
      			
      			if( thisPivotType == pivotTypeLowerHigh || thisPivotType == pivotTypeHigherHigh )
      			{
      				ExitLong();
      				EnterShortStop( Low[0], "Bearish reversal" );
      			}			
              }

      Comment


        #4
        The flexibility you are seeking is done through manually controlling stop/target orders. This is done through the use of IOrders and monitoring ExitShortStop() and ExitShortLimit() not through Set() methods.

        In terms of the delay, run with CalculateOnBarClose = false. In backtesting, this will always be the case. You need to wait for the bar close before you can do anything.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          I think the cleanest way to do what you want is to move the SetProfitTarget() and SetStopLoss() from the Initialize section to the OnBarUpdate section.

          This will allow you to tie the same signal name to all the orders - your position and the corresponding stop and profit orders.

          SetProfitTarget(string fromEntrySignal, CalculationMode mode, double value)

          fromEntrySignal
          The entry signal name. This ties the profit target exit to the entry and exits the position quantity represented by the actual entry.



          Basically you need to tie the all the orders together so they are identified as a group rather than individual orders. You do that in ninja with the signal names.

          As far as I understand things that should do what you want it to do.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by kevinenergy, 02-17-2023, 12:42 PM
          115 responses
          2,699 views
          1 like
          Last Post kevinenergy  
          Started by prdecast, Today, 06:07 AM
          1 response
          4 views
          0 likes
          Last Post NinjaTrader_LuisH  
          Started by Christopher_R, Today, 12:29 AM
          1 response
          14 views
          0 likes
          Last Post NinjaTrader_LuisH  
          Started by chartchart, 05-19-2021, 04:14 PM
          3 responses
          577 views
          1 like
          Last Post NinjaTrader_Gaby  
          Started by bsbisme, Yesterday, 02:08 PM
          1 response
          15 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Working...
          X