Automatic indicator that show Fibonacci levels of the low and high of the day

 

Hi it's me again,

I am almost done with my indicator I only have 2 problem left. So the indicator turn RED when it's a downtrend

and is Green when it s an UPTREND. It update automatically when there is a new high or low. My problem is that

when the trend change in the middle of the day that fib doesnt change colors and levels, so I have delete and put

back my indicator each time it reverses. My 2nd problem is that sometime when i change timeframe  it pick the wrong

fib for the trend and i dont know why! Theres the code

thank you

 

Whoseen 

Files:
 
#property indicator_chart_window

#property  indicator_buffers 8
extern string TopComment = "<(====_TEV_====)>";
extern color TrendUpColor = Green;
extern color TrendDownColor = Red;

color          FiboColour=Black;  
double         SwingHigh, SwingLow;
double         high, low;
double         Entry, TP1, TP2, TP3;
double         extent = SwingHigh - SwingLow;

double f_1[];
double f_2[];
double f_3[];
double f_4[];
double f_5[];
double f_6[];
double f_7[];


datetime today     = iTime(NULL, PERIOD_D1, 0);
int      iToday    = iBarShift(Symbol(),0,today);
int      iTodayHigh = Highest(NULL,0,MODE_HIGH, iToday+1, 0); // or not incl. 0 bar: ..iToday, 1)
int      iTodayLow = Lowest(NULL,0,MODE_LOWER, iToday+1, 0);
        

  
void DeleteAllObjects()
{
   int objs = ObjectsTotal();
   string name;
   for(int cnt=ObjectsTotal()-1;cnt>=0;cnt--)
   {
      name=ObjectName(cnt);
      if (StringFind(name,"V_",0)>-1) ObjectDelete(name);
      if (StringFind(name,"H_",0)>-1) ObjectDelete(name);
      if (StringFind(name,"f_",0)>-1) ObjectDelete(name);
      if (StringFind(name,"Fibo",0)>-1) ObjectDelete(name);
      if (StringFind(name,"trend",0)>-1) ObjectDelete(name);
      WindowRedraw();
   }
}

int init()
  {
  DeleteAllObjects();
  SetIndexBuffer(0,f_1);
  SetIndexBuffer(1,f_2);
  SetIndexBuffer(2,f_3);
  SetIndexBuffer(3,f_4);
  SetIndexBuffer(4,f_5);
  SetIndexBuffer(5,f_6);
  SetIndexBuffer(6,f_7);
   return(0);
  }

int deinit()
  {
   DeleteAllObjects();
   return(0);
  }

void GetSwing()
{
   SwingHigh = iHigh(Symbol(),PERIOD_D1,0);
   SwingLow = iLow(Symbol(),PERIOD_D1,0);
   CalculateTradeLevels();  
}


void CalculateTradeLevels()
{  
   DeleteAllObjects();
   high = SwingHigh;
   low = SwingLow;
  
   int i = 0;
  
   if (iTodayHigh>iTodayLow && SwingHigh>SwingLow)
      {
      for(i = 0; i < 500; i++)
      {
         f_1[i] = high;
         f_2[i] = NormalizeDouble(SwingLow + (extent * 1.41 / 100), Digits);
         f_3[i] = NormalizeDouble(SwingLow + (extent * 28.2 / 100), Digits);
         f_4[i]= NormalizeDouble(SwingLow + (extent * 41.4 / 100), Digits);
         f_5[i] = NormalizeDouble(SwingLow + (extent * 64.2 / 100), Digits);
         f_6[i] = low;
      }
   }else{
      for(i = 0; i < 500; i++)
      {
         f_1[i] = high;
         f_1[i] = NormalizeDouble(SwingHigh - (extent * 1.41 / 100), Digits);
         f_1[i] = NormalizeDouble(SwingHigh - (extent * 28.2 / 100), Digits);
         f_1[i] = NormalizeDouble(SwingHigh - (extent * 41.4 / 100), Digits);
         f_1[i] = NormalizeDouble(SwingHigh - (extent * 64.2 / 100), Digits);
         f_1[i] = low;
      }
   }
    
   if (iTodayHigh>iTodayLow && SwingHigh>SwingLow)
   {
      WindowRedraw();
      FiboColour = TrendDownColor;    
   }
   else{
      WindowRedraw();
      FiboColour = TrendUpColor;      
   }
}  
  
