Fibonacci levels in MQL4

 
Good day to all! I have a question - when creating a Fibonacci Levels object, is it possible to make each level a different colour, and how can I assign a signal value to an EA to each level? Thank you in advance! Sincerely, Eugene!
 
it is better to calculate their values directly in the EA
 

Is this how the function can be prescribed? But the colour does not change at individual levels can there be an error somewhere or something else should be added???

int fibo(datetime X1, double Y1, datetime X2, double Y2)

{

ObjectCreate("Fibo", OBJ_FIBO, 0, 0, 0, 0); // Create object of Fibonacci levels

ObjectSet("Fibo",OBJPROP_COLOR,clrRed); // colour of the diagonal

//ObjectSet("Fibo",OBJPROP_LEVELCOLOR,clrBlue); //the colour of the levels is the same

//ObjectSetInteger(0, "Fibo",OBJPROP_BACK,true);//--- display on the foreground (false) or background (true)


ObjectSet("Fibo",OBJPROP_FIBOLEVELS,13); //-set the number of object levels


ObjectSet("Fibo",OBJPROP_FIRSTLEVEL+0,0.000);//set the value of each object level

ObjectSetDouble(0, "Fibo",OBJPROP_LEVELVALUE,0,0.000);

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELCOLOR,0,clrGreen); // level colour

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELSTYLE,0,STYLE_SOLID);//Line

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELWIDTH,0,1); //--- level thickness

ObjectSetFiboDescription("Fibo",0, "0.000");


ObjectSet("Fibo",OBJPROP_FIRSTLEVEL+1,0.146);

ObjectSetDouble(0, "Fibo",OBJPROP_LEVELVALUE,1,0.146);

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELCOLOR,1,clrGreen); // level colour

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELSTYLE,1,STYLE_SOLID);//Line

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELWIDTH,1,1); //--- level thickness

ObjectSetFiboDescription("Fibo",1, "0.146");


ObjectSet("Fibo",OBJPROP_FIRSTLEVEL+2,0.236);

ObjectSetDouble(0, "Fibo",OBJPROP_LEVELVALUE,2,0.236);

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELCOLOR,2,clrGreen); // level colour

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELSTYLE,2,STYLE_SOLID);//Line

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELWIDTH,2,1); //--- level thickness

ObjectSetFiboDescription("Fibo",2, "0.236");


ObjectSet("Fibo",OBJPROP_FIRSTLEVEL+3,0.382);

ObjectSetDouble(0, "Fibo",OBJPROP_LEVELVALUE,3,0.382);

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELCOLOR,3,clrGreen); // level colour

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELSTYLE,3,STYLE_SOLID);//Line

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELWIDTH,3,1); //--- level thickness

ObjectSetFiboDescription("Fibo",3, "0.382");



ObjectSet("Fibo",OBJPROP_FIRSTLEVEL+4,0.5);

ObjectSetDouble(0, "Fibo",OBJPROP_LEVELVALUE,4,0.5);

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELCOLOR,4,clrGreen); // level colour

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELSTYLE,4,STYLE_SOLID);//Line

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELWIDTH,4,1); //--- level thickness

ObjectSetFiboDescription("Fibo",4, "0.5");


ObjectSet("Fibo",OBJPROP_FIRSTLEVEL+5,0.618);

ObjectSetDouble(0, "Fibo",OBJPROP_LEVELVALUE,5,0.618);

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELCOLOR,5,clrGreen); // level colour

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELSTYLE,5,STYLE_SOLID);//Line

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELWIDTH,5,1); //--- level thickness

ObjectSetFiboDescription("Fibo",5, "0.618");


ObjectSet("Fibo",OBJPROP_FIRSTLEVEL+6,0.764);

ObjectSetDouble(0, "Fibo",OBJPROP_LEVELVALUE,6,0.764);

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELCOLOR,6,clrGreen); // level colour

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELSTYLE,6,STYLE_SOLID);//Line

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELWIDTH,6,1); //--- level thickness

ObjectSetFiboDescription("Fibo",6, "0.764");


ObjectSet("Fibo",OBJPROP_FIRSTLEVEL+7,1);

ObjectSetDouble(0, "Fibo",OBJPROP_LEVELVALUE,7,1);

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELCOLOR,7,clrGreen); // level colour

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELSTYLE,7,STYLE_SOLID);//Line

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELWIDTH,7,1); //--- level thickness

ObjectSetFiboDescription("Fibo",7, "1");


ObjectSet("Fibo",OBJPROP_FIRSTLEVEL+8,1.236);

ObjectSetDouble(0, "Fibo",OBJPROP_LEVELVALUE,8,1.236);

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELCOLOR,8,clrGreen); // level colour

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELSTYLE,8,STYLE_SOLID);//Line

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELWIDTH,8,1); //--- level thickness

ObjectSetFiboDescription("Fibo",8, "1.236");


