Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Enter in reverse order in strategy

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

    Enter in reverse order in strategy

    Hello,

    I'm making a strategy and I'm interested in opening a contrary position if the SL jumps. I'm having several problems:

    If I do it manually, I have no way to open the position at the same point where the order closed. It opens the order at the beginning or end of the M5 candle

    Code:
    SubmitOrderUnmanaged(0, orderAction, OrderType.Market, lots);
    To find that precision, I have to launch the simulator with tick precision (Order fill resolution = High). But in that case, sometimes I get an error when opening the order:

    Code:
    ordenCompraStop = SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.StopMarket, lotes, 0, maxVelaGatillo + TicksEntry * TickSize, sOCO, "CompraStop");
    
    //I get this error
    Strategy 'Test': Error on calling 'OnBarUpdate' method on bar 45: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
    ​
    // If I log, it tells me that there is no information for BarsArray[1]
    12:42:40 - [Test.OnBarUpdate] -     Create order BUY STOP BarsArray[0].Count: 276 BarsArray[1].Count: 0


    I think it's because there is no tick information on older periods.

    In this sample, you can see the second order after SL triggers at begin of M5 candle

    Click image for larger version

Name:	image.png
Views:	189
Size:	12.2 KB
ID:	1327275

    In this sample, is OK

    Click image for larger version

Name:	image.png
Views:	106
Size:	24.9 KB
ID:	1327276​​


    Is there some kind of order I can use so that if the SL is executed, it automatically opens a counter order?​

    How can I test this behaviour?




    Thanks

    #2
    Hello Powerbucker,

    The error you are seeing is a general error letting you know you tried to access data at an incorrect time. To address that you would need to make sure you are trying to do that action after enough bars passed. For example if you are getting 1 bar ago prices you need to wait at least 1 bar before being able to do that. In a multi series script you would need to wait for the series being used to have enough data.

    Comment


      #3
      Hi,

      why if I execute the analyzer with Order fill resolution = Standard, it works? The same code, when I execute with Order fill resolution = High, fails.

      And

      Is there some kind of order I can use so that if the SL is executed, it automatically opens a counter order?​

      How can I test this behaviour?​

      Thanks

      Comment


        #4
        Hello Powerbucker,

        I wouldnt be able to say by just looking at the code, you would have to debug it in that use case to see what specifically is happening and which line is having an error. You can use prints to help identify where in the code the problem is.

        Comment


          #5
          Yes, i told you what line and what error in the first post

          Comment


            #6
            Hello Powerbucker,

            From the code provided I don't see any bars ago being used so you may need to re debug the situation to find what series is being used when you get that error. The error you are seeing is a bars ago error. Printing the count of series won't be relevant for processing, you would need to check what the CurrentBar is for both series being used and then make sure you are using valid bars ago on the series where you use them. Additionally to use high fill resolution your script cannot have a secondary series, if you are using a secondary series the only option is to use standard fill resolution.

            Comment


              #7
              Hi,

              I think you aren't understanding me.

              My strategy only use 1 series (M5 chart series).When I execute the analyzer with Standard resolution every thing is ok.
              And there is only one serie (BarsArray[0])


              But when I execute with high resolution, this line throws an error

              Code:
              ordenCompraStop = SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.StopMarket, lotes, 0, maxVelaGatillo + TicksEntry * TickSize, sOCO, "CompraStop");
              Then I can see, the strategy has 2 series

              Code:
              // If I log, it tells me that there is no information for BarsArray[1]
              12:42:40 - [Test.OnBarUpdate] - Create order BUY STOP BarsArray[0].Count: 276 [B]BarsArray[1].Count: 0[/B]​
              I think, when strategy is executed with high resolution (ticks) it adds ticks series. But, when I execute the analyzer on old days (1 year ago) there isn't ticks info. Because if I execute the test on last week, for example, it goes OK.

              Got it?

              And my finally question: does exist an SL order that open reversal order when fires?

              Comment


                #8
                Hello Powerbucker,

                If you are using High fill resolution you dont need to make any code changes or check BarsArray[1], that is not part of how that system works. That only applies to fills, it does not change how your OnBarUpdate logic runs.

                No a stoploss does not exist for reversals, you need to do that process yourself using logic.

                Comment


                  #9
                  Ok, then is it possible the cause of error is there isn't tick info from a lot of bars ago? Is there any way to load this info?

                  Comment


                    #10
                    Hello Powerbucker,

                    I wouldn't be able to say what the problem is because I don't see any reason for that in the line of code that you provided, you may need to use prints and see where specifically the code execution stops when you see the error, that will give a better idea of what the problem may be. A bars ago error generally deals with series like Close Open High or Low.

                    Comment


                      #11
                      I have the index out of range error for my strategy too. I tried every method to address this issue. Finally, instead of finding out the root cause, I use a workaround. It is try catch block in the link below. Just need to figure out what the code need to do when the error happens.

                      Indicator: Using Try-Catch Blocks - NinjaTrader Support Forum

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Today, 05:17 AM
                      0 responses
                      23 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      120 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      63 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      41 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      45 views
                      0 likes
                      Last Post TheRealMorford  
                      Working...
                      X