iTime returns wrong time from iBarshift

 

I have created an indicator that plots two trendlines; one from the highest point of last month to the highest point of last week and the other from the lowest point of last month to the lowest point of last week. When the lines are drawn, OBJPROP_PRICE1 & OBJPROP_PRICE2 in both instances appear to be correct, however, OBJPROP_TIME1 is drawn correctly intermittently in both cases and OBJPROP_TIME2 shifted much too far to the right of the chart every time.


I know I am missing something obvious, but I can't see it. Could someone take a look at my code and tell me where I am going wrong please? TIA


//+------------------------------------------------------------------+

//| Trend-SR.mq4 |

//| Copyright © 2009, Limstylz |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2009, Limstylz"

#property indicator_chart_window


//+------------------------------------------------------------------+

//| Custom indicator initialization function |

//+------------------------------------------------------------------+



int init()

{

//---- indicators



return(0);

}

//+------------------------------------------------------------------+

//| Custom indicator deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

ObjectDelete("MN2WKBottom");

ObjectDelete("MN2WKTop");


return(0);

}

//+------------------------------------------------------------------+

//| Custom indicator iteration function |

//+------------------------------------------------------------------+

int start()

{


//Last Month

datetime lastmonth=iTime(NULL,PERIOD_MN1,1);

int lastmonthshift=iBarShift(NULL,PERIOD_M1,lastmonth);

int lastmonthhighbar=iHighest(NULL,PERIOD_M1,MODE_HIGH,lastmonthshift,0);

datetime lastmonthhightime=iTime(NULL,PERIOD_M1,lastmonthhighbar);

int lastmonthlowbar=iLowest(NULL,PERIOD_M1,MODE_LOW,lastmonthshift,0);

datetime lastmonthlowtime=iTime(NULL,PERIOD_M1,lastmonthlowbar);

double lastmonthhighprice=iHigh(NULL,PERIOD_MN1,1);

double lastmonthlowprice=iLow(NULL,PERIOD_MN1,1);

//Last Week

datetime lastweek=iTime(NULL,PERIOD_W1,1);

int lastweekshift=iBarShift(NULL,PERIOD_M1,lastweek);

int lastweekhighbar=iHighest(NULL,PERIOD_M1,MODE_HIGH,lastweekshift,0);

datetime lastweekhightime=iTime(NULL,PERIOD_M1,lastweekhighbar);

int lastweeklowbar=iLowest(NULL,PERIOD_M1,MODE_LOW,lastweekshift,0);

datetime lastweeklowtime=iTime(NULL,PERIOD_M1,lastweeklowbar);

double lastweekhighprice=iHigh(NULL,PERIOD_W1,1);

double lastweeklowprice=iLow(NULL,PERIOD_W1,1);

ObjectCreate("MN2WKTop", OBJ_TREND, 0, 0, 0, 0, 0);

ObjectSet ("MN2WKTop",OBJPROP_COLOR,Maroon);

ObjectSet ("MN2WKTop",OBJPROP_WIDTH,1);

ObjectSet ("MN2WKTop",OBJPROP_PRICE1,lastmonthhighprice);

ObjectSet ("MN2WKTop",OBJPROP_PRICE2,lastweekhighprice);

ObjectSet ("MN2WKTop",OBJPROP_TIME1,lastmonthhightime);

ObjectSet ("MN2WKTop",OBJPROP_TIME2,lastweekhightime);

ObjectCreate("MN2WKBottom", OBJ_TREND, 0, 0, 0, 0, 0);

ObjectSet ("MN2WKBottom",OBJPROP_COLOR,DarkGreen);

ObjectSet ("MN2WKBottom",OBJPROP_WIDTH,1);

ObjectSet ("MN2WKBottom",OBJPROP_PRICE1,lastmonthlowprice);

ObjectSet ("MN2WKBottom",OBJPROP_PRICE2,lastweeklowprice);

ObjectSet ("MN2WKBottom",OBJPROP_TIME1,lastmonthlowtime);

ObjectSet ("MN2WKBottom",OBJPROP_TIME2,lastweeklowtime);

return(0);

}

//+------------------------------------------------------------------+

 

Hi L

A couple of questions, why are using Minutes "PERIOD_M1 " I thought you are only interested in weeks and months shouldn't they be MN1? Do you want the indicator to run on lower timeframes than weeks?

Also be warned if you are intentionally using 1 minute then the broker may not hold data for that on the 1 minute timescale so you will have to use 5 or 15 minutes.

 

Hi Ruptor,


