//+------------------------------------------------------------------+ //| BearBull.mq4| //| Copyright 2024, MetaQuotes Software Corp. | //| https://www.metaquotes.net | //+------------------------------------------------------------------+ #property strict #property indicator_chart_window // Definiert das Fenster des Indikators //--- input parameters input color BullishLineColor = clrGreen; // Farbe der vertikalen Linie für bullische Kerzen input color BearishLineColor = clrRed; // Farbe der vertikalen Linie für bärische Kerzen input int ButtonXSize = 60; // Breite des Buttons input int ButtonYSize = 25; // Höhe des Buttons input int ButtonXDistance = 60; // X-Abstand des Buttons input int ButtonYDistance = 10; // Y-Abstand des Buttons input string ButtonFont = "Arial Black"; // Schriftart des Buttons input int ButtonFontSize = 9; // Schriftgröße des Buttons input color ButtonFontColor = clrBlack; // Schriftfarbe des Buttons input color ButtonOnColor = clrLime; // Farbe des Buttons, wenn er eingeschaltet ist input color ButtonOffColor = clrRed; // Farbe des Buttons, wenn er ausgeschaltet ist input color ButtonBackgroundColor = clrGold; // Hintergrundfarbe des Buttons input color ButtonBorderColor = clrBlack; // Randfarbe des Buttons input ENUM_BASE_CORNER corner = CORNER_RIGHT_UPPER; // Ecke des Buttons input double FiboLevel1 = 0.382; // Fibonacci Level 1 input double FiboLevel2 = 0.5; // Fibonacci Level 2 input double FiboLevel3 = 0.618; // Fibonacci Level 3 input double FiboLevel4 = 1.0; // Fibonacci Level 4 input double FiboLevel5 = 1.5; // Fibonacci Level 5 input double FiboLevel6 = 2.0; // Fibonacci Level 6 input double FiboLevel7 = -0.382; // Fibonacci Level 7 input double FiboLevel8 = -0.5; // Fibonacci Level 8 input double FiboLevel9 = -0.618; // Fibonacci Level 9 input double FiboLevel10 = -1.0; // Fibonacci Level 10 input int FiboBarsRight = 2; // FiboLevels-Verängerung input int NumberOfBars = 100; // BarsBack bool showLines = true; // Variable zum Ein-/Ausschalten der Linien //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- create button CreateButton(); //--- initialization done return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- remove all vertical lines and button RemoveAllObjects(); } //+------------------------------------------------------------------+ //| 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[]) { //--- check if there are enough bars if(rates_total < 2 || !showLines) return(0); //--- remove all relevant objects RemoveAllObjects(); //--- loop through the specified number of bars int end = MathMin(NumberOfBars, rates_total); for(int i = 1; i < end; i++) { //--- check for bearish candle followed by bullish candle with lower low, higher high, open <= close of bearish candle, and close >= open of bearish candle if(close[i] < open[i] && close[i-1] > open[i-1] && low[i-1] < low[i] && high[i-1] > high[i] && open[i-1] <= close[i] && close[i-1] >= open[i]) { //--- draw vertical line at bullish candle datetime time_line = time[i-1]; ObjectCreate(0, "BullishLine"+IntegerToString(i), OBJ_VLINE, 0, time_line, 0); ObjectSetInteger(0, "BullishLine"+IntegerToString(i), OBJPROP_COLOR, BullishLineColor); DrawFibonacci(i-1, FiboLevel1, FiboLevel2, FiboLevel3, FiboLevel4, FiboLevel5, FiboLevel6, FiboLevel7, FiboLevel8, FiboLevel9, FiboLevel10, FiboBarsRight); } //--- check for bullish candle followed by bearish candle with higher high, lower low, open >= close of bullish candle, and close <= open of bullish candle if(close[i] > open[i] && close[i-1] < open[i-1] && high[i-1] > high[i] && low[i-1] < low[i] && open[i-1] >= close[i] && close[i-1] <= open[i]) { //--- draw vertical line at bearish candle datetime time_line = time[i-1]; ObjectCreate(0, "BearishLine"+IntegerToString(i), OBJ_VLINE, 0, time_line, 0); ObjectSetInteger(0, "BearishLine"+IntegerToString(i), OBJPROP_COLOR, BearishLineColor); DrawFibonacci(i-1, FiboLevel1, FiboLevel2, FiboLevel3, FiboLevel4, FiboLevel5, FiboLevel6, FiboLevel7, FiboLevel8, FiboLevel9, FiboLevel10, FiboBarsRight); } } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ //| Draw vertical line | //+------------------------------------------------------------------+ void DrawVerticalLine(int index, color lineColor) { string name = "Line_" + IntegerToString(index); if(ObjectFind(0, name) != 0) { ObjectCreate(0, name, OBJ_VLINE, 0, Time[index], 0); ObjectSetInteger(0, name, OBJPROP_COLOR, lineColor); ObjectSetInteger(0, name, OBJPROP_WIDTH, 1); } } //+------------------------------------------------------------------+ //| Draw Fibonacci levels | //+------------------------------------------------------------------+ void DrawFibonacci(int index, double level1, double level2, double level3, double level4, double level5, double level6, double level7, double level8, double level9, double level10, int barsRight) { string fiboName = "Fibo_" + IntegerToString(index); // Entferne das alte Fibonacci-Objekt, falls es existiert if(ObjectFind(0, fiboName) == 0) { ObjectDelete(0, fiboName); } double high = High[index]; double low = Low[index]; datetime endTime = Time[index] + PeriodSeconds() * barsRight; // Erstelle das neue Fibonacci-Objekt ObjectCreate(0, fiboName, OBJ_FIBO, 0, Time[index], high, endTime, low); ObjectSetInteger(0, fiboName, OBJPROP_COLOR, clrMagenta); ObjectSetInteger(0, fiboName, OBJPROP_RAY_RIGHT, false); // RAY bleibt immer false ObjectSetInteger(0, fiboName, OBJPROP_LEVELS, 10); // Setze die Anzahl der Levels auf 10 ObjectSetDouble(0, fiboName, OBJPROP_LEVELVALUE, 0, level1); ObjectSetDouble(0, fiboName, OBJPROP_LEVELVALUE, 1, level2); ObjectSetDouble(0, fiboName, OBJPROP_LEVELVALUE, 2, level3); ObjectSetDouble(0, fiboName, OBJPROP_LEVELVALUE, 3, level4); ObjectSetDouble(0, fiboName, OBJPROP_LEVELVALUE, 4, level5); ObjectSetDouble(0, fiboName, OBJPROP_LEVELVALUE, 5, level6); ObjectSetDouble(0, fiboName, OBJPROP_LEVELVALUE, 6, level7); ObjectSetDouble(0, fiboName, OBJPROP_LEVELVALUE, 7, level8); ObjectSetDouble(0, fiboName, OBJPROP_LEVELVALUE, 8, level9); ObjectSetDouble(0, fiboName, OBJPROP_LEVELVALUE, 9, level10); } //+------------------------------------------------------------------+ //| Create button function | //+------------------------------------------------------------------+ void CreateButton() { string buttonName = "Engulf"; if(ObjectFind(0, buttonName) != 0) { ObjectCreate(0, buttonName, OBJ_BUTTON, 0, 0, 0); ObjectSetInteger(0, buttonName, OBJPROP_CORNER, corner); ObjectSetInteger(0, buttonName, OBJPROP_XSIZE, ButtonXSize); ObjectSetInteger(0, buttonName, OBJPROP_YSIZE, ButtonYSize); ObjectSetInteger(0, buttonName, OBJPROP_XDISTANCE, ButtonXDistance); ObjectSetInteger(0, buttonName, OBJPROP_YDISTANCE, ButtonYDistance); ObjectSetInteger(0, buttonName, OBJPROP_COLOR, ButtonBorderColor); ObjectSetInteger(0, buttonName, OBJPROP_STYLE, STYLE_SOLID); ObjectSetInteger(0, buttonName, OBJPROP_WIDTH, 1); ObjectSetInteger(0, buttonName, OBJPROP_HIDDEN, true); ObjectSetInteger(0, buttonName, OBJPROP_FONTSIZE, ButtonFontSize); ObjectSetInteger(0, buttonName, OBJPROP_BORDER_TYPE, BORDER_RAISED); ObjectSetString(0, buttonName, OBJPROP_TEXT, "Engulf"); ObjectSetInteger(0, buttonName, OBJPROP_COLOR, ButtonFontColor); ObjectSetInteger(0, buttonName, OBJPROP_BACK, true); ObjectSetInteger(0, buttonName, OBJPROP_SELECTABLE, true); ObjectSetInteger(0, buttonName, OBJPROP_SELECTED, true); ObjectSetInteger(0, buttonName, OBJPROP_HIDDEN, true); ObjectSetInteger(0, buttonName, OBJPROP_ZORDER, 0); ObjectSetInteger(0, buttonName, OBJPROP_CORNER, corner); ObjectSetInteger(0, buttonName, OBJPROP_XSIZE, ButtonXSize); ObjectSetInteger(0, buttonName, OBJPROP_YSIZE, ButtonYSize); ObjectSetInteger(0, buttonName, OBJPROP_XDISTANCE, ButtonXDistance); ObjectSetInteger(0, buttonName, OBJPROP_YDISTANCE, ButtonYDistance); ObjectSetInteger(0, buttonName, OBJPROP_COLOR, ButtonBorderColor); ObjectSetInteger(0, buttonName, OBJPROP_STYLE, STYLE_SOLID); ObjectSetInteger(0, buttonName, OBJPROP_WIDTH, 1); ObjectSetInteger(0, buttonName, OBJPROP_HIDDEN, true); ObjectSetInteger(0, buttonName, OBJPROP_FONTSIZE, ButtonFontSize); ObjectSetInteger(0, buttonName, OBJPROP_BORDER_TYPE, BORDER_RAISED); ObjectSetString(0, buttonName, OBJPROP_TEXT, "Engulf"); ObjectSetInteger(0, buttonName, OBJPROP_COLOR, ButtonFontColor); ObjectSetInteger(0, buttonName, OBJPROP_BACK, true); ObjectSetInteger(0, buttonName, OBJPROP_SELECTABLE, true); ObjectSetInteger(0, buttonName, OBJPROP_SELECTED, true); ObjectSetInteger(0, buttonName, OBJPROP_HIDDEN, true); ObjectSetInteger(0, buttonName, OBJPROP_ZORDER, 0); } } //+------------------------------------------------------------------+ //| Button click event handler | //+------------------------------------------------------------------+ void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { if(id == CHARTEVENT_OBJECT_CLICK && sparam == "Engulf") { showLines = !showLines; if(showLines) { ObjectSetInteger(0, "Engulf", OBJPROP_COLOR, ButtonOnColor); } else { ObjectSetInteger(0, "Engulf", OBJPROP_COLOR, ButtonOffColor); RemoveAllObjects(); } } } //+------------------------------------------------------------------+ //| Timer event function | //+------------------------------------------------------------------+ void OnTimer() { //--- remove timer EventKillTimer(); //--- force recalculation ChartRedraw(); } //+------------------------------------------------------------------+ //| Remove all relevant objects | //+------------------------------------------------------------------+ void RemoveAllObjects() { for(int i = ObjectsTotal()-1; i >= 0; i--) { string name = ObjectName(i); if(StringFind(name, "BullishLine") == 0 || StringFind(name, "BearishLine") == 0 || StringFind(name, "Fibo_") == 0) { ObjectDelete(name); } } }