How to draw Fibbos in an EA I think ive caused myself a problem in the way ive done this already

 

Hey desperate for some help here!

 

trying to control the way indicator draws fibbo scales automatically and think ive perhaps gone about it the wrong way from the start.

 

 I have it setting everything as buffers so the datawindow is populated with what I want. But what i need is for it to only re-draw if the HighValue or Low Value doesnt equal the previously drawn fib scales.

Because of how it draws it as a Fib Level I dont know how to store this data as an array then use the array functions to query if the values of HighValue and LowValue are different then re-draw the fib scales.

 

Below is how I start out setting the buffers 

 

#property indicator_chart_window
#property  indicator_buffers 8

int    StartBar     = 0;
extern int    BarsBack     = 72;
bool   Pause        = false;


color VerticalLinesColor = Blue;
color FiboLinesColors1 = Red;
color FiboLinesColors2 = Blue;

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

int OnInit()
{
  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);
  SetIndexBuffer(7,f_8);
  SetIndexLabel(0,"Fibo_"+DoubleToStr(0.000,4));
  SetIndexLabel(1,"Fibo_"+DoubleToStr(0.236,4));
  SetIndexLabel(2,"Fibo_"+DoubleToStr(0.382,4));
  SetIndexLabel(3,"Fibo_"+DoubleToStr(0.500,4));
  SetIndexLabel(4,"Fibo_"+DoubleToStr(0.618,4));
  SetIndexLabel(5,"Fibo_"+DoubleToStr(0.764,4));
  SetIndexLabel(6,"Fibo_"+DoubleToStr(0.886,4));
  SetIndexLabel(7,"Fibo_"+DoubleToStr(1.000,4));
    return(0);
}

 

I've tried trying to write a simple logic to use the Buffer value difference but that doesn't work as every tick it sets the buffers and the values are 0 then it writes the values below this by using the High Low values.


 Then I use onStart to work these out: I feel that this slows things down as its calling the process to delete all and calc fibo on every tick but this is a fixed process and all we want it to do is call that code when a change is there rather than on every tick.

int start()

{
   CalcFibo();
  return(0);
}

void DeleteAllObjects()
{
   int objs = ObjectsTotal();
   string name;
   for(int cnt=ObjectsTotal()-1;cnt>=0;cnt--)
   {
         name=ObjectName(cnt);
         if (StringFind(name,"v_u_hl",0)>-1) ObjectDelete(name);
         if (StringFind(name,"v_l_hl",0)>-1) ObjectDelete(name);
         if (StringFind(name,"v_u_lh",0)>-1) ObjectDelete(name);
         if (StringFind(name,"v_l_lh",0)>-1) ObjectDelete(name);
         if (StringFind(name,"Fibo_hl",0)>-1) ObjectDelete(name);
         if (StringFind(name,"trend_hl",0)>-1) ObjectDelete(name);
         if (StringFind(name,"Fibo_lh",0)>-1) ObjectDelete(name);
         if (StringFind(name,"trend_lh",0)>-1) ObjectDelete(name);
         //WindowRedraw();
      }
      }

