Having trouble identifying trouble in alarm trigger code - page 2

 
dabbler:
Post the complete code and we'll take a look.


Sorry about that!

//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 2
#property  indicator_color1  Yellow
#property  indicator_color2  Red
#property  indicator_width1  1

//---- indicator parameters
extern string PairName = "";   // Leave blank for the pair of the chart, enter other pair name to compare correlated pairs

extern int StdDev.MA.Period=12;  // D1=20
extern int StdDev.MA.Shift=0;    //
extern int StdDev.MA.Method = 0; // 0=SMA 1=EMA 2=Smoothed 3=Linear Weighted
extern int StdDev.MA.Price = 0;  // 0 Close price, 1 Open price, 2 High price, 3 Low price, 4 Median price, (high+low)/2, 5 Typical price, (high+low+close)/3, 6 Weighted close price, (high+low+close+close)/4

extern int MA.Fast.Period = 3;
extern int MA.Fast.Method = 2;   //  0=SMA 1=EMA 2=Smoothed 3=Linear Weighted
extern int MA.Fast.Shift = 0;

extern bool CheckOncePerBar = true;

int i, limit, counted_bars;
static string Pair1;

datetime CurrentTimeStamp;

//---- indicator buffers
double     STDBuffer[];
double     stddevma[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  
   IndicatorDigits(Digits+1);
     
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE); // 
   SetIndexStyle(1,DRAW_LINE);

      
//---- indicator buffers mapping
   SetIndexBuffer(0, STDBuffer);
   SetIndexBuffer(1, stddevma);

   
   if (PairName == "") Pair1 = Symbol();
   else Pair1 = PairName;

//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("SFX TOR: "+Pair1+"("+StdDev.MA.Period+")");
   SetIndexLabel(0,"StdDev");
   SetIndexLabel(1,"StdDev MA");

//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {

   counted_bars=IndicatorCounted();
   
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
//---- macd counted in the 1-st buffer
  for(int i=limit-1; i>=0; i--){
     STDBuffer[i]=iStdDev(Pair1,0,StdDev.MA.Period, StdDev.MA.Shift, StdDev.MA.Method, StdDev.MA.Price, i);
     stddevma[i] = iMAOnArray(STDBuffer, 0, MA.Fast.Period, MA.Fast.Shift, MA.Fast.Method, i);
   } 
    
//Execute on bar Open
    if( CheckOncePerBar == true ){
      if( CurrentTimeStamp == Time[0] )
         return( 0 );
   }
   
   CurrentTimeStamp = Time [0];
   
//Tests
        
      if( STDBuffer[1] > stddevma[1] ){
      if( STDBuffer[2] <= stddevma[2]){
         PlaySound("alert.wav");
         Alert(Symbol()," M",Period()," Crossing");
         SendMail("Crossing","Crossing");
      }   
   }
   else{ // if it's not greater then it is either lesser than or equal
      if( STDBuffer[2] >= stddevma[2]){
         PlaySound("alert.wav");
         Alert(Symbol()," M",Period()," Crossing");
         SendMail("Crossing","Crossing");  
      }
   }
          
//---- done
   return(0);
  }
 
NewCoder47:

I do have one question however at the moment, each new bar, the code seems to be calculating for anywhere between half a second and about 10 seconds.

SendMail takes time.
 

Sadly I am an idiot :-(

//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 2
#property  indicator_color1  Yellow
#property  indicator_color2  Red

//---- indicator parameters
extern string PairName = "";   // Leave blank for the pair of the chart, enter other pair name to compare correlated pairs

extern int StdDev.MA.Period=12;  // D1=20
extern int StdDev.MA.Shift=0;    //
extern int StdDev.MA.Method = 0; // 0=SMA 1=EMA 2=Smoothed 3=Linear Weighted
extern int StdDev.MA.Price = 0;  // 0 Close price, 1 Open price, 2 High price, 3 Low price, 4 Median price, (high+low)/2, 5 Typical price, (high+low+close)/3, 6 Weighted close price, (high+low+close+close)/4

extern int MA.Fast.Period = 3;
extern int MA.Fast.Method = 2;   //  0=SMA 1=EMA 2=Smoothed 3=Linear Weighted
extern int MA.Fast.Shift = 0;

extern bool CheckOncePerBar = true;

string Pair1;

datetime CurrentTimeStamp;

//---- indicator buffers
double     STDBuffer[];
double     stddevma[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init(){
  
   IndicatorDigits(Digits+1);
     
   //---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
      
   //---- indicator buffers mapping
   SetIndexBuffer(0, STDBuffer);
   SetIndexBuffer(1, stddevma);

   if( PairName == "")
      Pair1 = Symbol();
   else
      Pair1 = PairName;

   //---- name for DataWindow and indicator subwindow label
   IndicatorShortName("SFX TOR: "+Pair1+"("+StdDev.MA.Period+")");
   SetIndexLabel(0,"StdDev");
   SetIndexLabel(1,"StdDev MA");

   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start(){

   int counted_bars=IndicatorCounted();
   
   //---- last counted bar will be recounted
   if( counted_bars>0 )
       counted_bars--;
   
   int limit= Bars - counted_bars;
   
   //---- macd counted in the 1-st buffer
   for(int i=limit-1; i>=0; i--){
      STDBuffer[i]= iStdDev(Pair1,0,StdDev.MA.Period, StdDev.MA.Shift, StdDev.MA.Method, StdDev.MA.Price, i);
   } 
   
   for(i=limit-1; i>=0; i--){
      stddevma[i] = iMAOnArray(STDBuffer, 0, MA.Fast.Period, MA.Fast.Shift, MA.Fast.Method, i);
   }
   
   //Execute on bar Open
   if( CheckOncePerBar == true ){
      if( CurrentTimeStamp == Time[0] )
         return( 0 );
   }
   
   CurrentTimeStamp = Time [0];
   
   //Tests
   if( STDBuffer[1] > stddevma[1] ){
      if( STDBuffer[2] <= stddevma[2]){
         PlaySound("alert.wav");
         Alert(Symbol()," M",Period()," Crossing");
         SendMail("Crossing","Crossing");
      }   
   }
   else{ // if it's not greater then it is either lesser than or equal
      if( STDBuffer[2] >= stddevma[2]){
         PlaySound("alert.wav");
         Alert(Symbol()," M",Period()," Crossing");
         SendMail("Crossing","Crossing");  
      }
   }
          
   return(0);
}

The iMAOnArray can't go in the same loop as the array it is using as a source since the source won't be initialized before it is used!

 
dabbler:

Sadly I am an idiot :-(

The iMAOnArray can't go in the same loop as the array it is using as a source since the source won't be initialized before it is used!

Thanks for all your help dabbler, you have helped me learn alot.

Thanks once again.

Mike