run code once

 

I would like to run this code once on Sunday at the opening of the broker. However, I have this EA running on 12 different pairs.

Any ideas on how to run through the code just once for each pair?

I thought about putting in the Hour and Minutes and seconds but it relies on a tick coming through to start the EA:


            if (DayOfWeek() == 0) { //and time and hour
                  GlobalVariableSet(Symbol()+"OrderCreatedThisWeek",0);
                  double HAvalOpen=iCustom(NULL, 0, "Heiken Ashi",Red,Blue,Red,Blue,2,1); 
                  double HAvalClose=iCustom(NULL, 0, "Heiken Ashi",Red,Blue,Red,Blue,3,1); 
                  if (HAvalOpen<HAvalClose) GlobalVariableSet(Symbol()+"BiasDirection",1); //bullish
                  if (HAvalOpen>HAvalClose) GlobalVariableSet(Symbol()+"BiasDirection",-1); //bearish candle
            }
 
SanMiguel:

I would like to run this code once on Sunday at the opening of the broker. However, I have this EA running on 12 different pairs.

Any ideas on how to run through the code just once for each pair?

I thought about putting in the Hour and Minutes and seconds but it relies on a tick coming through to start the EA:


//init
bool Myflag==false;

//start
if (Myflag==false)
{
Myflag=true;
//your code...
}

if (DayOfWeek() != 0) Myflag==false;
Not tested...but it should work
 
Matutin:
Not tested...but it should work


But that's the problem, the Ea runs in 12 pairs, so if one pair sets the flag to true, then none of the other pairs will run their code?
 
SanMiguel:

But that's the problem, the Ea runs in 12 pairs, so if one pair sets the flag to true, then none of the other pairs will run their code?

Oh, 1 EA for 12 pairs and not 12 pairs in 12 charts with the same EA ?
 
Matutin:

Oh, 1 EA for 12 pairs and not 12 pairs in 12 charts with the same EA ?

12 pairs in 12 charts with the same EA for each, ie 12 EAs running.
 
Each instance of the EA will run independently and any variables set by an ea are local to that ea.
 
Viffer:
Each instance of the EA will run independently and any variables set by an ea are local to that ea.

Ok, I get some errors during ht einit part of the code here:


int init()
{
   bool Myflag=false;
}
int start()
{

   double MovetoBreakEven;
   double Buffer;
   int nDigits;
   double  varHigh = iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_HIGH, 0);
   double  varLow = iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_LOW, 0);
   double StopLoss = (varHigh - varLow)/2;
   double Lots = NormalizeDouble(AccountBalance()*0.0001, 2);
   
   if (GlobalVariableGet(Symbol()+"BiasDirection") == 0) {return(0);}
   //adjust biases according to last week's candle colour. Red=down, Blue = up

   int total = OrdersTotal();
   for(int i=total-1;i>=0;i--)  
   {
      if( OrderSelect(i,SELECT_BY_POS) && OrderCloseTime()==0)
      {
            // ----- 0 -----
            //If Sunday night, reset all the orderflags to zero and work out the direction bias from last week's candle
            //if the bias is set to neutral above, then we won't get this far
            //that serves as the manual override for any pair if needed.
                      
            if (DayOfWeek() == 0 && Myflag==false) { //and time and hour
                  GlobalVariableSet(Symbol()+"OrderCreatedThisWeek",0);
                  double HAvalOpen=iCustom(NULL, 0, "Heiken Ashi",Red,Blue,Red,Blue,2,1); 
                  double HAvalClose=iCustom(NULL, 0, "Heiken Ashi",Red,Blue,Red,Blue,3,1); 
...

says Myflag has not been defined but if I put it in the start code then it will be reset every tick.
 
SanMiguel:

Ok, I get some errors during ht einit part of the code here:.....

says Myflag has not been defined but if I put it in the start code then it will be reset every tick.


So either put it on the global scope of the EA or better, declare it as a static variable inside start.

Have a read of this section

https://book.mql4.com/variables/types

V

Reason: