Got excited with new static keyword

i want to use it in places where i need to pass arrays between my backtester to my Report Charts.
I have the static keyword on top of my backtesting code.
for eg:
backtester code has the following

 static cntTradesWithoutPullback;
      static cntTradesWithPullback;
       static sumRewardWithPullback , sumMaeWithPullback , sumMfeWithPullback;
  cntTradesWithoutPullback=5;

Report chart needs to access this static variable. I get error

#pragma enable_static_decl(prefix) 
EnableTextOutput( 3 ); // enable HTML output into report (Version 5.84 or higher!)
function GenAnalysisTableHTML( )
{
       printf( "<tr bgcolor='#eeffee' >\n" );
     printf( "<td><b>" +"Classification"  + "</b></td>" );
      printf( "<td><b>" +"Trades"  + "</b></td>" );
          printf( "<td><b>" +"MFE"  + "</b></td>" );
               printf( "<td><b>" +"MAE"  + "</b></td>" );
                 printf( "<td><b>" +"AveProfits"  + "</b></td>" );
     
 printf( "</tr>\n" );
       printf( "<tr>\n" );
     printf( "<td><b>" + "Mfe with Pullback"  + "</b></td>" );
     printf( "<td><b>" +NumToStr( cntTradesWithPullback, 1.0 )  + "</b></td>" );
          printf( "<td><b>" +NumToStr( sumMfeWithPullback / cntTradesWithPullback, 1.3 )  + "</b></td>" );
               printf( "<td><b>" +"MAE"  + "</b></td>" );
                 printf( "<td><b>" +"AveProfits"  + "</b></td>" );
     
 printf( "</tr>\n" ); 
printf( "</table>\n" );  
    
    
   
 }   

////////////////////////////
// Main program
////////////////////////////
GenAnalysisTableHTML();

@suresh,

read following threads entirely

as well as here

And your specfic question is answered in first mentioned thread here with example


So in first code (of your 1st AFL) you are missing

#pragma enable_static_decl(suresh_test1) 

so there do write:

#pragma enable_static_decl(suresh_test1) 

static _cntTradesWithoutPullback, _cntTradesWithPullback;
static _sumRewardWithPullback, _sumMaeWithPullback, _sumMfeWithPullback;

_cntTradesWithoutPullback=5;

// ....

(It is good practice to use (e.g.) underscore before static variable name to distinguish static variables from standard ones as recommended in first link by @Tomasz in 2nd post there).


And in second code of your post (in your 2nd AFL) your are missing

static _cntTradesWithoutPullback, _cntTradesWithPullback;
static _sumRewardWithPullback, _sumMaeWithPullback, _sumMfeWithPullback;

So there do write:

#pragma enable_static_decl(suresh_test1) 

static _cntTradesWithoutPullback, _cntTradesWithPullback;
static _sumRewardWithPullback, _sumMaeWithPullback, _sumMfeWithPullback;

// further code below and calling static variables of first AFL

Alternativally in 2nd AFL you may use

prefix = "suresh_test1";

_cntTradesWithoutPullback = StaticVarGet( prefix + "_cntTradesWithoutPullback" );

// etc.

BTW, is your previous thread solved? No feedback so far after having time spent to answer to it.

4 Likes

this clarifies it a lot. I believe the userguide chm file needs to be updated with more examples escpecially the section with static keyword.

@fxshrat you are a awesome help. the other issue is sorted now.

As I said.... please read mentioned threads entirely. Because then you can read there that that specific feature is marked as private API so it won't come with further documentation (as of yet).

To allow faster evolution and freedom of experimenting without “legacy” baggage some APIs (AFL functions but in the future not only that) will be marked as “Private APIs”.

Private APIs

  • don’t come with backward compatibility guarantees
  • they are subject to (even drastic) change from version to version
  • come without any help, support, hand-holding, spoon feeding, extra education except written documentation in the readme/release notes/manual
  • come “as is”, use them at your own risk/discretion
  • have target audience of advanced users only, able to understand all complexities required

All functionality provided by #pragma directive is now marked as Private API.

Link:

and at top of thread

CHANGES FOR VERSION 6.26.0 (as compared to 6.25.0)

  1. AFL: new static keyword: declare identifier as static variable – a little ‘revolution’ in static variable use, declare variable as static and use as ‘regular’ variable, no need to call functions - this is Private API(1)

Documentation of that private API static declaration feature in the users guide:
https://www.amibroker.com/guide/keyword/static.html

1 Like