After signal is valid. Save its time, add xx seconds to elapse.

 

Hi again,

I still have difficulties in coding this. Maybe someone could advise me please.

So far I made signal() checked on every bar open.

int signal()
  {
   
   // Execute on bar open
    if(CheckOncePerBar == true)
       {
          int BarShift = 1;
          if(CurrentTimeStamp != Time[0]) 
                  {
                          CurrentTimeStamp = Time[0]; //---this is saved as GV
                          bool NewBar = true;
                  }
          else NewBar = false;
       }
    else 
       {
          NewBar = true;
          BarShift = 0;
       } 
   // Moving averages
   double FastMA = iMA(NULL,0,FastMAPeriod,0,1,0,BarShift);
   double SlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,BarShift);
   double LastFastMA = iMA(NULL,0,FastMAPeriod,0,1,0,BarShift+1);
   double LastSlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,BarShift+1);   
   if(NewBar == true)
      { 
         if(FastMA > SlowMA && LastFastMA <= LastSlowMA) signal = 1;
         if(FastMA < SlowMA && LastFastMA >= LastSlowMA) signal = 2;
         else signal = 3; 
      }  
   return(signal); //-----output is saved globally
  }

Now, as signal SHOULD appear only once in a while(on MA cross). Signal is 1or2. else is 3.

NOTE : once i delete ,,else signal =3;,, EA process will work , but timer not. As it will remeber only 1 or 2 outputs. But with output 3 EA dont work at all, but timer seems to work closely to success.

Therefore I want to save time of new bar openning once MA just crossed: 

datetime CrossStamp()
   {
    signal = signal();
    //setting cross time and remembering it in global variable datetime TS. (tried also static datetime format)
    if(signal == 1)
      {
       TS = TimeCurrent(); //----using Time[0] will show results for TS as 0:00 1970 etc.       
      
      }
    else if(signal == 2)
      {
       TS = TimeCurrent();       
       
      }
    Print("TS=",TimeToStr(TS, TIME_MINUTES|TIME_SECONDS|TIME_DATE));  
    return(TS); //------output saved globally
   }    

 The idea is to look for signal per bar open. Find it - save its time, set time to expiry trading under such signal. Trade per tick(look for entry price per tick).  Look for another signal. and so on.

// Start function
int start()
   {                   
     bool PriceMatch = PriceMatch(); //ignore this now
     datetime CS = CrossStamp();
     
     //Check for entry price in 6 hours period from cross happening only.
     bool TradeAllowed = false;
     datetime CurrentTime = TimeCurrent();
     datetime StartTime = CS;
     datetime EndTime = CS + AllowedTradingPeriod; //----external var in seconds, lets say 6hrs = 21600 secs
     // Check for trade condition
     if(StartTime <= CurrentTime && EndTime > CurrentTime) 
              {
                     TradeAllowed = true;
                     Print("Trade Allowed");                                    
              }
     if(TradeAllowed == false) Print("Not Allowed"); 

     if(TradeAllowed == true)
      { 
         // Close Order, open order etc.....

 I think I should use Time[] not TimeCurrent(). And search in array Time[] for openning and closing times i need for ,,TradeAllowed,,

Im just confiused now. 

 

Sounds like you're using too many Global/Static variables and over-complicating it.

if( SaveTime == 0 && signal() != 0 ){ SaveTime=TimeCurrent(); }
if( SaveTime != 0 && SaveTime+30_Second < TimeCurrent() ){ SaveTime=0; }

//Note: TimeCurrent because you're working in Seconds for Expiration.
//Note: SaveTime should be of Stored Type Variable.
//Note: Not sure if it'll solve your problem, just my 2Cents.
 
Sirru82:

Hi again,

...

Im just confiused now. 

 

Me too ;-) What are you trying to do exactly ? Check you signal (cross ma) on each new bar I suppose ? And what about your "timing" ?
 

ok..:)

Let me explain : I want to limit trading time. Same way as its used when StartTime is PRE-SET BY USER, and EndTime is just StartTime + some seconds. But in my example StartTime is not pre-seted by user - instead its generated by event = signal = MA cross.

I hope thats clear now?

P.S. Thank you Ubzen for your 2cents i will gladly play arround with ur approach now. Will update on results.

Cheers 

Reason: