auto_fibo_main_optpq

Auto Fibo Retracement

This MetaTrader 4 indicator automatically draws a Fibonacci retracement on your chart given a set of parameters. The indicator can also be set to highlight the retraced and un-retraced area on the chart.

Download

Product Description

The Auto Fibo Retracement indicator draws a Fibonacci retracement and optionally, the retraced and/or unretraced zone automatically on your chart. The retracement and un/retraced zones can update automatically during a live candle (default setting) or they can wait for the candle close. The retracement and corresponding zones will update automatically when you switch time frames and even symbols.

The default parameters work great, but are limited to the visible bars on the chart (which may be too many or too little). Make sure to experiment with your own parameters. In a nutshell, the calculation parameters can be tweaked to give you a retracement for any area on the chart. You could even isolate the current bar if you wanted to (maybe handy for a scalper)!

Why stop at one? You can add more instances with different parameter sets for multiple retracements.

 

PARAMETERS AND USAGE

  • FiboLevels: the retracement levels to be calculated in the chart object. This parameter must be entered as a string with the retracement levels in decimal form and separated by commas. Levels greater than 1.0 (or 100%) are OK. However, negative retracement levels won’t work on live fibo retracements because a breach of this level will trigger the object to be redrawn.
  • LookbackBars: optional number of bars to be used in the calculation of the retracement levels. If the parameter is left at 0, the indicator will use the visible bars in the calculation (in a sense, this parameter is disregarded if set to 0) and will update on chart zoom in/out.  This parameter is affected significantly by BarShift.
  • BarShift: optional bar shift to start the calculation. The first bar on the chart is 0. So for example, if BarShift is set to 2, the fibo calculation starts 2 bars back. If this is set to 0, the fibo will update during the live candle (unless WaitForCloseToUpdate is set to true, in which case, BarShift should really be set to 1 or higher).
    An example including LookbackBars is if LookbackBars is set to 100 and BarShift is set to 5. The indicator will begin looking for high/low values 5 bars back (BarShift) and continue looking for another 100 bars (LookbackBars). In other words, the bars in calculation are 5 to 105 (remember that most recent bars have a lower number starting from the current bar which has an index of 0) . Finally, the high, low, and trend of bar 5 to 105 are used to calculate and draw the Fibonacci retracement. It’s also worth noting that if BarShift is set to 0, the number of visible bars on the chart will replace it.
  • WaitForCloseToUpdate: only used if BarShift is set to 0 and tells the indicator whether to update the retracement and corresponding retraced/unretraced zones during a live candle or wait for the candle to close.
  • ShowRetracedZone: if true, a rectangle is drawn in the retracement zone. This zone is drawn from 0.0 to the furthest retraced price.
  • RetracedZoneColor: the color of the retracement zone rectangle (if shown).
  • ShowUnretracedZone: if true, a rectangle is drawn in the unretraced zone. This zone is drawn from  the furthest retraced price (edge of retracement zone) to 1.0 (100%).
  • UnretracedZoneColor: the color of the unretraced zone rectangle (if shown).
  • FiboObjPrefix: thenaming  prefix for all chart objects created by the indicator. Most people don’t need to change this. This can be used by other indicators, expert advisors, or scripts to find objects created by this indicator.

Feel free to tweak the above parameters as you see fit. However, the indicator should work fine with the default settings and can be simply added to charts after installation. It is perfectly OK to edit the inputs after applying the indicator to a chart from right click->Indicators List (or CTRL + I).

If you are isolating a single bar or two for retracement, the zone rectangle(s) may not draw properly.

You can also automatically generate more than one retracement by adding more instances of the indicator to the chart. Please note that this only makes sense if each instance has different parameters. Otherwise, there will be redundant chart objects using up resources with no additional utility.

Each instance tracks its own objects and only those objects will be removed when it is removed from a chart.