void DrawFib()
{
  
//======================================================
//To put gold at 00:00 and not 23:55
//======================================================
      datetime OpenTime, CloseTime;    
      OpenTime = iTime(NULL, PERIOD_D1, 0);
      CloseTime = iTime(NULL, PERIOD_D1, 0);
      int shift=iBarShift(Symbol(),0,OpenTime);
      
      if  (Time[shift]!=OpenTime)
         {
            OpenTime=Time[shift-1];
         }
        
      if  (Time[shift]!=CloseTime)
         {
            CloseTime=Time[shift-1];
         }  
//======================================================
//Gold 00:00
//======================================================

      if (iTodayHigh>iTodayLow && SwingHigh>SwingLow)
      {        
         ObjectCreate("Fibo",OBJ_FIBO,0,OpenTime,SwingHigh,CloseTime,SwingLow);
         ObjectSet("Fibo", OBJPROP_COLOR, FiboColour);
         ObjectSet("Fibo", OBJPROP_LEVELCOLOR, FiboColour);
         ObjectSet("Fibo", OBJPROP_WIDTH, 1);
         ObjectSet("Fibo", OBJPROP_FIBOLEVELS, 6);    
         ObjectSet("Fibo", OBJPROP_FIRSTLEVEL+0, 0);
         ObjectSet("Fibo", OBJPROP_FIRSTLEVEL+1, 0.141);
         ObjectSet("Fibo", OBJPROP_FIRSTLEVEL+2, 0.282);
         ObjectSet("Fibo", OBJPROP_FIRSTLEVEL+3, 0.414);
         ObjectSet("Fibo", OBJPROP_FIRSTLEVEL+4, 0.642);
         ObjectSet("Fibo", OBJPROP_FIRSTLEVEL+5, 1);

         ObjectSetFiboDescription("Fibo", 0, "  %$ -" + " (0)");
         ObjectSetFiboDescription("Fibo", 1, "  %$ -" + " (1.41)");
         ObjectSetFiboDescription("Fibo", 2, "  %$ -" + " (2.82)");
         ObjectSetFiboDescription("Fibo", 3, "  %$ -" + " (41.4)");
         ObjectSetFiboDescription("Fibo", 4, "  %$ -" + " (64.2)");
         ObjectSetFiboDescription("Fibo", 5, "  %$ -" + " (100)");
       }else{
  
         ObjectCreate("Fibo",OBJ_FIBO,0,OpenTime,SwingLow,CloseTime,SwingHigh);
         ObjectSet("Fibo", OBJPROP_COLOR, FiboColour);
         ObjectSet("Fibo", OBJPROP_LEVELCOLOR, FiboColour);
         ObjectSet("Fibo", OBJPROP_WIDTH, 1);
         ObjectSet("Fibo", OBJPROP_FIBOLEVELS, 6);    
         ObjectSet("Fibo", OBJPROP_FIRSTLEVEL+5, 0);
         ObjectSet("Fibo", OBJPROP_FIRSTLEVEL+4, 0.141);
         ObjectSet("Fibo", OBJPROP_FIRSTLEVEL+3, 0.282);
         ObjectSet("Fibo", OBJPROP_FIRSTLEVEL+2, 0.414);
         ObjectSet("Fibo", OBJPROP_FIRSTLEVEL+1, 0.642);
         ObjectSet("Fibo", OBJPROP_FIRSTLEVEL+0, 1);
         ObjectSetFiboDescription("Fibo", 5, "  %$ -" + " (0)");
         ObjectSetFiboDescription("Fibo", 4, "  %$ -" + " (1.41)");
         ObjectSetFiboDescription("Fibo", 3, "  %$ -" + " (2.82)");
         ObjectSetFiboDescription("Fibo", 2, "  %$ -" + " (41.4)");
         ObjectSetFiboDescription("Fibo", 1, "  %$ -" + " (64.2)");
         ObjectSetFiboDescription("Fibo", 0, "  %$ -" + " (100)");
          
  }  
}


int start()
  {
  GetSwing();
  CalculateTradeLevels();
  DrawFib();
  return(0);
  }
  

  
 

I haven't been through all your code, but I can see a few issues:

color          FiboColour=Black;  
double         SwingHigh, SwingLow;
double         high, low;
double         Entry, TP1, TP2, TP3;
double         extent = SwingHigh - SwingLow;

You use 'extent' in your code multiple times, and you update 'SwingHigh' and 'SwingLow' multiple times, but never this variable. So it is stuck at the value when it first ran.

Try:

void GetSwing()
{
   SwingHigh = iHigh(Symbol(),PERIOD_D1,0);
   SwingLow = iLow(Symbol(),PERIOD_D1,0);
   extent = SwingHigh - SwingLow;
   CalculateTradeLevels();  
}

 

