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

SetTradeStop() not executing?

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

    SetTradeStop() not executing?

    From my debugging output I can see that my SetTradeStop() is being set properly, but the moment that the position should be sold the SetTradeStop() just decides to not sell my position and just disappear.

    Code:
    if (Position.MarketPosition == MarketPosition.Long && CrossAbove(High, Bollinger(2, 20).Upper, 1) || High[0] > Bollinger(2, 20).Upper[0])
    {
        if ( Typical[0] * 1.03 >= Bollinger(2, 20).Upper[0])
        {
            Print("Trailing Stop Activated");
            SetTrailStop(CalculationMode.Percent, 0.03);
        } 
        else
        {
            Print("Bollinger Top Sell Activated");
            ExitLong("Bollinger Top Sell", "");
        }
        #region Output
        Print("Exit Long: " + ToDay(Time[0]));
        Print("Bollinger Band Top");
        Print("Sell Price: " + Close[0]);
        Print("");
        #endregion
    
    }
    Here is what my debugging output is telling me...
    Code:
    Enter Long: 20051209
    # of Long Indicators: 1
    Buy Price: 410
    Long Stop Price (S1): 402.89
        RSI(2): 0
        RSI Div: 0
        EMA(5): 1
        EMA(13) > EMA(26): 1
        Reg Ch: 1
        Bollinger: 0
    # of Short Indicators: 0
        RSI(2): 0
        RSI Div: 0
        EMA(5): 0
        EMA(13) > EMA(26): 0
        Reg Ch: 0
        Bollinger: 0
    
    Trailing Stop Activated
    Exit Long: 20051216
    Bollinger Band Top
    Sell Price: 430.1
    
    Trailing Stop Activated
    Exit Long: 20051219
    Bollinger Band Top
    Sell Price: 424.9
    
    Trailing Stop Activated
    Exit Long: 20051221
    Bollinger Band Top
    Sell Price: 426
    
    Trailing Stop Activated
    Exit Long: 20060104
    Bollinger Band Top
    Sell Price: 445.73
    
    Trailing Stop Activated
    Exit Long: 20060105
    Bollinger Band Top
    Sell Price: 451.6
    
    Trailing Stop Activated
    Exit Long: 20060106
    Bollinger Band Top
    Sell Price: 465.3
    
    Trailing Stop Activated
    Exit Long: 20060109
    Bollinger Band Top
    Sell Price: 467.4
    
    Trailing Stop Activated
    Exit Long: 20060110
    Bollinger Band Top
    Sell Price: 470.35
    
    [B] Trailing Stop Activated
    Exit Long: 20060111
    Bollinger Band Top
    Sell Price: 473
    
    Date: 20060131
    Typical Price: 389.653333333333
    Long Stop Price: 402.89
    S1: 402.89[/B] 
    
    Trailing Stop Activated
    Exit Long: 20060329
    Bollinger Band Top
    Sell Price: 394.97
    
    Trailing Stop Activated
    Exit Long: 20060330
    Bollinger Band Top
    Sell Price: 388.43
    Attached is a picture that visually shows what is going wrong.
    Attached Files
    Josh P.NinjaTrader Customer Service

    #2
    You need to have your stops/target in place before (!) the actual position is entered.

    Comment


      #3
      What about this simulation thing I'm reading in the help file?

      I only want to use a trailing stop if the price is above the upper bollinger band. If I need to set the stop before entry it would put a trailing stop even on trades that I don't want to use the trailing stop exit strategy for.

      Code:
      SetTrailStop("", CalculationMode.Percent, 0.03, true);
      Using this code doesn't seem to make a difference. Maybe I'm using it wrong?

      Is there also a way I can use SetStopLoss() to have it set at a certain price instead of a certain currency?
      Last edited by NinjaTrader_JoshP; 05-22-2007, 01:23 PM.
      Josh P.NinjaTrader Customer Service

      Comment


        #4
        Then what you need to do is provide a signal name for entries above the upper BB and in the SetTrailStop() method, provide the name of this entry signal. That way you tie the two together and the trail stop will ONLY be affective on this entry.

        For setting by price, please check the SetStopLoss() method Help Guide and use the CalculationMode.Price parameter.
        RayNinjaTrader Customer Service

        Comment


          #5
          Let me rephrase what I am trying to do. I have one set of entry criteria which all entries are based upon. After entering, I have two competing exit strategies.

          For example, I enter into a long position. In the next few days the price is rising steadily and then on the current day it passes above the upper bollinger band. If the price went above the upper bollinger band by more than a certain percentage I want to use a trailing stop to just ride the momentum and get out when there is less forward momentum. If the price went above the upper bollinger band but did NOT get above the threshold percentage I set, I want it to just use a regular exit long market order immediately instead of tagging along.

          Wouldn't using signal names require me to need separate entry types?
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            Thanks for the clarification.

            SetTrailStop() will not work in this case, will not work as it is currently implemented. We do have on our list to change this implementation so that you can call this method AFTER a position is entered.

            You would have to code your trail stop method yourself using ExitLongStop().
            RayNinjaTrader Customer Service

            Comment


              #7
              I have run into another problem with the SetStopLoss(). I am on a daily chart. On a certain day my strategy is triggered to sell. It sends out the ExitShort() order. Because the market is closed for the day the order is sent, execution should occur at the next day's open. For some reason that order is canceled in favor for the Stop Loss order. This doesn't make sense because my ExitShort() should have been honored first because it is a market order at the open price and the stop price isn't reached until later in the day.

              If I comment out my SetStopLoss() line my ExitShort() order is executed perfectly. Why is the SetStopLoss() order taking precedence when hypothetically it shouldn't have even been triggered at the open yet?

              From the attached pictures you can see that my stop is set at $42.23 and that price is way above the open price of $40.40 which I was trying to sell at.
              Attached Files
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                I will need to check with development but I suspect that stop and profit target orders are checked for fills before Exit() methods are checked. I see your point about a market order taking precedence and will see if anything can be done to change the current behaviour.
                RayNinjaTrader Customer Service

                Comment


                  #9
                  There also seems to be another issue with SetStopLoss(). I have an Enter() order set awaiting execution at the opening on the next day and my SetStopLoss() is set at a certain price in relation to the current day's actions. On the next day if there is a sizeable dip causing the Enter() order to be executed well below the SetStopLoss() NT seems to think it is still capable of selling the position at the Stop price. It then proceeds to show the results of the trade as a massive win instead of as a loss.

                  I'm not even sure as to how its really suppose to work. Entering a position below your stop price should cause an instant turn around? Wasn't there some rule that doesn't let you set your stop very close to the bid/ask to prevent riskless scalpers?

                  From the attachment you can see the trade entered at $31.00. The stop was set at $56.25 which is completely out of that day's high/low range of $30.37-$31.75. The execution at $56.25 is impossible.
                  Attached Files
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Sell stop orders are filled at higher value of either the bar low or the stop price itself. In this case, it was the stop price itself. In reality, the execution is impossible and in real-time, your stop order would have been rejected. From a backtesting perspective, there is no way to know if that stop price was valid or invalid.

                    For example, you could have an existing stop running intraday at several ticks below the low of the current bar. Then on the next bar, the price gaps down where the open and high of the bar is below the stop price. In this case, the stop would trigger and fill. In backtest, this would display as a stop being filled above the high of the bar.
                    RayNinjaTrader Customer Service

                    Comment


                      #11
                      Why is it that the stop orders are filled at max(bar low, stop price)? I thought they were designed to be triggered at the stop price and then sent as a market order when triggered.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        In real-time a stop order is filled at market but in backtest, when a stop order is triggered (stop price within the bar) in backtest, we just take the higher value of the low of the bar or the stop price - slippage...I forgot to mentiont the slippage part. This ensures that you are filled on the bar itself.
                        RayNinjaTrader Customer Service

                        Comment


                          #13
                          Is there absolutely no way I can get it to mimic how it would behave in real-time? Perhaps I can access a file somewhere that has the SetStopLoss() defined and try modifying it to the way it works for me?

                          I can see the logic behind having the stop behave the way it is right now, but I would rather have it done more realistically.
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            No this is not possible. Keep in mind, if this was realistic, the stop order would then be rejected and you would never get out of your position.
                            RayNinjaTrader Customer Service

                            Comment


                              #15
                              Hmm. From what I can tell my market orders are sent at the end of the current day for execution the next morning. Is there anyway I can have the order sent on the morning of the next day so I can first have a pre-check of the opening price against the previous closing price? Ideally I wouldn't want my strategy to enter a position if it gapped down so much. I tried toying with entering using stops and limits, but they just aren't doing the trick. I want to allow for some gap downs/ups. Just not the massive ones like the one in the screenshot.

                              I know this is very simple in real life, but the way its running right now is making all my backtest data rather useless from inaccuracy.
                              Josh P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by trilliantrader, Today, 03:01 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post trilliantrader  
                              Started by pechtri, 06-22-2023, 02:31 AM
                              9 responses
                              122 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by frankthearm, 04-18-2024, 09:08 AM
                              16 responses
                              67 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by habeebft, Today, 01:18 PM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by benmarkal, Today, 12:52 PM
                              2 responses
                              19 views
                              0 likes
                              Last Post benmarkal  
                              Working...
                              X