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 gbourque, Today, 06:39 AM
            2 responses
            14 views
            0 likes
            Last Post gbourque  
            Started by rexsole, Today, 08:39 AM
            0 responses
            4 views
            0 likes
            Last Post rexsole
            by rexsole
             
            Started by trilliantrader, Yesterday, 03:01 PM
            3 responses
            31 views
            0 likes
            Last Post NinjaTrader_Clayton  
            Started by cmtjoancolmenero, Yesterday, 03:58 PM
            5 responses
            26 views
            0 likes
            Last Post cmtjoancolmenero  
            Started by Brevo, Today, 01:45 AM
            1 response
            14 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Working...
            X