Test Studies to help Learn IntraBarPersist

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
bowlesj3
Posts: 2028
Joined: 21 Jul 2007
Has thanked: 174 times
Been thanked: 405 times

Test Studies to help Learn IntraBarPersist

Postby bowlesj3 » 16 Nov 2009

Understanding Intrabarpersist is important when programming MC EL. Specifically understanding what happens with it being on a variable and without it being on a variable. These are studies I used to learn how it works.

All studies can be cut and paste out to a new study form and compiled clean for use.

Here is a very good test to study.
I left intrabarstatus off for a few items.
it is run on 10 second bars of the e-mini ES
There are three print statements in the study.
The first is a spacer to make it easier to read.
The 2nd and 3rd print statements are exactly the same.
the "BN = BarNumber;" is stuck right in between the 2nd and 3rd prints.
This test tells you several things.

1/ As of 2009/Nov you can not test barstatus = 0 to detect the start of a bar because there is a bug. Instead you have to use time_s compared to its previous value.

2/ When you run it study the BN variables and how the variable during the first tick of a new bar has the old barnumber value until the move command "BN = BarNumber;" takes place. This explains what they mean in the help for the IntraBarPersist command.

3/ It shows that BN and BN[0] are exactly the same thing in memory because the move command "BN = BarNumber;" is only used on BN yet BN[0] gets its value immediately.

4/ Getting back to observation #2 above, notice how the BN is different between the print statements because of the move which is between them. this difference stays the same for every single tick within the bar. However if you put IntraBarPersist on BN it changes and the BN value is only different between the pair of printouts for the first tick. Give it a try (redo it with intrabarpersist and keep both printouts to compare them). This explains what IntraBarPersist does with an actual example.

One more note. Before this test I was thinking that variables without IntraBarPersist get their current bar table value reset to their initial value at the start of the study with each new tick. This test shows their current bar table value gets set to the previous bar's value at the start of the study.

Code: Select all

{A_TestBarNUmber}

var: marker("");
{used in the first study to mark the last tick of the bar in the printout - make it easier to analyse}


var: IntraBarPersist TimePrev(0);
{needed to be sure we have a new bar since BarStatus = 0 is not trustable}

var: BN(0);
{BarNumber, a variable to store bar number across ticks}
{study it in the print out carefully to fully understand how inclusion and exclusion of IntraBarPersist effects your run.}
{In other words run this study once with BN not having IntrabarPersist then run it again with BN having Intrabarpersist and study the differences between the printouts very carefully}
 
 
If LastBarOnChart_s then
   Begin
 
   marker = "";
   If Barstatus = 2 then
      marker = "LastTick";
   if Time_s > TimePrev then
      marker = "FirstTick";
   TimePrev = Time_s;

 
   Print( File("C:\ALog_A_BobTestNoIntraBarPersist.txt"),
             " ");
 
   Print( File("C:\ALog_A_BobTestNoIntraBarPersist.txt"),
 
              " Barstatus", " " , 
             Barstatus , " " , 
              
              " BarNumber", " " ,
             BarNumber , " " ,
             
              " BN", " " ,
             BN, " " ,
 
              " BN[0]", " " ,
             BN[0], " " ,
 
               " BN[1]", " " ,
             BN[1], " " ,
 
              " BN[2]", " " ,
             BN[2], " " ,

             Marker, " " ,
 
           " ");

   BN = BarNumber;
   
    Print( File("C:\ALog_A_BobTestNoIntraBarPersist.txt"),
 
              " Barstatus", " " , 
             Barstatus , " " , 
              
              " BarNumber", " " ,
             BarNumber , " " ,
             
              " BN", " " ,
             BN, " " ,
 
               " BN[0]", " " ,
             BN[0], " " ,
 
              " BN[1]", " " ,
             BN[1], " " ,
 
              " BN[2]", " " ,
             BN[2], " " ,
 
             Marker, " " ,
 
            " ");
 
    
   End; {If LastBarOnChart_s then }

Attachments
A_TestBarNumber.zip
(1.82 KiB) Downloaded 294 times
Last edited by bowlesj3 on 21 Sep 2011, edited 4 times in total.

bowlesj3
Posts: 2028
Joined: 21 Jul 2007
Has thanked: 174 times
Been thanked: 405 times

Postby bowlesj3 » 16 Nov 2009

Here is another study that helps understand Intrabarpersist.

Notes for trying it:

2/ each print statement has a different marker "Here1 & here2", Here3 and Here4, Here5 and Here6. They are grouped in pairs based upon the if statements.

