Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

stoploss

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

    stoploss

    I am trying to set a stop loss at the low of one bar before entry and I can't get it.

    Example:

    Bar 1: low = 49
    Bar 2: entry point = 50

    I want the stop loss to be constant at 49. Isn't this the code:

    SetStopLoss(CalculationMode.Price, Low[1]);

    Its not working for me.

    #2
    Hello shawnkm,

    Thank you for writing in.

    One way you can do this is by setting the stop loss within OnExecution(). OnExecution() is called on an incoming fill. So, once the order fills, place your stop loss as desired.

    Example:

    Code:
    protected override void OnBarUpdate()
    {
         // entry logic
         EnterLong("Entry");
    }
    
    protected override void OnExecution(IExecution e)
    {
         if (e.Order.Name == "Entry" && e.Order.OrderState == OrderState.Filled)
              SetStopLoss(CalculationMode.Price, Low[1]);
    }
    For more information about OnExecution(), please take a look at this help guide link: https://ninjatrader.com/support/help...nexecution.htm

    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      This is still getting me.... for a day and a half I can not get this.

      I tried the on execution, and it works in some areas and some areas it doesn't.

      Is there an easy code to set the stoploss... or it can be a simple ExitLong command to look at the low of the bar before the entry to set an exit?

      Thanks again!

      Comment


        #4
        Hello shawnkm,

        Can you please clarify what you mean by SetStopLoss() working sometimes and not other times?

        Are you getting an error? Have you ensured that you're matching up the signal name of your entry to the order name of the execution calling OnExecution()?
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          Sure... i am not getting an error, but I scan the backtest strategy and i notice that sometimes it worked and others it didn't.

          I labeled the long entries just to make sure. Labeled them "test1". Sometimes "test1" used the stoploss and other times it didn't.

          Can we just write an exitlong command instead of the execution call to simplify the process?

          Comment


            #6
            Hello shawnkm,

            The stop loss will not appear on the chart unless it has been hit and has exited your order, nor will it appear under the Executions tab of the backtest results as this would only show filled orders.

            Additionally, if the "exit on close" backtest parameter is set to true, your strategy may exit before even hitting your established stop loss.

            If you would like to use an ExitLong() to exit your order, you'll have to check the instrument's current close price and compare it to some variable set to Low[1] when you entered.

            If the instrument current price is less than or equal to the variable, call ExitLong().

            As an example:
            Code:
            private double prevBarLow = 0;
            
            protected override void OnBarUpdate()
            {
                 // entry logic
                 if (Position.MarketPosition == MarketPosition.Flat)
                 {
                      EnterLong();
                      prevBarLow = Low[1];
                 }
            
            ........
            
                 if (Position.MarketPosition == MarketPosition.Long && Close[0] <= prevBarLow)
                      ExitLong();
            }
            Zachary G.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            80 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            46 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            29 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            32 views
            0 likes
            Last Post TheRealMorford  
            Started by Mindset, 02-28-2026, 06:16 AM
            0 responses
            66 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Working...
            X