Static Question?

 
int DetermineChangeInDirection()
{
            static int LastDirection=0;
            static int CurrentDirection=1;
            

                                   
                  if (CurrentActiveTrades==6)CurrentDirection=2;//Swap Buy and Sell Parameters
                  
                  if (CurrentDirection!= LastDirection)// Direction has changed
                  
            LastDirection=CurrentDirection;
            
            Comment("The Current Direction is ",LastDirection);
            return(LastDirection);
            
           }

Is it at all possible that when the last direction ie "2", it then remains static untill the CurrentActiveTrades equals 6 again, so that i reverts back to "1" again?

Appreciate your comments.

 

W

Declare the variables in the globals section of the EA and initialise them in the init function as

extern int MagicNumber = 911;

// End externs section

static int LastDirection;
static int CurrentDirection;


init ()
{

   LastDirection=0;
   CurrentDirection=1;


}

deinit ()
{

}

start()
{

  //============= Main EA code starts here =================




  //============= Main EA code ends here =================

}


int DetermineChangeInDirection()
{

                                   
     if (CurrentActiveTrades==6)CurrentDirection=2;//Swap Buy and Sell Parameters
                  
     if (CurrentDirection!= LastDirection)// Direction has changed
                  
     LastDirection=CurrentDirection;
            
     Comment("The Current Direction is ",LastDirection);
     return(LastDirection);
            
}

Good Luck

-BB-

 
BarrowBoy wrote >>

W

Declare the variables in the globals section of the EA and initialise them in the init function as

Good Luck

-BB-

Thank you very much. I'll try it.

 

Hi

I do not wish to contradict BB, but I think your codes works as it is. CurrentDirection is 1 until CurrentActiveTrades == 6, then CurrentDirection is 2 and LastDirection is 1. After that CurrentDirection is not changed.

If that's not what you want it to do then I think your problem is with the logic of the algorithm, not to do with static variables.

Cheers

Jellybean

 
Jellybean wrote >>

...I do not wish to contradict BB...

JB

Feel free :)

I understand that many people declare & use statics the first way shown

I dont know what should be best practise, its just that I have had failures to preserve the values when the scope is limited

I didnt document this, I just moved on and always use global scope for statics - through paranoia rather than any purist theory!

-BB-

 
Jellybean wrote >>

Hi

I do not wish to contradict BB, but I think your codes works as it is. CurrentDirection is 1 until CurrentActiveTrades == 6, then CurrentDirection is 2 and LastDirection is 1. After that CurrentDirection is not changed.

If that's not what you want it to do then I think your problem is with the logic of the algorithm, not to do with static variables.

Cheers

Jellybean

Hi Jellybean, you were right that the CurrentDirection does not change after the initial change to 2. Is there any way that I'll be able to get it changed back to 1 if the ActiveTrades==6 again. Perhaps I'm missing something.

 

Hi Wasabiman


There may be better ways, but...

I've changed CurrentActiveTrades==6 to CurrentActiveTrades>=6 in case the value jumps past 6 for some reason. Just to be safe.

I've added a variable to keep the previous value of CurrentActiveTrades to prevent the swap occuring every tick.

This logic does require CurrentActiveTrades to reduce in value after the CurrentDirection is changed. If CurrentActiveTrades reaches 6 then increases further, CurrentDirection will swap every time it changes above 6 (but I suspect you don't want this).

This is what I used to test it.

int start()                            // Special function start()
{
   static int Direction = 1, PrevDirection = 0;
   for( int i=0; i<100; i++ ) {
      Sleep( 1000 );
      Direction = DetermineChangeInDirection();
   
      // for test purposes
      if( Direction != PrevDirection )
      {
         CurrentActiveTrades = 0;
         PrevDirection = Direction;
      }

      CurrentActiveTrades++;
   }   
   return(0);                             // start() exit operator
}


int DetermineChangeInDirection()
{
//   static int LastDirection=0;
   static int PrevCurrentActiveTrades=0;
   static int CurrentDirection=1;
                                   
   if ( CurrentActiveTrades >= 6 && PrevCurrentActiveTrades != CurrentActiveTrades )
   {                                   // CurrentActiveTrades has just reached 6
      if( CurrentDirection == 1 )      //Swap Buy and Sell Parameters
         CurrentDirection = 2;
      else
         CurrentDirection = 1;
   }
   PrevCurrentActiveTrades = CurrentActiveTrades;  // store the previous value

   Comment("CurrentActiveTrades is ", CurrentActiveTrades, "\nThe Current Direction is ", CurrentDirection );
   
   return( CurrentDirection );
}

One other suggestion - if you put your code into a source box (by clicking on the SRC button) it's much easier to read.

Cheers

Jellybean

 

BarrowBoy

On the subject of static variables, I also haven't been using them until recently. I have been lately just to reduce the long list of global variables at the top of the code and I'm trying to write more modular re-usable code. I think static variables make it easier to copy & paste functions.

I (naively) assumed they were reliable, though. I enjoy reading your posts, by the way.

Cheers

Jellybean

 
Jellybean wrote >>

Hi Wasabiman

There may be better ways, but...

I've changed CurrentActiveTrades==6 to CurrentActiveTrades>=6 in case the value jumps past 6 for some reason. Just to be safe.

I've added a variable to keep the previous value of CurrentActiveTrades to prevent the swap occuring every tick.

This logic does require CurrentActiveTrades to reduce in value after the CurrentDirection is changed. If CurrentActiveTrades reaches 6 then increases further, CurrentDirection will swap every time it changes above 6 (but I suspect you don't want this).

This is what I used to test it.

One other suggestion - if you put your code into a source box (by clicking on the SRC button) it's much easier to read.

Cheers

Jellybean

 
Thank you Jellybean, Tried the code and at long last it works perfectly as per my requirements. I must admit that it is great to have "Einstein's" available on the forum, as it provides just that little information which spawns new ideas! Warmest regards Wasbiman