Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Create strategy for exiting existing position, not handling the actual entry itself

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

    Create strategy for exiting existing position, not handling the actual entry itself

    Thanks for reading my question.

    I would like to know how to go about for creating a strategy that handles the position after it has been started manually.

    So for example: lets say I by 100 shares of Tesla at 800. I enter this trade manually at the day close. Then on the next day I would like a strategy pick up the position and handle it on an intraday basis. Perhaps to move stoploss levels, exit partial positions etc.

    My question is: is this possible? or would the strategy always want to have an entry triggered by that same strategy as well?

    Or could I simply start the strategy with a conditional check for MarketPosition.Long and then handle an existing position?

    Any guidance on this would be greatly appreciated.

    #2
    Hello siroki,

    Thanks for your question.

    A strategy can be programmed to use AdoptAccountPosiiton, in addition to skipping historical processing. When the strategy transitions to realtime processing, it can check what the Position is that was captured on enablement, and then place target/stop loss orders to protect that position. I have attached an example.

    We look forward to assisting.
    Attached Files

    Comment


      #3
      Thank you Jim for your help.

      I'm trying to understand the IsUnmanaged boolean value. So as I understand I cannot take the simple approach of managed orders when adopting an existing position?

      The exit strategy that I'm trying to build is very simple. I would run this on a 1-min interval. So I would have to run this within the OnBarUpdate method I would assume.

      // Exit position if the first bar close is below the prior day close
      if(Position.MarketPosition == MarketPosition.Long && Bars.IsFirstBarOfSession && Close[0] < PriorDayOHLC().PriorClose[0]) {
      ExitLong();
      return;
      }

      // After first bar handling
      if(Position.MarketPosition == MarketPosition.Long) {
      SetStopLoss(CalculationMode.Price, Low[0]-TickSize);
      return;
      }

      Would I be able to run these ExitLong() and SetStopLoss() methods using the AdoptAccountPositionAware ?

      I have been trying to put the following code into the OnBarUpdate():

      if(Position.MarketPosition == MarketPosition.Long && Bars.IsFirstBarOfSession && Close[0] < PriorDayOHLC().PriorClose[0]) {
      stopOrder = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Market, Position.Quantity, 0, Low[0]-TickSize /*execution.Order.AverageFillPrice - 10 * TickSize*/, oco, "StopLossLong");
      //ExitLong();
      return;
      }

      if(Position.MarketPosition == MarketPosition.Long && (Time[0].Date-entryOrder.Time.Date).Days > 0) {
      stopOrder = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, Position.Quantity, 0, Low[0]-TickSize /*execution.Order.AverageFillPrice - 10 * TickSize*/, oco, "StopLossLong");
      //SetStopLoss(CalculationMode.Price, Low[0]-TickSize);
      return;
      }

      So my question is then this:
      - Should I use Position.Quantity? Or use that sumFilled qty variable?

      Then there is a SubmitOrderUnmanaged() order in the OnBarUpdate when the position is flat:

      if (Position.MarketPosition == MarketPosition.Flat && entryOrder == null)
      {
      oco = GetAtmStrategyUniqueId() + "entry";

      SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Market, 1, 0, 0, oco, "MyEntry");
      }

      What does the 1,0,0 do? It seems to be taking 1 as qty and 0 for stop and limit? Obviously I would like the order to be taken from the live position. So quantity should be taken from the position and the price I guess from the average position price.

      Any further guidance would be greatly appreciated.

      Thanks,

      Glen
      Last edited by siroki; 01-27-2021, 04:55 AM.

      Comment


        #4
        Hello Glen,

        Thanks for your reply.

        You can use the Managed Approach. The example I have provided just demonstrates Unmanaged orders. I've attached a modification to demonstrate with the Managed Approach.

        Set methods prep NinjaTrader to submit targets and stops when an entry order fills. If the position is already made, I suggest using Exit methods. As we can see in State.Realtime, the strategy checks for a position and submits orders to protect that position using Exit methods with IsLiveUntilCancelled set to true.

        OnBarUpdate is a data driven event, this would be best suited for calculating when you want to make an entry or an exit based on price action. It is typically best to submit targets and stops in OnExecutionUpdate. The stop/target handling in the example is taken from our SampleOnOrderUpdate strategy. I suggest referencing that strategy and comparing it with the attached example for a full understanding on how Order objects are used and how OnOrderUpdate and OnExecutionUpdate can be used for order tracking and stop/target submission.

        SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

        For more information on Unmanaged orders and what the parameters do, please see the documentation below.



        We look forward to assisting.
        Attached Files

        Comment


          #5
          Is using this "AdoptAccountPosition" also the way to have a strategy continue after being adjusted?

          When I disable my strategy, perhaps adjust a parameter and re-enable, I want the strategy to pick up where it left off "adopting" the positions it opened before I adjusted it.

          Still a bit new to NinjaScript, it seems obvious to me that one needs a way to be able to re-enable a strategy without messing up the position / stops it already placed. For example, if I realize I have a bug in the code, I need to quickly fix it and reset it, but I do not want to lose the position & handling it has already begun.

          What is the best way to do this?

          Comment


            #6
            Hello DerkWehler,

            When using AdoptAccountPosition orders would only be resumed if they are submitted in historical at the same time and price and with the same name as they were in realtime.


            Below is a link to an example.


            Typically, Immediately Submit is used for resuming strategy positions and orders.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Yesterday, 05:17 AM
            0 responses
            64 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            139 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            75 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            45 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            50 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X