void CalcFibo()
{
  
  
  //DeleteAllObjects();
  int LowBar = 0, HighBar= 0;
  double LowValue = 0 ,HighValue = 0;
  
  int lowest_bar = iLowest(NULL,0,MODE_LOW,BarsBack,StartBar);
  int highest_bar = iHighest(NULL,0,MODE_HIGH,BarsBack,StartBar);
  
  
  double higher_point = 0;
  double lower_point = 0;
  HighValue=High[highest_bar];
  LowValue=Low[lowest_bar];

What i've done with it so far, is changed the logic that originally was set on Init to define which way to draw the scale as a retracement or extension effectively so that when the High Bar or Low bar switch it deletes the old scale and draws the new one. This works as its fairly simple logic and isn't time relevant the moment the Low bar is in front of the High bar its re-drawing on every tick anyway so it deletes the scales first then is redrawing in the new scale and colour.

Issue I have now though is that I only want it to actually proceed with drawing the scales at all If a new value for HighBar or Low bar replaces the previous calculation.

 

My Head tells me I need to restructure this code completely and look at a different approach to it. using onCalculate functions and data arrays but im not that experienced with this to know where to start and sometimes so far into a project you cloud your own judgement.

 

Anyone have experience of this? Where should I start and what have I got wrong here because clearly my logic is flawed somewhere.

I am then using the values from this as an Icustom Call in an EA of mine. so need to use the data in buffers so cant get rid of that bit. 



 

 
void CalcFibo()
{
  
   int lowest_bar = iLowest(NULL,0,MODE_LOW,BarsBack,StartBar);
   int highest_bar = iHighest(NULL,0,MODE_HIGH,BarsBack,StartBar);
  
   static int prev_lowest_bar,prev_highest_bar;
   if(prev_lowest_bar!=lowest_bar || prev_highest_bar!=highest_bar)
   {
      prev_lowest_bar=lowest_bar;
      prev_highest_bar=highest_bar;
   }
   else
      return;
  
   DeleteAllObjects();
 
The above code will redraw once per bar, when indexes change. 

If StartBar is zero, it will not redraw on a new extreme because the indexes still match.

Don't store indexes, store the extreme prices.
 
whroeder1:
The above code will redraw once per bar, when indexes change. 

If StartBar is zero, it will not redraw on a new extreme because the indexes still match.

Don't store indexes, store the extreme prices.

Thanks Ernst, Im assuming then would be able to use Ihigh to get the value of the previous bars like you would with the current bar etc. Then that would work like roeder says using the price not the bar index incase you was on bar 0 where it wouldnt re-draw.

 


void CalcFibo()
{
  
   int lowest_bar = iLowest(NULL,0,MODE_LOW,BarsBack,StartBar);
   int highest_bar = iHighest(NULL,0,MODE_HIGH,BarsBack,StartBar);
     

   HighValue1=High[prev_highest_bar];
   LowValue1=Low[prev_lowest_bar]; 

   HighValue2=High[highest_bar;

   LowValue2=Low[lowest_bar]; 
  
   static int prev_lowest_bar,prev_highest_bar;
   if(HighValue1!=HighValue2 || LowValue1!=LowValue2)
   {
      prev_lowest_bar=lowest_bar;
      prev_highest_bar=highest_bar;
   }
   else
      return;
  
   DeleteAllObjects();
 

Added the code as attached and just looking at this it looks like its working but not properly in my EA still. Could this be an issue for strategy tester only? when i have the EA running on strategy tester it still runs slow and the fib scales and lines are all flickering asif they are deleting and re-drawing on every tick.

Im going to try and re-check my code in the EA to make sure ive not got any input parameters that have been deleted or something that its trying to use in the Icustom calls as I know that might be causing it to reload and delete the Indi on every tick.

 

I did this with the code guys based on your suggestions above. Moved the calculation to the Start function to Call the CalcFIbo rather than running it in line with the current If Function that defines what way to draw the fib Blue or Red for buy or sell retracement.

So OnStart it now runs "CheckFibs" instead of CalcFibo and the Check Fibs function below runs the CalcFibs if True. does this look right? 

void CheckFibs()
  {
   double LowValue2=0,HighValue2=0;
   double LowValue1=0,HighValue1=0;
   int lowest_bar=iLowest(NULL,0,MODE_LOW,BarsBack,StartBar);
   int highest_bar=iHighest(NULL,0,MODE_HIGH,BarsBack,StartBar);
   static int prev_lowest_bar, prev_highest_bar;
   HighValue1=High[prev_highest_bar];
   LowValue1=Low[prev_lowest_bar];
   HighValue2=High[highest_bar];
   LowValue2=Low[lowest_bar];

   if (HighValue1 != HighValue2 || LowValue1 != LowValue2)
      CalcFibo();
    
    
  }


 

 
Thehallster:

Added the code as attached and just looking at this it looks like its working but not properly in my EA still. Could this be an issue for strategy tester only? when i have the EA running on strategy tester it still runs slow and the fib scales and lines are all flickering asif they are deleting and re-drawing on every tick.

Im going to try and re-check my code in the EA to make sure ive not got any input parameters that have been deleted or something that its trying to use in the Icustom calls as I know that might be causing it to reload and delete the Indi on every tick.

 

I did this with the code guys based on your suggestions above. Moved the calculation to the Start function to Call the CalcFIbo rather than running it in line with the current If Function that defines what way to draw the fib Blue or Red for buy or sell retracement.

So OnStart it now runs "CheckFibs" instead of CalcFibo and the Check Fibs function below runs the CalcFibs if True. does this look right? 

void CheckFibs()
  {
   double LowValue2=0,HighValue2=0;
   double LowValue1=0,HighValue1=0;
   int lowest_bar=iLowest(NULL,0,MODE_LOW,BarsBack,StartBar);
   int highest_bar=iHighest(NULL,0,MODE_HIGH,BarsBack,StartBar);
   static int prev_lowest_bar, prev_highest_bar;
   HighValue1=High[prev_highest_bar];
   LowValue1=Low[prev_lowest_bar];
   HighValue2=High[highest_bar];
   LowValue2=Low[lowest_bar];

   if (HighValue1 != HighValue2 || LowValue1 != LowValue2)
      CalcFibo();
    
    
  }


 

I wouldn't want to change fibos on the last bar, especially when using them for an exit strategy as well. Anyway this should work.

void CalcFibo()
{
  
  int lowest_bar=iLowest(NULL,0,MODE_LOW,BarsBack,StartBar);
  int highest_bar=iHighest(NULL,0,MODE_HIGH,BarsBack,StartBar);
  double lowest=Low[lowest_bar];
  double highest=High[highest_bar];
  
  static double prev_lowest,prev_highest;
  if(prev_lowest!=lowest || prev_highest!=highest)
  {
         prev_lowest=lowest;
         prev_highest=highest;
  }
  else
         return;
  
  DeleteAllObjects();
 
Ernst Van Der Merwe:

I wouldn't want to change fibos on the last bar, especially when using them for an exit strategy as well. Anyway this should work.

void CalcFibo()
{
  
  int lowest_bar=iLowest(NULL,0,MODE_LOW,BarsBack,StartBar);
  int highest_bar=iHighest(NULL,0,MODE_HIGH,BarsBack,StartBar);
  double lowest=Low[lowest_bar];
  double highest=High[highest_bar];
  
  static double prev_lowest,prev_highest;
  if(prev_lowest!=lowest || prev_highest!=highest)
  {
         prev_lowest=lowest;
         prev_highest=highest;
  }
  else
         return;
  
  DeleteAllObjects();

MAGIC ERNST! that worked perfect Can you use Static Double and do Prev Values for anything or has to be certain values?

 

I Don't suppose in your wealth of knowledge you have experience with limiting EA's with Time Strings?? I have another post about Time restrictions for EA's had no responses as yet!

 

At the mo I do it crude as hell using like 4 inputs start Minute and Hours and use IF functions for each.

If Time current < Start Hour then return 0 etc etc really crude looking and runs every tick.

 

Thought I could do it using like On Timer or something using a time input string 08:00:00 rather than individual minutes and hour input variable fields. 

 
attached the completed code for future assistance