Also, I'm guessing you meant to use different buffers here (like the block above it)

   if (iTodayHigh>iTodayLow && SwingHigh>SwingLow)
      {
      for(i = 0; i < 500; i++)
      {
         f_1[i] = high;
         f_2[i] = NormalizeDouble(SwingLow + (extent * 1.41 / 100), Digits);
         f_3[i] = NormalizeDouble(SwingLow + (extent * 28.2 / 100), Digits);
         f_4[i]= NormalizeDouble(SwingLow + (extent * 41.4 / 100), Digits);
         f_5[i] = NormalizeDouble(SwingLow + (extent * 64.2 / 100), Digits);
         f_6[i] = low;
      }
   }else{
      for(i = 0; i < 500; i++)
      {
         f_1[i] = high;
         f_1[i] = NormalizeDouble(SwingHigh - (extent * 1.41 / 100), Digits);
         f_1[i] = NormalizeDouble(SwingHigh - (extent * 28.2 / 100), Digits);
         f_1[i] = NormalizeDouble(SwingHigh - (extent * 41.4 / 100), Digits);
         f_1[i] = NormalizeDouble(SwingHigh - (extent * 64.2 / 100), Digits);
         f_1[i] = low;
      }
   }
 
Whoseen:

Hi it's me again,

...

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


 
honest_knave:

I haven't been through all your code, but I can see a few issues:

color          FiboColour=Black;  
double         SwingHigh, SwingLow;
double         high, low;
double         Entry, TP1, TP2, TP3;
double         extent = SwingHigh - SwingLow;

You use 'extent' in your code multiple times, and you update 'SwingHigh' and 'SwingLow' multiple times, but never this variable. So it is stuck at the value when it first ran.

Try:

void GetSwing()
{
   SwingHigh = iHigh(Symbol(),PERIOD_D1,0);
   SwingLow = iLow(Symbol(),PERIOD_D1,0);
   extent = SwingHigh - SwingLow;
   CalculateTradeLevels();  
}

 

Also, I'm guessing you meant to use different buffers here (like the block above it)

   if (iTodayHigh>iTodayLow && SwingHigh>SwingLow)
      {
      for(i = 0; i < 500; i++)
      {
         f_1[i] = high;
         f_2[i] = NormalizeDouble(SwingLow + (extent * 1.41 / 100), Digits);
         f_3[i] = NormalizeDouble(SwingLow + (extent * 28.2 / 100), Digits);
         f_4[i]= NormalizeDouble(SwingLow + (extent * 41.4 / 100), Digits);
         f_5[i] = NormalizeDouble(SwingLow + (extent * 64.2 / 100), Digits);
         f_6[i] = low;
      }
   }else{
      for(i = 0; i < 500; i++)
      {
         f_1[i] = high;
         f_1[i] = NormalizeDouble(SwingHigh - (extent * 1.41 / 100), Digits);
         f_1[i] = NormalizeDouble(SwingHigh - (extent * 28.2 / 100), Digits);
         f_1[i] = NormalizeDouble(SwingHigh - (extent * 41.4 / 100), Digits);
         f_1[i] = NormalizeDouble(SwingHigh - (extent * 64.2 / 100), Digits);
         f_1[i] = low;
      }
   }

Tanks you for your help!! :)

 
Alain Verleyen:

ohh ok thank you for the tip
 
void DeleteAllObjects()
{
   int objs = ObjectsTotal();
   string name;
   for(int cnt=ObjectsTotal()-1;cnt>=0;cnt--)
   {
      name=ObjectName(cnt);
      if (StringFind(name,"V_",0)>-1) ObjectDelete(name);
      if (StringFind(name,"H_",0)>-1) ObjectDelete(name);
      if (StringFind(name,"f_",0)>-1) ObjectDelete(name);
      if (StringFind(name,"Fibo",0)>-1) ObjectDelete(name);
      if (StringFind(name,"trend",0)>-1) ObjectDelete(name);
      WindowRedraw();
   }
}

Stuff like that harms experts and indicators which are coded proper! This is pretty bad stuff.

You cannot really delete all objects of chart just because the names contain these letters. Every day I have to deal with stuff like that and its simply annoying. Either you should use objects (OOP), arrays where you save all your objects or more complex names - but please dont do it like that.

 
Doerk Hilger:
void DeleteAllObjects()
{
   int objs = ObjectsTotal();
   string name;
   for(int cnt=ObjectsTotal()-1;cnt>=0;cnt--)
   {
      name=ObjectName(cnt);
      if (StringFind(name,"V_",0)>-1) ObjectDelete(name);
      if (StringFind(name,"H_",0)>-1) ObjectDelete(name);
      if (StringFind(name,"f_",0)>-1) ObjectDelete(name);
      if (StringFind(name,"Fibo",0)>-1) ObjectDelete(name);
      if (StringFind(name,"trend",0)>-1) ObjectDelete(name);
      WindowRedraw();
   }
}

Stuff like that harms experts and indicators which are coded proper! This is pretty bad stuff.

You cannot really delete all objects of chart just because the names contain these letters. Every day I have to deal with stuff like that and its simply annoying. Either you should use objects (OOP), arrays where you save all your objects or more complex names - but please dont do it like that.

It would be nice if it was possible to cancel an object deletion :-D
 
#define tag "SomeUniqueNameForMyIndicator"
ObjectCreate(0,tag+"thisobjectsname",...

ObjectsDeleteAll(0,tag);
Reason: