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

Strategy dynamic trailing stop

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

    Strategy dynamic trailing stop

    Hi,

    I am using a strategy that works as expected but I would like to ask how to go about the following. When the entry order is filled it places a stop straight away. What I would like to do in addition is move the stop dynamically. The original stop is placed using OnExecution (I am using IOrders). Where would I place the code to the above? Is it in OnBarUpdate?

    I am storing a variable when the entry order is triggered. What I want to do is update the variable to the current variable only if it is lower than the previous variable otherwise I want to leave the variable as is until either two events happen, 1. the variable is adjusted lower because the current variable is lower than the previously stored variable or 2. the stop is filled.

    Cheers,
    suprsnipes

    #2
    Hello suprsnipes,
    You can reassign the stop order again (say in OnBarUpdate).

    For example if your original stop order is

    Code:
    protected override void OnExecution(IExecution execution)
    {
            if (execution.Order != null && execution.Order.OrderState == OrderState.Filled && Position.MarketPosition == MarketPosition.Long)
    	{
    			stopOrd = this.ExitLongStop(Position.AvgPrice  - 10 * TickSize);
    	}
    }
    The you can change it in OnBarUpdate as

    Code:
    if (Close[0] >= Position.AvgPrice * 10* TickSize)
         if (buyOrd != null) 
    		stopOrd = ExitLongStop(Position.AvgPrice);
    Please let me know if I can assist you any further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Code:
      //If we have a short position and the upper bollinger band is lower now then it was previously then
                  if (Position.MarketPosition == MarketPosition.Short);
                  {
      
                      // Checks to see if our Stop Order has been submitted already
                      if (stopOrder != null && Trail(2, 9).Upper[0],2 < UBPL)
                      {
                          // Modifies stop-loss to trail upper
                          stopOrder = ExitShortStop(0, true, 1, Trail(2, 9).Upper[0],2, "stop", "short entry");
                          UBPL = Trail(2, 9).Upper[0],2;
                      }
                  }
      Although I haven't been able to test if for long, I have trace orders set to true so when onbarupdate is firing I get this message often. Ignored PlaceOrder()method Reason='There already is a matching order with same prices and quantity'. I want to stop it, do I need to put some additional code in the onbarupdate above? Is there any other way I can ensure this stop doesn't adjust up, only down?

      Comment


        #4
        Hello suprsnipes,
        To assist you further, may I know what entry orders codes you are using. Are both the stop orders codes share similar names?


        You can be able to change/adjust the stop orders only down by further custom coding the coding logics.

        I look forward to assisting you further.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          The entryOrder code is just "short entry" and the stopOrder is "stop".

          Comment


            #6
            Hello suprsnipes,
            Can you send a toy NinjaScript code* replicating the behavior to support[AT]ninjatrader[DOT]com

            Please append Attn:Joydeep in the subject line of the email and give a reference of this thread in the body of the email.

            I look forward to assisting you further.

            *The "toy" just means something that is a stripped down version that isn't necessarily the whole logic. It makes things easier to rout out.
            JoydeepNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by rayyyu12, Today, 05:38 PM
            0 responses
            2 views
            0 likes
            Last Post rayyyu12  
            Started by xepher101, Yesterday, 12:19 PM
            2 responses
            28 views
            0 likes
            Last Post xepher101  
            Started by thumper57, Today, 04:30 PM
            0 responses
            4 views
            0 likes
            Last Post thumper57  
            Started by OllieFeraher, 05-09-2024, 11:14 AM
            5 responses
            16 views
            0 likes
            Last Post MisterTee  
            Started by jackiegils, Yesterday, 11:05 PM
            1 response
            11 views
            0 likes
            Last Post marcus2300  
            Working...
            X