Could someone please help me(trendline question)

 
Hi guys, im trying to write a simple indicator for MT4 that plot a trend line from the previously fractal High to the current candle High and a trend line from the previously fractal Low the current candle Low..im new in mq4 so an help would be appreciated.. thanx in advance!!!!!
 

Use Object to create your TrendLine type OBJ_TREND

Current candle low is Low[0] current candle high is High[0]

You can use iFractals to get the fractals

 

Hey RaptorUK, thanx for your quick answer!, i found this code

void MarkItDownX(int iBackBars, int iForwardBars)

{

// Other free stuff at http://www.selectfx.net/free_stuff_menu.htm

int iHighIndex, iLowIndex;

double dHi, dLo;

datetime dtStart, dtEnd;





// Get high/low of last n periods

iHighIndex = iHighest(NULL, PERIOD_M5, MODE_HIGH, iBackBars, 1);

iLowIndex = iLowest(NULL, PERIOD_M5, MODE_LOW, iBackBars, 1);

dHi = iHigh(NULL, PERIOD_M5, iHighIndex);

dLo = iLow(NULL, PERIOD_M5, iLowIndex);

dtStart = TimeCurrent()-(iBackBars*300); // n*5 min bars

dtEnd = TimeCurrent()+(iForwardBars*300); // n*5 min bars

sstrCurrentUpLine = "X UP " + TimeToStr(TimeCurrent()); // Set current active line name

sstrCurrentDownLine = "X DN " + TimeToStr(TimeCurrent());

ObjectCreate(sstrCurrentUpLine, OBJ_TREND, 0, dtStart, dLo, dtEnd, dHi);

ObjectSet(sstrCurrentUpLine, OBJPROP_COLOR, Red);

ObjectSet(sstrCurrentUpLine, OBJPROP_STYLE, STYLE_SOLID);

ObjectSet(sstrCurrentUpLine, OBJPROP_WIDTH, 2);

ObjectSet(sstrCurrentUpLine, OBJPROP_RAY, False);



ObjectCreate(sstrCurrentDownLine, OBJ_TREND, 0, dtStart, dHi, dtEnd, dLo);

ObjectSet(sstrCurrentDownLine, OBJPROP_COLOR, Blue);

ObjectSet(sstrCurrentDownLine, OBJPROP_STYLE, STYLE_SOLID);

ObjectSet(sstrCurrentDownLine, OBJPROP_WIDTH, 2);

ObjectSet(sstrCurrentDownLine, OBJPROP_RAY, False);

}

can you help me to adjust it to my purpose?

thank you a lot!


 
AtApi:

Hey RaptorUK, thanx for your quick answer!, i found this code

can you help me to adjust it to my purpose?

thank you a lot!

Please use the SRC button to post code . . .

I'll help you code it but I'm not coding it for you. If you don't know how get reading: https://book.mql4.com//

 
RaptorUK:

Please use the SRC button to post code . . .

I'll help you code it but I'm not coding it for you. If you don't know how get reading: https://book.mql4.com//


Thanx RaptorUK.. ill see what i can do and then ill post the code here...im really thankful for your help!

cheers

 

ok i have modify the code a bit.. but no good result

   void MarkItDownX(int iBackBars, int iForwardBars)
{
 // Other free stuff at http://www.selectfx.net/free_stuff_menu.htm
 int iHighIndex, iLowIndex;
 double dHi, dLo;
 datetime dtStart, dtEnd;

iFractals(NULL,PERIOD_M5,MODE_HIGH,iBackBars);
  // Get high/low of last n periods 
  iHighIndex = iHighest(NULL, PERIOD_M5, MODE_HIGH, iBackBars, 1); //iFractals(NULL,PERIOD_M5,MODE_UPPER,iBackBars); 
  iLowIndex =  iLowest(NULL, PERIOD_M5,  MODE_LOW, iBackBars, 1); // iFractals(NULL,PERIOD_M5,MODE_LOWER,iBackBars); 
  
  dHi = iFractals(NULL,PERIOD_M5,MODE_UPPER,iHighIndex); // iHigh(NULL, PERIOD_M5, iHighIndex);
  dLo  = iFractals(NULL,PERIOD_M5,MODE_LOWER,iLowIndex); // iLow(NULL, PERIOD_M5, iLowIndex);
  
  dtStart = TimeCurrent()-(iBackBars*300); // n*5 min bars
  dtEnd   = TimeCurrent()+(iForwardBars*300); // n*5 min bars
 
 sstrCurrentUpLine = "X UP " + TimeToStr(TimeCurrent()); // Set current active line name
 sstrCurrentDownLine = "X DN " + TimeToStr(TimeCurrent());
 
 ObjectCreate(sstrCurrentUpLine, OBJ_TREND, 0, dtStart, dHi, dtEnd, High[0]);
 ObjectSet(sstrCurrentUpLine, OBJPROP_COLOR, Red);
 ObjectSet(sstrCurrentUpLine, OBJPROP_STYLE, STYLE_SOLID);
 ObjectSet(sstrCurrentUpLine, OBJPROP_WIDTH, 2);
 ObjectSet(sstrCurrentUpLine, OBJPROP_RAY, False);


 ObjectCreate(sstrCurrentDownLine, OBJ_TREND, 0, dtStart, dLo, dtEnd, Low[0]);
 ObjectSet(sstrCurrentDownLine, OBJPROP_COLOR, Blue);
 ObjectSet(sstrCurrentDownLine, OBJPROP_STYLE, STYLE_SOLID);
 ObjectSet(sstrCurrentDownLine, OBJPROP_WIDTH, 2);
 ObjectSet(sstrCurrentDownLine, OBJPROP_RAY, False);
 
  

}

