CChartObjectFiboTimes - Get Bar Time Based On Each Level?

 

Hi,


I'm using CChartObjectFiboTimes and assigning custom percentage levels. I would like to draw some boxes between the levels (as a coloured background) but don't know the bar times of each of the vertical levels drawn by CChartObjectFiboTimes as these are calculated at runtime.

I appreciate that the vertical levels need not necessarily align with a bar but they generally do, at least as far as I can see. Is there any way to deduce the closest bar time to each level in the  CChartObjectFiboTimes levels collection?

Many thanks.

 
You can try the following:
  1. Get the current time of the chart using the  TimeCurrent()  function
  2. Iterate through the levels in the  CChartObjectFiboTimes  object
  3. For each level, calculate the time difference between the level's price and the current bar's open time
  4. Find the level with the smallest time difference, which will be the closest to the current bar

Sample code:

#include <ChartObject.mqh>

void DrawBoxesOnFiboLevels(CChartObjectFiboTimes* fibo_object) {
    double levels[] = fibo_object.Levels();
    int num_levels = ArraySize(levels);

    for (int i = 0; i < num_levels; i++) {
        double level_price = levels[i];
        datetime level_time = 0;

        // Find the closest bar time to the current level
        double min_time_diff = double.MAX_VALUE;
        for (int j = 0; j < Bars(); j++) {
            datetime bar_time = Time[j];
            double time_diff = MathAbs(bar_time - TimeCurrent());
            if (time_diff < min_time_diff) {
                min_time_diff = time_diff;
                level_time = bar_time;
            }
        }

        // Draw a box between the current level and the next level
        if (i < num_levels - 1) {
            double next_level_price = levels[i + 1];
            ObjectCreate("FiboBox_" + IntegerToString(i), OBJ_RECTANGLE, 0, level_time, level_price, level_time, next_level_price);
            ObjectSetInteger(0, "FiboBox_" + IntegerToString(i), OBJPROP_COLOR, clrLightBlue);
            ObjectSetInteger(0, "FiboBox_" + IntegerToString(i), OBJPROP_FILL, true);
            ObjectSetInteger(0, "FiboBox_" + IntegerToString(i), OBJPROP_BACK, true);
        }
    }
}
 
Oleksandr Medviediev #:
You can try the following:
  1. Get the current time of the chart using the  TimeCurrent()  function
  2. Iterate through the levels in the  CChartObjectFiboTimes  object
  3. For each level, calculate the time difference between the level's price and the current bar's open time
  4. Find the level with the smallest time difference, which will be the closest to the current bar

Sample code:

Hey,

Thank you very much indeed for the above code/suggestion. It's a fantastic way to approach this problem that I hadn't considered. I really appreciate you taking the time to help! Cheers! :)

 
Oleksandr Medviediev #:
You can try the following:
  1. Get the current time of the chart using the  TimeCurrent()  function
  2. Iterate through the levels in the  CChartObjectFiboTimes  object
  3. For each level, calculate the time difference between the level's price and the current bar's open time
  4. Find the level with the smallest time difference, which will be the closest to the current bar

Sample code:

Hey,

I've been working through your approach and I can't see any way to get the CChartObjectFiboTimes level's price.  There is an inherited Price method on the level class but it doesn't return a meaningful value. The LevelValue method just returns the % value, not a price. Am I missing something? Many thanks!

 

In steps:

  1. Get the current time of the chart using TimeCurrent() function
  2. Iterate through the levels in CChartObjectFiboTimes object
  3. For each level, calculate the time difference between the level's price and the current bar's open time
  4. Find the level with the smallest time difference, which will be the closest to the current bar
Consider using iTime()  instead of Time[]
 
Oleksandr Medviediev #:

In steps:

  1. Get the current time of the chart using TimeCurrent() function
  2. Iterate through the levels in CChartObjectFiboTimes object
  3. For each level, calculate the time difference between the level's price and the current bar's open time
  4. Find the level with the smallest time difference, which will be the closest to the current bar
Consider using iTime()  instead of Time[]

Hey thanks for replying again. I hear what you say above but, the issue is, there doesn't appear to be a way to get the level's price from the levels in the levels collection/array. The only double values from a level represents the percentage of the level, not the price of the level. Cheers!

 
I see, good point.

Try this modification
#include <ChartObject.mqh>

void DrawBoxesOnFiboLevels(CChartObjectFiboTimes* fibo_object) {
    double levels[] = fibo_object.Levels();
    int num_levels = ArraySize(levels);

    // Get the highest and lowest prices on the chart
    double chart_high = High[iHighest(NULL, 0, MODE_HIGH, Bars(), 0)];
    double chart_low = Low[iLowest(NULL, 0, MODE_LOW, Bars(), 0)];

    for (int i = 0; i < num_levels; i++) {
        double level_percent = levels[i];
        double level_price = chart_low + (chart_high - chart_low) * level_percent;

        // Find the closest bar time to the current level
        datetime level_time = 0;
        double min_time_diff = double.MAX_VALUE;
        for (int j = 0; j < Bars(); j++) {
            datetime bar_time = Time[j];
            double time_diff = MathAbs(bar_time - TimeCurrent());
            if (time_diff < min_time_diff) {
                min_time_diff = time_diff;
                level_time = bar_time;
            }
        }

        // Draw a box between the current level and the next level
        if (i < num_levels - 1) {
            double next_level_percent = levels[i + 1];
            double next_level_price = chart_low + (chart_high - chart_low) * next_level_percent;
            ObjectCreate("FiboBox_" + IntegerToString(i), OBJ_RECTANGLE, 0, level_time, level_price, level_time, next_level_price);
            ObjectSetInteger(0, "FiboBox_" + IntegerToString(i), OBJPROP_COLOR, clrLightBlue);
            ObjectSetInteger(0, "FiboBox_" + IntegerToString(i), OBJPROP_FILL, true);
            ObjectSetInteger(0, "FiboBox_" + IntegerToString(i), OBJPROP_BACK, true);
        }
    }
}
Reason: