Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Modifying the price of stop loss and profit target orders

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

    Modifying the price of stop loss and profit target orders

    Hi guys,

    Please give me a hand with my problem. I would like to set 3 different profit targets for the trades and when each target is reached the stop loss should rise to the level of the previous target (or any other value that was previously set ).

    For 1 profit target and 1 stop loss moved to the specified level, I used the code that I partially posted below.

    Please tell me how can I do this with 2 more targets?

    Thank you advance!

    Code:
           protected override void Initialize()
            {
    			SetStopLoss(CalculationMode.Ticks, stoplossticks);
    			SetProfitTarget(CalculationMode.Ticks, profittargetticks);
    			
                CalculateOnBarClose = true;
            }
    
           protected override void OnBarUpdate()
            {
    		
    			if (Position.MarketPosition == MarketPosition.Flat)
    			{
    				SetStopLoss(CalculationMode.Ticks, stoplossticks);
    			}
    						
    			else if (Position.MarketPosition == MarketPosition.Long)
    			{
    				
    				if (Close[0] > Position.AvgPrice + 50 * TickSize)
    				{
    					SetStopLoss(CalculationMode.Price, Position.AvgPrice + 30 * TickSize);
    				}
    												
    			}

    #2
    Hi Mitnick_d,

    Thank you for posting.

    You can use this overload of the SetProfitTarget -
    Code:
    SetProfitTarget(string fromEntrySignal, CalculationMode mode, double value)
    The entry signal will allow you to call the specific profit target later in the code for when you need to change it.

    You can also see the help guide on the properties of these methods form the link below -
    http://www.ninjatrader.com/support/h...ofittarget.htm

    Let me know if I can be of further assistance.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Hi Cal,

      Thank you for your answer, but how can I move my stop loss, because I tried using the same code (posted below) and it didn't work. Let's say I want the stop to be at Position.AvgPrice + 30 when Close[0] > Position.AvgPrice + 50, ( this one I got ) and I want the same thing for two more levels( when price is at Position.AvgPrice + 80 the stop should be at at one Position.AvgPrice + 60 and so on). How can I do it?

      {

      if (Close[0] > Position.AvgPrice + 50 * TickSize)
      {
      SetStopLoss(CalculationMode.Price, Position.AvgPrice + 30 * TickSize);
      }

      }

      Comment


        #4
        Hi Mitnick,

        I would suggest looking at the reference sample below on modifying your stop loss and profit targets
        http://www.ninjatrader.com/support/f...ead.php?t=3222

        This sample will show you how to change the price of your stop loss and profit targets in the code.
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Hi Cal,

          Unfortunately it doesn't, what I am using is the same as that example. And it works just for one rising of the stop loss, I need two more. I already searched the forum for possible answers and couldn't find any, I was hoping from some help from you guys.

          Thank you!

          Comment


            #6
            Mitnick,

            You need to have multiple Sets(), setup in your initialize section -
            E.G.
            Code:
            SetStopLoss(Long1, CalculationMode.Ticks, 15);
            SetStopLoss(Long2, CalculationMode.Ticks, 20);
            SetStopLoss(Long3, CalculationMode.Ticks, 25);
            SetProfitTarget(Long1, CalculationMode.Ticks, 15);
            SetProfitTarget(Long2, CalculaitonMode.Ticks, 20);
            SetProfitTarget(Long3, CalculationMode.Ticks, 25);
            Your entry methods will then have the signal name attached to them so you can have each order to its own Set() and then be able to modify those later in the code.

            Take a look at the link from Post #2 about the FromEntrySignal and then take a look at the Entry methods from the link below.

            http://www.ninjatrader.com/support/h...?enterlong.htm

            Let me know if this helps.
            Cal H.NinjaTrader Customer Service

            Comment


              #7
              Can this method be used for just a single trade? Because I saw something similar but it opened 3 positions and for each one it had a different stop and profit order.

              Comment


                #8
                Mitnick,

                If you are doing one trade and not multiple orders then you would only need to use the idea form the sample reference.
                Each order can only have one SetStopLoss() and one SetProfitTarget() but you can modify those prices of the Sets() by using the sample from the reference I supplied you.

                Let me know if this clarified things for you.
                Cal H.NinjaTrader Customer Service

                Comment


                  #9
                  Cal,

                  I struggled all day with the script referred by you, and I couldn't raise the stop for the second time. That's why I tried to get some help from the forum.

                  PS: I am not a experimented coder

                  Comment


                    #10
                    Mitnick,

                    While the code you supplied has the syntax correct, there could be an issue that the condition is not being met for the change of the stoploss to take place.

                    I would suggest using the Print() function to see if the condition is being met and if the SetStopLoss() is being modified for when you are in a long position.

                    First of all you would want to use Print() statements to verify values are what you expect - Debugging your NinjaScript code.

                    For strategies add TraceOrders = true to your Initialize() method and you can then view valuable output related to strategy submitted orders through Tools > Output window - TraceOrders
                    Cal H.NinjaTrader Customer Service

                    Comment


                      #11
                      Cal,

                      I posted below the code with the stop being raised for the second time. This one doesn't work. The one initially posted had just one stop and work perfectly. I am sure that it's a programing error, even though it compiles.

                      Thank you for your support!

                      protected override void OnBarUpdate()
                      {
                      // Resets the stop loss to the original value when all positions are closed
                      if (Position.MarketPosition == MarketPosition.Flat)
                      {
                      SetStopLoss(CalculationMode.Ticks, stoplossticks);
                      }

                      // If a long position is open, allow for stop loss modification to breakeven
                      else if (Position.MarketPosition == MarketPosition.Long)
                      {
                      // Once the price is greater than entry price+50 ticks, set stop loss to breakeven
                      if (Close[0] > Position.AvgPrice + 50 * TickSize)
                      {
                      SetStopLoss(CalculationMode.Price, Position.AvgPrice + 30 * TickSize);
                      }
                      if (Position.MarketPosition == MarketPosition.Flat)
                      if (Close[0] > Position.AvgPrice + 70 * TickSize)
                      {
                      SetStopLoss(CalculationMode.Price, Position.AvgPrice + 50 * TickSize);
                      }


                      }

                      Comment


                        #12
                        Mitnick,

                        Let's take a look this code here -
                        Code:
                        else if (Position.MarketPosition == MarketPosition.Long)
                        {
                        // Once the price is greater than entry price+50 ticks, set stop loss to breakeven
                          if (Close[0] > Position.AvgPrice + 50 * TickSize)
                          {
                             SetStopLoss(CalculationMode.Price, Position.AvgPrice + 30 * TickSize);
                          }
                        
                          if (Position.MarketPosition == MarketPosition.Flat)	
                          if (Close[0] > Position.AvgPrice + 70 * TickSize)
                          {
                             SetStopLoss(CalculationMode.Price, Position.AvgPrice + 50 * TickSize);
                          }	
                        }
                        C# will process your code logically. Since, that is the case we know we can change our code around to account for this.

                        E.G.
                        if(Close[0] > Position.AvgPrice + 50 * TickSize)

                        When this statement becomes true then it will process this request of SetStopLoss.

                        This would work logically with your code, however you have

                        if (Position.MarketPosition == MarketPosition.Flat)
                        Meaning the following line below will be invalid since this is nested inside a condition that checks if your long.

                        You will need to get rid of this MarketPosition.Flat check and then you would be able to process your code logically.

                        You can also use this method of the SetStopLoss so that you know which one will get processed instead of doing them both at the same time in the condition check.

                        Code:
                        else if (Position.MarketPosition == MarketPosition.Long)
                        {
                        // Once the price is greater than entry price+50 ticks, set stop loss to breakeven
                          if (Close[0] > Position.AvgPrice + 70 * TickSize)
                          {
                             SetStopLoss(CalculationMode.Price, Position.AvgPrice + 50 * TickSize);
                          }
                        
                          else if (Close[0] > Position.AvgPrice + 50 * TickSize)
                          {
                             SetStopLoss(CalculationMode.Price, Position.AvgPrice + 30 * TickSize);
                          }	
                        }
                        Let me know if this helps
                        Cal H.NinjaTrader Customer Service

                        Comment


                          #13
                          Cal,


                          I also tried removing the "if (Position.MarketPosition == MarketPosition.Flat)" line, but without any success.

                          I tried with the code you suggested and it still executes the fist command (sets the stop at 30 ) and after that....nothing, till the final take profit order.

                          I also attached the full script, maybe it helps.

                          Thanks
                          Attached Files

                          Comment


                            #14
                            Mitnick,

                            This not a matter of the code being syntax wrong but the conditions you have set.

                            What instrument are you going to be trading this to?

                            Your TickSize checks might be too big of a gap with the market data.

                            Try lowering and changing the conditions around to see if those might reach better results.

                            When I applied the strategy to the chart with lower settings I noticed that the conditions get called more often cause they are more likely to be true.

                            Use the Print() statements in your conditions. When they get called they will print a statement to the Output window, Tools > Output Window.

                            This will allow you to see if your conditions are indeed getting called when expected.
                            Cal H.NinjaTrader Customer Service

                            Comment


                              #15
                              Thank you Cal. Due to the fact that I am quite tired I was applying it to the wrong data series.

                              Thank you very much for your support, you've been very helpful.

                              Best regards,
                              Mitnick

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by gemify, 11-11-2022, 11:52 AM
                              6 responses
                              803 views
                              2 likes
                              Last Post ultls
                              by ultls
                               
                              Started by ScottWalsh, Today, 04:52 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post ScottWalsh  
                              Started by ScottWalsh, Today, 04:29 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post ScottWalsh  
                              Started by rtwave, 04-12-2024, 09:30 AM
                              2 responses
                              22 views
                              0 likes
                              Last Post rtwave
                              by rtwave
                               
                              Started by tsantospinto, 04-12-2024, 07:04 PM
                              5 responses
                              70 views
                              0 likes
                              Last Post tsantospinto  
                              Working...
                              X