and i call the function with

MarkItDownX(20, 10);

thats what happen

it seams the lower trendline start correctly but is not touching the Low[0]..the upper trendline is not even on the highest fractal...

I know im close but maybe a little help?...

thank you

 

well now that i look the pic better also the lower trendline is wrong it should touch the lower fractal which is the following one...

 
OK, I see what you are doing, your time values for the TrendLines are wrong . . . you are trying to make the lines go past the Fractal to the left and past the current bar to the right . . . . you can do that but you will need to calculate the correct price value at this projected position. You can calculate it manually, or draw the line correctly then use GetValueByShift to get the correct price values and then re-draw the line to the projected values.
 

Hey Buddy, thanx for your help, i have a couple of questions..look at this

 void MarkItDownX(int iBackBars, int iForwardBars)
{

 int iHighIndex, iLowIndex;
 double dHi, dLo;
 datetime dtStartUP, dtStartDW, dtEnd;



  // Get high/low of last n periods 
  iHighIndex = iHighest(NULL, PERIOD_M5, MODE_HIGH, iBackBars, 1); 
  iLowIndex =  iLowest(NULL, PERIOD_M5,  MODE_LOW, iBackBars, 1); 
  
  dHi = iFractals(NULL,PERIOD_M5,MODE_UPPER,iHighIndex); // High[iHighIndex];                     
  dLo  =iFractals(NULL,PERIOD_M5,MODE_LOWER,iLowIndex); // Low[iLowIndex];                     
  
  dtStartUP = Time[iHighIndex];//TimeCurrent()-(iBackBars*300); // n*5 min bars
  dtStartDW = Time[iLowIndex];
  dtEnd   = Time[0];//TimeCurrent()+(iForwardBars*300); // n*5 min bars
 
 sstrCurrentUpLine = "X UP " + TimeToStr(TimeCurrent()); // Set current active line name
 sstrCurrentDownLine = "X DN " + TimeToStr(TimeCurrent());
 
 ObjectCreate(sstrCurrentUpLine, OBJ_TREND, 0, dtStartUP, dHi, dtEnd, High[0]);
 ObjectSet(sstrCurrentUpLine, OBJPROP_COLOR, Red);
 ObjectSet(sstrCurrentUpLine, OBJPROP_STYLE, STYLE_SOLID);
 ObjectSet(sstrCurrentUpLine, OBJPROP_WIDTH, 2);
 ObjectSet(sstrCurrentUpLine, OBJPROP_RAY, False);


 ObjectCreate(sstrCurrentDownLine, OBJ_TREND, 0, dtStartDW, dLo, dtEnd, Low[0]);
 ObjectSet(sstrCurrentDownLine, OBJPROP_COLOR, Blue);
 ObjectSet(sstrCurrentDownLine, OBJPROP_STYLE, STYLE_SOLID);
 ObjectSet(sstrCurrentDownLine, OBJPROP_WIDTH, 2);
 ObjectSet(sstrCurrentDownLine, OBJPROP_RAY, False);
 
  

}

i have changed the time values and now it works off course is not projecting n bars forward and backwards, but this will be the next step, my question is what iFractals returns? the index of the highest fractal n bars back(like iHighest) or the price? or should i do a loop? because what im trying to do is to get is the value of the previously highest fractal for the upper trendline and the previously lowest value of fractal for the lower trendline....

 
also in this way when i call this function with MarkItDownX(20, 10); and if there is no fractal within these values it will plot a vertical line so probably i have to do a loop backward but in this case it will consider the highest and lowest fractal and not just the previously one... i hope you are following me.. sorry if is a bit confusing..
 
iFractals returns a double so I assume it's a price . . . I've never used it so can't say for sure. I think you are going to have to experiment a little . . .