I want that my program run everytime a new candle is created

 

I am not very familiar with MQL4, I am a Python programmer and I found this script on Google that export all the objects in the chart. I changed it so that it could also be used as an Expert, but the only way I could find it to run, for example, every 30 minutes is a time sleep. Is there a way to export objects every time a candle is created?


//+------------------------------------------------------------------+

//|                                          List Object Details.mq4 |

//+------------------------------------------------------------------+

#property show_inputs



#include <hanover --- function header (np).mqh>



extern string OutputFilename        = "Object List---[ccy],[tf].csv";

extern string ObjectTypesToList     = "VLine,HLine,TLine,TBAngle,LRChan,Chan,SDChan,GLine,GFan,GGrid,Fib,FibTime,FibFan,FibArc,FibExp,FibChan,Rectangle,Triangle,Ellipse,Pfork,Cycles,Text,Arrow,Label";

extern string ObjectNameMustContain = "";

extern string ObjectDescMustContain = "";

extern string FieldSeparator        = ",";

extern bool   OutputHeaderLine      = true;

extern string ObjectNumberFormat    = "T5";

extern string ObjectNameFormat      = "T30";

extern string ObjectTypeFormat      = "T30";

extern string ObjectDescFormat      = "'\x22'T64'\x22'";

extern string ObjectPriceFormat     = "TBR-3.5";

extern string ObjectTimeFormat      = "BY.M.D H:I";

extern string ObjectWidthFormat     = "T2";

extern string ObjectPosFormat       = "TB6";



string OT[24];



//+------------------------------------------------------------------+

int start()  {

//+------------------------------------------------------------------+

       if (FieldSeparator == "^")   FieldSeparator = "\x09";

  string ObjectTypes = "VLine,HLine,TLine,TBAngle,LRChan,Chan,SDChan,GLine,GFan,GGrid,Fib,FibTime,FibFan,FibArc,FibExp,FibChan,Rectangle,Triangle,Ellipse,Pfork,Cycles,Text,Arrow,Label";

  StrToStringArray(ObjectTypes,OT);

  for (int i=0; i<24; i++)  {

    if (StringFind(StringTrim(StringLower(ObjectTypesToList)),StringTrim(StringLower(OT[i]))) < 0)

      OT[i] = " ";

  }

  while(true){

  string fname = OutputFilename;

  fname = StringReplace(fname,"[ccy]",ExpandCcy(Symbol()));

  fname = StringReplace(fname,"[tf]",TFToStr(Period()));

  int h = FileOpen(fname,FILE_CSV|FILE_WRITE,'~');

  if (OutputHeaderLine)  {

    string outstr = "#,Name,Type,Description,Time1,Price1,Time2,Price2,Color,Style,Width,BG,Ray,Arrow,Corner,Hpos,Vpos";

    outstr = StringReplace(outstr,"-",FieldSeparator);

    FileWrite(h,outstr);              

  }  

  for (i=0; i<ObjectsTotal(); i++)   {

    string objname = ObjectName(i);

    string objdesc = ObjectDescription(objname);

    int    type    = ObjectType(objname);

    if (StringLen(ObjectNameMustContain) > 0  &&  StringFind(StringUpper(objname),StringUpper(ObjectNameMustContain)) < 0)    continue;

    if (StringLen(ObjectDescMustContain) > 0  &&  StringFind(StringUpper(objdesc),StringUpper(ObjectDescMustContain)) < 0)    continue;

    if (OT[type] != " ")   {

      outstr = NumberToStr(i,ObjectNumberFormat)

             + FieldSeparator + StrToStr(objname,ObjectNameFormat)

             + FieldSeparator + StrToStr(OT[type],ObjectTypeFormat)

             + FieldSeparator + StrToStr(objdesc,ObjectDescFormat)

             + FieldSeparator + StringTrimRight(DateToStr(ObjectGet(objname,OBJPROP_TIME1),ObjectTimeFormat))

             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_PRICE1),ObjectPriceFormat)

             + FieldSeparator + StringTrimRight(DateToStr(ObjectGet(objname,OBJPROP_TIME2),ObjectTimeFormat))

             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_PRICE2),ObjectPriceFormat)

             + FieldSeparator + ColorToStr(ObjectGet(objname,OBJPROP_COLOR))

             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_STYLE),ObjectWidthFormat)

             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_WIDTH),ObjectWidthFormat)

             + FieldSeparator + BoolToStr(ObjectGet(objname,OBJPROP_BACK))

             + FieldSeparator + BoolToStr(ObjectGet(objname,OBJPROP_RAY))

             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_ARROWCODE),ObjectPosFormat)

             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_CORNER),ObjectWidthFormat)

             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_XDISTANCE),ObjectPosFormat)

             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_YDISTANCE),ObjectPosFormat);

      FileWrite(h,outstr);              

  } }

  FileClose(h);

  //MessageBox("Done");

  Sleep(30000);

  return(0);

 }

}



