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

Using strategy code inside indicators?

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

    Using strategy code inside indicators?

    I was able to use the strategy builder to get going, then manually edit to finish.
    As a strategy the code works great.
    I can't make it work as an indicator.
    (BoxTime & BoxSize are declared for the indicator just like the strategy)

    All of the code I inserted (outside the indicator wizard) is shown below.
    It displays nothing when deployed / enabled as indicator.
    No errors at deployment on chart, no errors during compile.

    MUCH thanks in advance.
    Sean



    protected override void Initialize()
    {
    CalculateOnBarClose = false;
    Overlay = true;
    DrawOnPricePanel = true;
    }

    protected override void OnBarUpdate()
    {
    if (Open[0] < Close[1])
    {
    RemoveDrawObjects();
    DrawRectangle("DownBox", false, BoxTime, Low[0], -4, Low[0] + BoxSize * TickSize, Color.IndianRed, Color.IndianRed, 2);
    }

    if (Open[0] > Open[1])
    {
    BoxSize = BoxSize * -1;
    RemoveDrawObjects();
    DrawRectangle("BoxUp", false, BoxTime, High[0], -4, High[0] - BoxSize * TickSize, Color.PaleGreen, Color.PaleGreen, 2);
    }
    }

    #2
    Hello,

    Check the log tab of the Control Center, you will likely find errors in yellow hinting at the problem.

    When working with strategies, it does not start processing until the min. bars required have been met (default of 20). However your indicator starts working on the first bar on the chart (bar 0)

    Where you're referencing Open[1] and Close[1], this value does not exist on the first bar on the chart and will throw an error if you check the log tab of the Control Center saying the "index is out of range".

    To prevent this from happening, you can simply add the following check at the beginning of your OnBarUpdate() method:

    Code:
    protected override void OnBarUpdate()
    {
    
    [B]if(CurrentBar < 1)
    return;[/B]
    
    if (Open[0] < Close[1])
    {
    
    //... the rest of your code here ...
    More information on this can be found below:

    MatthewNinjaTrader Product Management

    Comment


      #3
      NinjaTrader_Matthew
      Thank you, I didn't know about the control center log.
      You are correct there is an out of range error.
      It doesn't make sense.

      Some additional info:
      1. Range chart @ 8 ticks
      2. 100+ bars showing / processed / history on the chart.

      The log says Bar 0 is out of range.
      Do I need to access the current bar in some other way?

      Thanks
      Sean

      Comment


        #4
        The out of range error is happening on bar 0 as it cannot reference Close[1] from the bar before.

        Were you able to add the Current Bar check to your code? This should resolve the error and allow it to calculate correctly.
        MatthewNinjaTrader Product Management

        Comment


          #5
          I am probably not doing a good job of explaining things.
          Attached is the strategy that works. (RangeBox.cs)
          Attached is the indicator that doesn't work. (BoxTracking.cs)

          Range chart of 8 ticks.
          It has plenty of bars to use for calculation.

          Before code changed recommended:
          >Error accessing index out of range

          After code change:
          >DrawRectangle startBarsAgo out of valid range 0 through 1

          No errors are created when I start the strategy.

          I am lost, thank you for the help.
          Best regards,
          Sean
          Attached Files

          Comment


            #6
            Hi John,

            Thanks for the additional clarification.

            It's a similar issue, however since you're trying to draw back on your "BoxTime" variable, you'll need to escpate this sequence until it's possible as well. So instead of CurrentBar < 1, you can just use if(CurrentBar < BoxTime) return;


            Please let me know if you need further assistance.
            MatthewNinjaTrader Product Management

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by TraderBCL, Today, 04:38 AM
            2 responses
            17 views
            0 likes
            Last Post TraderBCL  
            Started by martin70, 03-24-2023, 04:58 AM
            14 responses
            106 views
            0 likes
            Last Post martin70  
            Started by Radano, 06-10-2021, 01:40 AM
            19 responses
            609 views
            0 likes
            Last Post Radano
            by Radano
             
            Started by thanajo, 05-04-2021, 02:11 AM
            4 responses
            471 views
            0 likes
            Last Post tradingnasdaqprueba  
            Working...
            X