I need to be able to view the lines on lower TF's. As data will not be going as far back as 2 months previous, the issue with broker not holding data for M1 TF is not a problem as a lot of brokers hold at least 3 months of M1 data in my experience (at least alpari UK and FXopen do). Any assitance you can give me with regards to drawing the lines accurately will be greatly appreciated.

 
This is the problem: The times aren't being shifted properly because the start bar in iHighest and iLowest have not been defined although the number of periods to count have, giving the illusion of working occassionally. How do I work out how many bars to shift?
 

Show me a picture because I have loaded it and just see two angled lines pointing in opposte ways to oblivion.

 

Here is a picture for you.

The red arrow indicates where the lowest of last month to the lowest of last week trendline should end

The blue arrow indicates where the highest of last month to the highest of last week trendline should end

The yellow arrows indicate where the trend lines currently end



 
How about drawing lines instead of arrows on your example.
 

So has all gone to pot then. I didn't understand your previous question " How do I work out how many bars to shift?"

Have you printed the values out and checked which one is wrong? If not use this and lets try and get the upper line right first.


Comment(lastmonthshift," ",lastmonthhighbar," ",lastmonthlowbar," ",lastmonthhighprice," ",lastmonthlowprice);
//Last Week


Sorry to be a bit slow I am having a bad day thinking wise today.

 

Take a look at this and see if it is nearer to what you intended. Note the change to the last two lines where I made it get the prices for the high and low bars from the 1M time period and not the range from start to end of month in hift calls.


//Last Month
datetime lastmonthopen=iTime(NULL,PERIOD_MN1,1);
datetime lastmonthclose=iTime(NULL,PERIOD_MN1,0);
int lastmonthstart=iBarShift(NULL,PERIOD_M1,lastmonthopen);
int lastmonthend=iBarShift(NULL,PERIOD_M1,lastmonthclose);
int lastmonthhighbar=iHighest(NULL,PERIOD_M1,MODE_HIGH,lastmonthstart,lastmonthend);
datetime lastmonthhightime=iTime(NULL,PERIOD_M1,lastmonthhighbar);
int lastmonthlowbar=iLowest(NULL,PERIOD_M1,MODE_LOW,lastmonthstart,lastmonthend);
datetime lastmonthlowtime=iTime(NULL,PERIOD_M1,lastmonthlowbar);
double lastmonthhighprice=iHigh(NULL,PERIOD_M1,lastmonthhighbar);
double lastmonthlowprice=iLow(NULL,PERIOD_M1,lastmonthlowbar);
//double lastmonthlowprice=iLow(NULL,PERIOD_MN1,1);


Can't see why the low start price is not at the start of the month but it must be the "lastmonthstart" varible is not correct.

Hope this gives you more to look at.

 

Ruptor,


I got around the issue in a similar manner to yours by doing the following:


datetime currentmonth=iTime(NULL,PERIOD_MN1,0);

int lastmonthshift=iBarShift(NULL,PERIOD_M1,currentmonth);

int lastmonthhighbar=iHighest(NULL,PERIOD_M1,MODE_HIGH,lastmonthshift,lastmonthshift);

datetime lastmonthhightime=iTime(NULL,PERIOD_M1,lastmonthhighbar);

int lastmonthlowbar=iLowest(NULL,PERIOD_M1,MODE_LOW,lastmonthshift,lastmonthshift);

datetime lastmonthlowtime=iTime(NULL,PERIOD_M1,lastmonthlowbar);

double lastmonthhighprice=iHigh(NULL,PERIOD_MN1,1);

double lastmonthlowprice=iLow(NULL,PERIOD_MN1,1);


But yours looks like a more elegant solution. I will try yours, see how it compares and let you know. Thanks very much for your assistance!

 

Ruptor,


Your code works better than mine with the exception of the week start which seemed to be plotting the week previous. I have had to use shifting of iHighest and iLowest as in my post above:


int lastweeklowbar=iLowest(NULL,PERIOD_M1,MODE_LOW, lastweekend, lastweekend);

int lastweekhighlowbar=iHighest(NULL,PERIOD_M1,MODE_HIGH, lastweekend, lastweekend);


I can't work out why that works better though as the math doesn't make sense to me... so be it.


Your suggestion to use:


double lastmonthhighprice=iHigh(NULL,PERIOD_M1,lastmonthhighbar);
double lastmonthlowprice=iLow(NULL,PERIOD_M1,lastmonthlowbar);


rather than the range is more elegant and also seems to work better as I was still getting over/undershoots with my original code.


Thanks once again for your help. I will be sure to credit you in the code if I decide to post it anywhere.


Cheers, fella!

Reason: