Hello Forum
I am in the process of building an indicator that I can add to the short time frame chart that I normally trade, and show the high and low of the current bars for higher time frames (and hence likely support and resistance levels)
But to keep clarity I am wondering how to show these highs and lows as short horizontal lines that "ONLY"appear on the chart to the right of current price action.
I guess you have chart shift set and enabled and want to pace these line, must be trend lines, objects in the future ? it's not difficult to do but you will have some issues plotting the lines ahead of the markets closing at the week end . . . you have to plot them by time, you can't use bar numbers as they are to the right of bar 0 . . . and if you plot them ahead of where the market closes they will not be plotted when the market re-opens . . so you will need to take care of this. It might be as simple as adjusting their datetime position at the start of each new bar.
Thanks Raptor, I have tried to do as you suggested and with the help of one of a post by "phy" https://forum.mql4.com/16511 and adapting some work of "bdeyes" https://www.mql5.com/en/code/10463
But still falling short of getting this indicator to work.
I hasten to add that this is my first time dealing with "Objects" that I need to create within my customised indicators, so apologies for the stupid errors.
I am wondering if the problem is in the way I have defined the start and end date times for my trendlines.
I am struggling to understand how to reference datetime position properly
Hope someone might be able to point out my error. My attempt at the code is below. I am not sure why the trend lines are not visible on my 1 min chart when I attach the indicator
Thanks as always
Dave
//+------------------------------------------------------------------+ //| | //| Copyright © 1999-2008, MetaQuotes Software Corp. | //| http://www.metaquotes.ru | //+------------------------------------------------------------------+ //---- indicator settings #property indicator_chart_window #property indicator_buffers 0 //---- //---- indicator parameters extern int timeframe = 30; extern int bars_forward = 5; extern int font_size = 9; // exported variables extern string note1 = "----timeframe----"; extern string help1 = "set to true to add timeframe to chart"; extern bool TF1_HIGH = true; // set true to display MA's extern bool TF1_LOW = true; // set true to display //---- indicator buffers //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { IndicatorShortName("HTF-levels"); //---- initialization done return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { ObjectDelete("TF1_HIGH"); ObjectDelete("TF1_HIGH__label"); ObjectDelete("TF1_LOW"); ObjectDelete("TF1_LOW_LABEL"); return(0); } //+------------------------------------------------------------------+ //| Other timeframe line levels | //+------------------------------------------------------------------+ int start() { int nLimit, i; int nCountedBars; int ShiftDif; nCountedBars=IndicatorCounted(); //---- check for possible errors if(nCountedBars<0) return(-1); //---- last counted bar will be recounted if(nCountedBars>0) nCountedBars--; nLimit=Bars-nCountedBars; //---- main loop for(i=0; i<nLimit; i++) //---- Higher Time Frame (TF1) LEVEL LINES //---- bool ObjectCreate(string name, int type, int window, datetime time1, double price1, datetime time2=0,double price2=0, datetime time3=0, double price3=0) //---- Time[0] + numberOfSecondsIntoTheFuture is: Time[0]+q*Period()*60; // q bars into the future //---- if (TF1_HIGH) // draw if true { if (ObjectFind("TF1_HIGH")!= -1) ObjectDelete("TF1_HIGH"); // Delete old lines if present ObjectCreate("TF1_HIGH", OBJ_TREND, 0, (Time[0]+20000), iHigh(NULL,timeframe,0), (Time[0]+24000), iHigh(NULL,timeframe,0)); ObjectSet("TF1_HIGH", OBJPROP_RAY, false); ObjectSet("TF1_HIGH", OBJPROP_TIME1, (Time[0]+bars_forward*60*timeframe)); ObjectSet("TF1_HIGH", OBJPROP_TIME2, (Time[0]+2*bars_forward*60*timeframe)); ObjectSet("TF1_HIGH", OBJPROP_COLOR, Lime); ObjectSet("TF1_HIGH",OBJPROP_WIDTH,3); } if (TF1_LOW) // draw if true { if (ObjectFind("TF1_LOW")!= -1) ObjectDelete("TF1_LOW"); // Delete old lines if present ObjectSet("TF1_LOW", OBJPROP_RAY, false); ObjectCreate("TF1_LOW", OBJ_TREND, 0, (Time[0]+bars_forward*60*timeframe), iLow(NULL,timeframe,0), (Time[0]+bars_forward*60*timeframe), iLow(NULL,timeframe,0)); ObjectSet("TF1_Low", OBJPROP_TIME1, (Time[0]+bars_forward*60*timeframe)); ObjectSet("TF1_Low", OBJPROP_TIME2, (Time[0]+bars_forward*60*timeframe)); ObjectSet("TF1_Low", OBJPROP_COLOR, Lime); ObjectSet("TF1_Low",OBJPROP_WIDTH,3); } // ---- Higher Time Frame (TF1) Line Labels if (TF1_HIGH) // draw if true { if (ObjectFind("TF1_HIGH_label") != -1) ObjectDelete("TF1_HIGH_label"); // Delete old labels if present and draw new one ObjectCreate("TF1_HIGH_label", OBJ_TEXT, 0, (Time[0]+bars_forward*60*timeframe), iOpen(NULL, PERIOD_D1,0)); ObjectSet("TF1HIGH_label", OBJPROP_TIME1, (Time[0]+bars_forward*60*timeframe)); ObjectSet("TF1_HIGH_label", OBJPROP_COLOR, Lime); ObjectSetText("TF1_HIGH_label", "TF1 HIGH", font_size, "Arial", Lime); } if (TF1_LOW) // draw if true { if (ObjectFind("TF1_LOW_label") != -1) ObjectDelete("TF1_LOW_label"); // Delete old labels if present and draw new one ObjectCreate("TF1_LOW_label", OBJ_TEXT, 0, (Time[0]+bars_forward*60*timeframe), iLow(NULL, PERIOD_D1,0)); ObjectSet("TF1_LOW_label", OBJPROP_TIME1, (Time[0]+bars_forward*60*timeframe)); ObjectSet("TF1_LOW_label", OBJPROP_COLOR, Lime); ObjectSetText("TF1_LOW_label", "TF1 LOW", font_size, "Arial", Lime); } //---- return(0); } //+------------------------------------------------------------------+
Thanks Raptor, I have tried to do as you suggested and with the help of one of a post by "phy" https://forum.mql4.com/16511 and adapting some work of "bdeyes" https://www.mql5.com/en/code/10463
You are only interested in current price, yes ? so the price for bar 0 ? what is the purpose of your main loop ?
//---- main loop for(i=0; i<nLimit; i++)
If you need it, great . . . don't just copy code without understanding what it does though, you may end up copying incorrect or bad code . . .
You don't need to do this . . .
if (ObjectFind("TF1_HIGH")!= -1) ObjectDelete("TF1_HIGH"); // Delete old lines if present
do this instead . . .
if (ObjectFind("TF1_HIGH")== -1) // if Object doesn't exist create it ObjectCreate("TF1_HIGH", OBJ_TREND, 0, 0, 0, 0, 0); // add these lines ObjectSet("TF1_HIGH", OBJPROP_PRICE1, iHigh(NULL,timeframe,0)); ObjectSet("TF1_HIGH", OBJPROP_PRICE2, iHigh(NULL,timeframe,0));
This won't fix your issue . . . I will look more in the Morning, time for bed now.
You are only interested in current price, yes ? so the price for bar 0 ? what is the purpose of your main loop ?
If you need it, great . . . don't just copy code without understanding what it does though, you may end up copying incorrect or bad code . . .
You don't need to do this . . .
do this instead . . .
This won't fix your issue . . . I will look more in the Morning, time for bed now.
Thanks, yes only interested in the high and low prices of the current higher time frame candle, in this example 30 min, but obviously the intent is to have the ability to vary the timeframe.
My base premise is that these price values may be likely support and resistance levels and for clarity I want to show those levels as lines on my chart to the right of the most recent 1 min candle, and in the section that is vacant when using "Chart Shift". This removes the clutter around the actual price candles.
Plotting the start of the trendlines relative to the current 1 min candle is giving me trouble !!
And yes point taken about copying lines of code that I may not understand, I am spending hours and hours with the MQL4 documentation and trying to learn through examples, but struggling still :-) IN future I will extract isolated pieces of code that I don't understand and ask for some explanation on the forum, if I haven't found the answer elsewhere.
Will insert your suggested changes tonight.
thanks !!!!!
Thanks, yes only interested in the high and low prices of the current higher time frame candle, in this example 30 min, but obviously the intent is to have the ability to vary the timeframe.
My base premise is that these price values may be likely support and resistance levels and for clarity I want to show those levels as lines on my chart to the right of the most recent 1 min candle, and in the section that is vacant when using "Chart Shift". This removes the clutter around the actual price candles.
Plotting the start of the trendlines relative to the current 1 min candle is giving me trouble !!
And yes point taken about copying lines of code that I may not understand, I am spending hours and hours with the MQL4 documentation and trying to learn through examples, but struggling still :-) IN future I will extract isolated pieces of code that I don't understand and ask for some explanation on the forum, if I haven't found the answer elsewhere.
Will insert your suggested changes tonight.
thanks !!!!!
Also found a mistake in the ObjectCreate line below, fixing this alone results in the level lines actually plotting, but not visible on 1 min chart, but visible on 15 min chart ??
Will work on suggested changes as soon as work allows
if (TF1_HIGH) // draw if true { if (ObjectFind("TF1_HIGH")!= -1) ObjectDelete("TF1_HIGH"); // Delete old lines if present ObjectCreate("TF1_HIGH", OBJ_TREND, 0, (Time[0]+20000), iHigh(NULL,timeframe,0), (Time[0]+24000), iHigh(NULL,timeframe,0)); should read ObjectCreate("TF1_HIGH", OBJ_TREND, 0, (Time[0]+bars_forward*60*timeframe), iHigh(NULL,timeframe,0), (Time[0]+2*bars_forward*60*timeframe), iHigh(NULL,timeframe,0));
And yes point taken about copying lines of code that I may not understand, I am spending hours and hours with the MQL4 documentation and trying to learn through examples, but struggling still :-) IN future I will extract isolated pieces of code that I don't understand and ask for some explanation on the forum, if I haven't found the answer elsewhere.
You also need to sort out your indenting . . . . if you have a consistent style that you understand it will help you spot problems, for example . . . .
nLimit=Bars-nCountedBars; //---- main loop for(i=0; i<nLimit; i++) // this loop only applies to this block . . . //---- Higher Time Frame (TF1) LEVEL LINES //---- bool ObjectCreate(string name, int type, int window, datetime time1, double price1, datetime time2=0,double price2=0, datetime time3=0, double price3=0) //---- Time[0] + numberOfSecondsIntoTheFuture is: Time[0]+q*Period()*60; // q bars into the future //---- if (TF1_HIGH) // draw if true // . . . . . here. { if (ObjectFind("TF1_HIGH")!= -1) ObjectDelete("TF1_HIGH"); // Delete old lines if present ObjectCreate("TF1_HIGH", OBJ_TREND, 0, (Time[0]+20000), iHigh(NULL,timeframe,0), (Time[0]+24000), iHigh(NULL,timeframe,0)); ObjectSet("TF1_HIGH", OBJPROP_RAY, false); ObjectSet("TF1_HIGH", OBJPROP_TIME1, (Time[0]+bars_forward*60*timeframe)); ObjectSet("TF1_HIGH", OBJPROP_TIME2, (Time[0]+2*bars_forward*60*timeframe)); ObjectSet("TF1_HIGH", OBJPROP_COLOR, Lime); ObjectSet("TF1_HIGH",OBJPROP_WIDTH,3); } if (TF1_LOW) // draw if true // this block is outside the for loop above . . . { if (ObjectFind("TF1_LOW")!= -1) ObjectDelete("TF1_LOW"); // Delete old lines if present ObjectSet("TF1_LOW", OBJPROP_RAY, false); ObjectCreate("TF1_LOW", OBJ_TREND, 0, (Time[0]+bars_forward*60*timeframe), iLow(NULL,timeframe,0), (Time[0]+bars_forward*60*timeframe), iLow(NULL,timeframe,0)); ObjectSet("TF1_Low", OBJPROP_TIME1, (Time[0]+bars_forward*60*timeframe)); ObjectSet("TF1_Low", OBJPROP_TIME2, (Time[0]+bars_forward*60*timeframe)); ObjectSet("TF1_Low", OBJPROP_COLOR, Lime); ObjectSet("TF1_Low",OBJPROP_WIDTH,3); }
This is incorrect, you can't do a ObjectSet() when you have just deleted the Object . . .
if (TF1_LOW) // draw if true { if (ObjectFind("TF1_LOW")!= -1) ObjectDelete("TF1_LOW"); // Delete old lines if present ObjectSet("TF1_LOW", OBJPROP_RAY, false); ObjectCreate("TF1_LOW", OBJ_TREND, 0, (Time[0]+bars_forward*60*timeframe), iLow(NULL,timeframe,0), (Time[0]+bars_forward*60*timeframe), iLow(NULL,timeframe,0));
Move the ObjectCreate() up above the ObjectSet.
The Object name is incorrect in this line . . .
ObjectSet( "TF1HIGH_label", OBJPROP_TIME1, (Time[0]+bars_forward*60*timeframe));
Where you are calculating the offset time positions such as this . . .
Time[0] + bars_forward * 60 * timeframe
don't use timeframe, use Period() instead . . .
Time[0] + bars_forward * 60 * Period()
This will keep the Trend Lines in the same position (X axis) regardless of the timeframe you have your chart set to.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello Forum
I am in the process of building an indicator that I can add to the short time frame chart that I normally trade, and show the high and low of the current bars for higher time frames (and hence likely support and resistance levels)
But to keep clarity I am wondering how to show these highs and lows as short horizontal lines that "ONLY"appear on the chart to the right of current price action.
The attached code for simplicity, just plots 1 high level for the time period 30 minutes, which obviously I would duplicate for other time frames and lows etc
But I do not know how to change from the current plot of a horizontal line across the whole chart to a small line at the same level in the blank section of
a chart to the right of price action.
Would someone mind guiding me on how to get the high / low lines in my preferred form. (only visible on chart right edge)
I think this approach would make the chart easier to read
thanks in advance
dave