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

Dynamic stop management

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

    Dynamic stop management

    I have a strategy that uses explicit order management so EnterLong() and EnterShort() are used to enter positions but limit orders used to exit so for a long position:


    if (_currentPosition == CurrentPos.Long)
    {
    _stop = entryPrice - StopTicks * TickSize;
    _profit = entryPrice + ProfitTicks * TickSize;

    _profitOrder = ExitLongLimit(2, true, _posSize, _profit, ProfitLong, EnterLongName);
    _stopOrder = ExitLongStopMarket(2, true, _posSize, _stop, StopLong, EnterLongName);

    }

    Currently StopTicks = 15 and ProfitTicks =6. However I would like to dynamically adjust the stop depending on market conditions so for example if going countertrend:

    if(countertrend == true)
    {
    StopTicks = 4;

    }
    else
    {
    StopTicks=6;

    }


    Can stops and targets be dynamically adjusted depending on conditions in this manner? I have been using fixed stops/targets until now.
    Last edited by mballagan; 08-10-2021, 01:47 PM.

    #2
    Hi mballagan, thanks for posting.

    The price can be changed by calling ChangeOrder() or calling the order entry method again with the modified parameters when the condition to change the order is triggered. I attached a strategy builder example that implements a break even order for reference.

    Kind regards.
    Attached Files
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChrisL View Post
      Hi mballagan, thanks for posting.

      The price can be changed by calling ChangeOrder() or calling the order entry method again with the modified parameters when the condition to change the order is triggered. I attached a strategy builder example that implements a break even order for reference.

      Kind regards.
      Chris,

      Thanks for your reply.

      If we enter a position with 2 contracts and have a target of 20 ticks and wish to exit one contract if a target of 10 ticks is achieved and move the stop to breakeven for the remaining open position how would this be implemented with the ChangeOrder() function ? From the NT8 guide there is a code snippet:

      // Raise stop loss to breakeven when you are at least 4 ticks in profit
      if (stopOrder != null && stopOrder.StopPrice < P osition.AveragePrice && Close[0] >= Position.AveragePrice + 4 * TickSize)
      ChangeOrder(stopOrder, stopOrder.Quantity, 0, Po sition.AveragePrice);



      Would the original logic itself above need to be modified after the position is entered? I assume that both _profitOrder and _stopOrder would need to be changed. The logic is slightly different to the NT8 example as part of the position is closed when the first profit target is hit:

      if (_currentPosition == CurrentPos.Long)
      {
      _stop = entryPrice - StopTicks * TickSize;
      _profit = entryPrice + ProfitTicks * TickSize;

      _profitOrder = ExitLongLimit(2, true, _posSize, _profit, ProfitLong, EnterLongName);
      would this be split into two orders with size _posSize/2 with two different targets say 10 ticks and 20 ticks?


      _stopOrder = ExitLongStopMarket(2, true, _posSize, _stop, StopLong, EnterLongName); this would be modified it the first target is hit to move to breakeven but with _posSize/2 as half position closed ?

      }

      Comment


        #4
        Hi mballagan,

        If there are more than one exit targets then you must use unique signal names with your orders and target those entries using the fromEntrySignal property that the exit methods have:

        EnterLong(int quantity, string signalName);

        ExitLongLimit(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double limitPrice, string signalName, string fromEntrySignal)

        Kind regards,
        -ChrisL
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChrisL View Post
          Hi mballagan,

          If there are more than one exit targets then you must use unique signal names with your orders and target those entries using the fromEntrySignal property that the exit methods have:

          EnterLong(int quantity, string signalName);

          ExitLongLimit(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double limitPrice, string signalName, string fromEntrySignal)

          Kind regards,
          -ChrisL

          Chris,

          Thanks for your reply.

          I have implemented multiple exits using the function below SetStopProfit(). This is working correctly. (There is also other associated code not shown here)


          private void SetStopProfit(Execution execution)
          {
          var entryPrice = execution.Order.AverageFillPrice;
          _pos1 = Math.Round((_posSize*PositionSplitPercent/100),0);
          _pos2 = _posSize -_pos1;


          if (_currentPosition == CurrentPos.Long)
          {
          _stop = entryPrice - StopTicks * TickSize;
          _profit1 = entryPrice + ProfitOneTicks * TickSize;
          _profit2 = entryPrice + ProfitTwoTicks * TickSize;

          _profitOrder1 = ExitLongLimit(0, true, (int)_pos1, _profit1, ProfitLong1, EnterLongName);
          _profitOrder2 = ExitLongLimit(0, true, (int)_pos2, _profit2, ProfitLong2, EnterLongName);
          _stopOrder = ExitLongStopMarket(0, true, _posSize, _stop, StopLong, EnterLongName);

          }
          else if (_currentPosition == CurrentPos.Short)
          {
          _stop = entryPrice + StopTicks * TickSize;
          _profit1 = entryPrice - ProfitOneTicks * TickSize;
          _profit2 = entryPrice - ProfitTwoTicks * TickSize;

          _profitOrder1 = ExitShortLimit(0, true, (int)_pos1, _profit1, ProfitShort1,EnterShortName);
          _profitOrder2 = ExitShortLimit(0, true, (int)_pos2, _profit2, ProfitShort2,EnterShortName);
          _stopOrder = ExitShortStopMarket(0, true, _posSize, _stop, StopShort, EnterShortName);

          }


          }



          From the OnExecutionUpdate() method I call the following functions to see if profitOrder1 has been filled (first profit target hit). This works correctly and similar functions are implemented to check if profit target two has been hit.


          private bool IsProfitTargetOneFilled(Execution execution)
          {

          return execution.Order.OrderState == OrderState.Filled && IsProTargetOneOrder(execution.Order.Name);

          }

          private bool IsProTargetOneOrder(string orderName)
          {
          return orderName.Contains(ProfitLong1) || orderName.Contains(ProfitShort1);

          }



          When theprofit (first or second target) or stop loss has been hit the strategy is exiting correctly when running in realtime simulation mode. However the code for detecting the stop loss has been hit does not seem to work correctly. So for example we have for profit target 1 not hit and stop loss reached:

          private bool IsProfitTargetOneFailed(Execution execution)
          {
          return execution.Order.OrderState == OrderState.Cancelled && IsProTargetOneOrder(execution.Order.Name);

          }

          private bool IsProTargetOneOrder(string orderName)
          {
          return orderName.Contains(ProfitLong1) || orderName.Contains(ProfitShort1);

          }


          Should we be checking for an order state == Cancelled (this checks that the order has been cancelled via the exchange)?

          Alternatively we can check the status of the stop order however I would prefer to check profitorder1 and profitorder2 separately as we can have a situation where 1st target is hit but we are stopped out before price reaches target2.



          Comment


            #6
            Hi mballagan,

            To cancel an order use CancelOrder() on the order object to cancel any order in a strategy. We have an example here. You can test your strategy by printing out: Print( execution.Order.OrderState); and noting each state the order sees throughout the order entry process. Using Prints in this way is the best way to see all data coming from the strategy once enabled.

            Kind regards,
            -ChrisL

            Chris L.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by alifarahani, Today, 09:40 AM
            6 responses
            38 views
            0 likes
            Last Post alifarahani  
            Started by Waxavi, Today, 02:10 AM
            1 response
            18 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Started by Kaledus, Today, 01:29 PM
            5 responses
            15 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by Waxavi, Today, 02:00 AM
            1 response
            12 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Started by gentlebenthebear, Today, 01:30 AM
            3 responses
            17 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Working...
            X