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

Custom Spread Indicator does not work properly on same instrument different expirati

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

    Custom Spread Indicator does not work properly on same instrument different expirati

    Hello,
    I am testing an indicator which calculates the spread between two instruments.

    The indicator works fine when is used on two different Futures contracts. However when used on the same future contract (with different expiration) does not work.

    Or in other words the following pairs:


    NQ 09-17 and TF 09-17 - OK
    ES 09-17 and NQ 09-17 - OK

    ES 09-17 and ES 12-17 - NOT Ok (Indicator is blank)

    One more thing, I disabled the historical data merging for ES in order to be able to properly populate the chart of the ES 12-17 (because it was merging the data upon any refresh).

    After I did that, ES stopped working with other instruments as well e.g. ES 09-17 and NQ 09-17 - NOT ok. Before the merge disable they were working.

    I have no idea what is happening, please help!

    Here is the code to the indicator.




    protected override void Initialize()
    {
    Add(SecondSymbol, PeriodType.Minute, SecondMinutes);
    Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Dot, "Spread"));
    Overlay= false;
    }

    protected override void OnBarUpdate()
    {
    Plot0.Set(Closes[0][0]-Closes[1][0]);
    }

    #2
    Hello nikolaalx,

    Thanks for your post.

    With the policy of mergebackadjusted, ES 12-17 would load up the same historical data as you see on ES 09-17 and then would connect to the live data of ES 12-17 which would likely result in a long bar connecting the two different prices of the two different ES contracts.

    With the policy of DoNotMerge, ES 12-17 would show only the historical data related to ES 12-17 which will be scarce data compared to the current contract ES 09-17. In this mode each instrument will only show its own historical data.

    Can you define/clarify what you mean by "does not work.". When you run the indicator and it does not work, do you see any error messages listed in the "Log" tab of the control center? If no errors, can you provide a screenshot with both contracts and the indicator applied?
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      When I run it on ES 09-17 / ES 12-17 with Do Not Merge policy, the log shows the following error:

      "
      Error on calling 'OnBarUpdate' method for indicator 'SpreadIndicator' on bar 0: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
      "

      By "Does not work" I mean that the indicator is loading a new panel, which is completely blank. Nothing is drawn neither historically nor in real time.

      When I run the same indicator for e.g. NQ 09-17 / TF 09-17 it plots everything without any problems.

      Comment


        #4
        Hello nikolaalx,

        Thanks for your reply.

        In general when a script (strategy or indicator) is not functioning as expected a quick check of the "log" tab would show any "run time" type errors. These are errors that cannot be found when compiling and would occur during the start-up or running of the script.

        In the case of the error, it is advising that a bar does not exist at a moment when requested. Each data series can call the OnbarUpdate and it s possible then when the first dataseries calls OnBarUpdate() that the other dataseries has not yet loaded it first bar

        In this code:
        protected override void OnBarUpdate()
        {
        Plot0.Set(Closes[0][0]-Closes[1][0]);
        }


        You can provide a check to ensure that you have data before processing. Also, in multiseries scripts, each script can call OnBarUpdate() so you may also want to restrict the processing to just one bar series, in this case as a plot is involved it should be the chart bars series (which would be the first data series listed in the data series window). For example:

        protected override void OnBarUpdate()
        {
        if (CurrentBars[0] < 1 || CurrentBars[1] < 1) return; // go no further until we have at least one bar in each dataseries

        if (BarsInProgress != 0) return; // only process plot when called by chart bar data series

        Plot0.Set(Closes[0][0]-Closes[1][0]);
        }


        References:


        Attached Files
        Paul H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by kujista, Today, 05:44 AM
        0 responses
        4 views
        0 likes
        Last Post kujista
        by kujista
         
        Started by ZenCortexCLICK, Today, 04:58 AM
        0 responses
        5 views
        0 likes
        Last Post ZenCortexCLICK  
        Started by sidlercom80, 10-28-2023, 08:49 AM
        172 responses
        2,281 views
        0 likes
        Last Post sidlercom80  
        Started by Irukandji, Yesterday, 02:53 AM
        2 responses
        18 views
        0 likes
        Last Post Irukandji  
        Started by adeelshahzad, Today, 03:54 AM
        0 responses
        8 views
        0 likes
        Last Post adeelshahzad  
        Working...
        X