Why are upper and lower pitchfork lines staying red in MetaTrader 4?

Why are upper and lower pitchfork lines staying red in MetaTrader 4?

Manage alerts

Loading saved threads...

Nicolò Mugetti · External communityPost link
External question — Stack Overflow Stack Exchange Author: Nicolò Mugetti Original post: https://stackoverflow.com/questions/79039457 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I’m working on an MQL4 code for MetaTrader 4 that creates a custom Pitchfork with three lines: the central, upper, and lower lines. The central line, which is supposed to be red, works correctly. However, the upper and lower lines, which should be blue (as set in the default options), remain red. I’ve checked the default color settings in MetaTrader, which clearly show that the upper and lower lines should be blue, while the central red line is displayed correctly. Despite this, when I place the Pitchfork on the chart, all three lines appear red. MT4 input blue and red line MT4 all lines of pitchfork are red I’m looking for a solution that will allow the upper and lower lines of the Pitchfork to respect the default settings and display in blue, while the central line remains red. I’ve attached the MQL4 code, hoping that someone can spot what’s causing this issue and suggest how to fix it. Ideally, I’d like the outcome to be similar to what I get on TradingView, where the upper and lower lines are blue, and the central one stays red. TradingView pitchfork with blue and red line Code: #property copyright "Your Name" #property link "http://www.yourwebsite.com" #property version "1.00" #property strict #property indicator_chart_window input datetime Time1 = D'2022.01.01 00:00'; // Tempo punto 1 input double Price1 = 0; // Prezzo punto 1 input datetime Time2 = D'2022.01.02 00:00'; // Tempo punto 2 input double Price2 = 0; // Prezzo punto 2 input datetime Time3 = D'2022.01.03 00:00'; // Tempo punto 3 input double Price3 = 0; // Prezzo punto 3 input color InpUpperColor = clrBlue; // Colore linea superiore input color InpMiddleColor = clrRed; // Colore linea centrale input color InpLowerColor = clrBlue; // Colore linea inferiore input color InpCursorColor = clrWhite; // Colore cursore e testo input ENUM_LINE_STYLE InpLineStyle = STYLE_SOLID; // Stile linee input int InpLineWidth = 1; // Spessore linee input int InpProximity = 10; // Prossimità per snap (in pixel) bool drawing = true; int point_count = 0; datetime time1, time2, time3; double price1, price2, price3; string cursor_line = "CursorLine"; string price_text = "PriceText"; string time_text = "TimeText"; string pitchfork_name = "Pitchfork"; int OnInit() { EventSetTimer(1); ChartSetInteger(0, CHART_EVENT_MOUSE_MOVE, true); if(Time1 != D'2022.01.01 00:00' && Price1 != 0 && Time2 != D'2022.01.02 00:00' && Price2 != 0 && Time3 != D'2022.01.03 00:00' && Price3 != 0) { time1 = Time1; price1 = Price1; time2 = Time2; price2 = Price2; time3 = Time3; price3 = Price3; DrawPitchfork(); drawing = false; } return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { EventKillTimer(); ObjectDelete(0, pitchfork_name); ObjectDelete(0, cursor_line); ObjectDelete(0, price_text); ObjectDelete(0, time_text); } 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[]) { return(rates_total); } void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { if(id == CHARTEVENT_CLICK && drawing) { datetime clicked_time; double clicked_price; int window; if(ChartXYToTimePrice(0, (int)lparam, (int)dparam, window, clicked_time, clicked_price)) { int bar = iBarShift(NULL, 0, clicked_time); datetime bar_time = iTime(NULL, 0, bar); double bar_open = iOpen(NULL, 0, bar); double bar_high = iHigh(NULL, 0, bar); double bar_low = iLow(NULL, 0, bar); double bar_close = iClose(NULL, 0, bar); // Applica lo snap al prezzo più vicino clicked_price = GetNearestPrice(clicked_price, bar_open, bar_high, bar_low, bar_close); point_count++; switch(point_count) { case 1: time1 = bar_time; price1 = clicked_price; break; case 2: time2 = bar_time; price2 = clicked_price; break; case 3: time3 = bar_time; price3 = clicked_price; DrawPitchfork(); drawing = false; break; } } } else if(id == CHARTEVENT_MOUSE_MOVE && drawing) { datetime cursor_time; double cursor_price; int window; if(ChartXYToTimePrice(0, (int)lparam, (int)dparam, window, cursor_time, cursor_price)) { int bar = iBarShift(NULL, 0, cursor_time); datetime bar_time = iTime(NULL, 0, bar); double bar_open = iOpen(NULL, 0, bar); double bar_high = iHigh(NULL, 0, bar); double bar_low = iLow(NULL, 0, bar); double bar_close = iClose(NULL, 0, bar); // Applica lo snap al prezzo più vicino cursor_price = GetNearestPrice(cursor_price, bar_open, bar_high, bar_low, bar_close); ObjectDelete(0, cursor_line); ObjectCreate(0, cursor_line, OBJ_TREND, 0, bar_time, 0, bar_time, ChartGetDouble(0, CHART_PRICE_MAX)); ObjectSetInteger(0, cursor_line, OBJPROP_COLOR, InpCursorColor); ObjectSetInteger(0, cursor_line, OBJPROP_STYLE, STYLE_DOT); ObjectDelete(0, price_text); ObjectCreate(0, price_text, OBJ_TEXT, 0, bar_time, cursor_price); ObjectSetString(0, price_text, OBJPROP_TEXT, DoubleToString(cursor_price, _Digits)); ObjectSetInteger(0, price_text, OBJPROP_COLOR, InpCursorColor); ObjectDelete(0, time_text); ObjectCreate(0, time_text, OBJ_TEXT, 0, bar_time, 0); ObjectSetString(0, time_text, OBJPROP_TEXT, TimeToString(bar_time)); ObjectSetInteger(0, time_text, OBJPROP_COLOR, InpCursorColor); ChartRedraw(); } } } double GetNearestPrice(double price, double bar_open, double bar_high, double bar_low, double bar_close) { // Calcolo dinamico dei prezzi senza array statici double nearest_price = price; double min_distance = DBL_MAX; // Controlla bar_open double distance = MathAbs(price - bar_open); if (distance <= InpProximity && distance < min_distance) { min_distance = distance; nearest_price = bar_open; } // Controlla bar_high distance = MathAbs(price - bar_high); if (distance <= InpProximity && distance < min_distance) { min_distance = distance; nearest_price = bar_high; } // Controlla bar_low distance = MathAbs(price - bar_low); if (distance <= InpProximity && distance < min_distance) { min_distance = distance; nearest_price = bar_low; } // Controlla bar_close distance = MathAbs(price - bar_close); if (distance <= InpProximity && distance < min_distance) { min_distance = distance; nearest_price = bar_close; } return nearest_price; } void DrawPitchfork() { if(ObjectFind(0, pitchfork_name) != -1) ObjectDelete(0, pitchfork_name); ObjectCreate(0, pitchfork_name, OBJ_PITCHFORK, 0, time1, price1, time2, price2, time3, price3); ObjectSetInteger(0, pitchfork_name, OBJPROP_COLOR, InpMiddleColor); // Linea centrale ObjectSetInteger(0, pitchfork_name, OBJPROP_LEVELCOLOR, 1, InpUpperColor); // Primo livello (superiore) ObjectSetInteger(0, pitchfork_name, OBJPROP_LEVELCOLOR, 2, InpLowerColor); // Secondo livello (inferiore) ObjectSetInteger(0, pitchfork_name, OBJPROP_STYLE, InpLineStyle); ObjectSetInteger(0, pitchfork_name, OBJPROP_WIDTH, InpLineWidth); ObjectSetInteger(0, pitchfork_name, OBJPROP_SELECTABLE, true); ObjectSetInteger(0, pitchfork_name, OBJPROP_LEVELWIDTH, 1, InpLineWidth); // Primo livello ObjectSetInteger(0, pitchfork_name, OBJPROP_LEVELWIDTH, 2, InpLineWidth); // Secondo livello ObjectSetInteger(0, pitchfork_name, OBJPROP_LEVELS, 2); ObjectDelete(0, cursor_line); ObjectDelete(0, price_text); ObjectDelete(0, time_text); ChartRedraw(); } void OnTimer() { if(drawing) { string message = "Seleziona il punto " + IntegerToString(point_count + 1) + " del Pitchfork"; Comment(message); } else { Comment("Pitchfork completato. Modifica i punti negli input per aggiustare."); } }
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: Nicolò Mugetti Source score (net votes, not local likes): 1 Original post: https://stackoverflow.com/questions/79039457 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I’m working on an MQL4 code for MetaTrader 4 that creates a custom Pitchfork with three lines: the central, upper, and lower lines. The central line, which is supposed to be red, works correctly. However, the upper and lower lines, which should be blue (as set in the default options), remain red. I’ve checked the default color settings in MetaTrader, which clearly show that the upper and lower lines should be blue, while the central red line is displayed correctly. Despite this, when I place the Pitchfork on the chart, all three lines appear red. MT4 input blue and red line MT4 all lines of pitchfork are red I’m looking for a solution that will allow the upper and lower lines of the Pitchfork to respect the default settings and display in blue, while the central line remains red. I’ve attached the MQL4 code, hoping that someone can spot what’s causing this issue and suggest how to fix it. Ideally, I’d like the outcome to be similar to what I get on TradingView, where the upper and lower lines are blue, and the central one stays red. TradingView pitchfork with blue and red line Code: #property copyright "Your Name" #property link "http://www.yourwebsite.com" #property version "1.00" #property strict #property indicator_chart_window input datetime Time1 = D'2022.01.01 00:00'; // Tempo punto 1 input double Price1 = 0; // Prezzo punto 1 input datetime Time2 = D'2022.01.02 00:00'; // Tempo punto 2 input double Price2 = 0; // Prezzo punto 2 input datetime Time3 = D'2022.01.03 00:00'; // Tempo punto 3 input double Price3 = 0; // Prezzo punto 3 input color InpUpperColor = clrBlue; // Colore linea superiore input color InpMiddleColor = clrRed; // Colore linea centrale input color InpLowerColor = clrBlue; // Colore linea inferiore input color InpCursorColor = clrWhite; // Colore cursore e testo input ENUM_LINE_STYLE InpLineStyle = STYLE_SOLID; // Stile linee input int InpLineWidth = 1; // Spessore linee input int InpProximity = 10; // Prossimità per snap (in pixel) bool drawing = true; int point_count = 0; datetime time1, time2, time3; double price1, price2, price3; string cursor_line = "CursorLine"; string price_text = "PriceText"; string time_text = "TimeText"; string pitchfork_name = "Pitchfork"; int OnInit() { EventSetTimer(1); ChartSetInteger(0, CHART_EVENT_MOUSE_MOVE, true); if(Time1 != D'2022.01.01 00:00' && Price1 != 0 && Time2 != D'2022.01.02 00:00' && Price2 != 0 && Time3 != D'2022.01.03 00:00' && Price3 != 0) { time1 = Time1; price1 = Price1; time2 = Time2; price2 = Price2; time3 = Time3; price3 = Price3; DrawPitchfork(); drawing = false; } return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { EventKillTimer(); ObjectDelete(0, pitchfork_name); ObjectDelete(0, cursor_line); ObjectDelete(0, price_text); ObjectDelete(0, time_text); } 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[]) { return(rates_total); } void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { if(id == CHARTEVENT_CLICK && drawing) { datetime clicked_time; double clicked_price; int window; if(ChartXYToTimePrice(0, (int)lparam, (int)dparam, window, clicked_time, clicked_price)) { int bar = iBarShift(NULL, 0, clicked_time); datetime bar_time = iTime(NULL, 0, bar); double bar_open = iOpen(NULL, 0, bar); double bar_high = iHigh(NULL, 0, bar); double bar_low = iLow(NULL, 0, bar); double bar_close = iClose(NULL, 0, bar); // Applica lo snap al prezzo più vicino clicked_price = GetNearestPrice(clicked_price, bar_open, bar_high, bar_low, bar_close); point_count++; switch(point_count) { case 1: time1 = bar_time; price1 = clicked_price; break; case 2: time2 = bar_time; price2 = clicked_price; break; case 3: time3 = bar_time; price3 = clicked_price; DrawPitchfork(); drawing = false; break; } } } else if(id == CHARTEVENT_MOUSE_MOVE && drawing) { datetime cursor_time; double cursor_price; int window; if(ChartXYToTimePrice(0, (int)lparam, (int)dparam, window, cursor_time, cursor_price)) { int bar = iBarShift(NULL, 0, cursor_time); datetime bar_time = iTime(NULL, 0, bar); double bar_open = iOpen(NULL, 0, bar); double bar_high = iHigh(NULL, 0, bar); double bar_low = iLow(NULL, 0, bar); double bar_close = iClose(NULL, 0, bar); // Applica lo snap al prezzo più vicino cursor_price = GetNearestPrice(cursor_price, bar_open, bar_high, bar_low, bar_close); ObjectDelete(0, cursor_line); ObjectCreate(0, cursor_line, OBJ_TREND, 0, bar_time, 0, bar_time, ChartGetDouble(0, CHART_PRICE_MAX)); ObjectSetInteger(0, cursor_line, OBJPROP_COLOR, InpCursorColor); ObjectSetInteger(0, cursor_line, OBJPROP_STYLE, STYLE_DOT); ObjectDelete(0, price_text); ObjectCreate(0, price_text, OBJ_TEXT, 0, bar_time, cursor_price); ObjectSetString(0, price_text, OBJPROP_TEXT, DoubleToString(cursor_price, _Digits)); ObjectSetInteger(0, price_text, OBJPROP_COLOR, InpCursorColor); ObjectDelete(0, time_text); ObjectCreate(0, time_text, OBJ_TEXT, 0, bar_time, 0); ObjectSetString(0, time_text, OBJPROP_TEXT, TimeToString(bar_time)); ObjectSetInteger(0, time_text, OBJPROP_COLOR, InpCursorColor); ChartRedraw(); } } } double GetNearestPrice(double price, double bar_open, double bar_high, double bar_low, double bar_close) { // Calcolo dinamico dei prezzi senza array statici double nearest_price = price; double min_distance = DBL_MAX; // Controlla bar_open double distance = MathAbs(price - bar_open); if (distance <= InpProximity && distance < min_distance) { min_distance = distance; nearest_price = bar_open; } // Controlla bar_high distance = MathAbs(price - bar_high); if (distance <= InpProximity && distance < min_distance) { min_distance = distance; nearest_price = bar_high; } // Controlla bar_low distance = MathAbs(price - bar_low); if (distance <= InpProximity && distance < min_distance) { min_distance = distance; nearest_price = bar_low; } // Controlla bar_close distance = MathAbs(price - bar_close); if (distance <= InpProximity && distance < min_distance) { min_distance = distance; nearest_price = bar_close; } return nearest_price; } void DrawPitchfork() { if(ObjectFind(0, pitchfork_name) != -1) ObjectDelete(0, pitchfork_name); ObjectCreate(0, pitchfork_name, OBJ_PITCHFORK, 0, time1, price1, time2, price2, time3, price3); ObjectSetInteger(0, pitchfork_name, OBJPROP_COLOR, InpMiddleColor); // Linea centrale ObjectSetInteger(0, pitchfork_name, OBJPROP_LEVELCOLOR, 1, InpUpperColor); // Primo livello (superiore) ObjectSetInteger(0, pitchfork_name, OBJPROP_LEVELCOLOR, 2, InpLowerColor); // Secondo livello (inferiore) ObjectSetInteger(0, pitchfork_name, OBJPROP_STYLE, InpLineStyle); ObjectSetInteger(0, pitchfork_name, OBJPROP_WIDTH, InpLineWidth); ObjectSetInteger(0, pitchfork_name, OBJPROP_SELECTABLE, true); ObjectSetInteger(0, pitchfork_name, OBJPROP_LEVELWIDTH, 1, InpLineWidth); // Primo livello ObjectSetInteger(0, pitchfork_name, OBJPROP_LEVELWIDTH, 2, InpLineWidth); // Secondo livello ObjectSetInteger(0, pitchfork_name, OBJPROP_LEVELS, 2); ObjectDelete(0, cursor_line); ObjectDelete(0, price_text); ObjectDelete(0, time_text); ChartRedraw(); } void OnTimer() { if(drawing) { string message = "Seleziona il punto " + IntegerToString(point_count + 1) + " del Pitchfork"; Comment(message); } else { Comment("Pitchfork completato. Modifica i punti negli input per aggiustare."); } }

Cancel quote

Checking account access…