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

IsFirstBarOfSessionByIndex stored to an Array

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

    IsFirstBarOfSessionByIndex stored to an Array

    Hey guys,

    How would I store the first bar of each session in an array for n previous days? I would think this should be possible without the sessions iterator. Here are some loops I've tried, some i haven't yet, but maybe you can give me an idea of what I'm doing wrong?

    #region Relative Volume ================================================== ==============

    if(Bars.IsFirstBarOfSessionByIndex(CurrentBar))
    {counter++;} //---------------------------> Get total amount of days loaded on chart

    Array.Resize(ref firstBarArr, counter);//-----------------------> Make room for new data

    for(int i = 0; i < counter; i++)//-------------------------------------------------------------------------->This has been tested and returns the last index value of IsFirstBarOfSession
    {
    if(Bars.IsFirstBarOfSessionByIndex(CurrentBar))
    {firstBarArr[i]=CurrentBar;}//--------------> Add each opening candle of day # to array
    }

    //Not Tested
    //================================================== =========
    foreach(int m in firstBarArr)
    {
    if(Bars.IsFirstBarOfSessionByIndex(CurrentBar))
    {firstBarArr[m]=CurrentBar;}
    }

    //Not Tested
    //================================================== =========
    for(int e = 0; e < CurrentBar; e++){
    for(int f = 0; f < counter; f++){
    if(Bars.IsFirstBarOfSessionByIndex(e)){
    firstBarArr[f]=e;}}}

    //Not Tested
    //================================================== =========
    for(int e = 0; e < CurrentBar; e++){
    foreach(int f in firstBarArr){
    if(Bars.IsFirstBarOfSessionByIndex(e)){
    firstBarArr[f]=e;}}}

    //Not Tested
    //================================================== =========
    for(int e = 0; e < CurrentBar; e++){
    if(Bars.IsFirstBarOfSessionByIndex(e)){
    for(int f = 0; f < counter; f++){
    firstBarArr[f]=e;}}}

    #2
    Hello drumguytx,

    Thank you for your post.

    I think you're making this a bit overly complicated - you don't need a loop to populate an array with this information. I've attached a really simple strategy that grabs the index of each of the first bar of the session for all days loaded on a chart. Here's essentially what I've done:
    Code:
            private    int[] firstBarArr = new int[1];
            private    int Counter = 1;
    
            protected override void OnBarUpdate()
            {
    
                if(Bars.IsFirstBarOfSession)  //will return true if this is the first bar of the session that we're currently processing
                {
                    // Make room for new data
                    Array.Resize(ref firstBarArr, Counter);
                    //assign this bar to our array
                    firstBarArr[Counter - 1] = CurrentBar;
                    //increment counter for next time
                    Counter++;
    
                }
             }
    Note that I've added a little extra code that prints the contents of the array on each bar, so you can see what happens.

    Please let us know if we may be of further assistance to you.
    Attached Files
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Sheesh, I did overcomplicate that. Thanks so much Kate!

      Comment


        #4
        Guys, I'm having a hard time calculating relative volume. I've got it working on historical data but it's not computing on the current day. This is not an indicator but calculated through the strategy, if that makes any difference. So to make it easier and convenient for the end user I added AddPlot(Brushes.Transparent, "Rel Vol"). When in real-time the RV value shows "1" on all current day candles but the previous days it's working perfect.

        The variable RVinput is set to 1 by default but can be overridden by the user in the default settings. Is it where in the code I'm initializing the variables or where (re)setting them to 0? Also, how would I filter out half trading days?

        Here's my code, can someone please help me? Thanks in advance!



        int Xdays = 10;
        double RV = 0;
        int delta = 0;
        int RVinput = 1;
        double todayVolume, totalVol;


        protected override void OnBarUpdate(){

        todayVolume = 0;
        totalVol = 0;

        if(Bars.IsFirstBarOfSession){
        Array.Resize(ref firstBarArr, counter);//-------------------> Make room for new data
        firstBarArr[counter - 1] = CurrentBar;//--------> Add each opening candle # to array
        counter++;}//----------------------------------------------------> Increment counter

        delta = CurrentBar - firstBarArr[firstBarArr.Length - 1]; ---->Grab the number of bars of the current day for RV calc

        //---------> Today's total volume
        for(int w = firstBarArr[firstBarArr.Length - 1]; w < CurrentBar; w++){
        todayVolume += Bars.GetVolume(w);}

        if(firstBarArr.Length - 1 > Xdays){//-------->Make sure there are more days on chart than Xdays
        for(int x = 0; x < Xdays; x++){//--------------------> Iterate through days and sum
        for(int y = firstBarArr[firstBarArr.Length - 2 - x]; y < firstBarArr[firstBarArr.Length - 2 - x] + delta; y++){
        totalVol += Bars.GetVolume(y);}}}

        avgVolXdays = totalVol/Xdays;
        RV = todayVolume/avgVolXdays;
        Values[0][0] = RV;
        }

        Comment


          #5
          Hello drumguytx,

          Thank you for your reply.

          While we can't debug your code for you in the Platform Support department, I would suggest that adding prints to your strategy that will show in the NinjaScript Output window, with information on what the variables you're using for your conditions are on a particular bar can be helpful.

          This forum post goes into great detail on how to use prints to help figure out where issues may stem from - this should get you going in the correct direction.

          https://ninjatrader.com/support/foru...ns-not-working

          If you run into issues like we saw here, the above information will allow you to print out all values used that may be evaluating differently from what you'd expect. With the printout information you can assess what is different between the two. I'd be happy to look at your resulting prints (if you right click on the NinjaScript Output window and click "Save As" you can save this information as a .txt file you can attach to your reply) if you can't find where the issue may stem from once you've added them.

          Please let us know if we may be of further assistance to you.
          Kate W.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by GwFutures1988, Today, 02:48 PM
          1 response
          5 views
          0 likes
          Last Post NinjaTrader_Clayton  
          Started by ScottWalsh, 04-16-2024, 04:29 PM
          6 responses
          30 views
          0 likes
          Last Post ScottWalsh  
          Started by frankthearm, Today, 09:08 AM
          10 responses
          36 views
          0 likes
          Last Post frankthearm  
          Started by mmenigma, Today, 02:22 PM
          1 response
          3 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by NRITV, Today, 01:15 PM
          2 responses
          9 views
          0 likes
          Last Post NRITV
          by NRITV
           
          Working...
          X