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
    JimNinjaTrader Customer Service

    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, 03: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.

        https://ninjatrader.com/support/help...runmanaged.htm

        We look forward to assisting.
        Attached Files
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by southpacific001, 02-26-2021, 09:25 PM
        12 responses
        56 views
        0 likes
        Last Post REI140205  
        Started by punkiy2111, Today, 05:22 PM
        0 responses
        2 views
        0 likes
        Last Post punkiy2111  
        Started by secrettrader, Today, 03:36 PM
        0 responses
        8 views
        0 likes
        Last Post secrettrader  
        Started by t3chn0, Today, 01:03 PM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_ChrisL  
        Started by jleira, Today, 02:27 PM
        0 responses
        25 views
        0 likes
        Last Post jleira
        by jleira
         
        Working...
        X