Download MetaTrader 5
To add comments, please log in or register
All Code Base apps are available from MetaTrader 5. You can publish your own one!
Mirko
156
Mirko 2016.03.08 19:47 

Hey guys,

before starting i want to apologize for my bad english. I will try to explain my problem and hope you can understand me und help me.


With this code below my text i'm trying to scan a lot of forex pairs from one chart.

The code should run the main part only in the first minute of an hour and show me if the pairs startet a new candle. After this i want to check the candle of the last hour (No.1) for traing set ups.

This code is an example for this post. It should show me the number of the candle of the last hour of each pair.

My problem is:

Sometimes it shows me the wrong number and i can't find why. I get the number of the candle two hours before

altough the next candle started.

Can anyone finde the misstake?


Thanks a lot!

//+------------------------------------------------------------------+
//|                                             Trendscanner 2.0.mq4 |
//|                                                           FamWue |
//|Signale des Trendscanners in beide Richtungen traden              |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "FamWue"
#property link      ""
double Pip;
string SymbolsList1[];  
string SymbolsList2[];
extern int     CheckBar     = 0; 
extern int     intPeriode = 60;
extern int     Obj_Size    = 10;
extern int     Obj_Left    = 4;
extern int     Obj_Top     = 13;
int countS;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   if(SymbolsList(SymbolsList1, true) > 0)
      {
      string Text1;
      int i;
      for(i=0;i<=ArraySize(SymbolsList1);i++)
         {
         if(i==0) Text1 = SymbolsList1[0];
         else Text1 = Text1 + " & "+SymbolsList1[i];
         }
     //Print("Listenausgabe: ",i,":  ", Text1);
     //Alert("Listenausgabe: ",i,":  ", Text1);
      }
   if(SymbolsList(SymbolsList2, false) > 0)
      {
      string Text2;
      for(i=0;i<=ArraySize(SymbolsList2);i++)
         {
         if(i==0) Text2 = SymbolsList2[0];
         else Text2 = Text2 + " & "+SymbolsList2[i];
         }
     //Print("Listenausgabe: ",i,":  ", Text2);
     //Alert("Listenausgabe: ",i,":  ", Text2);
     //Alert(TimeHour(TimeCurrent()));
     }
   
  }
  
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
  ObjectsDeleteAll();
  }
  
//Funktion zum checken ob neue Kerze
bool Check_New_Bar()
   {
   static datetime New_Time;
   if(New_Time == iTime(NULL,1,1) && TimeMinute(iTime(NULL,1,0))==CheckBar)
     {
     New_Time = iTime(NULL,1,0);
     return(true);
     }
   else 
      {
      New_Time = iTime(NULL,1,0); 
      return(false);
      }
   }

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   Main();
  }
  
int Main() //hier kommen die Ticks rein
   {
  int countAll,countNT,countTr,countTrE,countTrKE,countDel;
   if(Check_New_Bar() == true)
      {
      int cntlbl=0;
      int cnt;
      int LabelNr,Left,Top;
      
      ObjectsDeleteAll();
      for(int i=0;i<ArraySize(SymbolsList1);i++)
         {
         //Alert(SymbolsList1[i]," 1");
         
         if(StringFind(SymbolsList1[i],"#CZ3")>=0) break; //Beenden!
         countAll++; //Alle Underlyings zählen
         
         
         string Label;
         double CH4=0;
         for(int a=0;a<=2;a++)
            {
            if(iTime(SymbolsList1[i],intPeriode,0)==iTime(NULL,intPeriode,a)) {CH4=1-a; break;}
            }
         Label = CH4+"  "+a+" - "+SymbolsList1[i]+" - "+TimeToStr(iTime(SymbolsList1[i],intPeriode,0),TIME_SECONDS)+" == "+TimeToStr(iTime(NULL,intPeriode,a),TIME_SECONDS)+" != "+TimeToStr(iTime(SymbolsList1[i],intPeriode,CH4),TIME_SECONDS)+" / "+iOpen(SymbolsList1[i],intPeriode,CH4)+" / "+iOpen(SymbolsList1[i],intPeriode,0)+" / "+iOpen(SymbolsList1[i],intPeriode,a);
         Alert(Label);
         
        
         
         LabelNr=5+cntlbl;
         if(LabelNr<24)
            {
            Left=Obj_Left;
            Top=Obj_Top+Obj_Top*(countS+5);
            }
         else
            {
            Left=Obj_Left+500;
            Top=Obj_Top+Obj_Top*(countS+5-19);
            }
         ObjectCreate("Label"+LabelNr, OBJ_LABEL, 0, 0, 0);
         ObjectSet("Label"+LabelNr, OBJPROP_XDISTANCE, Left);
         ObjectSet("Label"+LabelNr, OBJPROP_YDISTANCE, Top);
         
         ObjectSetText("Label"+LabelNr, Label , Obj_Size, "Times New Roman", clrWhite);
         cntlbl++;
         countS++;
               
            
         }
        
     }
   
   }  
  