ObjectSet("Fibo",OBJPROP_FIRSTLEVEL+9,1.618);

ObjectSetDouble(0, "Fibo",OBJPROP_LEVELVALUE,9,1.618);

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELCOLOR,9,clrGreen); // level colour

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELSTYLE,9,STYLE_SOLID);//Line

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELWIDTH,9,1); //--- level thickness

ObjectSetFiboDescription("Fibo",9, "1.618");


ObjectSet("Fibo",OBJPROP_FIRSTLEVEL+10,-0.236);

ObjectSetDouble(0, "Fibo",OBJPROP_LEVELVALUE,10,-0.236);

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELCOLOR,10,clrBlue); // level colour

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELSTYLE,10,STYLE_SOLID);//Line

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELWIDTH,10,1); //--- level thickness

ObjectSetFiboDescription("Fibo",10,"-0.236");


ObjectSet("Fibo",OBJPROP_FIRSTLEVEL+11,-0.618);

ObjectSetDouble(0, "Fibo",OBJPROP_LEVELVALUE,11,-0.618);

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELCOLOR,11,clrBlue); // level colour

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELSTYLE,11,STYLE_SOLID);//Line

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELWIDTH,11,1); //--- level thickness

ObjectSetFiboDescription("Fibo",11,"-0.618");


ObjectSet("Fibo",OBJPROP_FIRSTLEVEL+12,-1);

ObjectSetDouble(0, "Fibo",OBJPROP_LEVELVALUE,12,-1);

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELCOLOR,12,clrBlue); // level colour

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELSTYLE,12,STYLE_SOLID);//Line

ObjectSetInteger(0, "Fibo",OBJPROP_LEVELWIDTH,12,1); //--- level thickness

ObjectSetFiboDescription("Fibo",12,"-1");


ObjectMove("Fibo", 1, X1, Y1);

ObjectMove("Fibo",0, X2, Y2);

return(0);

}


 
int fFiboLevels(string Prefix,int Number
              ,int Bar1,double Price1
              ,int Bar2,double Price2
              ,int Bar3,double Price3
              ,color Цвет,color ЦветОтката
              ,int Видимость=0,int Толщина=1
              ,int УровнейПродолжения=6,int УровнейОтката=5
              ,int Стиль=STYLE_SOLID,int СтильУровня=STYLE_DOT
              ,bool Луч=false,bool Фон=false){
   int Error;
   string Name;
   datetime Time1=Time[Bar1],
            Time2=Time[Bar2],
            Time3=Time[Bar3];
   if( Bar1<=Bar2 || Bar2<=Bar3 || Bar3<1
    || Price1<Zero || Price2<Zero || Price3<Zero
    || Price3-MathMax(Price1,Price2)>Zero
    || MathMin(Price1,Price2)-Price3>Zero ) return(MyError);
   Name=Prefix+"Targets ";
   if( Number>0 ) Name=Name+Number;
   if( ObjectFind(Name)>=0 ) {
      if( !ObjectDelete(Name) ) {
         Error=GetLastError();
         PlaySound("alert.wav");
         Print("***** "+Name+" не создан, ошибка удаления "+Error);
         return(Error);
   }  }
   if( ObjectCreate(Name,OBJ_EXPANSION,0,Time1,Price1,Time2,Price2,Time3,Price3) ){
      if( !ObjectSet(Name, OBJPROP_FIBOLEVELS, УровнейПродолжения)
       || !ObjectSet(Name, OBJPROP_COLOR, Цвет)
       || !ObjectSet(Name, OBJPROP_STYLE, Стиль)
       || !ObjectSet(Name, OBJPROP_WIDTH, Толщина)
       || !ObjectSet(Name, OBJPROP_TIMEFRAMES, Видимость)
       || !ObjectSet(Name, OBJPROP_LEVELCOLOR, Цвет)
       || !ObjectSet(Name, OBJPROP_LEVELSTYLE, СтильУровня)
       || !ObjectSet(Name, OBJPROP_RAY, Луч)
       || !ObjectSet(Name, OBJPROP_BACK, Фон)
       || !ObjectSet(Name, OBJPROP_FIRSTLEVEL+0, 1)
       || !ObjectSet(Name, OBJPROP_FIRSTLEVEL+1, 1.618)
       || !ObjectSet(Name, OBJPROP_FIRSTLEVEL+2, 2.618)
       || !ObjectSet(Name, OBJPROP_FIRSTLEVEL+3, 4.236)
       || !ObjectSet(Name, OBJPROP_FIRSTLEVEL+4, 6.854)
       || !ObjectSet(Name, OBJPROP_FIRSTLEVEL+5, 11.09) ){
         Error=GetLastError();
         PlaySound("alert.wav");
         Print("****  "+Name+" - ошибка модификации параметров "+Error);
   }  }
   else {
      Error=GetLastError();
      PlaySound("alert.wav");
      Print("***** "+Name+" не создан, ошибка "+Error);
   }
   Name=Prefix+"Recoils ";
   if( Number>0 ) Name=Name+Number;
   if( ObjectFind(Name)>=0 ) {
      if( !ObjectDelete(Name) ) {
         Error=GetLastError();
         PlaySound("alert.wav");
         Print("***** "+Name+" не создан, ошибка удаления "+Error);
         return(Error);
   }  }
   if( ObjectCreate(Name,OBJ_FIBO,0,Time1,Price1,Time2,Price2) ){
      if( !ObjectSet(Name, OBJPROP_FIBOLEVELS, УровнейОтката)
       || !ObjectSet(Name, OBJPROP_COLOR, Цвет)
       || !ObjectSet(Name, OBJPROP_STYLE, Стиль)
       || !ObjectSet(Name, OBJPROP_TIMEFRAMES, Видимость)
       || !ObjectSet(Name, OBJPROP_LEVELCOLOR, ЦветОтката)
       || !ObjectSet(Name, OBJPROP_LEVELSTYLE, СтильУровня)
       || !ObjectSet(Name, OBJPROP_RAY, false)
       || !ObjectSet(Name, OBJPROP_BACK, Фон)
       || !ObjectSet(Name, OBJPROP_FIRSTLEVEL+0, 0)
       || !ObjectSet(Name, OBJPROP_FIRSTLEVEL+1, 0.236)
       || !ObjectSet(Name, OBJPROP_FIRSTLEVEL+2, 0.382)
       || !ObjectSet(Name, OBJPROP_FIRSTLEVEL+3, 0.618)
       || !ObjectSet(Name, OBJPROP_FIRSTLEVEL+4, 1) ){
         Error=GetLastError();
         PlaySound("alert.wav");
         Print("****  "+Name+" - ошибка модификации параметров "+Error);
   }  }
   else {
      Error=GetLastError();
      PlaySound("alert.wav");
      Print("***** "+Name+" не создан, ошибка "+Error);
   }
   return(Error);
}
 
