Tomasz Waszczyk
Tomasz Waszczyk

Reputation: 3159

How to get price of Chart HLine objects and calculate Fibonacci levels

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

Upvotes: 1

Views: 4390

Answers (3)

rgunning
rgunning

Reputation: 568

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);
    }

Upvotes: 1

Daniel Kniaz
Daniel Kniaz

Reputation: 4681

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;
 }

Upvotes: 2

rgunning
rgunning

Reputation: 568

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;   
}

Upvotes: 1

Related Questions