int SymbolsList(string &Symbols[], bool Selected)
   {
   string SymbolsFileName;
   int Offset, SymbolsNumber;
   
   if(Selected) SymbolsFileName = "symbols.sel"; //In der Liste ausgewählt?!
   else         SymbolsFileName = "symbols.raw"; //Alle
   
   int hFile = FileOpenHistory(SymbolsFileName, FILE_BIN|FILE_READ);
   if(hFile < 0) return(-1);
   if(Selected) { SymbolsNumber = (FileSize(hFile) - 4) / 128; Offset = 116;  }
   else         { SymbolsNumber = FileSize(hFile) / 1936;      Offset = 1924; }
   ArrayResize(Symbols, SymbolsNumber);
   if(Selected) FileSeek(hFile, 4, SEEK_SET);
   for(int i = 0; i < SymbolsNumber; i++)
   {
      Symbols[i] = FileReadString(hFile, 12);
      FileSeek(hFile, Offset, SEEK_CUR);
   }
   FileClose(hFile);
   return(SymbolsNumber);
   }



#include <stderror.mqh>
#include <stdlib.mqh>
Carl Schreiber
5927
Carl Schreiber 2016.03.08 21:16  

At the first look I see one problem: for different symbols the new candle appears at different times depending on the last tick.

If your chart is e.g. EURUSD with many ticks and another symbol e.g. NZDUSD the last tick of this symbol might still have a time stamp for the previous candle!

How do you deal with this?

To check the new bar why don't you just do:

static datetime tmeBar = 0;
bool newBar = false;
if ( tmeBar != iTime(_Symbol,_Period,0) ) {
   tmeBar = iTime(_Symbol,_Period,0);
   newBar = true;
}
Vasyl Nosal
18197
Vasyl Nosal 2016.03.08 22:14  

Maybe helps you.

int prevbars[];
string symbols[];


//////////////////////////////////////////////////////////////
int OnInit()
{ 
//////
int symbols_tot=SymbolsTotal(true);
ArrayResize(symbols,symbols_tot);
ArrayResize(prevbars,symbols_tot);

string name_symb="";
for(int i=0;i<symbols_tot;i++)
{
  name_symb=SymbolName(i,true);
  symbols[i]=name_symb;
  prevbars[i]=iBars(name_symb,TimeFrame);
}

EventSetTimer(1);

...
}

//////////////////////////////////////////////////////////////////////////////////////////////////
void OnTimer()
{
//all symbols
for(int i=0;i<ArraySize(symbols);i++)
{
if(prevbars[i]==iBars(symbols[i],TimeFrame)) continue;
...
prevbars[i]=iBars(symbols[i],TimeFrame);
}



}
Mirko
156
Mirko 2016.03.09 07:21  

Thank you for your help. I will test it. Sounds easy to keep the last bar in memory.

I'm wondering why my code failed sometimes.

 

Vasyl Nosal
18197
Vasyl Nosal 2016.03.09 11:14  
FamWue:

Thanks to whom?
/
To add comments, please log in or register