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

只愿长相守 提交于 2019-12-11 04:49:07

问题


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

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



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



回答3:


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


来源:https://stackoverflow.com/questions/44434413/how-to-get-price-of-chart-hline-objects-and-calculate-fibonacci-levels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!