3/ I indented the print statements such that when you look at the print file you clearly see where a new minute is being processed.

Special Note. If you set your indicator as "update on every tick", not having IntraBarPersist on a variable will cause the variable to be zeroed on each new tick up until the last tick of the bar (barstatus = 2) even if the variable is NumericSimple. You can see if it is numericsimple by putting your cursor over the variable in the definition area. To check this put in a second count called TestCnt2 and put in a statement like "TestCnt = TestCnt2[1]". You will see that the cursor over TestCnt2 shows it as NumericSeries rather than NumericSimple. TestCnt2 would have an internal table the size of MaxBarsBack. I was not aware of that when I did a lot of these tests. However the scripts still show the effect of IntraBarPersist.

Code: Select all

{A_TestCnt constantly reset back on each tick. =======================================================}


variables:
   testcnt(0);
if LastBarOnChart then
   begin

   if testcnt = 2 then
      begin
          Print( File("C:\ALog_2.txt"),
                         "         Here5", " " ,
                          " Time", " " ,
                         Time, " " ,
                          " TestCnt", " " ,
                         testcnt, " " ,
                      " ");
      testcnt = testcnt  + 1;
          Print( File("C:\ALog_2.txt"),
                            "         Here6", " " ,
                             " Time", " " ,
                            Time, " " ,
                             " TestCnt", " " ,
                            testcnt, " " ,
                         " ");
 
      end;
 
 
   if testcnt = 1 then
      begin
          Print( File("C:\ALog_2.txt"),
                         "     Here3", " " ,
                          " Time", " " ,
                         Time, " " ,
                          " TestCnt", " " ,
                         testcnt, " " ,
                      " ");
      testcnt = testcnt  + 1;
          Print( File("C:\ALog_2.txt"),
                            "     Here4", " " ,
                             " Time", " " ,
                            Time, " " ,
                             " TestCnt", " " ,
                            testcnt, " " ,
                         " ");
 
      end;
 

   if testcnt = 0 then
      begin
          Print( File("C:\ALog_2.txt"),
                         " Here1", " " ,
                          " Time", " " ,
                         Time, " " ,
                          " TestCnt", " " ,
                         testcnt, " " ,
                      " ");
      testcnt = testcnt  + 1;
          Print( File("C:\ALog_2.txt"),
                            " Here2", " " ,
                             " Time", " " ,
                            Time, " " ,
                             " TestCnt", " " ,
                            testcnt, " " ,
                         " ");
 
      end;




   end;

 



Using MC2.1.999.999 I changed the above code to have the IntraBarPersist reserve word. Now as you can see the minute times for all prints are showing it is the same intraday bar and the count is being incremented.

Code: Select all

 Here1  Time 921.00  TestCnt  0.00 
 Here2  Time 921.00  TestCnt  1.00 
     Here3  Time 921.00  TestCnt  1.00 
     Here4  Time 921.00  TestCnt  2.00 
         Here5  Time 921.00  TestCnt  2.00 
         Here6  Time 921.00  TestCnt  3.00 


Here is what happens without the "IntraBarPersist".

Code: Select all

 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
 Here1  Time 927.00  TestCnt  0.00 
 Here2  Time 927.00  TestCnt  1.00 
     Here3  Time 928.00  TestCnt  1.00 
     Here4  Time 928.00  TestCnt  2.00 
     Here3  Time 928.00  TestCnt  1.00 
     Here4  Time 928.00  TestCnt  2.00 
     Here3  Time 928.00  TestCnt  1.00 
     Here4  Time 928.00  TestCnt  2.00 
     Here3  Time 928.00  TestCnt  1.00 
     Here4  Time 928.00  TestCnt  2.00 
     Here3  Time 928.00  TestCnt  1.00 
     Here4  Time 928.00  TestCnt  2.00 
Attachments
A_TestCnt.zip
(1.36 KiB) Downloaded 241 times
Last edited by bowlesj3 on 22 Sep 2011, edited 5 times in total.

bowlesj3
Posts: 2028
Joined: 21 Jul 2007
Has thanked: 174 times
Been thanked: 405 times

Postby bowlesj3 » 16 Nov 2009

One thing that can cause initial confusion with MC's EL is the fact that the TL_new, Arw_New and Text_New commands keep having their Line/arrow/Text removed from the chart on the next tick up until the last tick of the bar when BarStatus = 2 occurs. Therefore you have to ensure that the whatever_New command keeps getting executed until BarStatus = 2. If there are 10 ticks in a bar and the conditions needed to put out the line only occur on tick #5 but not after that then you have to set on a flag to indicate that the command is to keep executing until the last tick of the Bar. The flag should have IntraBarPersist on it so it will not be set back to "N".


