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); }
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); }
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); } }
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.
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.
- www.mql5.com
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)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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?