What's wrong with these codes? - page 2

 

I thought the start() executes every time a new bar is formed :( What could I write so that it's following the logic only after the bar? Every tick is when the price changes, right? And the bar is a bar for a certain time period, like M1, M5 and so on? Am I understanding it correctly?

 
bdht:

I thought the start() executes every time a new bar is formed :( What could I write so that it's following the logic only after the bar? Every tick is when the price changes, right? And the bar is a bar for a certain time period, like M1, M5 and so on? Am I understanding it correctly?

Time[0] is opening time of a new bar, you can set your logic around this code.
 

Start() is executed on every tick

Your code can determine when a new bar arrives

Here is a little "indicator" to illuminate those thoughts:

datetime oldBarTime;      // declared in global space so init and start can both see it
 
int init(){
   oldBarTime = Time[0]; // get the time of the current bar on startup
}

int deinit(){
   return(0);            // deinit code is run on exit
}




int start(){
   static int tickCount; // static variable is initialized only one time
   tickCount++;          // count executions of "start'

   Comment("Tick # ", tickCount, " has arrived");
   if(oldBarTime != Time[0]){
      Alert("New Bar Has Arrived");
      oldBarTime = Time[0]; // set oldBarTime to current bar time for future comparison
   }
   return(0);
}
 
Hi Phy, I'm trying to create an indicator which will showing an arrow simply when D+ and D- from adx indicator crossing each other, but it's failed to do so. What's wrong with it?

//+------------------------------------------------------------------+
//|                                                    ADX_cross.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
 
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 DeepSkyBlue
#property indicator_color2 Red
//---- input parameters
extern int       ADX_Period=10;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double dp, dp1, dm, dm1; 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,217);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexEmptyValue(0,0.0);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,218);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexEmptyValue(1,0.0);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    i, nShift, counted_bars = IndicatorCounted();
      
   if(ADX_Period <= 0) return(0);
   if(Bars <= ADX_Period) return(0);
   
   int limit = Bars - counted_bars;
   
   switch(Period())
    {
      case     1: nShift = 1;   break;    
      case     5: nShift = 3;   break; 
      case    15: nShift = 5;   break; 
      case    30: nShift = 10;  break; 
      case    60: nShift = 15;  break; 
      case   240: nShift = 20;  break; 
      case  1440: nShift = 80;  break; 
      case 10080: nShift = 100; break; 
      case 43200: nShift = 200; break;               
    }
   
   for(i=limit;i>0;i--)
    {
      dp=iADX(Symbol(),0,ADX_Period,PRICE_CLOSE,MODE_PLUSDI,i);
      dp1=iADX(Symbol(),0,ADX_Period,PRICE_CLOSE,MODE_PLUSDI,i+1);
      dm=iADX(Symbol(),0,ADX_Period,PRICE_CLOSE,MODE_MINUSDI,i);
      dm1=iADX(Symbol(),0,ADX_Period,PRICE_CLOSE,MODE_MINUSDI,i+1);
            
      if(dp1<=dm1 && dp>dm) 
       {
         ExtMapBuffer1[i]=Low[0]-nShift;
         ExtMapBuffer2[i]=0;
       }  
      if(dp1>=dm1 && dp<dm) 
       {
         ExtMapBuffer1[i]=0;
         ExtMapBuffer2[i]=High[0]+nShift;
       }
    }
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
Thank you
 

ExtMapBuffer1[i]=Low[i]-nShift*Point;

ExtMapBuffer2[i]=High[i]+nShift*Point;

 
phy:

ExtMapBuffer1[i]=Low[i]-nShift*Point;

ExtMapBuffer2[i]=High[i]+nShift*Point;

Hello Phy, thanks for pointing out that, it seems I'm working on EA a bit too much.

Thank you
 
I'm starting to understand your code. Thanks, mate. I'll try to implement it to my code.
Reason: