Hello,
I have an indicator in a separate window and I apply the default MT4 Bollinger Band indicator in this Window i.e Applied to Previous Indicators Data. It works well for me seeing when the line goes outside the Bollinger Bands. I can see the two sets of data in DataWindow, but I would like to access these data in an EA. I am perplexed about how I actually do this. The complication being the BB are applied to this indicators values and not price in the chart proper.
I have obtained the MetaQuotes Bollinger Bands but can't see how to apply to Previous Indicators Data. I tried substituting the iMA with the alternative indicator data (but not averaged as I would like)
Can anyone point me in the right direction how to solve this? I really only want to have the Bollinger Bands mid-line as the moving average N periods of the other indicator and then have the envelopes x StdDev above and below.
You have to create new Bollinger Bands, collect the indicator data using iCustom(), then use iMAOnArray() for bands middle line and iBandsOnArray() for bands upper/lower line.
You have to create new Bollinger Bands, collect the indicator data using iCustom(), then use iMAOnArray() for bands middle line and iBandsOnArray() for bands upper/lower line.
Trying one step at a time (getting the midline right first), I replaced
// MovingBuffer[i]=iMA(NULL,0,BandsPeriod,BandsShift,MODE_SMA,PRICE_CLOSE,i); commented out original MovingBuffer[i]=iCustom(NULL,0,"DetrendIndicator", 10, true, 0,i); with double DI = iCustom(NULL,0,"DetrendIndicator", 10, true, 0,i); // Collect the indicator data MovingBuffer[i]=iMAOnArray(DI,0,BandsPeriod, BandsShift, MODE_SMA, i); //(I also tried ... MovingBuffer[i]=iMAOnArray(DI,0,BandsPeriod, BandsShift, MODE_SMA, 0,i);)
but all my attempts are not working out. I read the three links you gave and one referenced called ArraySetAsSeries but I'm not successful.
Thanks for taking the time to reply phi.nuts.
Trying one step at a time (getting the midline right first), I replaced
but all my attempts are not working out. I read the three links you gave and one referenced called ArraySetAsSeries but I'm not successful.
I said create a new one - from a scratch.
double data[], middle[], upper[], lower[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators IndicatorBuffers(4); SetIndexBuffer (0, data); SetIndexBuffer (1, middle); SetIndexBuffer (2, upper); SetIndexBuffer (3, lower); SetIndexStyle (1, DRAW_LINE); SetIndexStyle (2, DRAW_LINE); SetIndexStyle (3, DRAW_LINE); //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { return(0);} //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { //---- int pos, indicator_counted; // collecting data for (pos = Bars - indicator_counted - 1; pos >= 0; pos --) { Data [pos] = iCustom(NULL, 0, "DetrendIndicator", 10, true, 0, pos); } // creating middle band for (pos = Bars - indicator_counted - 1; pos >= 0; pos --) { Middle [pos] = iMAOnArray (data, Bars, ... , pos); } // creating upper/lower band for (pos = Bars - indicator_counted - 1; pos >= 0; pos --) { Upper [pos] = iBandsOnArray (Data, Bars, ... , pos); Lower [pos] = iBandsOnArray (Data, Bars, ... , pos); } indicator_counted = IndicatorCounted(); //---- return(0); } //+------------------------------------------------------------------+
I did not test all of the code let alone compile it - so do experimenting and hopes other forumer also helps you.
Experimenting like create Band with the code above except the data is the close price (Close[pos]) then compare the result with the standard Band from MT.
I said create a new one - from a scratch.
I did not test all of the code let alone compile it - so do experimenting and hopes other forumer also helps you.
Experimenting like create Band with the code above except the data is the close price (Close[pos]) then compare the result with the standard Band from MT.
Many thanks for taking time to do this. I really appreciate. I'll try it out now. Very pleased you have responded.
Many thanks for taking time to do this. I really appreciate. I'll try it out now. Very pleased you have responded.
Changed the upper to lower case for "Data", "Upper", "Middle" and "Lower" to agree with defined doubles (in lower case) below the properties.
Read in docs.mql4 that "Bars" means all if 0 is used and I experimented to find what was needed to substitute the "..." with to get it to compile properly. I guess I got this wrong but it compiles without errors.
The result is the middle line is identical to the source indicator data (DetrendIndicator) i.e. Buffer 0 data rather than the intended Moving Average 10 (Buffer 1) I expected. The Upper and Lower Bands do not appear.
I hope the changes I made have not deviated from what I was expected to do and I ask if it is possible to guide me to achieving this indicator plotting the MA of the source indicator data and the upper and lower bands.
Applying Bollinger Bands to any of several indicators in a separate window can be very useful. If we can get this working properly, changing the source indicator name/properties would only required for this to be a very good indicator.
#property copyright "Copyright © 2005, MetaQuotes Software Corp." #property link "http://www.metaquotes.net/" #property indicator_separate_window #property indicator_color1 Lime #property indicator_color2 Tomato #property indicator_color3 Orange double data[], middle[], upper[], lower[]; // all lower case //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators IndicatorBuffers(4); SetIndexBuffer (0, data); SetIndexBuffer (1, middle); SetIndexBuffer (2, upper); SetIndexBuffer (3, lower); SetIndexStyle (1, DRAW_LINE); SetIndexStyle (2, DRAW_LINE); SetIndexStyle (3, DRAW_LINE); //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { return(0);} //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { //---- int pos, indicator_counted; // collecting data for (pos = Bars - indicator_counted - 1; pos >= 0; pos --) { data [pos] = iCustom(NULL, 0, "DetrendIndicator", 10, true, 0, pos); // changed Data to data } // creating middle band for (pos = Bars - indicator_counted - 1; pos >= 0; pos --) { middle [pos] = iMAOnArray (data, 0,10,0,MODE_SMA, pos); // changed Middle to middle } // creating upper/lower band for (pos = Bars - indicator_counted - 1; pos >= 0; pos --) { upper [pos] = iBandsOnArray (data, 0, 10,2,0, MODE_UPPER , pos); //changed Upper to upper lower [pos] = iBandsOnArray (data, 0, 10,2,0, MODE_LOWER , pos); // changed Lower to lower } indicator_counted = IndicatorCounted(); //---- return(0); } //+------------------------------------------------------------------+
I have tried many combinations to get this working (and I am learning new things).
Changed the upper to lower case for "Data", "Upper", "Middle" and "Lower" to agree with defined doubles (in lower case) below the properties.
Read in docs.mql4 that "Bars" means all if 0 is used and I experimented to find what was needed to substitute the "..." with to get it to compile properly. I guess I got this wrong but it compiles without errors.
The result is the middle line is identical to the source indicator data (DetrendIndicator) i.e. Buffer 0 data rather than the intended Moving Average 10 (Buffer 1) I expected. The Upper and Lower Bands do not appear.
I hope the changes I made have not deviated from what I was expected to do and I ask if it is possible to guide me to achieving this indicator plotting the MA of the source indicator data and the upper and lower bands.
Applying Bollinger Bands to any of several indicators in a separate window can be very useful. If we can get this working properly, changing the source indicator name/properties would only required for this to be a very good indicator.
Aha, good one NewtonLeo,
The other lines are not showing because we (actually I) forget to define number of buffer property. And lets make the data also show it self, so you don't have to attach your DeTrend Indicator and then attach the BB for DeTrend Indicator.
Again I haven't test this yet but yours is compiled and that's good.
Don't forget to manually compare this with your DeTrend Indicator and BB Previous Indicator Data especially on live tick.
#property copyright "Copyright © 2005, MetaQuotes Software Corp." #property link "http://www.metaquotes.net/" #property indicator_separate_window #property indicator_buffers 4 // this is will make all lines showing up #property indicator_color1 Yellow // added line #property indicator_color2 Lime #property indicator_color3 Tomato #property indicator_color4 Orange // extern input // some inputs extern string CI_Name = "DetrendIndicator"; extern int DeTrend_Period = 10; extern int DeTrend_True = true; // ..................................... and add some input for MA and BB here double data[], middle[], upper[], lower[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators //IndicatorBuffers(4); // let's not use it anymore because .... SetIndexBuffer (0, data); // ... we're going to show ... SetIndexBuffer (1, middle); SetIndexBuffer (2, upper); SetIndexBuffer (3, lower); SetIndexStyle (0, DRAW_LINE); // ... DeTrend CI without attaching one. :) SetIndexStyle (1, DRAW_LINE); SetIndexStyle (2, DRAW_LINE); SetIndexStyle (3, DRAW_LINE); SetIndexLabel (0, "Data"); // All of this ... SetIndexLabel (1, "Middle"); // ... will make ... SetIndexLabel (2, "Upper"); // ... the data of all lines ... SetIndexLabel (3, "Lower"); // ... show in MT4 Data Window (Ctrl + D) string name = WindowExpertName(); IndicatorShortName (name+" "+DeTrend_Period+" "+DeTrend_True); // some displayed name //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { return(0);} //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { //---- int pos, indicator_counted; // collecting data for (pos = Bars - indicator_counted; pos >= 0; pos --) { data [pos] = iCustom(NULL, 0, CI_Name, DeTrend_Period, DeTrend_True, 0, pos); // Put the input in iCustom input parameter } // creating middle band for (pos = Bars - indicator_counted; pos >= 0; pos --) { middle [pos] = iMAOnArray (data, 0,10,0,MODE_SMA, pos); } // creating upper/lower band for (pos = Bars - indicator_counted; pos >= 0; pos --) { upper [pos] = iBandsOnArray (data, 0, 10,2,0, MODE_UPPER , pos); lower [pos] = iBandsOnArray (data, 0, 10,2,0, MODE_LOWER , pos); } indicator_counted = IndicatorCounted() - 1; // change it a little does not really matter //---- return(0); } //+------------------------------------------------------------------+
Aha, good one NewtonLeo,
The other lines are not showing because we (actually I) forget to define number of buffer property. And lets make the data also show it self, so you don't have to attach your DeTrend Indicator and then attach the BB for DeTrend Indicator.
Again I haven't test this yet but yours is compiled and that's good.
Don't forget to manually compare this with your DeTrend Indicator and BB Previous Indicator Data especially on live tick.
Thanks very much phi.nuts. This is encouraging for me that I wasn't so poor at implementing your code. I will get right onto this advanced version and feed back. Many thanks for your patience.
extern int DeTrend_True = true; Should this be extern bool DeTrend_True = true; ??Update: On first run, it faithfully created the DeTrend perfectly. But none of the BollingerBands were present. I changed the extern int to extern bool in case that was the problem but it did not bring the bans to show.
... and then I spotted an error of mine and IT WORKS!!!!! Thankyou so much.
Thanks very much phi.nuts. This is encouraging for me that I wasn't so poor at implementing your code. I will get right onto this advanced version and feed back. Many thanks for your patience.
Update: On first run, it faithfully created the DeTrend perfectly. But none of the BollingerBands were present. I changed the extern int to extern bool in case that was the problem but it did not bring the bans to show.
... and then I spotted an error of mine and IT WORKS!!!!! Thankyou so much.
Congrats,
Yes it should be bool - according to your DeTrend Indicator, not int. I copy pasted from DeTrend_Period :(.
Congrats,
Yes it should be bool - according to your DeTrend Indicator, not int. I copy pasted from DeTrend_Period :(.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I have an indicator in a separate window and I apply the default MT4 Bollinger Band indicator in this Window i.e Applied to Previous Indicators Data. It works well for me seeing when the line goes outside the Bollinger Bands. I can see the two sets of data in DataWindow, but I would like to access these data in an EA. I am perplexed about how I actually do this. The complication being the BB are applied to this indicators values and not price in the chart proper.
I have obtained the MetaQuotes Bollinger Bands but can't see how to apply to Previous Indicators Data. I tried substituting the iMA with the alternative indicator data (but not averaged as I would like)
Can anyone point me in the right direction how to solve this? I really only want to have the Bollinger Bands mid-line as the moving average N periods of the other indicator and then have the envelopes x StdDev above and below.