MT4 software—Gann theory indicator Fibonacci sector line top and bottom formula source code

I want to find some foreign exchange MT4 indicators. The source code is attached. It can assist when trading. You can refer to it when writing indicators. You can edit it when you want to improve it.
Gann’s theory believes that financial investment markets such as stocks and futures also have natural rules in the universe. The price trend of the market is not chaotic, but can be predicted through mathematical methods. Its essence is to establish a strict trading order in the seemingly disorderly market, which can be used to discover when the price will retrace and what price it will retrace to.
Fibonacci fan lines can provide you with support and resistance levels as well as time factors, find retracement levels and breakthrough levels, and can also predict the movement trend of future market conditions. At the same time, it can also realize all the technical functions of multiple analysis systems. . However, Fibonacci Fans draw a series of angled trend lines rather than parallel “staircase lines.”

It should be noted during use that Fibonacci fan lines cannot be applied to small periods. Because the accuracy of the test is poor in small period charts, it is best to use period charts of more than 1 day, week, or month for measurement.

According to Gann, price movements occur in predictable geometric cycles. The angles created by this price action act as support or resistance on the price chart. It is recommended to use it in cycles of 15 minutes or more for better results.

The 45-degree tilt trend is sustainable and will last longer.
Go long:
Price closed above 45 degrees; price closed above the Fibonacci Fan 61.8 level
Short selling:
Price closed below 45 degrees; price closed below the Fibonacci Fan 61.8 level



Gann theory Fibonacci fan line top and bottom indicator source code

The technical indicators shared below are sub-chart analysis formula source codes, which are suitable for MT4 software and are adapted according to common methods. It is only used to expand strategic ideas and is not recommended to be used directly in actual foreign exchange investments such as currency exchange, gold, and crude oil (investment is risky, so be cautious when entering the market).

Traders can adapt the indicator source code provided by Aiyun Strategy and combine it with their usual trading experience to form their own trading strategy.
#property indicator_chart_window

#property indicator_buffers 1

#property indicator_color1 AliceBlue

#property indicator_width1 0

#property indicator_style1 2

//—-

extern double angle_up = 22.5;

extern double angle_dn = 22.5;

extern int Width = 0;

extern int Style = 2;

extern int kol_lev = 8;

extern color ResistanceColor = Tomato;

extern color SupportColor = DodgerBlue;

extern color Level_0 = LightSteelBlue;

extern bool lev_V = true;

extern color Level_V = LightSteelBlue;

extern int Complect = 0;

color ?a?ò_óe?aí?;

extern int ExtDepth=21;

extern int ExtDeviation=5;

extern int ExtBackstep=3;

//—-

//—-

double ZigZagBuffer[];

int timeFirstBar=0;

int flag;

bool work=true;

double vel_prev;

//±————————————————– —————— +

//| ZigZag initialization function |

//±————————————————– —————— +

int init()

{

//—-

SetIndexBuffer(0,ZigZagBuffer);

SetIndexStyle(0,DRAW_SECTION,2);

SetIndexEmptyValue(0,0.0);

IndicatorShortName(“ZigZag(” + ExtDepth + “,” + ExtDeviation + “,” + ExtBackstep + “)”);

//—-

return(0);

}

//±————————————————– —————— +

//| deinit |

//±————————————————– —————— +

int deinit()

{

//—-

ObjDel();

Comment(“”);

//—-

return(0);

}

//±————————————————– —————— +

//| ZigZag iteration function |

//±————————————————– —————— +

int start()

