1

Three part question:

  1. How to find 2 user created horizontal lines on a chart by name and return the price of each.
  2. Then determine which HLine was crossed by the price most recently to determine trend direction.
  3. Calculate Fibonacci levels based on prices and direction
rgunning
  • 568
  • 2
  • 16
Tomasz Waszczyk
  • 2,680
  • 5
  • 35
  • 73

3 Answers3

2
double value = ObjectGetDouble(0,nameOfHLine,OBJPROP_PRICE1);

this is your value if you have name of the object, if you dont have it - need to loop over all objects:

 string name;
 for(int i=ObjectsTotal()-1;i>=0;i--){
    name = ObjectName(i);
    if(ObjectType(name)!=OBJ_HLINE) continue;
 }
Daniel Kniaz
  • 4,603
  • 2
  • 14
  • 20
1

Difficult to understand exactly what you are after, not sure if you are trying to find the graphical objects or just calculate levels based on the prices. Assuming you have the price of the two horizontal lines, the following structure and function can be used to calculate Fibonacci levels. (price 1 is earlier in time than price 2).

Calculation based on formula found here

struct FibLevel {
    double retrace38;
    double retrace50;
    double retrace61;
    double extension61;
    double extension100;
    double extension138;
    double extension161;
};

void FibLevel(double price1, double price2,FibLevel &fiblevel)
{
    double range = MathAbs(price1-price2);
    fiblevel.retrace38   =(price1<price2)?price2-range*0.382:price1+range*0.382;
    fiblevel.retrace50   =(price1<price2)?price2-range*0.500:price1+range*0.500;
    fiblevel.retrace61   =(price1<price2)?price2-range*0.618:price1+range*0.618;
    fiblevel.extension61 =(price1<price2)?price2+range*0.618:price1-range*0.618;
    fiblevel.extension100=(price1<price2)?price2+range      :price1-range;
    fiblevel.extension138=(price1<price2)?price2+range*1.382:price1-range*1.382;
    fiblevel.extension161=(price1<price2)?price2+range*1.618:price1-range*1.618;   
}
rgunning
  • 568
  • 2
  • 16
  • Thanks. I want to write an EA. User creates two horizontal lines and EA have to based on those horizontal line to calculate fibonacci levels - without showing, just to calculate. I do not know how to get a price from horizontal line on chart to source code. – Tomasz Waszczyk Jun 08 '17 at 13:40
  • 1
    ok, so the real question is how to get price of user created horizontal lines. Grabbing specific user created lines and determining which line corresponds to the earlier point in time is more challenging than just calculating the fibonacci levels – rgunning Jun 08 '17 at 13:50
  • Exactly. I just want to grab user created lines (name of lines are given) read a prices from lines and then calculate fibonacci levels. – Tomasz Waszczyk Jun 08 '17 at 13:53
  • a simpler approach would be to create a fibonacci object in the EA which the user can drag about then return the levels from the object. Saves looking for two lines and determining start and end time – rgunning Jun 08 '17 at 14:20
  • From the line I just want to grab the price. Not time. – Tomasz Waszczyk Jun 08 '17 at 14:31
  • @TomaszWaszczyk you need the time to work out the trend direction. the Placement of the fibonnacci levels changes between up and down trends as can be seen in the function above – rgunning Jun 08 '17 at 15:08
1

Working example of Fibonacci object that can be edited by the user and printing of fibonacci levels.

#include <ChartObjects/ChartObjectsFibo.mqh>
CChartObjectFibo *Fibo;

int OnInit()
    {
     Fibo = new CChartObjectFibo();
     #Create object and set some defaults
     if(!Fibo.Create(0,"Fibonacci",0,Time[5],Open[5],Time[0],Open[0]))
     {
            return(INIT_FAILED);
     }
     # Allow user to drag object
     Fibo.Selectable(true);
     return(INIT_SUCCEEDED);
    }

void OnDeinit(const int reason)
    {
     delete Fibo;
    }

void OnTick()
    {
     string level_description;
     double level_value;
     string printString="Fibonacci Levels - ";
     # Get the two anchor prices
     double p1 = Fibo.GetDouble(OBJPROP_PRICE,0);
     double p2 = Fibo.GetDouble(OBJPROP_PRICE,1);
     # Calculate range
     double range=MathAbs(p1-p2);
     for(int i=0;i<Fibo.LevelsCount();i++)
     {
            level_description=Fibo.LevelDescription(i);
            # Calculate price of level
            level_value=(p2>p1)?p2-range*Fibo.LevelValue(i):p2+range*Fibo.LevelValue(i);
            printString=StringFormat("%s %s:%.5f",printString,level_description,level_value);
     }
     Print(printString);
    }
rgunning
  • 568
  • 2
  • 16
  • Thanks. How to change a default color for fibonacci object? I was looking for some property for that https://www.mql5.com/en/docs/standardlibrary/chart_object_classes/obj_fibonacci/cchartobjectfibo/cchartobjectfibocreate but I cannot find. – Tomasz Waszczyk Jun 08 '17 at 17:14
  • Fibo.Color(clrBlue) or Fibo.LevelColor(i,clrBlue) https://www.mql5.com/en/docs/standardlibrary/chart_object_classes/cchartobject – rgunning Jun 08 '17 at 17:19