AutoFiboRetracementIndicator.mq4 (Currently Viewing)
RetracementDrawer.mqh View
/*
 * Copyright (c) 2010-2015 TradertoolsFX
 *  
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#property copyright "Copyright (c) 2010-2015 TradertoolsFX"
#property link      "http://www.tradertools-fx.com"
#property description "This content is released under the terms of the MIT license (http://opensource.org/licenses/mit-license.html)."
#property version "4.00"
#property strict
#property indicator_chart_window

#include <RetracementDrawer.mqh>

// User Parameters
input string FiboLevels = "0.0, 0.236, 0.382, 0.50, 0.618, 0.786, 1.0";
input string _desc1 = "===Enter parameters for fibonacci calculation below===";
input int LookbackBars = 0; // If 0, uses visible bars
input int BarShift = 0;
input bool WaitForCloseToUpdate = false;
input string _desc2 = "===Enter retraced/unretraced zone options below===";
input bool ShowRetracedZone = true;
input color RetracedZoneColor = Green;
input bool ShowUnretracedZone = true;
input color UnretracedZoneColor = Red;
input string _desc3 = "===Other parameters===";
input string FiboObjPrefix = "AutoFibo_";

RetracementDrawer *g_drawer;
bool g_redraw;
int g_end, g_start;

int init() 
{
    string prefix = StringConcatenate(FiboObjPrefix, TimeLocal(), "_");          
    double levels[];
    int num_levels = str_explode_double(FiboLevels, levels, ',');
    
    g_drawer = new RetracementDrawer(levels,
                                     num_levels, 
                                     prefix,
                                     RetracedZoneColor,
                                     UnretracedZoneColor);

    /* Use timer rather than start because if no ticks come in,
     * the chart objects will not respond to zoom. */
    EventSetTimer(1);                                                                                                                       
    return 0;
}

int deinit() 
{
    EventKillTimer();
    delete g_drawer;
    return 0;
}

void OnTimer()
{
    draw();
}

int start()
{
    return 0;
}

int draw() 
{   
    set_drawing_vars();
    
    // If drawing in real time or this is a new bar 
    if (!(WaitForCloseToUpdate || BarShift) || 
            is_new_bar()) {
        g_drawer.set_range(BarShift, g_end);
        
        draw_fibo();
        
        if (ShowRetracedZone) {
            draw_retraced_zone();
        } 
        
        if (ShowUnretracedZone) {
            draw_unretraced_zone();
        } 
    }   
   
    return 0;
}

/* Draw or update fibonacci retracement. */
void draw_fibo()
{
    int res = g_drawer.draw_fibo(g_redraw);
    if (res == -1) { // Failure
        Print("Failed to draw fibo");
    } else if (res == -2) { // Already exists
        if (g_drawer.update_fibo() != 0) { // Failure
            Print("Failed to update fibo");
        }
    }
}

/* Draw or update retraced zone. */
void draw_retraced_zone()
{    
    int res = g_drawer.draw_retraced_zone(g_redraw);
    if (res == -1) { // Failure
        Print("Failed to draw retraced zone");
    } else if (res == -2) { // Already exists
        if (g_drawer.update_retraced_zone() != 0) { // Failure
            Print("Failed to update retraced zone");
        }
    }    
}

/* Draw or update unretraced zone. */
void draw_unretraced_zone()
{
    int res = g_drawer.draw_unretraced_zone(g_redraw);
    if (res == -1) { // Failure
        Print("Failed to draw unretraced zone");
    } else if (res == -2) { // Already exists
        if (g_drawer.update_unretraced_zone() != 0) { // Failure
            Print("Failed to update unretraced zone");
        }
    }
}

/* Set globals that control drawing. */
void set_drawing_vars()
{
    int tmp;
    
    if (LookbackBars) {
        g_end = LookbackBars;
        g_redraw = false;
    } else {
        tmp = WindowFirstVisibleBar();
        g_redraw = (tmp != g_end);
        g_end = tmp;
    }
    
    g_start = BarShift;
}

bool is_new_bar() 
{
    static datetime open = 0;
    if (open != Time[0]) {
        open = Time[0];
        return true;
    }
    return false;
}

int str_explode_double(string list, 
                       double& out_array[], 
                       ushort delimiter=',') 
{
    string double_str[];
    int n = StringSplit(list, delimiter, double_str);
    
    if (ArrayResize(out_array, n) == -1) {
        Print(__FUNCTION__, " failed to resize explode_array!");
        return -1;
    }
    
    for (int i = 0; i < n; i++) {
        out_array[i] = StringToDouble(double_str[i]);
    }
    
    return n;
}