Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trades immediately stopped out when entered

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

    Trades immediately stopped out when entered

    Hi,

    I've created a simple breakout strategy in the Strategy Builder. I'm trading NQ 09-22, on the 1 minute chart. I've set a 20 tick (5 points) trailing stop loss.

    My issue is, most trades are closed instantly (i.e. upon opening) in this strategy. I've used a 3rd party to develop the strategy and there it works fine. It also works in real life settings.

    What could be the problem with my code? See below:

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    
    {
    
    public class ORBreakoutStrategy : Strategy
    
    {
    
    private BzvOpeningRange4 BzvOpeningRange41;
    
    
    
    
    protected override void OnStateChange()
    
    {
    
    if (State == State.SetDefaults)
    
    {
    
    Description = @"Enter the description for your new custom Strategy here.";
    
    Name = "ORBreakoutStrategy";
    
    Calculate = Calculate.OnEachTick;
    
    EntriesPerDirection = 1;
    
    EntryHandling = EntryHandling.AllEntries;
    
    IsExitOnSessionCloseStrategy = true;
    
    ExitOnSessionCloseSeconds = 30;
    
    IsFillLimitOnTouch = false;
    
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    
    OrderFillResolution = OrderFillResolution.Standard;
    
    Slippage = 3;
    
    StartBehavior = StartBehavior.WaitUntilFlat;
    
    TimeInForce = TimeInForce.Gtc;
    
    TraceOrders = true;
    
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    
    BarsRequiredToTrade = 20;
    
    // Disable this property for performance gains in Strategy Analyzer optimizations
    
    // See the Help Guide for additional information
    
    IsInstantiatedOnEachOptimizationIteration = true;
    
    StopLoss = 20;
    
    }
    
    else if (State == State.Configure)
    
    {
    
    }
    
    else if (State == State.DataLoaded)
    
    {
    
    BzvOpeningRange41 = BzvOpeningRange4(Close, @"0930", @"1000", @"1600");
    
    BzvOpeningRange41.Plots[0].Brush = Brushes.LimeGreen;
    
    BzvOpeningRange41.Plots[1].Brush = Brushes.OrangeRed;
    
    AddChartIndicator(BzvOpeningRange41);
    
    SetTrailStop(@"Long", CalculationMode.Ticks, StopLoss, false);
    
    }
    
    }
    
    
    
    
    protected override void OnBarUpdate()
    
    {
    
    if (BarsInProgress != 0)
    
    return;
    
    
    
    
    if (CurrentBars[0] < 1)
    
    return;
    
    
    
    
    // Set 1
    
    if (CrossAbove(Close, BzvOpeningRange41.RangeHighSeries, 1))
    
    {
    
    EnterLong(Convert.ToInt32(DefaultQuantity), @"Long");
    
    }
    
    
    
    }
    
    
    
    
    #region Properties
    
    [NinjaScriptProperty]
    
    [Range(1, int.MaxValue)]
    
    [Display(Name="StopLoss", Order=1, GroupName="Parameters")]
    
    public int StopLoss
    
    { get; set; }
    
    #endregion
    
    
    }
    
    }

    #2
    Hello isarnil,

    Thank you for your post.

    I can't test this as is, because I don't have the indicator you're calling in this script, but if I replace that with something simple like an SMA crossover I'm not seeing the same - the trailing stop starts 20 ticks from the entry.

    Is this occurring in real time operation? Or is this occurring in Playback with Market Replay data or in the Strategy Analyzer?

    Thanks in advance; I look forward to assisting you further.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Message TBU
      Last edited by Isarnil; 09-09-2022, 04:39 PM.

      Comment


        #4
        Hi Kate,

        Thank you for the quick response.

        - The results occur during backtesting in Strategy Analyzer
        - Using a simple SMA strategy, the trailing stop works
        - However, I get very weird results with the Bzv opening range indicator (updated for NT8 by your staff-member PatrickH, see here: https://ninjatraderecosystem.com/use...ing-range-4-2/)

        And just to iterate: What's super strange is that I get correct entries using the indicator, but when trades are live they immediately get closed out (in back test)
        - This happens regardless of what the stop-loss range is (20, 50, 200 ticks)

        See an example from chart, where trades get stopped out too tightly and immediately after trade commences. This is with a theoretical trailing stop loss of 10x the actual traded range on the chart.
        Click image for larger version

Name:	Screenshot 2022-09-12 at 00.33.27.png
Views:	351
Size:	34.2 KB
ID:	1215102

        It might not be visually obvious here what's happening, but I can assure you: The trailing stop in the picture is 50 points - much wider than what the stop loss suggests.

        Are there any common problems that can cause this phenomenon to occur? How can I find out better what's happening in relation to this indicator?

        Best,
        Emil

        Comment


          #5
          Hello Emil,

          Thank you for your reply.

          Sorry for the delay, I was out of the office yesterday.

          What is the Instrument and Interval you are using for the backtest? I'm testing on an ES 12-22 1 minute chart and am seeing the trail stop executing as I would expect with the strategy using the linked indicator.

          Thanks in advance; I look forward to assisting you further.
          Kate W.NinjaTrader Customer Service

          Comment


            #6
            Hi Kate,

            Thanks for the input.

            The results I showed you were NQ 09-22 on 1-minute chart.

            When I try the ES 12-22 I get the same poor results. On a backtest from 2022-06-01 to 2022-09-12 I see 0 won trades and -707 ticks of net profit. Trades are still closed out in the same bar as entry, on every occasion. This is with a 3-tick slippage. Do you also get 100% closed trades on the same bar? If so, is it due to price closing below the entry level, and therefore triggering the SL? Or is it something else that I don't understand?

            Forgive me, I'm new to the platform, but I cannot figure out why.

            Best,
            Emil

            Comment


              #7
              Hello Emil,

              Thank you for your reply.

              Here's an example of what I see with this when using 20 or 50 ticks:

              Click image for larger version

Name:	2022-09-13_11-51-37.png
Views:	271
Size:	167.9 KB
ID:	1215310

              If you test with the same settings, and you aren't seeing the same, the first thing I would suggest would be to delete your cache and test again.

              To delete the cache, follow the steps below.
              • Shut down NinjaTrader.
              • Open the Documents > NinjaTrader 8 > db folder.
              • Delete the sub-folder named 'cache'.
              • Restart NinjaTrader and test.
              Please let us know if we may be of further assistance.
              Kate W.NinjaTrader Customer Service

              Comment


                #8
                Hi Kate,

                Thank you! Much appreciated!! ES 12-22 now works properly and produces results similar to my expectance.

                BUT: NQ still doesn't work. I've tried with both NQ 09-22 and NQ 12-22 on the 1-minute time-frame. Would you check if you don't get stopped out on the same bar with NQ?

                Best,
                Emil

                Comment


                  #9
                  #Emil “Are there any common problems that can cause this phenomenon to occur” - not the same insofar as resulting from manually moving an ATM Stop on the DOM to trail causing Exit by moving Target instead. Happens repeatedly but is erratic. May be common/related cause, maybe not.
                  KR

                  Comment


                    #10
                    Hello Isarnil,

                    Thank you for your reply.

                    This would not be related to what brucerobinson mentioned. NQ bars just tend to have a really large range. Here's a screenshot that gives an example of what I'm seeing using a 50 tick trailing stop on NQ 12-22:

                    Click image for larger version

Name:	2022-09-14_10-00-02.png
Views:	272
Size:	186.9 KB
ID:	1215487

                    This page of our help guide that goes over how NinjaTrader calculates historical fill prices may be helpful:



                    Please let us know if we may be of further assistance to you.
                    Kate W.NinjaTrader Customer Service

                    Comment


                      #11
                      @Bruce: Thanks for chiming in, much appreciated!

                      @Kate: Thanks for the link to the calculation article. This does indeed seem to be the problem! When I switched my backtests from 1-minute to 200 tick charts I got much more realistic results. Not perfect yet, but it's a start And thanks again for all the help!

                      Comment


                        #12
                        Hey y'all,

                        I'm having a similar issue with a trailing stop immediately closing out. Clearing the cache folder helped initially but then it went back to immediately closing out. I'm following what the Help Guide suggests to have it be static and initialized in State.Configure. Is there a way to clear or reset whatever is causing this when my position is flat?

                        Thanks

                        Comment


                          #13
                          Hello Rex_505,

                          Just to confirm, the order price is not between the high and low of the bar?

                          What is the submission price of the order? What is the high and low of the bar?

                          Are you using SetTrailStop()?
                          If so, what CalculationMode is used?

                          Are you using CalculationMode.Price and setting a starting price, or a number of ticks?
                          If you are using a price, are you resetting the price by calling SetTrailStop() before submitting the entry order?
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi Chelsea,

                            The order price is accurate when it enters a trade but the SetTrailStop() is closing it out at the same price instantly. I am using CalculationMode.Ticks and setting the value from a parameter (Chart>Strategy>Properties>Parameter). The SetTrailStop() is in the State.Configure and isn't called anywhere else. Do I reset the price by calling SetTrailStop() on a bar when the market position is flat?

                            Comment


                              #15
                              Hello Rex_505,

                              What is the submission price of the trail stop order?
                              What is the high and low of the bar?

                              When using CalculationMode.Ticks, you do not need to reset.

                              My thoughts are the order is being submitted inside of the bar and filling.

                              If you set the ticks to 100 does the trail stop order still fill immediately?
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by helpwanted, Today, 03:06 AM
                              1 response
                              11 views
                              0 likes
                              Last Post sarafuenonly123  
                              Started by Brevo, Today, 01:45 AM
                              0 responses
                              9 views
                              0 likes
                              Last Post Brevo
                              by Brevo
                               
                              Started by aussugardefender, Today, 01:07 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post aussugardefender  
                              Started by pvincent, 06-23-2022, 12:53 PM
                              14 responses
                              242 views
                              0 likes
                              Last Post Nyman
                              by Nyman
                               
                              Started by TraderG23, 12-08-2023, 07:56 AM
                              9 responses
                              387 views
                              1 like
                              Last Post Gavini
                              by Gavini
                               
                              Working...
                              X