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

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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by usazencort, Today, 01:16 AM
        0 responses
        1 view
        0 likes
        Last Post usazencort  
        Started by kaywai, 09-01-2023, 08:44 PM
        5 responses
        603 views
        0 likes
        Last Post NinjaTrader_Jason  
        Started by xiinteractive, 04-09-2024, 08:08 AM
        6 responses
        22 views
        0 likes
        Last Post xiinteractive  
        Started by Pattontje, Yesterday, 02:10 PM
        2 responses
        21 views
        0 likes
        Last Post Pattontje  
        Started by flybuzz, 04-21-2024, 04:07 PM
        17 responses
        230 views
        0 likes
        Last Post TradingLoss  
        Working...
        X