{

//—- + ?e?a?eêà êè÷òaà áàe?a íà ?òàò?÷íòü ? ê?ee?êòí? eà?÷? òà èí?èêàò?eà

if (Bars-1

//—- + ?a?íè? ? e?ì?íí ?àì?òè ? e÷?òà èí?èêàò?eà ò? ?üê? íà í?÷èòàíí áàeà?

static int time2,time3,time4;

//—- + ?a?íè? e?ì?íí ? àaàtù?é ò?÷ê?é ? e÷?òà èí?èêàò? eà òüê? íà í?÷èòàíí áàeà?

static double ZigZag2,ZigZag3,ZigZag4;

//—- + ?a?íè? ? e?ì?íí ? e÷?òà èí?èêàò?eà òüê? íà í÷èòàíí áàeà? è ?ó÷?íè? ó ÷èòàíí áàe?a

int MaxBar,limit,supr2_bar,supr3_bar,supr4_bar,counted_bars=IndicatorCounted();

//—- ?e?a?eêà íà aìí èáêè

if (counted_bars<0)return(-1);

//—- íèé ÷èòàíí?é áàe ?í á?òü e÷èòàí

if (counted_bars>0) counted_bars–;

//—- + ?a?íè? e?ì?íí

int index, shift, back,lasthighpos,lastlowpos;

double val,res,TempBuffer[1];

double curlow,curhigh,lasthigh,lastlow;

int metka=0; // =0 – ea? e?ìà ZZ. =1 – èù?ì ì?òêè ìàê?èìóì?a. =2 – èù?ì ì? òêè ìèíèìóì?a.

//—- eíè? í?ì?eà ?àì? ?òàe? áàeà, íà÷èíà? ? ê?ò?e? áóò ?e?èí é e÷?ò a? áàe?a

MaxBar=Bars-ExtDepth;

//—- eíè? í?ì?eà ?òàeò?a? áàeà a ?èê, íà÷èíà? ? ê?ò?e? áó? ?ò ?e?è?èòü e÷?ò í?a áàe?a

if (counted_bars==0 || Bars-counted_bars>2)

 {

  limit=MaxBar;

 }

else

 {

  //----

  supr2_bar=iBarShift(NULL,0,time2,TRUE);

  supr3_bar=iBarShift(NULL,0,time3,TRUE);

  supr4_bar=iBarShift(NULL,0,time4,TRUE);

  //----

  limit=supr3_bar;

  if ((supr2_bar<0)||(supr3_bar<0)||(supr4_bar<0))

     {

      limit=MaxBar;

     }

 }

//—- èíè?èà?è?à?è? íó

if (limit>=MaxBar || timeFirstBar!=Time[Bars-1])

 {

  timeFirstBar=Time[Bars-1];

  limit=MaxBar;

 }

//—-

//—- è?ì?í?íè? eà?ì?eà ae?ì?íí? áóeà

if (limit==MaxBar) ArrayResize(TempBuffer,Bars); else ArrayResize(TempBuffer,limit + ExtBackstep + 1);

//—-±————————————————- —– +

//—- + íà÷à ea? áü ?èê?à

for(shift=limit; shift>=0; shift–)

 {

  //--- Low

  val=Low[Lowest(NULL,0,MODE_LOW,ExtDepth,shift)];

  if(val==lastlow) val=0.0;

  else

    {

     lastlow=val;

     if((Low[shift]-val)>(ExtDeviation*Point)) val=0.0;

     else

       {

        for(back=1; back<=ExtBackstep; back + + )

          {

           res=ZigZagBuffer[shift + back];

           if((res!=0) & amp; & amp;(res>val)) ZigZagBuffer[shift + back]=0.0;

          }

       }

    }

  if (Low[shift]==val)

    {

     ZigZagBuffer[shift]=val;

     //if (ExtLabel==1) la[shift]=val;

    }

  else ZigZagBuffer[shift]=0.0;





  //--- High

  val=High[Highest(NULL,0,MODE_HIGH,ExtDepth,shift)];

  if(val==lasthigh) val=0.0;

  else

    {

     lasthigh=val;

     if((val-High[shift])>(ExtDeviation*Point)) val=0.0;

     else

       {

        for(back=1; back<=ExtBackstep; back + + )

          {

           res=TempBuffer[shift + back];

           if((res!=0) & amp; & amp;(res<val)) TempBuffer[shift + back]=0.0;

          }

       }

    }

  if (High[shift]==val)

    {

     TempBuffer[shift]=val;

     //if (ExtLabel==1) ha[shift]=val;

    }

  else TempBuffer[shift]=0.0;

 }

//—- + ê?í ea? áü ?èê?à

// final cutting

 lasthigh=-1; lasthighpos=-1;

  lastlow= -1; lastlowpos= -1;

//—-±————————————————- —– +

//—- + íà÷à aò?e? áü ?èê?à

for(shift=limit; shift>=0; shift–)

 {

  curlow=ZigZagBuffer[shift];

  curhigh=TempBuffer[shift];

  if((curlow==0) & amp; & amp;(curhigh==0)) continue;

  //---

  if(curhigh!=0)

    {

     if(lasthigh>0)

       {

        if(lasthigh<curhigh) TempBuffer[lasthighpos]=0;

        else TempBuffer[shift]=0;

       }

     //---

     if(lasthigh<curhigh || lasthigh<0)

       {

        lasthigh=curhigh;

        lasthighpos=shift;

       }

     lastlow=-1;

    }

  //----

  if(curlow!=0)

    {

     if(lastlow>0)

       {

        if(lastlow>curlow) ZigZagBuffer[lastlowpos]=0;

        else ZigZagBuffer[shift]=0;

       }

     //---

     if((curlow<lastlow)||(lastlow<0))

       {

        lastlow=curlow;

        lastlowpos=shift;

       }

     lasthigh=-1;

    }

 }

//—- + ê?í aò?e? áü ?èê?à

//—-±————————————————- —– +

//—- + íà÷à òe?òü? ?èê?à

for(shift=limit; shift>=0; shift–)

 {

   res=TempBuffer[shift];

   if(res!=0.0) ZigZagBuffer[shift]=res;

 }

 //---- + ê?í òe?òü? ?èê?à

// ?e?a?eêà ea? ?ó÷à

int i=0,j=0;

res=0;

for (shift=0;i<3;shift + + )

 {

  if (ZigZagBuffer[shift]>0)

    {

     i + + ;

     if (i==1 & amp; & amp; ZigZagBuffer[shift]==High[shift])

       {

        j=shift;

        res=ZigZagBuffer[shift];

       }

     if (i==2 & amp; & amp; res>0 & amp; & amp; ZigZagBuffer[shift]==High[shift])

       {

        if (ZigZagBuffer[shift]>=ZigZagBuffer[j]) ZigZagBuffer[j]=0; else ZigZagBuffer[shift]=0;

        res=0;

        i=0;

        j=0;

        shift=0;

       }

    }

 }

//±– ?òàí?aíè? ?íà÷?íèé èí?èêàò?eí? áó?eà, ê?ò?e ì?è á?òü óò?e?í?

if (limit

 {

  ZigZagBuffer[supr2_bar]=ZigZag2;

  ZigZagBuffer[supr3_bar]=ZigZag3;

  ZigZagBuffer[supr4_bar]=ZigZag4;

  for(int qqq=supr4_bar-1; qqq>supr3_bar; qqq--)ZigZagBuffer[qqq]=0;

  for(int ggg=supr3_bar-1; ggg>supr2_bar; ggg--)ZigZagBuffer[ggg]=0;

 }

//±– + ============================================ +

//±– èeàaíè? aíèêàtùè? eá?a

double vel1, vel2, vel3, vel4;

int bar1, bar2, bar3, bar4;

int count;

if (limit==MaxBar)supr4_bar=MaxBar;

for(int bar=supr4_bar; bar>=0; bar–)

{

 if (ZigZagBuffer[bar]!=0)

  {

   count + + ;

   vel4=vel3;bar4=bar3;

   vel3=vel2;bar3=bar2;

   vel2=vel1;bar2=bar1;

   vel1=ZigZagBuffer[bar];bar1=bar;

   ObjDel();

   if (count<3)continue;

   if ((vel3<vel2) & amp; & amp;(vel2<vel1)){ZigZagBuffer[bar2]=0;bar=bar3 + 1;}

   if ((vel3>vel2) & amp; & amp;(vel2>vel1)){ZigZagBuffer[bar2]=0;bar=bar3 + 1;}

   if ((vel2==vel1) & amp; & amp;(vel1!=0 )){ZigZagBuffer[bar1]=0;bar=bar3 + 1;}

 }

}

//±– ?àìèíàíè? ae?ì?íè òe íè? eèá?a ?èà?à è ?íà÷?íèé èí? èêàò?eà a yòè? ò?÷êà?

time2=Time[bar2];

time3=Time[bar3];

time4=Time[bar4];

ZigZag2=vel2;

ZigZag3=vel3;

ZigZag4=vel4;

if(bar1>=2)

{

if(Low[bar1]==vel1)

{

flag=1;

for(i = 1; i <= kol_lev; i + + )

{

PlotLine(“lev “ + bar1 + ”” + Complect + “_” + i,vel1,bar1,bar1,0,angle_up*i, flag);

}

}

else

{

flag=-1;

for(i = 1; i <= kol_lev; i + + )

{

PlotLine(“lev “ + bar1 + ”” + Complect + “_” + i,vel1,bar1,bar1,0,angle_dn*i, flag);

}

}

PlotLineM(“lev “ + bar1 + ”” + Complect + “_”,vel1,bar1,bar1,0,flag);

}

//±–

if(Low[bar2]==vel2)

{

flag=1;

for(i = 1; i <= kol_lev; i + + )

{

PlotLine(“lev “ + bar2 + ”” + Complect + “_” + i,vel2,bar2,bar1,1,angle_up*i, flag);

}

}

else

{

flag=-1;

for(i = 1; i <= kol_lev; i + + )

{

PlotLine(“lev “ + bar2 + ”” + Complect + “_” + i,vel2,bar2,bar1,1,angle_dn*i, flag);

}

}

PlotLineM(“lev “ + bar2 + ”” + Complect + “_”,vel2,bar2,bar1,1,flag);

//±–

if(Low[bar3]==vel3)

{

flag=1;

for(i = 1; i <= kol_lev; i + + )

{

PlotLine(“lev “ + bar3 + ”” + Complect + “_” + i,vel3,bar3,bar2,1,angle_up*i, flag);

}

}

else

{

flag=-1;

for(i = 1; i <= kol_lev; i + + )

{

PlotLine(“lev “ + bar3 + ”” + Complect + “_” + i,vel3,bar3,bar2,1,angle_dn*i, flag);

}

}

PlotLineM(“lev “ + bar3 + ”” + Complect + “_”,vel3,bar3,bar2,1,flag);

//±–

if(Low[bar4]==vel4)

{

flag=1;

for(i = 1; i <= kol_lev; i + + )

{

PlotLine(“lev “ + bar4 + ”” + Complect + “_” + i,vel4,bar4,bar3,1,angle_up*i, flag);

}

}

else

{

flag=-1;

for(i = 1; i <= kol_lev; i + + )

{

PlotLine(“lev “ + bar4 + ”” + Complect + “_” + i,vel4,bar4,bar3,1,angle_dn*i, flag);

}

}

PlotLineM(“lev “ + bar4 + ”” + Complect + “_”,vel4,bar4,bar3,1,flag);

return(0);

}

//- + ±———————————————— ———————– +

void PlotLineM(string name,double Price1,int Date1,int Date2,int lev0,int a?e?_aíè?)

{

int D2;

double P1;

 if(lev0==1)

  D2=Time[Date2];

 else

  D2=Time[0] + 50*Period()*60;

       

 ObjectDelete(name + " 0");

 ObjectCreate(name + " 0",OBJ_TREND,0,Time[Date1],Price1,D2,Price1);

 ObjectSet(name + " 0",OBJPROP_COLOR,Level_0);

 ObjectSet(name + " 0",OBJPROP_STYLE,0);

 ObjectSet(name + " 0",OBJPROP_WIDTH,1);

 ObjectSet(name + " 0",OBJPROP_RAY,false);

//- +

 if(a?e?_aíè?==1)

  P1=Price1-2*Point;

 else if(a?e?_aíè?==-1)

  P1=Price1 + 4*Point;

 ObjectDelete(name + " 0txt");

 ObjectCreate(name + " 0txt", OBJ_TEXT, 0, Time[Date1], P1);

 ObjectSetText(name + " 0txt", DoubleToStr(Price1,Digits), 8, "Tahoma",Level_0);

//- +

if(lev_V)

{

 ObjectDelete(name + " V");

 ObjectCreate(name + " V",OBJ_VLINE,0,Time[Date1],0);

 ObjectSet(name + " V",OBJPROP_COLOR,Level_V);

 ObjectSet(name + " V",OBJPROP_STYLE,2);

 ObjectSet(name + " V",OBJPROP_WIDTH,0);

 ObjectSet(name + " V",OBJPROP_BACK,true);

}

//- +

}

//- + ±———————————————— ———————– +

void PlotLine(string name,double Price1,int Date1,int Date2,int lev0,double gr,int a?e?_aíè?)

{

double level,points;

int D2,nBar;

if(Digits5 || Digits3)

points=Point*10;

else

points=Point;

 if(a?e?_aíè?==1)

   {

   level=MathSqrt(Price1/points) + gr/180;

   level=MathPow(level,2)*points;

   ?a?ò_óe?aí?=SupportColor;

   }

   else

   if(a?e?_aíè?==-1)

   {

   level=MathSqrt(Price1/points)-gr/180;

   level=MathPow(level,2)*points;

   ?a?ò_óe?aí?=ResistanceColor;

   }

   if(lev0==1)

   D2=Time[Date2];

   else

   D2=Time[0] + 50*Period()*60;



 ObjectDelete(name);

 ObjectCreate(name,OBJ_TREND,0,Time[Date1],level,D2,level);

 ObjectSet(name,OBJPROP_COLOR,?a?ò_óe?aí?);

 ObjectSet(name,OBJPROP_STYLE,Style);

 ObjectSet(name,OBJPROP_WIDTH,Width);

 ObjectSet(name,OBJPROP_RAY,false);

//-

ObjectDelete(name + ” txt”);

if(lev0==1)

{

nBar=Date1-8;

ObjectCreate(name + ” txt”, OBJ_TEXT, 0, Time[nBar], level);

}

else

{

ObjectCreate(name + ” txt”, OBJ_TEXT, 0, Time[0] + 8*Period()*60, level);

}

ObjectSetText(name + ” txt”, DoubleToStr(gr,1) + “° ” + DoubleToStr(level,Digits), 8, “Tahoma”,?a?ò_óe?aí?);

//-

}

//———————————————— ———-

void ObjDel()

{

for ( int i = ObjectsTotal() – 1; i >= 0; i – )

{

if ( StringFind( ObjectName(i), “_”, 0 ) == 0 )

{

ObjectDelete( ObjectName(i) );

}

}

}