You have mixed code from an Indicator using the "OnCalculate()" event and a Script with a "OnStart()" event. You can't mix them. It must be either a Script or an Indicator but not both.
Assuming, that you want it to be an Indicator then you must remove the "OnStart()" event (and any other Script related code) and implement the logic you want only in the "OnCalculate()" event.
Also, if using the modern format, the de-initialization event should be "void OnDeinit(const int reason)" and not "int deinit()".
... I understand what you mean, but I can not integrate the code. OnCalculate () I used it for the calculation of support and resistances, while OnStart I used it to create the Label (I took from the example of the library), but I can not update the Text. If I go to touch the code I make so many mistakes ...
No, it does not seem that you do understand. You CAN'T USE OnStart() Event in an Indicator and you MUST USE OnCalculate() event. Those are the "rules" for coding an Indicator.
You either learn how to code it properly or you hire someone to code it for you at the "Freelance" section.Thanks, Max.
//--- indicator settings #property indicator_chart_window #property indicator_buffers 2 #property indicator_type1 DRAW_ARROW #property indicator_width1 1 #property indicator_color1 0 #property indicator_label1 "Buy" #property indicator_type2 DRAW_ARROW #property indicator_width2 1 #property indicator_color2 0 #property indicator_label2 "Buy" //--- indicator buffers double Buffer1[]; double Buffer2[]; extern int Numero_Candele=12; bool input Linea_SupRes=true; double Somma; double myPoint; //initialized in OnInit //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void DrawLine(string objname, double price, int count, int start_index) //creates or modifies existing object if necessary { if((price < 0) && ObjectFind(objname) >= 0) { ObjectDelete(objname); } else if(ObjectFind(objname) >= 0 && ObjectType(objname) == OBJ_TREND) { ObjectSet(objname, OBJPROP_TIME1, Time[start_index]); ObjectSet(objname, OBJPROP_PRICE1, price); ObjectSet(objname, OBJPROP_TIME2, Time[start_index+count-1]); ObjectSet(objname, OBJPROP_PRICE2, price); } else { ObjectCreate(objname, OBJ_TREND, 0, Time[start_index], price, Time[start_index+count-1], price); ObjectSet(objname, OBJPROP_RAY, false); ObjectSet(objname, OBJPROP_COLOR, C'0x00,0x00,0xFF'); ObjectSet(objname, OBJPROP_STYLE, STYLE_SOLID); ObjectSet(objname, OBJPROP_WIDTH, 2); } } //+------------------------------------------------------------------+ //| Costruzione Supporto | //+------------------------------------------------------------------+ double Support(int time_interval, bool fixed_tod, int hh, int mm, bool draw, int shift) { int start_index = shift; int count = time_interval / 60 / Period(); if(fixed_tod) { datetime start_time; if(shift == 0) start_time = TimeCurrent(); else start_time = Time[shift-1]; datetime dt = StringToTime(StringConcatenate(TimeToString(start_time, TIME_DATE)," ",hh,":",mm)); //closest time hh:mm if (dt > start_time) dt -= 86400; //go 24 hours back int dt_index = iBarShift(NULL, 0, dt, true); datetime dt2 = dt; while(dt_index < 0 && dt > Time[Bars-1-count]) //bar not found => look a few days back { dt -= 86400; //go 24 hours back dt_index = iBarShift(NULL, 0, dt, true); } if (dt_index < 0) //still not found => find nearest bar dt_index = iBarShift(NULL, 0, dt2, false); start_index = dt_index + 1; //bar after S/R opens at dt } double ret = Low[iLowest(NULL, 0, MODE_LOW, count, start_index)]; if (draw) DrawLine("Support", ret, count, start_index); return(ret); } //+------------------------------------------------------------------+ //| Costruzione Resistenza | //+------------------------------------------------------------------+ double Resistance(int time_interval, bool fixed_tod, int hh, int mm, bool draw, int shift) { int start_index = shift; int count = time_interval / 60 / Period(); if(fixed_tod) { datetime start_time; if(shift == 0) start_time = TimeCurrent(); else start_time = Time[shift-1]; datetime dt = StringToTime(StringConcatenate(TimeToString(start_time, TIME_DATE)," ",hh,":",mm)); //closest time hh:mm if (dt > start_time) dt -= 86400; //go 24 hours back int dt_index = iBarShift(NULL, 0, dt, true); datetime dt2 = dt; while(dt_index < 0 && dt > Time[Bars-1-count]) //bar not found => look a few days back { dt -= 86400; //go 24 hours back dt_index = iBarShift(NULL, 0, dt, true); } if (dt_index < 0) //still not found => find nearest bar dt_index = iBarShift(NULL, 0, dt2, false); start_index = dt_index + 1; //bar after S/R opens at dt } double ret = High[iHighest(NULL, 0, MODE_HIGH, count, start_index)]; if (draw) DrawLine("Resistance", ret, count, start_index); return(ret); } //+------------------------------------------------------------------+ //|Inizio costruzione LABEL | //+------------------------------------------------------------------+ #property script_show_inputs //--- input parameters of the script string InpName="Label"; // Label name int InpX=150; // X-axis distance int InpY=100; // Y-axis distance string InpFont="Arial"; input string Impostazione_label=""; // Font input int ImpFontSize=10; // Font size input color ImpColore=clrYellow; // Color double InpAngle=0.0; // Slope angle in degrees ENUM_ANCHOR_POINT InpAnchor=ANCHOR_CENTER; // Anchor type input bool Imp_Avanti_Dietro=false; // Background object input bool ImpSelezione=true; // Highlight to move bool InpHidden=true; // Hidden in the object list long InpZOrder=0; // Priority for mouse click //+------------------------------------------------------------------+ //| Create a text label | //+------------------------------------------------------------------+ bool LabelCreate(const long chart_ID=0, // chart's ID const string name="Label", // label name const int sub_window=0, // subwindow index const int x=0, // X coordinate const int y=0, // Y coordinate const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER, // chart corner for anchoring const string text="Label", // text const string font="Arial", // font const int font_size=10, // font size const color clr=clrRed, // color const double angle=0.0, // text slope const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type const bool back=false, // in the background const bool selection=false, // highlight to move const bool hidden=true, // hidden in the object list const long z_order=0) // priority for mouse click { //--- reset the error value ResetLastError(); //--- create a text label if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0)) { Print(__FUNCTION__, ": failed to create text label! Error code = ",GetLastError()); return(false); } //--- set label coordinates ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x); ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y); //--- set the chart's corner, relative to which point coordinates are defined ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner); //--- set the text ObjectSetString(chart_ID,name,OBJPROP_TEXT,text); //--- set text font ObjectSetString(chart_ID,name,OBJPROP_FONT,font); //--- set font size ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size); //--- set the slope angle of the text ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle); //--- set anchor type ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor); //--- set color ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr); //--- display in the foreground (false) or background (true) ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back); //--- enable (true) or disable (false) the mode of moving the label by mouse ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection); ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection); //--- hide (true) or display (false) graphical object name in the object list ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden); //--- set the priority for receiving the event of a mouse click in the chart ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order); //--- successful execution return(true); } //+------------------------------------------------------------------+ //| Move the text label | //+------------------------------------------------------------------+ bool LabelMove(const long chart_ID=0, // chart's ID const string name="Label", // label name const int x=0, // X coordinate const int y=0 ) // Y coordinate { //--- reset the error value ResetLastError(); //--- move the text label if(!ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x)) { Print(__FUNCTION__, ": failed to move X coordinate of the label! Error code = ",GetLastError()); return(false); } if(!ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y)) { Print(__FUNCTION__, ": failed to move Y coordinate of the label! Error code = ",GetLastError()); return(false); } //--- successful execution return(true); } //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { IndicatorBuffers(2); SetIndexBuffer(0, Buffer1); SetIndexEmptyValue(0, 0); SetIndexArrow(0, 241); SetIndexBuffer(1, Buffer2); SetIndexEmptyValue(1, 0); SetIndexArrow(1, 241); //initialize myPoint myPoint = Point(); if(Digits() == 5 || Digits() == 3) { myPoint *= 10; } return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime& time[], const double& open[], const double& high[], const double& low[], const double& close[], const long& tick_volume[], const long& volume[], const int& spread[]) { //++++++++++++++++++++++++++++++ double Distanza_Canale=(Resistance(Numero_Candele*PeriodSeconds(),false,00,00,true,1)-Support(Numero_Candele*PeriodSeconds(),false,00,00,true,1))*100000; string text; text="Larghezza canale= "+Distanza_Canale+" decimi"; //+++++++++++++++++++++++++++++++ int limit = rates_total - prev_calculated; //--- counting from 0 to rates_total ArraySetAsSeries(Buffer1, true); ArraySetAsSeries(Buffer2, true); //--- initial zero if(prev_calculated < 1) { ArrayInitialize(Buffer1, 0); ArrayInitialize(Buffer2, 0); } else limit++; //--- main loop for(int i = limit-1; i >= 0; i--) { if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation //Indicator Buffer 1 if(Close[i] > Support(Numero_Candele * PeriodSeconds(), false, 00, 00, true, 1+i) //Candlestick Close > Support ) { Buffer1[i] = Low[i]; //Set indicator value at Candlestick Low } else { Buffer1[i] = 0; } //Indicator Buffer 2 if(Close[i] < Resistance(Numero_Candele * PeriodSeconds(), false, 00, 00, true, 1+i) //Candlestick Close < Resistance ) { Buffer2[i] = Low[i]; //Set indicator value at Candlestick Low } else { Buffer2[i] = 0; } } return(rates_total); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { double Distanza_Canale=(Resistance(Numero_Candele*PeriodSeconds(),false,00,00,true,1)-Support(Numero_Candele*PeriodSeconds(),false,00,00,true,1))*100000; //--- prepare initial text for the label string text; text="Larghezza canale= "+Distanza_Canale+" decimi"; //--- create a text label on the chart if(!LabelCreate(0,InpName,0,InpX,InpY,CORNER_LEFT_UPPER,text,InpFont,ImpFontSize, ImpColore,InpAngle,InpAnchor,Imp_Avanti_Dietro,ImpSelezione,InpHidden,InpZOrder)) { return; } //--- redraw the chart ChartRedraw(); } //+------------------------------------------------------------------+ int deinit() { ObjectsDeleteAll(); Comment(""); // Cancella tutti gli objects creati return(0); } //+----------------------------------------------------------------
Sorry to say this, but it just a big mess:
- You did NOT remove the OnStart() event handler.
- You did NOT implement the said functionality in the OnCalculate().
- You did NOT correct the OnDeinit() event handler.
- Most of the code is not yours and you have no idea how it works or even if it is correct for use in an Indicator.
You should first learn basic coding skills like in C or C++, before you even tackle MQL. Then first practice code for basic Indicators and learn to code things yourself. Using another person's code without understanding it is just going to end up in a mess.
This is not a question of correcting a bug or a small issue. You are basically requesting for someone to code and entire indicator for you, because you have no idea what you are doing.
Sorry, if that sounds mean and strong, but that is just how it is! So, I suggest that you hire someone to code it for you at the "Freelance" section and then use that code to study it and help your own learning of MQL.
Remember, this forum is for sharing and to help out, not to provide free tuition or couching. If that is what you need or want, then request that in the "Freelance" section and I am sure there will be people willing and able to provide you with tuition for MQL.
Before publishing the code I read many forum posts and I saw that many times were very full answers.
If my job was to be a programmer, I would certainly not here to ask for help.
Anyway, thanks again, Max.
Before publishing the code I read many forum posts and I saw that many times were very full answers.
If my job was to be a programmer, I would certainly not here to ask for help.
Anyway, thanks again, Max.
FMIC gave you good advice, but you chose to ignore that advice.
How can anyone help you if you don't follow their suggestions?
If you really want to learn, start with something simpler. Copying and pasting blocks of code does not work if you cannot make the necessary modifications so that they work together.
Hello, GumRai. I fully agree with you, do not get me wrong. I started with the simplest things, and now I can do the alert, very simple but they work. I tried to do something more complicated, for me, and I set off from the code I posted. I wanted from this code to start the corrections and get it running, trying to figure out where the errors. Initially nothing worked, and I corrected up to make disappear all errors. Now I wanted to figure out how to update the label, but that alone did not succeed. I tried to integrate the code as I said FMIC but I did not succeed. The result was that I have no errors, but the chart is blank. However, there are no problems, thank you all again, Max.
Your code shows that you don't know anything about basics of mql coding. FMIC was very patient with you and he told you what is wrong, but you choose to ignore his advice.
If you a serious about mql programming, first you should learn basics from this book: https://book.mql4.com/
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Thanks for everything, Massimo.