Array and object delete in MQL4 indicator

Array and object delete in MQL4 indicator

Manage alerts

Loading saved threads...

Andrea Ramengo · External communityPost link
External question — Stack Overflow Stack Exchange Author: Andrea Ramengo Original post: https://stackoverflow.com/questions/76753464 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 trying to create an indicator that identifies some price zones that interest me. I was able to create it perfectly and the logic works, the only problem is that I would like to eliminate the price zones that are invalidated (ie exceeded) but it is proving very difficult for me. I've tried saving the values of the object in an array, and then comparing them to the prices, but no luck. Now I've tried another approach, saving the name in the array, then fetching the price with Objectget. With a comment I can see that the name is saved in the array, but not working :( This is the code I use in a while loop inside oncalculate to create the zones and save the name : if (isCandleSequenceLong) { string LongName = "Bullish" + DoubleToString (Low[i + 2 ], 0 ); ObjectCreate (LongName, OBJ_RECTANGLE , 0 , Time[i + 3 ], High[i + 2 ], EMPTY_VALUE , Low[i + 2 ]); ObjectSet(LongName, OBJPROP_COLOR , Lime); if (!FillZone) ObjectSet(LongName, OBJPROP_BACK , false ); rectangleLongName[i] = LongName; isCandleSequenceLong = false ; } if (isCandleSequenceShort) { string ShortName = "Bearish" + DoubleToString (High[i + 2 ], 0 ); ObjectCreate (ShortName, OBJ_RECTANGLE , 0 , Time[i + 3 ], High[i + 2 ], EMPTY_VALUE , Low[i + 2 ]); ObjectSet(ShortName, OBJPROP_COLOR , Red); if (!FillZone) ObjectSet(ShortName, OBJPROP_BACK , false ); rectangleShortName[i] = ShortName; isCandleSequenceShort = false ; } And this is the function that should eliminate them : void DeleteBrokeZone() { for ( int j = 0 ; j < ObjectsTotal ()- 1 ; j++) { double LongPrice = ObjectGet(rectangleLongName[j],OBJPROP_PRICE2); double ShortPrice= ObjectGet(rectangleShortName[j],OBJPROP_PRICE1); Comment (LongPrice); if (Close[j + 1 ] > LongPrice && Close[j] < LongPrice ) { if (!ObjectDelete (rectangleLongName[j])) Print ( "Error:" + rectangleLongName[j]); } if (Close[j + 1 ] < ShortPrice && Close[j] > ShortPrice) { if (!ObjectDelete (rectangleShortName[j])) Print ( "Error:" + rectangleShortName[j]); } } } Thanks to whoever can help me!
Quote
Report
Andrea Ramengo · External communityPost link
External answer — Stack Overflow Stack Exchange Author: Andrea Ramengo Original post: https://stackoverflow.com/a/76786788 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. if(isCandleSequenceLong) { static int longcounter = 1000; string LongName = "Bullish" + IntegerToString(longcounter, 0); ObjectCreate(LongName, OBJ_RECTANGLE, 0, Time[i + 3 ], High[i + 2 ], EMPTY_VALUE, Low[i + 2 ]); ObjectSet(LongName, OBJPROP_COLOR, Lime); if(!FillZone) ObjectSet(LongName, OBJPROP_BACK, false); rectangleLongName[longArrayCounter] = LongName; isCandleSequenceLong = false ; longcounter++; longArrayCounter++; } if(isCandleSequenceShort) { static int shortcounter = 1000; string ShortName = "Bearish" + IntegerToString(shortcounter, 0); ObjectCreate(ShortName, OBJ_RECTANGLE, 0, Time[i + 3 ], High[i + 2 ], EMPTY_VALUE, Low[i + 2 ]); ObjectSet(ShortName, OBJPROP_COLOR, Red); if(!FillZone) ObjectSet(ShortName, OBJPROP_BACK, false); rectangleShortName[shortArrayCounter] = ShortName; isCandleSequenceShort = false ; shortcounter++; shortArrayCounter++; } void DeleteBrokeZone() { for (int i = 0; i < ArraySize(rectangleLongName); i++) { if (StringLen(rectangleLongName[i]) > 0) { if (ObjectFind(NULL, rectangleLongName[i]) != -1) { price1 = ObjectGetDouble(0, rectangleLongName[i], OBJPROP_PRICE1); price2 = ObjectGetDouble(0, rectangleLongName[i], OBJPROP_PRICE2); if (Close[i] < price2) { if (!ObjectDelete(0, rectangleLongName[i])) { Print("Error deleting Long object: " + rectangleLongName[i]); } else Print("Deleted Long object: " + rectangleLongName[i]); rectangleLongName[i] = ""; RefreshRates(); } } } } for (int x = 0; x < ArraySize(rectangleShortName); x++) { if (StringLen(rectangleShortName[x]) > 0) { if (ObjectFind(NULL, rectangleShortName[x]) != -1) { price1 = ObjectGetDouble(0, rectangleShortName[x], OBJPROP_PRICE1); price2 = ObjectGetDouble(0, rectangleShortName[x], OBJPROP_PRICE2); if (Close[x] > price1) { if (!ObjectDelete(0, rectangleShortName[x])) { Print("Error deleting Short object: " + rectangleShortName[x]); } else Print("Deleted Short object: " + rectangleShortName[x]); rectangleShortName[x] = ""; RefreshRates(); } } } } } With these changes I can get it to delete some invalid zones, but unfortunately not all. Some remain designed even if surpassed by the price
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: Andrea Ramengo Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/a/76786788 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. if(isCandleSequenceLong) { static int longcounter = 1000; string LongName = "Bullish" + IntegerToString(longcounter, 0); ObjectCreate(LongName, OBJ_RECTANGLE, 0, Time[i + 3 ], High[i + 2 ], EMPTY_VALUE, Low[i + 2 ]); ObjectSet(LongName, OBJPROP_COLOR, Lime); if(!FillZone) ObjectSet(LongName, OBJPROP_BACK, false); rectangleLongName[longArrayCounter] = LongName; isCandleSequenceLong = false ; longcounter++; longArrayCounter++; } if(isCandleSequenceShort) { static int shortcounter = 1000; string ShortName = "Bearish" + IntegerToString(shortcounter, 0); ObjectCreate(ShortName, OBJ_RECTANGLE, 0, Time[i + 3 ], High[i + 2 ], EMPTY_VALUE, Low[i + 2 ]); ObjectSet(ShortName, OBJPROP_COLOR, Red); if(!FillZone) ObjectSet(ShortName, OBJPROP_BACK, false); rectangleShortName[shortArrayCounter] = ShortName; isCandleSequenceShort = false ; shortcounter++; shortArrayCounter++; } void DeleteBrokeZone() { for (int i = 0; i < ArraySize(rectangleLongName); i++) { if (StringLen(rectangleLongName[i]) > 0) { if (ObjectFind(NULL, rectangleLongName[i]) != -1) { price1 = ObjectGetDouble(0, rectangleLongName[i], OBJPROP_PRICE1); price2 = ObjectGetDouble(0, rectangleLongName[i], OBJPROP_PRICE2); if (Close[i] < price2) { if (!ObjectDelete(0, rectangleLongName[i])) { Print("Error deleting Long object: " + rectangleLongName[i]); } else Print("Deleted Long object: " + rectangleLongName[i]); rectangleLongName[i] = ""; RefreshRates(); } } } } for (int x = 0; x < ArraySize(rectangleShortName); x++) { if (StringLen(rectangleShortName[x]) > 0) { if (ObjectFind(NULL, rectangleShortName[x]) != -1) { price1 = ObjectGetDouble(0, rectangleShortName[x], OBJPROP_PRICE1); price2 = ObjectGetDouble(0, rectangleShortName[x], OBJPROP_PRICE2); if (Close[x] > price1) { if (!ObjectDelete(0, rectangleShortName[x])) { Print("Error deleting Short object: " + rectangleShortName[x]); } else Print("Deleted Short object: " + rectangleShortName[x]); rectangleShortName[x] = ""; RefreshRates(); } } } } } With these changes I can get it to delete some invalid zones, but unfortunately not all. Some remain designed even if surpassed by the price

Cancel quote

Checking account access…
Array and object delete in MQL4 indicator | Forex.com.bd