Some calculation error in my fraktal basesd trendline identifier

 

Hi,

Today i tryed to programm a indicator which draws trendline. the idea is following:

use all fraktals in the past 200 bars and the trendline is that one which is toucht by the most fraktals.

here is the code:

//+------------------------------------------------------------------+
//|                                                    AutoTrend.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "zzuegg"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Red
#property indicator_color2 Red
#property indicator_color3 Red
#property indicator_color4 Red
#property indicator_color5 Red
#property indicator_color6 Red
#property indicator_color7 Red
#property indicator_color8 Red
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];
double ExtMapBuffer7[];
double ExtMapBuffer8[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,ExtMapBuffer5);
   SetIndexStyle(5,DRAW_LINE);
   SetIndexBuffer(5,ExtMapBuffer6);
   SetIndexStyle(6,DRAW_LINE);
   SetIndexBuffer(6,ExtMapBuffer7);
   SetIndexStyle(7,DRAW_LINE);
   SetIndexBuffer(7,ExtMapBuffer8);
//----
   return(0);
  }

int fraktals.lower[];
int fraktals.upper[];

void updateFraktals(){
   int upper=0;
   int lower=0;
   for(int i=200;i>=3;i--){
      if(iFractals(Symbol(),Period(),MODE_UPPER,i)!=0){
         upper++;
         ArrayResize(fraktals.upper,upper);
         fraktals.upper[upper-1]=i;
      }
      if(iFractals(Symbol(),Period(),MODE_UPPER,i)!=0){
         lower++;
         ArrayResize(fraktals.lower,lower);
         fraktals.upper[lower-1]=i;
      }
   }
}

extern int BarsToWatch=200;
int treshold=0;
int start()
  {
   int    counted_bars=IndicatorCounted();
   treshold=10*Digits;
   updateFraktals();
   int max=0;
   int max.f1=0;
   int max.f2=0;
   for(int f1=0;f1<=ArraySize(fraktals.upper);f1++){
      for(int f2=f1+1;f2<=ArraySize(fraktals.upper);f2++){
         double d=High[fraktals.upper[f1]];
         double k=(High[fraktals.upper[f2]]-High[fraktals.upper[f1]])/(fraktals.upper[f1]-fraktals.upper[f2]);
         int count=0;
         for(int f3=f1+1;f3<=ArraySize(fraktals.upper);f3++){
            int relativePos=fraktals.upper[f1]-fraktals.upper[f3];
            double value=(k*relativePos)+d;
            if((High[fraktals.upper[f3]]<(value+treshold))&&(High[fraktals.upper[f3]]>(value-treshold))){
               count++;
            }
         }
         if(count>max){
            max=count;
            max.f1=f1;
            max.f2=f2;
         }
      }
   }
   Alert(max+"   "+max.f1+"   "+max.f2);   
   ObjectCreate("Trend.Upper",OBJ_TREND,0,0,0,0);
   ObjectSet   ("Trend.Upper",OBJPROP_TIME1,Time[fraktals.upper[max.f1]]);
   ObjectSet   ("Trend.Upper",OBJPROP_TIME2,Time[fraktals.upper[max.f2]]);  
   ObjectSet   ("Trend.Upper",OBJPROP_PRICE1,High[fraktals.upper[max.f1]]);
   ObjectSet   ("Trend.Upper",OBJPROP_PRICE2,High[fraktals.upper[max.f2]]);      
   return(0);
  }
//+------------------------------------------------------------------+

i know this try all and find the right is not the nicest code, but this is a proof of concept. the problem is that there is somewhere a logic error and i am not able to find it.

max.f1 and max.f2 are allways 0 and 1. (the first and the second fraktal in my array) with 19 touches...


maybe one of you can see the mistake..

thanks

//z

 
zzuegg:
the problem is that there is somewhere a logic error and i am not able to find it.
// treshold=10*Digits;  40 or 50 is not a valid price
  treshold=10*Point;
// or
 treshold=10*pips2dbl;

//++++ These are adjusted for 5 digit brokers.
double  pips2points,    // slippage  3 pips    3=points    30=points
        pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int init(){
    if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl


 
zzuegg:

Hi,

Today i tryed to programm a indicator which draws trendline. the idea is following:

use all fraktals in the past 200 bars and the trendline is that one which is toucht by the most fraktals.

here is the code:

i know this try all and find the right is not the nicest code, but this is a proof of concept. the problem is that there is somewhere a logic error and i am not able to find it.

max.f1 and max.f2 are allways 0 and 1. (the first and the second fraktal in my array) with 19 touches...


maybe one of you can see the mistake..

thanks

//z


What is the error? Why are thinking the code is not working?

Remember iFractals is not like iMA in that the indicator value itself does not "persist" until market prices invoke a change in its value. iFractals will only generate a non-zero value for the candle in which a fractalup or fractaldown level is changed.

 
WHRoeder:


thx a lot. i don't know why i multiplicated with Digits instead of Points. i went a few times trough the whole logic but that line it seems i forgot.

Reason: