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

Multi-Time Frame and development question

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

    Multi-Time Frame and development question

    according to the help guide (https://ninjatrader.com/support/help...nstruments.htm), especially under the example " Accessing the Price Data in a Multi-Bars NinjaScript", it is my current understanding that if you need an update from the secondary data series, you have to use the if (BarsInProgress == 1) repeatedly?

    To further illustrate my point, let's say you want to manually calculate the correlation value for YM and ES, and therefore you need values from both data series to compute different parts of the formula. This include calculating the standard deviations for both YM and ES. To do this, we can use a for-loop sampling 20 data points of Close values to calculate the mean first

    total = 0
    for (int x = 0, x < 20, x++) // calculate mean for YM
    {

    total = Close[0] + total;

    }

    YMmean = total / 20;


    total = 0
    for (int x = 0, x < 20, x++) // calculate mean for ES
    {

    total = Closes[1] + total;

    }

    ESmean = total / 20;


    I see that the above does not recognize the ES data series even though it is written as "Closes[1]".

    So I tried re-writing it based on BarsInProgress as such:

    if (BarsInProgress == 0)
    {
    total = 0
    for (int x = 0, x < 20, x++) // calculate mean for YM
    {

    total = Close[0] + total;

    }

    YMmean = total / 20;

    }

    if (BarsInProgress == 1)
    {

    total = 0
    for (int x = 0, x < 20, x++) // calculate mean for ES
    {

    total = Closes[1] + total;

    }

    ESmean = total / 20;
    }


    then it reads the values for ES. So does this mean that whenever we need values for the secondary data, we have to put in if (BarsInProgress == 1) every time? does a typical strategy only have the different sections once? like above, or does it have multiple sections of the same secondary index? For example

    if (BarsInProgress == 0)
    {
    do some calculation here for primary data series
    }


    if (BarsInProgress == 1)
    {
    do some calculation here for secondary data series
    }

    if (BarsInProgress == 0)
    {
    do some calculation here for primary data series AGAIN
    }

    Last edited by Boonfly8; 11-25-2019, 08:07 AM.

    #2
    Hello Boonfly8,

    Thanks for your post.

    OnBarUpdate will update for each data series added to the script. You can differentiate which data series is updating by checking the BarsInProgress index.

    If the script is processing the primary data series, BarsInProgress will equal 0. If OnBarUpdate is processing the first added data series, BarsInProgress will be 1. BarsInProgress does not change when OnBarUpdate is processing.

    If you reference Close[0], you are referencing the Close value for the data series that is processing in OnBarUpdate. I.E. Closes[BarsInProgress][0] == Close[0].

    Using the plural version of the a Price Series will allow you to reference a specific data series' OHLC data. For example: Closes[1][0] represents the Close of the secondary series and the reference would be the same when the script is processing BarsInProgress == 0 or BarsInProgress == 1.

    Further examples can be seen on the Closes and similar documentation pages.



    Please let me know if you have any questions.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi Jim,

      Thank you for your reply. I tried a lot of different things and reviewed the output logs regarding this, and at this time, I would like some confirmation from you based on my observations.

      I have written a simple code to test quotes as follows:

      protected override void OnBarUpdate()
      {
      if (CurrentBars[0] <= BarsRequiredToTrade || CurrentBars[1] <= BarsRequiredToTrade)
      return;


      //if(BarsInProgress ==0)
      {

      YMClose = Close[0];
      Print(Time[0] + " YM Close Update: " + YMClose);
      //Print(Time[0] + "ES Close: " + Closes[1][0]);

      }

      //if(BarsInProgress ==1)
      {

      ESClose = Closes[1][0];
      //Print(Time[0] + "YM Close: " + Close[0]);
      Print(Time[0] + " ES Close Update: " + ESClose);

      }

      Print(Time[0] + "");
      Print(Time[0] + " YM Close: " + YMClose + " ES Close: " + ESClose );
      Print(Time[0] + "");





      }

      with the above, I get the following output:
      11/19/2019 2:21:00 AM YM Close Update: 28065
      11/19/2019 2:21:00 AM ES Close Update: 3127
      11/19/2019 2:21:00 AM
      11/19/2019 2:21:00 AM YM Close: 28065 ES Close: 3127
      11/19/2019 2:21:00 AM
      11/19/2019 2:21:00 AM YM Close Update: 28065
      11/19/2019 2:21:00 AM ES Close Update: 3127
      11/19/2019 2:21:00 AM
      11/19/2019 2:21:00 AM YM Close: 28065 ES Close: 3127
      11/19/2019 2:21:00 AM
      11/19/2019 2:21:00 AM YM Close Update: 3127
      11/19/2019 2:21:00 AM ES Close Update: 3127
      11/19/2019 2:21:00 AM
      11/19/2019 2:21:00 AM YM Close: 3127 ES Close: 3127
      11/19/2019 2:21:00 AM
      11/19/2019 2:21:00 AM YM Close Update: 3126.75
      11/19/2019 2:21:00 AM ES Close Update: 3126.75
      11/19/2019 2:21:00 AM
      11/19/2019 2:21:00 AM YM Close: 3126.75 ES Close: 3126.75

      notice how the YM close values and the ES close values are the same, which is obviously wrong. This is a version where I am commenting out //if(BarsInProgress ==0) and //if(BarsInProgress ==1).
      Last edited by Boonfly8; 11-26-2019, 08:40 AM.

      Comment


        #4
        Hello Boonfly8,

        In your first example, you have the BarsInProgress check removed. All of your logic will be processed by all data series.

        Code:
        YMClose = Close[0];
        Print(Time[0] + " YM Close Update: " + YMClose);
        When the code reaches this it will print the close of the data series that is processing since you are using Close[0] and not Closes[][]. Close[0] can be YM or ES depending on the data series that is currently processing in OnBarUpdate.

        Close[0] (Not Plural) always reflects the data series that is processing. Closes[][] (Plural) always reflects the data series you specify. I.E. Closes[0][0] always reflects the primary data series while Closes[1][0] always reflects the secondary series, etc.

        Please let me know if you have any questions.
        JimNinjaTrader Customer Service

        Comment


          #5
          hi Jim, thank you for your explanation. So if one to use Closes[0][0] and Closes[1][0] to do calculations such as mine, do one even need to specify BIP[0] and BIP[1]?

          Comment


            #6
            Hello Boonfly8,

            Closes[1][0] will reflect the secondary data series regardless of what the BarsInProgress index is.

            Closes[0][0] will reflect the primary data series regardless of what the BarsInProgress index is.

            Let us know if you have any questions.
            JimNinjaTrader Customer Service

            Comment


              #7
              Hi Jim,

              If Closes[0] and Closes[1][0] reflects base and secondary data without specifying If(BarsInProgress ==0) and if(BarsInProgress==1), then when sample code specifies if( BarsInProgress > 0 ) return, as a first line after OnBarUpdate, do the updates still come in from the data stream from the secondary series if the method is returned for the secondary data series? Or do updates always come in if using Closes[0][0] or Closes[1][0]?

              Thanks

              Comment


                #8
                Hello Boonfly8,

                The data still updates for each data series. A BarsInProgress check in OnBarUpdate like you have will prevent logic from processing after return; if OnBarUpdate is processing for a BarsInProgress index greater than 0. I.E. if OnBarUpdate is processing for a secondary data series, do not execute this code.

                This will not change how a Price Series updates and Closes[0][0] and Closes[1][0] will have their same function and will receive their updates.

                If the matter is not fully clear, I suggest including BarsInProgress with your prints so you can visualize how each data series updates in OnBarUpdate.

                Please let us know if we can be of further assistance.
                JimNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by RookieTrader, Today, 09:37 AM
                3 responses
                15 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by kulwinder73, Today, 10:31 AM
                0 responses
                5 views
                0 likes
                Last Post kulwinder73  
                Started by terofs, Yesterday, 04:18 PM
                1 response
                23 views
                0 likes
                Last Post terofs
                by terofs
                 
                Started by CommonWhale, Today, 09:55 AM
                1 response
                4 views
                0 likes
                Last Post NinjaTrader_Erick  
                Started by Gerik, Today, 09:40 AM
                2 responses
                7 views
                0 likes
                Last Post Gerik
                by Gerik
                 
                Working...
                X