Code: Select all

Var: IntraBarPersist Line_Needed("N");
Var: LINE_ID(0);

If LastBarOnChart_s then
    begin

    {condition occurs on tick #5 telling MC to put out a new line}
    {On ticks #6 to #10 the condition does not exist}
    {therefore we set Line_Needed = "Y";       }
    if ticks = 5 then
         Line_Needed = "Y";

     if Line_Needed = "Y" then
       begin
       LINE_ID = TL_new_s
                     (Date,StartTime,StartVal,Date,EndTime,EndVal);
      if BarStatus = 2 then
      Line_Needed = "N";

    end: {If LastBarOnChart_s then}



Notice that the LINE_ID does not have IntraBarPersist. It is actually better that it not have it because you want it set back to zero on the next tick because the line has been removed. Once BarStatus = 2 is set on the last tick of the bar LINE_ID will hold its value. You may want to use that value to make changes to the line or delete it later.

bowlesj3
Posts: 2028
Joined: 21 Jul 2007
Has thanked: 174 times
Been thanked: 405 times

Postby bowlesj3 » 19 Jun 2010

Here is a very good way to explain what happens with and without intrabarpersist.

Lets say you have defined a variable.
MyClose(0).
and the MaxBarsBack is 2.
MyClose is actually represented like this in memory.

MyClose[2]
MyClose[1] (Note: this is the previous bar)
MyClose[0] (Note: this is the current bar}


if you have only one statement in your study like this
MyClose = Close;
what is actually going on is this.

Code: Select all

MC does this just before your study starts.
   If IntraBarPersist is not defined with MyClose then
       MyClose[0] = MyClose[1];
       
   Execute User Code
      MyClose = Close; {this is the same as: MyClose[0] = Close}
   User Code is done:

MC Executes this code just after your study finishes   
    If BarStatus = 2 then
        begin
        MyClose[2] = MyClose[1];
        MyClose[1] = MyClose[0];
        end;
(however MC will use a loop of course)



That is it. I did not specify whether you have defined MyClose with or without the IntraBarPersist command because all that would do is change the execution of the first if statement that MC has.

The above code (if you understand it) explains what the help says for IntraBarPersist. Unfortunately the help does not have a good example of this to explain what happens if you do not use IntraBarPersist (specifically that, without that command, your variable keeps getting reset back to the previous bar's value up until BarStatus = 2).

bowlesj3
Posts: 2028
Joined: 21 Jul 2007
Has thanked: 174 times
Been thanked: 405 times

Postby bowlesj3 » 21 Jun 2010

I created a more elaborate version of the above logic (see attached zip file) which takes into account the arrival of ticks to MC (and the charts) as well as processing of charts in sequence for each tick arrival (currently charts appear to be processed in random order).

Special note: To help understand the Ticks reserve word command, I pretend that MC is placing the tick number on the top right corner of the chart. I also explain what I figure the ticks reserve word is doing internally.

It is good to have all this thought out when writing EL code.

Special Note. If you set your indicator as "update on every tick", not having IntraBarPersist on a variable will cause the variable to be zeroed on each new tick up until the last tick of the bar (barstatus = 2) even if the variable is NumericSimple. You can see if it is numericsimple by putting your cursor over the variable in the definition area. To check this put in a second count called TestCnt2 and put in a statement like "TestCnt = TestCnt2[1]". You will see that the cursor over TestCnt2 shows it as NumericSeries rather than NumericSimple. TestCnt2 would have an internal table the size of MaxBarsBack. I was not aware of that when I did a lot of these tests. However the scripts still show the effect of IntraBarPersist. The zip document below pertains to variables that are defined as NumericSeries.
Attachments
MC_Execution_Logic_IntraBarPersist.zip
(3.74 KiB) Downloaded 433 times
Last edited by bowlesj3 on 22 Sep 2011, edited 3 times in total.

k-hen
Posts: 8
Joined: 08 Dec 2009

Postby k-hen » 22 Jun 2010

Thanks!

oliveagle
Posts: 20
Joined: 07 Jul 2011
Has thanked: 4 times
Been thanked: 4 times

Re: Test Studies to help Learn IntraBarPersist

Postby oliveagle » 08 Sep 2011

thx a lot. reading...


Return to “User Contributed Studies and Indicator Library”