Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Dynamic Stoploss

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

    Dynamic Stoploss

    I want to be able to place a market order and set the stoploss for that order based on some criteria. Right now, this is how I'm doing it:

    Code:
                myEntry = EnterLong(contracts,"Going Long");  // Enter long market order
                if (target > 0) SetProfitTarget(CalculationMode.Ticks, target);
                double stop = <some criteria that returns a stop price or 0>;
                if (stop > 0)  SetStopLoss(CalculationMode.Ticks, stop);
                else SetStopLoss(CalculationMode.Ticks, 0);
    But when an order is created, there is no stoploss set. The profit target works fine, but why is the stoploss not working?

    #2
    Hi, did you use TraceOrders to debug? http://www.ninjatrader-support.com/H...aceOrders.html

    You also want to use Print() statements to debug your conditions - http://www.ninjatrader-support2.com/...ead.php?t=3418

    To see sample code how to change the price of a StopLoss, please see this reference samlpe - http://www.ninjatrader-support2.com/...ead.php?t=3222

    Comment


      #3
      cassb,

      Also, check the Log tab in your control center.

      What you have should work.
      mrlogik
      NinjaTrader Ecosystem Vendor - Purelogik Trading

      Comment


        #4
        Originally posted by mrlogik View Post
        cassb,

        Also, check the Log tab in your control center.

        What you have should work.

        Good thinking, Mr. I do see an error in the log:

        Calculated stop order price for strategy 'TJ_BPS ' was smaller/equal 0. No stop order placed.


        I see my mistake now. I should be using CalculationMode.Price, not Ticks. D'oh -- it's always something simple isn't it?

        Thanks!
        Bryan

        Comment


          #5
          you got it!
          mrlogik
          NinjaTrader Ecosystem Vendor - Purelogik Trading

          Comment


            #6
            I've got the same problem with an dynamic stop loss, and I've written an basic strategy (with the use of this example: http://www.ninjatrader.com/support/f...ead.php?t=3222) and I still receive this error:

            Code:
            **NT** Calculated stop order price for strategy 'TestStrat' was smaller/equal 0. No stop order placed.
            My strategy:
            PHP Code:
            #region Using declarations
            using System;
            using System.ComponentModel;
            using System.Diagnostics;
            using System.Drawing;
            using System.Drawing.Drawing2D;
            using System.Xml.Serialization;
            using NinjaTrader.Cbi;
            using NinjaTrader.Data;
            using NinjaTrader.Indicator;
            using NinjaTrader.Gui.Chart;
            using NinjaTrader.Strategy;
            #endregion
            
            namespace NinjaTrader.Strategy
            {
                [Description("Enter the description of your strategy here")]
                public class TestStrat : Strategy
                {
                    protected override void Initialize()
                    {
                        CalculateOnBarClose = true;
                    }
            
                    protected override void OnBarUpdate()
                    {
                        if(Position.MarketPosition == MarketPosition.Flat)
                        {
                            SetStopLoss("EnterLong", CalculationMode.Price, 0, false);    
                        }
                        
                        if(CrossAbove(EMA(20), EMA(50), 1))
                        {
                            SetStopLoss("EnterLong", CalculationMode.Price, EMA(50)[0] - ATR(14)[0], false);
                            EnterLong("EnterLong");    
                            Print("EnterLong. Stoploss value is: " + (EMA(50)[0] - ATR(14)[0]));
                        }
                        
                        if (Position.MarketPosition == MarketPosition.Long)
                        {
                            SetStopLoss("EnterLong", CalculationMode.Percent, EMA(50)[0] - ATR(14)[0], false);
                            Print("Adjust Stoploss. Stoploss value is: " + (EMA(50)[0] - ATR(14)[0]));
                        }
                        
                        if(CrossBelow(EMA(20), EMA(50), 1))
                        {
                            ExitLong("ExitLong", "EnterLong");
                        }
                                
                    }
                }
            } 
            
            The output window output:
            Code:
            EnterLong. Stoploss value is: 202,539517134472
            **NT** Calculated stop order price for strategy 'TestStrat' was smaller/equal 0. No stop order placed.
            Adjust Stoploss. Stoploss value is: 202,786202213329
            Adjust Stoploss. Stoploss value is: 202,982148979163
            Adjust Stoploss. Stoploss value is: 203,229759331933
            Adjust Stoploss. Stoploss value is: 203,319350754973
            Adjust Stoploss. Stoploss value is: 203,429408226915
            I would love to hear any suggestion as how to solve this, also partly because I've already encountered numerous difficulties with a simple dynamic stop, so I would love to hear what's the best way to do this.

            Regards,

            Comment


              #7
              Please don't reset to a price value of 0 when flat, instead reset it to a default value expressed in CalculationMode.Ticks - would you then see the same issue in your test strategy?

              Thanks,

              Comment


                #8
                In addition to what Bertand said, also put your "Print()" statement BEFORE you do the SetStopLoss() statement. Then you will maybe see the "0" stoploss value in your Output window before the error happens.

                Comment


                  #9
                  Thanks Bertrand and Cassb for your comments, highly appreciated.

                  I've incorporated your suggestions, and found the problem. Instead of CalculationMode.Price, I had used CalculationMode.Percent. In the following statement this is corrected:
                  PHP Code:
                  if (Position.MarketPosition == MarketPosition.Long)
                              {
                                  Print(Time[0].ToShortDateString() + " Adjust Stoploss. Stoploss value is: " + (EMA(50)[0] - ATR(14)[0]));
                                  SetStopLoss("EnterLong", CalculationMode.Price, EMA(50)[0] - ATR(14)[0], false);
                              } 
                  
                  Woops, such an simple error that's quite shameful , so sorry for bothering both of you with this question.

                  Regards,

                  Comment


                    #10
                    Print out the entry price. I suspect that your stop loss price is above your entry price, so if the StopLoss were even placed, it would take you out immediately after entry. Just the safety of the managed approach.

                    Comment


                      #11
                      Originally posted by J_o_s View Post
                      Thanks Bertrand and Cassb for your comments, highly appreciated.

                      I've incorporated your suggestions, and found the problem. Instead of CalculationMode.Price, I had used CalculationMode.Percent. In the following statement this is corrected:
                      PHP Code:
                      if (Position.MarketPosition == MarketPosition.Long)
                                  {
                                      Print(Time[0].ToShortDateString() + " Adjust Stoploss. Stoploss value is: " + (EMA(50)[0] - ATR(14)[0]));
                                      SetStopLoss("EnterLong", CalculationMode.Price, EMA(50)[0] - ATR(14)[0], false);
                                  } 
                      
                      Woops, such an simple error that's quite shameful , so sorry for bothering both of you with this question.

                      Regards,
                      No worries -- I've made that same error myself! :-)

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      669 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      378 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      111 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      575 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      581 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X