jekk:

Is this how the function can be prescribed? But the colour does not change on individual levels can there be an error somewhere or is there something else to add???

Are you looking for an indicator or for an Expert Advisor? If you are using it for an Expert Advisor, then calculating the values of these levels will be enough.

 
For the EA - I want to understand what function can be used to separate each level to assign a signal value? The idea for ZigZag is to find the coordinates - the EA draws Fibonacci levels and from each level (at a distance slightly longer than the spread) we step back and assign more parallel levels - we take them as the basis for making decisions to open an order.
 
jekk:
For the EA - I want to understand what function can be used to separate each level to assign a signal value? My idea for ZigZag is to find coordinates - the Expert Advisor draws Fibonacci levels and from each level (at a distance a little longer than the spread) we step back and set parallel levels - we take them as the basis for decisions to open the order.

For OBJ_FIBO, the colour is the same for all levels. If you are really desperate, draw simple OBJ_TRENDLINE in different colours.
 
jekk:
For the EA - I want to understand what function can be used to separate each level to assign a signal value? My idea for ZigZag is to find coordinates - the EA draws Fibonacci levels and from each level (at a distance a little longer than the spread) we backtrack and set parallel levels - we take them as a base for making decisions about opening an order.

You don't have to separate anything, just take the formula and calculate each level directly in the EA, work with the obtained values, why do you need colours and lines at all?
 
evillive:

Are you looking for an indicator or for an EA? If for an EA, then just calculating the values of these levels is enough, you don't need any prettiness or colour at all.

The visual part should always be there to make it clear (at least in the beginning)
 
evillive:

You don't need to separate anything, you take the formula and calculate each level directly in the EA, work with the values obtained, why do you need colour and lines at all?

Oh, how does the formula look like, can I show you an example?
 
jekk:

Ah, what does the formula look like, can I show you an example???

Well, it's hard to call it a formula: we take the minimum and maximum prices - 0% and 100%, and divide them by the levels, and their values are written all over the world - 23.6%, 38.2%, 50%, 61.8%, 161.8% (or 0.236, 0.382, 0.5, 0.618, 1.618, etc. divided by 0.618, rounded to 3 digits).

for prices of 1.3254 and 1.3896 something like this:

directreverse
161.80% Level: 1.42930.0% Level:1.3896
100.0% Level: 1.389623.60% Level:1.3744
61.8% Level: 1.365138.2% Level:1.3651
50.0% Level: 1.357550.0% Level:1.3575
38.2% Level: 1.349961.8% Level:1.3499
23.6% Level:
1.3406100.0% Level:1.3254
0.0% Level: 1.3254161.8% Level: 1.2857


That is, (Price(100%)-Price(0%))*Level(in thousands) + Price(0%) for the forward calculation and Price(100%) - (Price(100%)-Price(0%)) * Level(in thousands) - for inverse calculation.

For each value, a separate line can be drawn with any colour from the palette of your choice.

Reason: