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

Setting the stop at the low of entry candle.

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

    Setting the stop at the low of entry candle.

    Hello,

    I have been trying for a while now to set my stop loss to the low of the entry candle (minus one tick), but without success. I've seen this in many posts, but haven't been able to figure out the solution. Most of my trades are stopped right at the bar that they are initiated. If there were some way to specify that I don't want to stop right at the entry bar (because I'd like to enter in the close - more about that towards the end), it would work, I think.

    I have tried many many different ways, so I don't even have one specific snippet of code I'd like help with. I just want to set my stop below the low of the entry candle.

    Another thing that's a bit related (but I know it's happening, so I can get around it (provided I can set my stops right)). The backtests give the entry on the OPEN of the candle AFTER my condition has happened, and not right in the close where it happens. This occurs with any strategy, for example buying if close[0] > SMA(21).

    Thanks in advance,

    hddietrich

    #2
    hddietrich,

    'Another thing that's a bit related (but I know it's happening, so I can get around it (provided I can set my stops right)). The backtests give the entry on the OPEN of the candle AFTER my condition has happened, and not right in the close where it happens. This occurs with any strategy, for example buying if close[0] > SMA(21).'

    That would be expected in backtesting, correct - as when the bar has closed + your condition triggered for trade entry, you are technically already on the new bar open, so we execute at the first possible point here for conservative backtest result reporting.

    Which NinjaScript methods were you using for your stop settings and adjustments? Was the value set before you actually entered the trade and reset when you were in flat state?
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thanks for your response Bertrand!

      Well, yes, technically that is how it works. It's okay, as it's not a major issue to me - just curiosity.

      I said I tried many things, but since you insist, this is what I have right now.

      Code:
      if (Position.MarketPosition == MarketPosition.Flat)
      			{
      				SetStopLoss(CalculationMode.Ticks, 50000);
      			}
      
      
      if (Position.MarketPosition == MarketPosition.Long)
      			{
      				SetStopLoss("Long", CalculationMode.Ticks, (Close[this.BarsSinceEntry("Long")+1] - Low[this.BarsSinceEntry("Long") +1])*100 +100, false);
      			}
      The *100 is there because of the 0.01 tick size (a 325 points stop would be 32500 ticks).
      The + 100 is to put it just below the low. I've used calculationmode.price as well in other ways, but always (90%), no matter what, I'm stopped right at the bar I come in. With this particular code, my stops are always 6 points below the entry point. I have no idea why.

      What I really suggest (because of my bad code) is just using the standard way of putting the stop at the low of the last bar - 1 tick.

      Thanks again.

      Comment


        #4
        hddietrich, have you then confirmed via prints and trace orders that the value you're calculating for your stop is correct and as you would expect?

        I would suggest setting the stop before your entry method is executed.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          SetStopLoss("Long", CalculationMode.Ticks, (Close[this.BarsSinceEntry("Long")+1] - Low[this.BarsSinceEntry("Long") +1])*100 +100, false);
          }
          (emphasis mine)

          You are adding an extra 100 ticks to the size of the StopLoss. On most stocks, that is a whole $1.00 below the entry bar. Is that what you intended?

          Comment


            #6
            Yes Konagan! It's an index whose tick is 5 points (in reality). In my NinjaTrader, it says the tick size is 0.01. So actually, 500 ticks in NT is equivalent to 1 tick in reality. So what I meant is to put the stop a little bit farther than the low. Thanks for your input though!

            Bertrand, I don't know how to use the print function in backtests, or trace orders, or setting the stop BEFORE the order is executed. I'm really lost in this. Really, I mean... I thought putting the stop below the entry's low (or candle before entry) would be something really trivial, for it is part of many trading systems I know. Is there no simple "default" way to do it?

            Thank you!

            Comment


              #7
              hddietrich, it would unfortunately not be directly build in, but doable as part of custom coding with our NinjaScript. Consider this very simple example to get started, it sets the stop to +- 1 tick of the entry bar.

              if (Position.MarketPosition == MarketPosition.Flat)
              SetStopLoss(CalculationMode.Ticks, 10);

              if (CrossAbove(Close, PriorDayOHLC().PriorHigh, 1)
              && Close[0] > CurrentDayOHL().CurrentOpen[0])
              {
              SetStopLoss(CalculationMode.Price, Low[0] - TickSize);
              EnterLong();
              }

              else if (CrossBelow(Close, PriorDayOHLC().PriorLow, 1)
              && Close[0] < CurrentDayOHL().CurrentOpen[0])
              {
              SetStopLoss(CalculationMode.Price, High[0] + TickSize);
              EnterShort();
              }


              TraceOrders and Debug prints are very helpful tools to understand your own code behavior deeper, so I would strongly suggest checking the respective tips here out -

              BertrandNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by frankthearm, Yesterday, 09:08 AM
              13 responses
              45 views
              0 likes
              Last Post frankthearm  
              Started by PaulMohn, Today, 12:36 PM
              2 responses
              16 views
              0 likes
              Last Post PaulMohn  
              Started by Conceptzx, 10-11-2022, 06:38 AM
              2 responses
              53 views
              0 likes
              Last Post PhillT
              by PhillT
               
              Started by Kaledus, Today, 01:29 PM
              1 response
              4 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by yertle, Yesterday, 08:38 AM
              8 responses
              37 views
              0 likes
              Last Post ryjoga
              by ryjoga
               
              Working...
              X