external static variable

 

The situation is, on the initial run, I set that number on the input. After EA run, it meet certain criteria, it will reset that variable to be 0, and keep it 0 until I change the input variable again to be something else. Is there anyway to do that?

 

Hello shonick,

Yes, use static memory class to "freeze" the value assigned to a designated datatype until your program arrives at a condition which assigns the "frozen" static variable with a new value.

I almost threw my laptop Betsy out'a window once, cuz I didn't understand why my Boolean datatype were changing their values. VERY frustrating! ;)

Also, I don't believe I've been able to code an external static variable. I assume 'external' is considered 'static' by default. If it is, (which I'm sure someone will stroll by this post and confirm or deny) there shouldn't

be a reason to assign static memory type to your external variable.


Thank you

 

Common (globally declared) variables including externals and (locally declared) static retain their values between calls.

Remember, they are not reset on deinit/init cycles (e.g. chart change.) only on load (new chart, recompile, etc.) Thus I use this paradigm:

Static not reset
TYPE Function(){
   static TYPE variable = 0; // Not reset on chart change.
   if (variable == 0){ variable = ...
Resetting static
int init(){
   OnInitFunction(); OnInitFunction2 ..
   :
}
TYPE functionVariable; void OnInitFunction(){ functionVariable = 0; }
TYPE Function(){
   if (functionVariable == 0){ functionVariable = ...
 
shonick:


 

The situation is, on the initial run, I set that number on the input. After EA run, it meet certain criteria, it will reset that variable to be 0, and keep it 0 until I change the input variable again to be something else. Is there anyway to do that?


External variables are statics. Once your code set it to 0 it retains 0 until the user/terminal re-load the EA.

 
WHRoeder:

Common (globally declared) variables including externals and (locally declared) static retain their values between calls.

Remember, they are not reset on deinit/init cycles (e.g. chart change.) only on load (new chart, recompile, etc.) Thus I use this paradigm:

Static not reset
Resetting static

Based on my experiences, any datatype without declaration of static memory class is reset to zero once the condition (assigning the datatype variable a value) returns false. This is why Boolean variables almost drove me to "coding madness" when these Boolean variable values returned false once conditions returned false. Unbelievably frustrating :) Hahaha!

This must be because these datatype variable values aren't saved between program execution periods. Hmm.

 

Thank you

 
Nathan #:

Based on my experiences, any datatype without declaration of static memory class is reset to zero once the condition (assigning the datatype variable a value) returns false. This is why Boolean variables almost drove me to "coding madness" when these Boolean variable values returned false once conditions returned false. Unbelievably frustrating :) Hahaha!

This must be because these datatype variable values aren't saved between program execution periods. Hmm.

 

Thank you

Hi Nathan,

i am encountering the exact same problem you described and its driving me nuts! 

can please provide some more specific guidance on how to solve this? i'm a newbie....

appreciate!

 
JimSingadventure #: i am encountering the exact same problem you described and its driving me nuts! 

What part of “Common (globally declared) variables including externals and (locally declared) static retain their values between calls” is unclear to you?

 
William Roeder #:
What part of “Common (globally declared) variables including externals and (locally declared) static retain their values between calls” is unclear to you?

Hi William,

I guess the crux of my problem is not so much about static but more about how to structure the logic to ensure my static variable "alertswitch" always accepts only the a non-zero value.

i am doing if-then statements scenarios and whenever a condition is met, i set alerswitch to a specific positive or negative numbers ranging from -10 to 10 inside a function, afterwhich i then set it to update and define the global static variable called by the same name : "alertswitch". This variable is being manipulated  inside  my function. ....my problem is, there are cases when my if-then conditions comes to a false and so the variable alertswitch is not called and when this happens, it defaults back to 0  which then is referenced back to update the global static alertswitch variable to 0..  I want my alertswitch variable to maintain the latest non-zero value and keep it "static" until the next if-then condition changes the variable again to a different non-zero variable. i do not want it to accept a zero value.  how do i do this?   attached is my simplified code ...

i tried all kinds of things to try to 

appreciate some guidance!

static char alertswitch;
static char alertswitch1;
   if (alertswitch1 ==0) alertswitch1 = alertswitch;

alertswitch = AssessTradeOpp(alertswitch1, BBConvDivergence, TotalScore, CurrentPrice, 
 FramaCurrentPrice1, topBB1, midBB, botBB1, topBB2, botBB2, topBBDir1, botBBDir1, topBBprevious, botBBprevious, 
 DeMScore, DeMPosition1, topScalpPrice, 
 botScalpPrice, RVIScore, ATRScore, FramaScore, DXYPriceDir, DXYRVIScore, DXYATRScore, ATRCurrent1, DXYATRStrength, 
 DXYDeMScore, DXYFramaScore, DXYDeMPosition, DXYFramaCurrentPrice, DXYtopBB1,  DXYbotBB1, DXYRVIStrength, PbarHigh, PbarLow, 
 PriceDir, entryLong, entryShort, ClosePrice, addScalpLONG, addScalpSHORT, addScalpFramaLONG, addScalpFramaSHORT);

  }
  

//----------------------------------------------+
//Function to Check Entry based on Indicators 
//----------------------------------------------+
 char AssessTradeOpp ( char &alertswitch1, string BBConvDivergence, char TotalScore, double CurrentPrice, 
   double FramaCurrentPrice1,  double topBB1, double midBB, double botBB1, double topBB2, double botBB2,char topBBDir1, char botBBDir1,
   double topBBprevious, double botBBprevious,
   char DeMScore, string DeMPosition1, double topScalpPrice,  double botScalpPrice, char RVIScore, char ATRScore, double ATRCurrent1, char FramaScore,
   char DXYPriceDir, char DXYRVIScore, char DXYATRScore, string DXYATRStrength, char DXYDeMScore, char DXYFramaScore, 
   string DXYDeMPosition, double DXYFramaCurrentPrice, double DXYtopBB1,  double DXYbotBB1, string DXYRVIStrength,
    double PbarHigh, double PbarLow, 
    char PriceDir,  double &entryLong, double &entryShort, double &ClosePrice, double & addScalpLONG, double &addScalpSHORT,
    double addScalpFramaLONG, double addScalpFramaSHORT)
    {

static char alertswitch=alertswitch1;  //to define previous non-zero value from the static global variable.
string alertcomment;
char Check=0;


 // Case 1
  if (    
            PbarHigh >= topBBprevious 
            && CurrentPrice > FramaCurrentPrice1 && CurrentPrice <= addScalpFramaLONG && CurrentPrice > midBB//a little above midBB.
            && FramaCurrentPrice1 < topBB1  
            && DeMPosition1 != "DEMTOP" 
            
            && alertswitch != 1 && alertswitch !=-2  && alertswitch != 3 && alertswitch != -4 && alertswitch !=7 
            && alertswitch !=-8 && alertswitch != 9  && alertswitch != -10 && ( alertswitch ==5  ||  alertswitch==0)
       )


            {
                alertswitch = 1;
                alertswitch1= alertswitch;
                entryLong=CurrentPrice; 
                alertcomment =  " FRAMA entryLONG ";
                Alert(_Symbol, alertcomment, TotalScore, " : ", CurrentPrice, " : ", FramaCurrentPrice1, " : ", BBConvDivergence, " : ", alertswitch);
                Check=1;
            }
          
           
 
   //Case -2...
   else if (
               
               PbarLow <= botBBprevious 
               && CurrentPrice < FramaCurrentPrice1 && CurrentPrice >= addScalpFramaSHORT && CurrentPrice < midBB
               && FramaCurrentPrice1 > botBB1
               && DeMPosition1 != "DEMBOTTOM" 
               && alertswitch != -2 && alertswitch !=1 && alertswitch !=3 && alertswitch !=-4 && alertswitch !=7 
               && alertswitch !=-8 && alertswitch != 9  && alertswitch != -10 && ( alertswitch ==-6  ||  alertswitch==0)
            )
             {
                 alertswitch = -2;
                  alertswitch1= alertswitch;
                  entryShort=CurrentPrice; 
                 alertcomment =  " FRAMA entrySHORT ";
                 Alert(_Symbol, alertcomment, TotalScore, " : ", CurrentPrice, " : ",FramaCurrentPrice1, " : ", BBConvDivergence, " : ", alertswitch);
                 Check=2;
             }
         
   

   //if alertswitch == 0
   else if (!alertswitch) { 
            alertswitch = alertswitch1;
            Alert(_Symbol,  "Else if :alertswitch is NULL :   alertswitch set = alertswitch1" );
            }
//----------------------------------------------------------------+
//Trade Execution Decision Tree (from AssessTradeOpp Function)
//----------------------------------------------------------------+


switch(Check){

 case 1:
        //LONG-Entry or Close
        FnWriteFile (_Symbol,  alertcomment, alertswitch,  BBConvDivergence,  entryLong, entryShort, ClosePrice, CurrentPrice, FramaCurrentPrice1, topBB1, botBB1, PbarHigh, PbarLow, DeMPosition1, FramaScore, RVIScore, 
        DeMScore, DeMPosition1, ATRScore,ATRCurrent1,  topScalpPrice, botScalpPrice, DXYPriceDir,DXYRVIScore, DXYATRScore,DXYATRStrength, 
        TimeCurrent()); 
        break;
        
        
 case 2:
     //SHORT Entry or Close (SELL)
     FnWriteFile (_Symbol,  alertcomment, alertswitch, BBConvDivergence, entryLong, entryShort, ClosePrice, CurrentPrice, FramaCurrentPrice1, topBB1, botBB1, PbarHigh, PbarLow, DeMPosition1, FramaScore,  RVIScore, 
     DeMScore, DeMPosition1, ATRScore,ATRCurrent1, topScalpPrice, botScalpPrice, DXYPriceDir, DXYRVIScore,DXYATRScore,DXYATRStrength, 
     TimeCurrent()); 
    
     break;
 
/*====================================================================
 CLOSE POSITIONS/  EXIT ALL
         : to add info of alertswitch of which it was closing from
======================================================================
*/      
     case 125:
          
         FnWriteFile (_Symbol,  alertcomment, alertswitch, BBConvDivergence,  entryLong, entryShort, ClosePrice, CurrentPrice, FramaCurrentPrice1, topBB1, botBB1, PbarHigh, PbarLow, DeMPosition1, FramaScore, RVIScore, DeMScore,
         TotalScore, ATRScore,ATRCurrent1,  topScalpPrice, botScalpPrice, DXYPriceDir, DXYRVIScore,DXYATRScore, 
         DXYATRStrength, TimeCurrent());
        alertswitch1= alertswitch;
          break; 
       
 default: //this is my attempt to ensure zero-valued alertswitch doesnt get updated to the global static variable but it's not working...i still get 0 every now and then.
         if (!alertswitch) { 
          //try to convert alertswitch to STring as well.
            alertswitch = alertswitch1;
             //Alert(_Symbol,  "default Case Switch : alertswitch is NULL :   alertswitch set = alertswitch1");
       }
             
     }//end of switch  
      
alertswitch1=alertswitch;
      
return (alertswitch);     

}//end of AssessTradeOpp function



 

To get problem with non static variable passing to function, problem can be with missing semicolon, in my case I get that 2 times, and at begginning I cant find problem. Later by accident I find missing semicolon and all problems was gone.

making struct with data can be quickly lost in that small mistakes.

Reason: