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

Strategy triggering before BarsRequiredToTrade

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

    Strategy triggering before BarsRequiredToTrade

    I am using a T3 moving average with the following inputs:

    T32b2 = T3(Closes[2], 8, 3, 0.7);

    so I set up

    BarsRequiredToTrade = 9

    as well as

    protected override void OnBarUpdate()
    {

    if (CurrentBars[0] < BarsRequiredToTrade
    || CurrentBars[2] < BarsRequiredToTrade)
    return;


    but the strategy triggers at the first condition after enabling and sometimes it works, but many times I get the error: " Error on calling 'EventHandlerBarsUpdate' method: Object reference not set to an instance of an object." in the output window, the strategy stops and the Log also shows: "Indicator EMA: Error on calling OnBarUpdate method on bar xxxx: You are trying to access an index with a value that is invalid since it is out of range. I E accessing a series [barsAgo] with an value of 5, when there are only 4 bars on the chart"

    Please see the output prints:

    Enabling NinjaScript strategy 'ATM2/178977585' : 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=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=True CancelExitsOnStrategyDisable=True Calculate=On each tick IsUnmanaged=False MaxRestarts=4 in 5 minutes
    Strategy start bar: 5130 - 12/11/2019 15:39:00
    ++++++++++
    Long condition at: 57.46 - Current Ask: 57.46 - S = 1 - Open: 57.44 - Current Bar: 5131 @ 12/11/2019 15:40:00
    'GetAtmStrategyEntryOrderStatus' method error: Order ID 'fbd7b92182f649d6a3eba9406bcf9cf5' does not exist
    'GetAtmStrategyEntryOrderStatus' method error: Order ID 'fbd7b92182f649d6a3eba9406bcf9cf5' does not exist
    'GetAtmStrategyEntryOrderStatus' method error: Order ID 'fbd7b92182f649d6a3eba9406bcf9cf5' does not exist
    'GetAtmStrategyEntryOrderStatus' method error: Order ID 'fbd7b92182f649d6a3eba9406bcf9cf5' does not exist
    'GetAtmStrategyEntryOrderStatus' method error: Order ID 'fbd7b92182f649d6a3eba9406bcf9cf5' does not exist
    'GetAtmStrategyEntryOrderStatus' method error: Order ID 'fbd7b92182f649d6a3eba9406bcf9cf5' does not exist
    'GetAtmStrategyEntryOrderStatus' method error: Order ID 'fbd7b92182f649d6a3eba9406bcf9cf5' does not exist
    Long ATM triggered at: 57.45 - Open: 57.44 - Current Bar: 5131 - Long order bar: 5131 - 12/11/2019 15:40:00
    Last edited by itrader46; 11-12-2019, 09:41 AM.

    #2
    Hello itrader46,

    This is likely that you are not using a complete condition to check for enough bars, you are only checking the first and third series.

    Rather than using the BarsRequiredToTrade here I would suggest that you look through your existing logic and determine what the maximum BarsAgo used was. Lets assume that was 5, you could then make a condition like the following which includes ALL series used:

    Code:
    if (CurrentBars[0] < 5
    [B]|| CurrentBars[1] < 5[/B]
    || CurrentBars[2] < 5)
    return;
    The BarsRequiredToTrade property is intended to be the amount of bars you want to wait before it places a trades, this causes an ignored order and log notification if your script tried to place a trade before that point. This is not quite the same as making sure enough data is present and serves a different purpose. After your data check (the code above) you could make another check if you wanted to make sure it has enough bars on that series to place a trade to avoid warnings (applies only to managed approach):

    Code:
    if(CurrentBar < BarsRequiredToTrade) return; // checking the current processing bar
    Can you check that adding a condition which covers all series you used makes a difference?


    I look forward to being of further assistance.
    Last edited by NinjaTrader_Jesse; 11-13-2019, 08:29 AM. Reason: making it more clear what BarsRequiredToTrade applies to
    JesseNinjaTrader Customer Service

    Comment


      #3
      Series 1 is a Tick, so it should definitely be enough bars in it within a few seconds, but I'll try that

      Comment


        #4
        The code below made no difference

        Code:
        else if (State == State.Configure)                
                        {
                            AddDataSeries(Data.BarsPeriodType.Tick, 1);
                            AddDataSeries(Data.BarsPeriodType.Minute, 5);
                        }
        ...
        protected override void OnBarUpdate()
                {
                   if (CurrentBars[0] < 9 || CurrentBars[1] < 9 || CurrentBars[2] < 9)
                        return;
        
                    if (CurrentBar < BarsRequiredToTrade)
                        return;
        ...
        else if (State == State.DataLoaded)
        
                        T31b2                = T3(Closes[2], 5, 3, 0.7);
                        T32b2                = T3(Closes[2], 8, 3, 0.7);
                        T31b0            = T3(Closes[0], 5, 3, 0.7);
                        T32b0            = T3(Closes[0], 8, 3, 0.7);
                        AddChartIndicator(T31b0);
                        AddChartIndicator(T32b0);
                        AddChartIndicator(T31b2);
                        AddChartIndicator(T32b2);
        These are the prints:

        Enabling NinjaScript strategy 'ATM2/178977600' : On starting a real-time strategy ...blah blah
        Strategy start bar: 5872 - 25/10/2019 05:01:00
        ----------
        Short condition at: 55.91 - Current Bid: 55.91 - R = 1 - Open: 55.95 - Current Bar: 5901 @ 25/10/2019 05:31:00
        Short ATM triggered at: 55.91 Open: 55.95 - Current Bar: 5901 Short order bar = 5901 - 25/10/2019 05:31:00

        Since the Series2 is 5 minutes, the strategy shouldn't have triggered before 45 after the start, but the first order is after 29 minutes

        I am testing in Market Replay to avoid waiting for 45 minutes to see what happens and I didn't have the errors now, but they did not come every time anyway

        Correction: on the next run, the error came back:

        Strategy start bar: 5872 - 25/10/2019 05:01:00
        ----------
        Short condition at: 55.91 - Current Bid: 55.91 - R = 1 - Open: 55.95 - Current Bar: 5901 @ 25/10/2019 05:31:00
        Short ATM triggered at: 55.91 Open: 55.95 - Current Bar: 5901 Short order bar = 5901 - 25/10/2019 05:31:00
        ##########
        XX<<-- Short condition false price: 55.91 - Trigger Short Price: 55.91 - Current Bar: 5902 - 25/10/2019 05:32:00
        Short ATM cancel cond 2 price: 55.88 - Trigger Short Price: 55.91 - Open: 55.89 - Current Bar: 5931 - 25/10/2019 06:01:00
        <<< Short Order ID Reset Price: 55.88 - Open: 55.89 - Current Bar: 5931 - 25/10/2019 06:01:00
        <<## Short ATM ID Reset Price: 55.88 - Open: 55.89 - Current Bar: 5931 - 25/10/2019 06:01:00
        ----------
        Short condition at: 55.88 - Current Bid: 55.88 - R = 1 - Open: 55.89 - Current Bar: 5931 @ 25/10/2019 06:01:00
        Short ATM triggered at: 55.88 Open: 55.89 - Current Bar: 5931 Short order bar = 5931 - 25/10/2019 06:01:00
        ##########
        Cond False Short Reset - Current Bid: 55.88 - Current Bar: 5931
        XX<<-- Short condition false price: 55.88 - Trigger Short Price: 55.88 - Current Bar: 5931 - 25/10/2019 06:01:00
        Cond False Short Reset - Current Bid: 55.88 - Current Bar: 5931
        XX<<-- Short condition false price: 55.88 - Trigger Short Price: 55.88 - Current Bar: 5931 - 25/10/2019 06:01:00
        Cond False Short Reset - Current Bid: 55.86 - Current Bar: 5935
        XX<<-- Short condition false price: 55.86 - Trigger Short Price: 55.88 - Current Bar: 5936 - 25/10/2019 06:06:00
        <<< Short Order ID Reset Price: 55.85 - Open: 55.86 - Current Bar: 5942 - 25/10/2019 06:12:00
        <<## Short ATM ID Reset Price: 55.9 - Open: 55.89 - Current Bar: 5959 - 25/10/2019 06:29:00
        ++++++++++
        Long condition at: 55.91 - Current Ask: 55.91 - S = 1 - Open: 55.89 - Current Bar: 5959 @ 25/10/2019 06:29:00
        Long ATM triggered at: 55.91 - Open: 55.89 - Current Bar: 5959 - Long order bar: 5959 - 25/10/2019 06:29:00
        ##########
        XX>>-- Long condition false price: 55.91 - Trigger Long Price: 55.91 - Current Bar: 5960 - 25/10/2019 06:30:00
        Long ATM cancel cond 1 price: 55.88 - Trigger Long Price: 55.91 - Open: 55.9 - Current Bar: 5961 - 25/10/2019 06:31:00
        >>> Long Order ID Reset Price: 55.88 - Open: 55.9 - Current Bar: 5961 - 25/10/2019 06:31:00
        >>## Long ATM ID Reset Price: 55.88 - Open: 55.9 - Current Bar: 5961 - 25/10/2019 06:31:00
        ++++++++++
        Long condition at: 55.92 - Current Ask: 55.92 - S = 1 - Open: 55.9 - Current Bar: 5988 @ 25/10/2019 06:58:00
        Long ATM triggered at: 55.92 - Open: 55.9 - Current Bar: 5988 - Long order bar: 5988 - 25/10/2019 06:58:00
        ##########
        Cond False Long Reset - Current Ask: 55.92 - Current Bar: 5988
        XX>>-- Long condition false price: 55.92 - Trigger Long Price: 55.92 - Current Bar: 5989 - 25/10/2019 06:59:00
        Cond False Long Reset - Current Ask: 55.94 - Current Bar: 5991
        XX>>-- Long condition false price: 55.94 - Trigger Long Price: 55.92 - Current Bar: 5991 - 25/10/2019 07:01:00
        >>> Long Order ID Reset Price: 55.95 - Open: 55.94 - Current Bar: 5992 - 25/10/2019 07:02:00
        Strategy 'ATM2': Error on calling 'EventHandlerBarsUpdate' method: Object reference not set to an instance of an object.

        The previous run prints:

        Long condition at: 55.92 - Current Ask: 55.92 - S = 1 - Open: 55.9 - Current Bar: 5988 @ 25/10/2019 06:58:00
        Long ATM triggered at: 55.92 - Open: 55.9 - Current Bar: 5988 - Long order bar: 5988 - 25/10/2019 06:58:00
        ##########
        Cond False Long Reset - Current Ask: 55.92 - Current Bar: 5988
        XX>>-- Long condition false price: 55.92 - Trigger Long Price: 55.92 - Current Bar: 5989 - 25/10/2019 06:59:00
        Cond False Long Reset - Current Ask: 55.94 - Current Bar: 5991
        XX>>-- Long condition false price: 55.94 - Trigger Long Price: 55.92 - Current Bar: 5991 - 25/10/2019 07:01:00
        >>> Long Order ID Reset Price: 55.95 - Open: 55.94 - Current Bar: 5992 - 25/10/2019 07:02:00
        >>## Long ATM ID Reset Price: 56.04 - Open: 56.08 - Current Bar: 6061 - 25/10/2019 08:11:00
        ....

        I 've made no changes between the last two runs
        Last edited by itrader46; 11-12-2019, 10:52 AM.

        Comment


          #5
          Hello itrader46,

          Thank you for trying that. If the solution was not simply a missed condition you may instead need to debug your logic from here to find the line in error. I could suggest to start by reducing your code to find what specific logic is being called when you see the error as that would help to better understand what solution is needed. Once you know what line of code is causing the error we could re asses why that is happening based on the syntax used there.

          The prints you provided are not specifically helpful in identifying what line is generating the error only that there was some bars processed before the problem. You could additionally add some prints like Print("HERE") to see where in your logic it is stopping to try and isolate this further. If you have a small sample of the indicators/syntax required to see the error I would be happy to give that a test as well.


          I look forward to being of further assistance.

          JesseNinjaTrader Customer Service

          Comment


            #6
            OK, but how am I supposed to debug a code that sometimes it works and sometimes it doesn't? It may not be a specific code line, coz otherwise it would do it every time. You can see below, it did it now after one hour...

            Strategy start bar: 5872 - 25/10/2019 05:01:00
            ----------
            Short condition at: 55.91 - Current Bid: 55.91 - R = 1 - Open: 55.95 - Current Bar: 5901 @ 25/10/2019 05:31:00
            Short ATM triggered at: 55.91 Open: 55.95 - Current Bar: 5901 Short order bar = 5901 - 25/10/2019 05:31:00
            ##########
            XX<<-- Short condition false price: 55.91 - Trigger Short Price: 55.91 - Current Bar: 5902 - 25/10/2019 05:32:00
            Short ATM cancel cond 2 price: 55.88 - Trigger Short Price: 55.91 - Open: 55.89 - Current Bar: 5931 - 25/10/2019 06:01:00
            <<< Short Order ID Reset Price: 55.88 - Open: 55.89 - Current Bar: 5931 - 25/10/2019 06:01:00
            <<## Short ATM ID Reset Price: 55.88 - Open: 55.89 - Current Bar: 5931 - 25/10/2019 06:01:00
            Strategy 'ATM2': Error on calling 'EventHandlerBarsUpdate' method: Object reference not set to an instance of an object.
            Disabling NinjaScript strategy 'ATM2/178977704'

            Also, why is it executing before 9 x Series2 bars (45 minutes)? Anyway, that doesn't seem to be the cause of the error, as in my previous post you can see the error occurred

            I uploaded the strategy so that you can extract what you want and test it. I am using a custom indicator, but I suppose you can replace that with anything you want
            Last edited by itrader46; 11-12-2019, 03:49 PM.

            Comment


              #7
              Hello itrader46,

              OK, but how am I supposed to debug a code that sometimes it works and sometimes it doesn't? It may not be a specific code line, coz otherwise it would do it every time. You can see below, it did it now after one hour...
              In some cases that may be part of the debugging process, not all problems will be easily reproducible and may require some of your time to figure out. In this case because the problem is variable my suggestion would still be to try and find a way to isolate the problem first. It sounds like you are doing that by using playback however it may take some time to find a specific situation to test with over and over. It is very likely still the same code causing the problem it just may be that doing different tests have subtle differences which allow the error to happen in different points.


              I uploaded the strategy so that you can extract what you want and test it. I am using a custom indicator, but I suppose you can replace that with anything you want
              I took a quick look at the strategy however this is not currently in a state which I could see what the problem is.

              As you noted this includes third party indicators so I cannot run this in the same way to test it. You also noted that you can replace the custom indicators with stock indicators while testing. This is mainly only going to be helpful for once you have actually isolated the syntax required so you know when you make that change if it affected the problem or not. I do suggest doing this however at this point without knowing if that is affecting the result that would not be helpful.

              For the existing prints you have, In what you provided the error comes after a specific print, that could potentially be used as a guide to try and isolate what logic comes next from that point to better understand the problem.



              I look forward to being of further assistance.






              JesseNinjaTrader Customer Service

              Comment


                #8
                OK, but none of this answers the topic of this thread: why is it triggering before the BarsRequiredToTrade?

                Comment


                  #9
                  Hello itrader46,

                  The BarsRequiredToTrade is not a hard limit which prevents logic, this will only ignore orders which were submitted before that number and will still allow your logic to execute. Your script should start working immediately from bar 0 if the logic allows for it. In your original post you were using this as a limit in the condition you made however you were not using it for all series, series 2 was able to continue execution which could cause problems.

                  I would suggest to continue to find a situation which reliably reproduces the problem, you can try using the playback mode and gradually reduce the data used each time you see the error. If you reduce the data and it no longer appears, try moving the starting point back by the amount of the reduction. This type of reducing could help to further isolate the steps/data needed to see the problem. Once you can semi reliably reproduce the problem adding more Prints into the logic would be a quick way to further isolate what region of code causes the error.


                  I look forward to being of further assistance.


                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    The BarsRequiredToTrade is not a hard limit which prevents logic, this will only ignore orders which were submitted before that number and will still allow your logic to execute. Your script should start working immediately from bar 0 if the logic allows for it. In your original post you were using this as a limit in the condition you made however you were not using it for all series, series 2 was able to continue execution which could cause problems.
                    As you can see from my post #4, I have followed your advice from post #2, but it still triggers immediately, even though, according to you, it shouldn't have

                    Comment


                      #11
                      Hello itrader46,

                      The script should trigger immediately if your conditions allow for it. Additionally BarsRequiredToTrade is not an ATM method, this applies to the managed approach orders in case you are expecting this to control the ATM orders in some way, I updated my prior post to make it more clear that this applies only to the managed approach.

                      The ATM orders specifically work on their own outside of your strategy and only have a few basic methods which your strategy can use to interact with them. The managed approach methods and properties will not apply to ATM's, you can read more about the ATM methods here: https://ninjatrader.com/support/help...gy_methods.htm

                      In regard to the error, if adding the condition was not a solution you would need to continue to debug the script to find the syntax which contributes to the problem. Once we know what syntax is being used at that time and have a general way to see the issue we could review that further to find a solution.



                      Please let me know if I may be of further assistance.
                      Last edited by NinjaTrader_Jesse; 11-13-2019, 08:30 AM.
                      JesseNinjaTrader Customer Service

                      Comment


                        #12
                        OK, forget about the BarsRequiredToTrade.

                        I thought the code below meant the conditions for the script to run are not met before the 9th bar?

                        Code:
                         protected override void OnBarUpdate()        
                        {          
                         if (CurrentBars[0] < 9 || CurrentBars[1] < 9 || CurrentBars[2] < 9)                
                        return;

                        Comment


                          #13
                          Hello itrader46,

                          This would prevent the logic below that point from being run before 9 bars had elapsed.





                          Please let me know if I may be of further assistance.
                          JesseNinjaTrader Customer Service

                          Comment


                            #14
                            Exactly! So why is it running, then?

                            If the script is running when it shouldn't, that makes me think it is possibly some bug in NT8, otherwise how can it be explained?

                            Maybe this is what causes the errors as well!
                            Last edited by itrader46; 11-13-2019, 02:51 PM.

                            Comment


                              #15
                              Hello itrader46,

                              From the prints you provided I cannot see anything that indicates this was happening before 9 bars, the prints included show the CurrentBar in the 5000's so I am not certain what you are referring to here. If something was happening before 9 bars, your CurrentBar print should show that as less than 9. If you have a specific sample of what you are referring to here, please extract that from the overall script and provide just that information as a sample. We could go over that alone to better understand that concept.

                              Because the previous sample was complex and there is not a known way to reproduce the problem with that sample you would need to debug the situation further before we can continue. I am not able to debug the script for you so this is a process you would need to go through to find the general syntax related to the problem. Once that is known, if it is not apparent why there is a problem with that syntax we review that simplified version to understand it.


                              Please let me know if I may be of further assistance.
                              JesseNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by pkefal, 04-11-2024, 07:39 AM
                              11 responses
                              36 views
                              0 likes
                              Last Post jeronymite  
                              Started by bill2023, Yesterday, 08:51 AM
                              8 responses
                              43 views
                              0 likes
                              Last Post bill2023  
                              Started by yertle, Today, 08:38 AM
                              6 responses
                              25 views
                              0 likes
                              Last Post ryjoga
                              by ryjoga
                               
                              Started by algospoke, Yesterday, 06:40 PM
                              2 responses
                              24 views
                              0 likes
                              Last Post algospoke  
                              Started by ghoul, Today, 06:02 PM
                              3 responses
                              16 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Working...
                              X