Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Update Mehtod

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

    Update Mehtod

    I have a primary and secondary bar series and a data series that contains all of contains all of the closes of the primary bar series apart from index 0 which is the close of the secondary bar series.

    I am calling the LinReg method on my DataSeries in the OnBarUpdate of the secondary bar series but it is not updating. How would I call the updtae method to force it to update?

    This is a code sample
    Code:
    // update fastClose series with the same number of elements needed in our indicator and with the current price in index 0
    						for (int i = 1; i <= sdPeriod; i++) 
    						{
    							fastClose.Set(i, Closes[0][i]);
    						}
    
    // always set index 0 of fastCLose to the currnet price
    					fastClose.Set(0, Closes[1][0]);
    
    // THIS IS WHAT I NEED UPDATED
    double answer = LinReg(fastClose,20)[0];
    					Print(" BarNumber " + CurrentBars[1] + "  FastClose LinReg 20 Period = " + answer + " Current Price " + Closes[1][0]);

    #2
    Hello GKonheiser,

    I suspect the items are not being synced. Can you clarify on what is not updating?

    I used the code below and I see the proper values:
    Code:
                if (CurrentBars[1] == 0)
    			{
    				fastClose = new DataSeries(SMA(BarsArray[1], 50));
    			}
    			
    			if (BarsInProgress == 0)
    			{
    				fastClose[0] = Closes[0][0];
    			}
    			if (BarsInProgress == 1)
    			{
    				fastClose[0] = Closes[1][0];
    			}
    			
    			if (BarsInProgress == 0 && fastClose.ContainsValue(0))
    			{
    				Print(Times[0][0] + " " + fastClose[0] + " " + LinReg(fastClose,20)[0]);
    			}
    My output was the following:
    ...
    4/4/2016 7:45:00 AM 2063.5 1995.03928571429
    4/4/2016 7:46:00 AM 2063.25 1996.55357142857
    4/4/2016 7:47:00 AM 2063 1996.09642857143
    4/4/2016 7:48:00 AM 2062.75 1996.96071428571
    4/4/2016 7:49:00 AM 2062.75 1996.15357142857
    4/4/2016 7:50:00 AM 2063 1994.63571428571
    4/4/2016 7:51:00 AM 2062.25 1996.62142857143
    4/4/2016 7:52:00 AM 2062 1998.10714285714
    4/4/2016 7:53:00 AM 2062.25 1996.83214285714
    4/4/2016 7:54:00 AM 2063 1995.63214285714
    4/4/2016 7:55:00 AM 2064.5 1991.71428571429
    4/4/2016 7:56:00 AM 2064 1993.33571428571
    4/4/2016 7:57:00 AM 2064 1994.25357142857
    4/4/2016 7:58:00 AM 2063.5 1995.45
    4/4/2016 7:59:00 AM 2064.25 1993.79285714286
    4/4/2016 8:00:00 AM 2064.75 1991.01071428571
    4/4/2016 8:01:00 AM 2064.5 1992.53571428571
    4/4/2016 8:02:00 AM 2065 1991.2

    Comment


      #3
      if I have a data series based on primary bars apart from index 0 which is the close of 1 tick bars. When I call LinReg on my custom series after I have called Set on my dataseries updating index 0 with the current tick I should get a different result if the price at index 0 is different but I dont, I only see a change after the primary bars update?

      Comment


        #4
        GKonheiser,

        That is correct as the Data Series is synced to that primary series.

        Comment


          #5
          Ok so how can I update the indicator as and when I need it ?

          Comment


            #6
            GKonheiser,

            Does the following code not do as needed?
            Code:
            			if (BarsInProgress == 0)
            			{
            				fastClose[0] = Closes[0][0];
            			}
            			if (BarsInProgress == 1)
            			{
            				fastClose[0] = Closes[1][0];
            			}

            Comment


              #7
              No. Let me maybe explain what I am doing so you have a better idea.

              When you run a strategy on live data with calculate on bars on close to false and call LinReg on primary bars (Closes[0]) , the data series it is looking at , Closes[0], contains the closes for the primary bar series at every index except index 0 which contains the current price, ie its updated with every tick. this means that if the price changes then the LinReg changes.

              I am trying to replicate the same behaviour using historical data for testing purposes. So I build my data series, FastClose, which contains Primary Bar closes except for index 0 which I set at every tick, my secondary bar series, to reflect the current price. When I then call LinReg on my data series, FastClose, it should change if the current price has changed. At the moment it is not so I need a way to force it to update?

              Comment


                #8
                The code below is performing in the manner desire for me historically and in real-time.
                Originally posted by NinjaTrader_PatrickH View Post
                Code:
                            if (CurrentBars[1] == 0)
                			{
                				fastClose = new DataSeries(SMA(BarsArray[1], 50));
                			}
                			
                			if (BarsInProgress == 0)
                			{
                				fastClose[0] = Closes[0][0];
                			}
                			if (BarsInProgress == 1)
                			{
                				fastClose[0] = Closes[1][0];
                			}
                			
                			if (BarsInProgress == 0 && fastClose.ContainsValue(0))
                			{
                				Print(Times[0][0] + " " + fastClose[0] + " " + LinReg(fastClose,20)[0]);
                			}

                Comment


                  #9
                  That is with tick bars being BarsArray 0 and the higher order bars being BarsArray 1 . How can i do it if I have tick bars as BarsArray 1 and higher order bars(10 min bars) as barsArray 0 ?

                  Comment


                    #10
                    Hi Patrick,

                    I have changed my strategy so the primary bars are 1 Tick bars and the secondary bars are the higher order bars that are the base of my own data series that I am calculating LinReg from. It is correct that it is now updating with every tick but is it not the same value as if I called LinReg on the Closes of my Higher order bars.


                    The expected behavior is that LinReg(Closes[1], x) == LinReg(MyDataSeries, x); , when calculate on bar close is set to false.

                    Here is a code example of what im seeing:

                    /
                    Code:
                    / min bars
                    				if(BarsInProgress == baseBars)
                    				{
                    					// set fastClose Data series to basebars
                    					if(firstBarProcessed == false)
                    					{
                    						// so not to process again
                    						firstBarProcessed = true;
                    						
                    						// data series that includes baseBar closes and the current price in index 0
                    						fastClose = new DataSeries(SMA(BarsArray[baseBars], 50));
                    					}
                    					
                    					
                    					// ensure fast Series reflect closes for base bars form index 1 back
                    					fastClose.Set(1, Closes[baseBars][1]);
                    }
                    
                    
                    if(BarsInProgress == calcBars && deviationSet && firstBarProcessed)
                    				{
                    							 
                    					
                    					//always set FastClose data Series index 0 to the current price
                    					fastClose.Set(0, Closes[calcBars][0]);
                    					
                    					if(fastClose.ContainsValue(0))
                    					{
                    						Print("*********");
                    						for (int i = 0; i < 20; i++) 
                    						{
                    							Print(" FaatClose[" + i + "] = " + fastClose[i]);
                    						}
                    						Print("Time: " + Time[0] + " Close[calcBars][1] " + Closes[1][1] + " FastClose[0] " + fastClose[0] + " Lin reg " + LinReg(fastClose,20)[0]);
                    						for (int i = 0; i < 20; i++) 
                    						{
                    							Print(" Closes[baseBars][" + i + "] = " + Closes[1][i]);
                    						}
                    						
                    						Print("Time: " + Time[0] + " Close[calcBars][1] " + Closes[1][1] + " FastClose[0] " + fastClose[0] + " Lin reg " + LinReg(Closes[1],20)[0]);
                    						Print("*********");
                    					}
                    And an example of different results despite the same input.
                    *********
                    FaatClose[0] = 163.38
                    FaatClose[1] = 163.36
                    FaatClose[2] = 163.4
                    FaatClose[3] = 163.35
                    FaatClose[4] = 163.34
                    FaatClose[5] = 163.39
                    FaatClose[6] = 163.43
                    FaatClose[7] = 163.41
                    FaatClose[8] = 163.38
                    FaatClose[9] = 163.53
                    FaatClose[10] = 163.57
                    FaatClose[11] = 163.56
                    FaatClose[12] = 163.51
                    FaatClose[13] = 163.53
                    FaatClose[14] = 163.5
                    FaatClose[15] = 163.47
                    FaatClose[16] = 163.46
                    FaatClose[17] = 163.43
                    FaatClose[18] = 163.43
                    FaatClose[19] = 163.41
                    Time: 01/04/2016 09:37:01 Close[calcBars][1] 163.36 FastClose[0] 163.38 Lin reg 163.365285714286
                    Closes[baseBars][0] = 163.38
                    Closes[baseBars][1] = 163.36
                    Closes[baseBars][2] = 163.4
                    Closes[baseBars][3] = 163.35
                    Closes[baseBars][4] = 163.34
                    Closes[baseBars][5] = 163.39
                    Closes[baseBars][6] = 163.43
                    Closes[baseBars][7] = 163.41
                    Closes[baseBars][8] = 163.38
                    Closes[baseBars][9] = 163.53
                    Closes[baseBars][10] = 163.57
                    Closes[baseBars][11] = 163.56
                    Closes[baseBars][12] = 163.51
                    Closes[baseBars][13] = 163.53
                    Closes[baseBars][14] = 163.5
                    Closes[baseBars][15] = 163.47
                    Closes[baseBars][16] = 163.46
                    Closes[baseBars][17] = 163.43
                    Closes[baseBars][18] = 163.43
                    Closes[baseBars][19] = 163.41
                    Time: 01/04/2016 09:37:01 Close[calcBars][1] 163.36 FastClose[0] 163.38 Lin reg 163.386
                    *********
                    Last edited by GKonheiser; 04-06-2016, 07:19 AM.

                    Comment


                      #11
                      Hello GKonheiser,

                      Thank you for your response.

                      For the next items I want to establish the following:
                      Primary series is 1 Minute
                      Added (secondary) series is 1 Tick

                      If we check the values of fastClose on the close of the Primary series we have yet to let the Secondary series close and therefore the closing tick has yet to be added to our custom data series.

                      So checking the values under BarsInProgress = 0 would result in the values not matching. Creating a secondary series and attempting to save each tick to that series will cause the series to have different values. The issue is caused by an order of operations problem. The first tick of BarsInProgress - 1, is occurring before a new bar is created for the custom data series synchronized with the primary series. The first tick that occurs after a new primary bar is being set as the last tick of the secondary series previous bar.
                      So to prevent this, when the first tick of the new bar occurs, set the custom series to the previous tick's values.

                      Attached is an example that should assist you in this process.
                      Attached Files

                      Comment


                        #12
                        Morning Patrick,

                        I understand what you are saying, we talked about this via email. In the previous example you sent to solve the problem of the LinReg on a custom DataSeries updating on every tick you have the primary bars as 1 Tick and the Secondary bars as the higher order bars.

                        This was your example:-

                        if (CurrentBars[1] == 0)
                        {
                        fastClose = new DataSeries(SMA(BarsArray[1], 50));
                        }

                        if (BarsInProgress == 0)
                        {
                        fastClose[0] = Closes[0][0];
                        }
                        if (BarsInProgress == 1)
                        {
                        fastClose[0] = Closes[1][0];
                        }

                        if (BarsInProgress == 0 && fastClose.ContainsValue(0))
                        {
                        Print(Times[0][0] + " " + fastClose[0] + " " + LinReg(fastClose,20)[0]);
                        }
                        So then the question is if I have 1 Tick Bars as my primary bars and 1 min bars as my secondary and CalculateOnBarsClose == False, how can I check for the close of the 1 min bars in order to set my custom series correctly , is there a flag I can look for?

                        Comment


                          #13
                          Hello GKonheiser,

                          Thank you for your response on this matter.

                          There is no flag to call, you would need to create your own. In the sample from my prior post you will find the following code:
                          Code:
                          			if (BarsInProgress == 0)
                          			{
                          				// set primaryClosed to 0 so BIP 1 knows the primary just closed
                          				primaryClosed = 0;
                          			}
                          And then...
                          Code:
                          			else if (BarsInProgress == 1)
                          			{
                          				// the first tick of the new primary bar
                          				if (primaryClosed == 0)
                          				{

                          Comment


                            #14
                            That wont work when I have CalculateOnBarClose == false; so I am going to use FirstTickOfBar, that should be OK right ?

                            Comment


                              #15
                              Originally posted by GKonheiser View Post
                              That wont work when I have CalculateOnBarClose == false; so I am going to use FirstTickOfBar, that should be OK right ?
                              Correct, just keep in mind when FirstTickOfBar is true the Close[1] is the last closed bar.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              558 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              324 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              101 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              545 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              547 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X