//+------------------------------------------------------------------+

#include <hanover --- extensible functions (np).mqh>
 
BeLoader:

I am not very familiar with MQL4, I am a Python programmer and I found this script on Google that export all the objects in the chart. I changed it so that it could also be used as an Expert, but the only way I could find it to run, for example, every 30 minutes is a time sleep. Is there a way to export objects every time a candle is created?


try this 


int start()  {



   if(!NewBar())

      return;



//+------------------------------------------------------------------+



       if (FieldSeparator == "^")   FieldSeparator = "\x09";



  string ObjectTypes = "VLine,HLine,TLine,TBAngle,LRChan,Chan,SDChan,GLine,GFan,GGrid,Fib,FibTime,FibFan,FibArc,FibExp,FibChan,Rectangle,Triangle,Ellipse,Pfork,Cycles,Text,Arrow,Label";



  StrToStringArray(ObjectTypes,OT);



  for (int i=0; i<24; i++)  {



    if (StringFind(StringTrim(StringLower(ObjectTypesToList)),StringTrim(StringLower(OT[i]))) < 0)



      OT[i] = " ";



  }



  while(true){



  string fname = OutputFilename;



  fname = StringReplace(fname,"[ccy]",ExpandCcy(Symbol()));



  fname = StringReplace(fname,"[tf]",TFToStr(Period()));



  int h = FileOpen(fname,FILE_CSV|FILE_WRITE,'~');



  if (OutputHeaderLine)  {



    string outstr = "#,Name,Type,Description,Time1,Price1,Time2,Price2,Color,Style,Width,BG,Ray,Arrow,Corner,Hpos,Vpos";



    outstr = StringReplace(outstr,"-",FieldSeparator);



    FileWrite(h,outstr);              



  }  



  for (i=0; i<ObjectsTotal(); i++)   {



    string objname = ObjectName(i);



    string objdesc = ObjectDescription(objname);



    int    type    = ObjectType(objname);



    if (StringLen(ObjectNameMustContain) > 0  &&  StringFind(StringUpper(objname),StringUpper(ObjectNameMustContain)) < 0)    continue;



    if (StringLen(ObjectDescMustContain) > 0  &&  StringFind(StringUpper(objdesc),StringUpper(ObjectDescMustContain)) < 0)    continue;



    if (OT[type] != " ")   {



      outstr = NumberToStr(i,ObjectNumberFormat)



             + FieldSeparator + StrToStr(objname,ObjectNameFormat)



             + FieldSeparator + StrToStr(OT[type],ObjectTypeFormat)



             + FieldSeparator + StrToStr(objdesc,ObjectDescFormat)



             + FieldSeparator + StringTrimRight(DateToStr(ObjectGet(objname,OBJPROP_TIME1),ObjectTimeFormat))



             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_PRICE1),ObjectPriceFormat)



             + FieldSeparator + StringTrimRight(DateToStr(ObjectGet(objname,OBJPROP_TIME2),ObjectTimeFormat))



             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_PRICE2),ObjectPriceFormat)



             + FieldSeparator + ColorToStr(ObjectGet(objname,OBJPROP_COLOR))



             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_STYLE),ObjectWidthFormat)



             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_WIDTH),ObjectWidthFormat)



             + FieldSeparator + BoolToStr(ObjectGet(objname,OBJPROP_BACK))



             + FieldSeparator + BoolToStr(ObjectGet(objname,OBJPROP_RAY))



             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_ARROWCODE),ObjectPosFormat)



             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_CORNER),ObjectWidthFormat)



             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_XDISTANCE),ObjectPosFormat)



             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_YDISTANCE),ObjectPosFormat);



      FileWrite(h,outstr);              



  } }



  FileClose(h);



  //MessageBox("Done");



  Sleep(30000);



  return(0);





bool NewBar()

  {

   datetime          currentTime =iTime(_Symbol,_Period,0);

   static datetime   priorTime   =currentTime;

   bool              result      =(currentTime!=priorTime);

   priorTime                     =currentTime;

   return(result);

  }

 
Daniel Cioca #:

try this 


I tried to compile, but there are some errors:

int start()  

