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

EnterLongStopMarket order not working correct

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

    EnterLongStopMarket order not working correct

    HI,

    I am working on a simple strategy with multi time frames.
    I would like to submit a stop market oder if I have got 4 daily bars in a row, having a higher close than entry. The long stop market order is based on the high of the previous daily bar plus xx ticks.
    See code in the attachment.

    When I look at the chart in the backtest, the order is just executed straight away without reaching the predifined price. (see picture)

    Has anybody an idea what I am doing wrong?

    Thanks
    Attached Files

    #2
    Hello elnoppo,

    Thank you for your reply.

    What is the primary data series you're running the strategy on in the backtest? 1 minute, 1 day, etc? I'd like to test on my end to see if I get the same thing.

    I note that in the strategy, you're not checking which bar series your strategy places the order on, so it could fire on either the primary series, or the additional minute or daily series. I'd recommend doing a bars check in there so you know which series your order is getting submitted The next thing I would recommend would be to turn on the Order Trace function:

    Strategy Builder > Default Properties > More Properties > Trace Orders, or:

    if (State == State.SetDefaults)
    {
    TraceOrders = true;
    }

    Once you then recompile the strategy, you can open a new NinjaScript Output window under New > NinjaScript Output. This will print a log of any orders submitted by the strategy during while it's running, along with any ignored orders. You can then look through and see what may be occurring.

    Here is a link to our help guide that goes into more detail on tracing orders:


    Trace orders alone may not give you the full picture of whether or not a trade should have been entered on a given bar, so adding prints to your strategy that will show in the NinjaScript Output window, with information on what the variables you're using for your conditions are on a particular bar, can be helpful.

    This forum post goes into great detail on how to use prints to help figure out where issues may stem from — this should get you going in the correct direction. You can even add these using the Strategy Builder.



    If you run into issues like we saw here, the above information will allow you to print out all values used in the condition in question that may be evaluating differently during a backtest. With the printout information you can assess what is different between the two.

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

    Comment


      #3
      Hello Kate,

      and thanks for your reply.

      What is the primary data series you're running the strategy on in the backtest?

      I was using a 60 minute time series for Palladium.

      I note that in the strategy, you're not checking which bar series your strategy places the order on, so it could fire on either the primary series, or the additional minute or daily series.

      True. I assumed that the entry order is always based on the primary data series.I added the following lines to the code (not sure if that is correct):
      if (BarsInProgress == 0)
      {
      alreadyTradedToday = true;
      EnterLongStopMarket(0,true,Convert.ToInt32(Default Quantity), (MAX1[0]+ (EntryPufferTicks)), "");
      }


      Once you then recompile the strategy, you can open a new NinjaScript Output window under New > NinjaScript Output. This will print a log of any orders submitted by the strategy during while it's running, along with any ignored orders. You can then look through and see what may be occurring.

      This is the result from TraceOders:
      Enabling NinjaScript strategy 'HIC4DaySeries2/193515747' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=False SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes

      I do not see anything relevant.

      Would highly appreciate if you could give me further support!

      Best
      ​​​​​​​elnoppo

      Comment


        #4
        Hello elnoppo,

        Thank you for your reply.

        With your addition, that would work to only place trades on the primary data series, yes.

        When you backtest, are you using the Strategy Analyzer?

        First, let's have you manually clear your cache so we can make sure there's not a corrupt cache issue happening.

        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.
        If this didn't make any difference, at times, when we see fill prices way out of the range of where the bars are, that can indicate a database issue. To test, let's have you temporarily rename your database to see if that may be the issue.
        • Shutdown NinjaTrader and Go to the Start Menu
        • Select My Documents--> NinjaTrader 8--> db--> "NinjaTrader.sdf" file (may only say 'NinjaTrader' depending on your Windows File Explorer settings)
        • Right click on NinjaTrader.sdf and select "Rename" *Name it "OLDNinjaTrader.sdf"
        • Then restart the software and NinjaTrader will create a fresh database file to use
        • Restart NinjaTrader and test with your strategy.
        If this did not resolve it, you may restore your old database by shutting down NinjaTrader, going back to the NinjaTrader 8 > DB folder, deleting the newly created NinjaTrader.sdf file, and renaming OLDNinjaTrader.sdf to NinjaTrader.sdf again, then restarting NinjaTrader.

        If this did resolve it, unfortunately the following items stored in the old database will be lost - Trading Hours templates, Instrument Lists / Custom Instruments, historical trade execution data, saved Strategy Analyzer backtest results.

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

        Comment


          #5
          Dear Kate,

          thanks again for your response.

          Yes - I am using the Strategy Analyzer. Did you have the time to test the code and check if you have got the same issues?
          Regarding the barsinprogress - is there a possibility to define the time series directly in the EnterLongStopMarket?

          I tried both approaches you mentioned in your previous post regarding the cache and the sdf file. Unfortunately, both approaches did not change anything.

          Any other idea how to solve that problem?

          Thanks in advance.

          Comment


            #6
            Hello elnoppo,

            Thank you for your reply.

            I had actually missed that in your original strategy you had the line "if (BarsInProgress !=2) return;" right before your entries. What this was doing was only sending the trades to the daily series, which is what's causing the weird fill prices.

            If you change it to this, does this look more like what you're expecting:

            Code:
                    protected override void OnBarUpdate()
                    {
            
                        if (CurrentBars[1] < 1 || CurrentBars[2] < 3)
                            return;
            
                        if (Time[0].DayOfWeek != Time[1].DayOfWeek) // new day, reset bool flag
                        {   
                            alreadyTradedToday = false; 
                        }
            
                        if (BarsInProgress == 0)
                        {
                            if ((Position.MarketPosition == MarketPosition.Flat && alreadyTradedToday == false)
                                 && (Closes[2][0] > Opens[2][0])
                                 && (Closes[2][1] > Opens[2][1])
                                 && (Closes[2][2] > Opens[2][2])
                                 && (Closes[2][3] > Opens[2][3])
                                 && (Times[0][0].TimeOfDay >= new TimeSpan(7, 0, 0)))
                            {
                                alreadyTradedToday = true;
                                EnterLongStopMarket((MAX1[0] + (EntryPufferTicks * TickSize)) , "");
                            }
                        }
                    }
            Please let us know if we may be of further assistance to you.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              Hi Kate,

              thanks again for your help and the code. We are getting closer With your code, the stop loss issue seems to be solved.
              However, for some reason the number of trades decreases significantly and I do not understand why (See enclosed pics using daily bars).

              Since the code is inteded to submit a stop market oder if there are 4 daily bars in a row, having a higher close than entry. The long stop market order is based on the high of the previous daily bar plus xx ticks. So why is the number of entries decreasing? Any idea?

              Thanks again!
              Attached Files

              Comment


                #8
                Hello elnoppo,

                Thank you for your reply.

                The first thing I would recommend here would be to turn on the Order Trace function:

                Strategy Builder > Default Properties > More Properties > Trace Orders, or:

                if (State == State.SetDefaults)
                {
                TraceOrders = true;
                }

                Once you then recompile the strategy, you can open a new NinjaScript Output window under New > NinjaScript Output. This will print a log of any orders submitted by the strategy during while it's running, along with any ignored orders. You can then look through and see what may be occurring.

                Here is a link to our help guide that goes into more detail on tracing orders:


                Trace orders alone may not give you the full picture of whether or not a trade should have been entered on a given bar, so adding prints to your strategy that will show in the NinjaScript Output window, with information on what the variables you're using for your conditions are on a particular bar, can be helpful.

                This forum post goes into great detail on how to use prints to help figure out where issues may stem from — this should get you going in the correct direction. You can even add these using the Strategy Builder.



                If you run into issues like we saw here, the above information will allow you to print out all values used in the condition in question that may be evaluating differently. With the printout information you can assess what is different between the two. If you're not sure what your prints show, I would be happy to go over your results with you.

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

                Comment


                  #9
                  Dear Kate,

                  I printed the output (see attachment), however I do not see anything in the log file telling me why the TrailStop is not working correctly or on which data series it is triggered.
                  I would highly appreciate your support in finding the reason why the trail stop is not triggered on the right data series.

                  Thanks again!
                  Attached Files

                  Comment


                    #10
                    Hello elnoppo,

                    Thank you for your reply.

                    Could you provide your current version of your strategy?

                    Have you made the change suggested in my previous post to restrict your trade entries to the primary data series? Any stop or target submitted along with the entry will also be submitted to that same data series.

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

                    Comment


                      #11
                      Hi Kate,

                      I messed up some of the entries. Now it is working like it should.

                      Thank you very much for your support.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by helpwanted, Today, 03:06 AM
                      1 response
                      7 views
                      0 likes
                      Last Post sarafuenonly123  
                      Started by Brevo, Today, 01:45 AM
                      0 responses
                      7 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
                      384 views
                      1 like
                      Last Post Gavini
                      by Gavini
                       
                      Working...
                      X