{

   if(!NewBar())

      return;



       if (FieldSeparator == "^")   FieldSeparator = "\x09";

  string ObjectTypes = "VLine,HLine,TLine,TBAngle,LRChan,Chan,SDChan,GLine,GFan,GGrid,Fib,FibTime,FibFan,FibArc,FibExp,FibChan,Rectangle,Triangle,Ellipse,Pfork,Cycles,Text,Arrow,Label";

  StrToStringArray(ObjectTypes,OT);

  for (int i=0; i<24; i++)  {

    if (StringFind(StringTrim(StringLower(ObjectTypesToList)),StringTrim(StringLower(OT[i]))) < 0)

      OT[i] = " ";

  }

  string fname = OutputFilename;

  fname = StringReplace(fname,"[ccy]",ExpandCcy(Symbol()));

  fname = StringReplace(fname,"[tf]",TFToStr(Period()));

  int h = FileOpen(fname,FILE_CSV|FILE_WRITE,'~');

  if (OutputHeaderLine)  {

    string outstr = "#,Name,Type,Description,Time1,Price1,Time2,Price2,Color,Style,Width,BG,Ray,Arrow,Corner,Hpos,Vpos";

    outstr = StringReplace(outstr,"-",FieldSeparator);

    FileWrite(h,outstr);              

  }  

  for (i=0; i<ObjectsTotal(); i++)   {

    string objname = ObjectName(i);

    string objdesc = ObjectDescription(objname);

    int    type    = ObjectType(objname);

    if (StringLen(ObjectNameMustContain) > 0  &&  StringFind(StringUpper(objname),StringUpper(ObjectNameMustContain)) < 0)    continue;

    if (StringLen(ObjectDescMustContain) > 0  &&  StringFind(StringUpper(objdesc),StringUpper(ObjectDescMustContain)) < 0)    continue;

    if (OT[type] != " ")   {

      outstr = NumberToStr(i,ObjectNumberFormat)

             + FieldSeparator + StrToStr(objname,ObjectNameFormat)

             + FieldSeparator + StrToStr(OT[type],ObjectTypeFormat)

             + FieldSeparator + StrToStr(objdesc,ObjectDescFormat)

             + FieldSeparator + StringTrimRight(DateToStr(ObjectGet(objname,OBJPROP_TIME1),ObjectTimeFormat))

             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_PRICE1),ObjectPriceFormat)

             + FieldSeparator + StringTrimRight(DateToStr(ObjectGet(objname,OBJPROP_TIME2),ObjectTimeFormat))

             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_PRICE2),ObjectPriceFormat)

             + FieldSeparator + ColorToStr(ObjectGet(objname,OBJPROP_COLOR))

             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_STYLE),ObjectWidthFormat)

             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_WIDTH),ObjectWidthFormat)

             + FieldSeparator + BoolToStr(ObjectGet(objname,OBJPROP_BACK))

             + FieldSeparator + BoolToStr(ObjectGet(objname,OBJPROP_RAY))

             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_ARROWCODE),ObjectPosFormat)

             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_CORNER),ObjectWidthFormat)

             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_XDISTANCE),ObjectPosFormat)

             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_YDISTANCE),ObjectPosFormat);

      FileWrite(h,outstr);              

  } }

  FileClose(h);

      return(0);

   bool NewBar()        //'(' - function definition unexpected  (75, 15)



   {

   datetime          currentTime =iTime(_Symbol,_Period,0); //'currentTime' - initialization expected   (78, 35)

   static datetime   priorTime   =currentTime; //'currentTime' - variable not defined (79, 36)

   bool              result      =(currentTime!=priorTime); // 'currentTime' - variable not defined (80, 35)

   priorTime                     =currentTime;

   return(result);

}
 
 
Dark Ryd3r #:

Read this article

Thank you for sharing that article. Reading it and trying some features I realized that in the compatible build (509 MQL4) of the file it is not possible to apply these features. I think I wrote in the wrong forum as I am asking for help with a code in MQL4, but in the end I managed to find a solution to my problem. I have only one question: when a new bar is created the program correctly extracts all the required objects in a new file, but when I go to check in the MT4 terminal, in the 'Expert' section, each bar writes 'not initialized string'. Would you have an idea why? (the expert works perfectly)

int    barsTotal             = 0;

//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
       if (FieldSeparator == "^")   FieldSeparator = "\x09";
  string ObjectTypes = "VLine,HLine,TLine,TBAngle,LRChan,Chan,SDChan,GLine,GFan,GGrid,Fib,FibTime,FibFan,FibArc,FibExp,FibChan,Rectangle,Triangle,Ellipse,Pfork,Cycles,Text,Arrow,Label";
  StrToStringArray(ObjectTypes,OT);
  for (int i=0; i<24; i++)  {
    if (StringFind(StringTrim(StringLower(ObjectTypesToList)),StringTrim(StringLower(OT[i]))) < 0)
      OT[i] = " ";
  }
  if(Bars > barsTotal){
  string fname = OutputFilename;
  fname = StringReplace(fname,"[ccy]",ExpandCcy(Symbol()));
  fname = StringReplace(fname,"[tf]",TFToStr(Period()));
  int h = FileOpen(fname,FILE_CSV|FILE_WRITE,'~');
  if (OutputHeaderLine)  {
    string outstr = "#,Name,Type,Description,Time1,Price1,Time2,Price2,Color,Style,Width,BG,Ray,Arrow,Corner,Hpos,Vpos";
    outstr = StringReplace(outstr,"-",FieldSeparator);
    FileWrite(h,outstr);              
  }  
  for (i=0; i<ObjectsTotal(); i++)   {
    string objname = ObjectName(i);
    string objdesc = ObjectDescription(objname);
    int    type    = ObjectType(objname);
    if (StringLen(ObjectNameMustContain) > 0  &&  StringFind(StringUpper(objname),StringUpper(ObjectNameMustContain)) < 0)    continue;
    if (StringLen(ObjectDescMustContain) > 0  &&  StringFind(StringUpper(objdesc),StringUpper(ObjectDescMustContain)) < 0)    continue;
    if (OT[type] != " ")   {
      outstr = NumberToStr(i,ObjectNumberFormat)
             + FieldSeparator + StrToStr(objname,ObjectNameFormat)
             + FieldSeparator + StrToStr(OT[type],ObjectTypeFormat)
             + FieldSeparator + StrToStr(objdesc,ObjectDescFormat)
             + FieldSeparator + StringTrimRight(DateToStr(ObjectGet(objname,OBJPROP_TIME1),ObjectTimeFormat))
             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_PRICE1),ObjectPriceFormat)
             + FieldSeparator + StringTrimRight(DateToStr(ObjectGet(objname,OBJPROP_TIME2),ObjectTimeFormat))
             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_PRICE2),ObjectPriceFormat)
             + FieldSeparator + ColorToStr(ObjectGet(objname,OBJPROP_COLOR))
             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_STYLE),ObjectWidthFormat)
             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_WIDTH),ObjectWidthFormat)
             + FieldSeparator + BoolToStr(ObjectGet(objname,OBJPROP_BACK))
             + FieldSeparator + BoolToStr(ObjectGet(objname,OBJPROP_RAY))
             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_ARROWCODE),ObjectPosFormat)
             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_CORNER),ObjectWidthFormat)
             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_XDISTANCE),ObjectPosFormat)
             + FieldSeparator + NumberToStr(ObjectGet(objname,OBJPROP_YDISTANCE),ObjectPosFormat);
      FileWrite(h,outstr);              
  } }
  FileClose(h);
  barsTotal = Bars;
  return(0);
 }
}
Files:
errore.JPG  36 kb
 
BeLoader #: Thank you for sharing that article. Reading it and trying some features I realized that in the compatible build (509 MQL4) of the file it is not possible to apply these features. I think I wrote in the wrong forum as I am asking for help with a code in MQL4, but in the end I managed to find a solution to my problem. I have only one question: when a new bar is created the program correctly extracts all the required objects in a new file, but when I go to check in the MT4 terminal, in the 'Expert' section, each bar writes 'not initialized string'. Would you have an idea why? (the expert works perfectly)

You are in the right forum section, but you are using very old style code that most coders no longer use, especially because build 509 and prior is no longer supported.

You should start learning and coding in the modern MQL4+ style and functions which is more compatible with MQL5 style and functionality.

 
Fernando Carreiro #:

You are in the right forum section, but you are using very old style code that most coders no longer use, especially because build 509 and prior is no longer supported.

You should start learning and coding in the modern MQL4+ style and functions which is more compatible with MQL5 style and functionality.

I am a Python programmer and for a project I was approaching the MQL4 world, but in the wrong way. I'll try to upgrade to the updated version of MQL4 by reading and studying something on the forum or maybe even something about MQL5. I have seen that you can do a lot of interesting things with Python on MT5 as well. Anyway, I think I have solved my initial problem and I only have one question, which is the one asked above. Do you have an idea of what that written might be?
 
BeLoader #: I am a Python programmer and for a project I was approaching the MQL4 world, but in the wrong way. I'll try to upgrade to the updated version of MQL4 by reading and studying something on the forum or maybe even something about MQL5. I have seen that you can do a lot of interesting things with Python on MT5 as well. Anyway, I think I have solved my initial problem and I only have one question, which is the one asked above. Do you have an idea of what that written might be?
Then if you are Python fan, go the MT5/MQL5 route instead, because MT5 has Python integration and you can call the MT5 API from Python.
Documentation on MQL5: Integration / MetaTrader for Python
Documentation on MQL5: Integration / MetaTrader for Python
  • www.mql5.com
MetaTrader for Python - Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
BeLoader #: Thank you for sharing that article.

For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
          New candle - MQL4 programming forum #3 (2014)

I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
          Running EA once at the start of each bar - MQL4 programming forum (2011)

Reason: