example
make an indicator display difference of EURUSD and GBPUSD
put moving average on indicators data and you get the moving average of the difference
same as iMAonArray
Thanks, but could you please tell what is wrong with the code above(if you know)?
Also, can you share this indicator?
Regards
I'm not that good at working with arrays, so I am not sure whether the fact that you do not size the MainClose arrays will cause problems or not.
Instead of all the calculating in the function, why not just use an additional buffer array and work with whatever time-frame is entered in the external parameters?
int start() { int counted_bars=IndicatorCounted(); int i; // Bar index int Counted_bars; // Number of counted bars Counted_bars=IndicatorCounted(); // Number of counted bars i=Bars-Counted_bars-1; // Index of the first uncounted while(i>=0) // Loop for uncounted bars { Buf_0[i]= iClose(MainPair,TimeFrame,i) - iClose(SecondPair,TimeFrame,i); Buf_1[i]= iMAOnArray(Buf_0,0,50,0,MODE_SMA,i); i--; } return(0); }
do it like this
int start() { int i, Counted_bars=IndicatorCounted(); //- ---- loop to accumulate values ---- i=Bars-Counted_bars-1; while(i>=0) { Buf_0[i] = iClose(MainPair,TimeFrame,i) - iClose(SecondPair,TimeFrame,i); i--; } //- ---- Loop for iMAOnArray ---- i=Bars-Counted_bars-1; while(i>=0) { Buf_1[i]= iMAOnArray(Buf_0,0,50,0,MODE_SMA,i); i--; } return(0); }
Thank you SDC. It worked. G-d bless you.
I see that each Buffer required a different loop. I thought it could be done in one same loop, but that was the very problem.
Thank you again.
Buf_0[i]= iClose(MainPair,TimeFrame,i) - iClose(SecondPair,TimeFrame,i);
You are also mixing apples and oranges and grapes. "i" is the index for the current pair/current time frame, but you are using it with other pairs/other time frames. Won't work.
datetime when = Time[i]; int iMain = iBarShift(MainPair,TimeFrame,when), iScnd = iBarShift(SecondPair,TimeFrame,when); Buf_0[i]= iClose(MainPair,TimeFrame,iMain) - iClose(SecondPair,TimeFrame,iScnd);
Thanks WHRoeder.
Like I have it coded now is like SDC instructed me above:
extern int Bars2Compare = 50; extern string MainPair = "GBPUSD"; extern string SecondPair = "EURUSD"; extern int TimeFrame = 1; double Buf_0[],Buf_1[]; double Aux_0[],Aux_1[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { SetIndexBuffer(0,Buf_0); SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2); SetIndexLabel(0,"Difference of Closing"); SetIndexBuffer(1,Buf_1); SetIndexStyle (1,DRAW_LINE,STYLE_SOLID,1); SetIndexLabel(1,"Average of Difference"); IndicatorShortName("Average between " + MainPair + " and " + SecondPair + " of Timeframe " + TimeFrame + " Bars2Compare:" + Bars2Compare); return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int counted_bars=IndicatorCounted(); int i; int Counted_bars; Counted_bars=IndicatorCounted(); //- ----loop to accumulate values---- i=Bars-Counted_bars-1; while(i>=0) { Buf_0[i] = iClose(MainPair,TimeFrame,i) - iClose(SecondPair,TimeFrame,i); i--; } //- ----Loop for iMAOnArray---- i=Bars-Counted_bars-1; while(i>=0) { Buf_1[i]= iMAOnArray(Buf_0,0,Bars2Compare,0,MODE_SMA,i); i--; } return(0); }
And this is what I got:
It's being correctly calculated for the other time frames?
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Dear Forum,
I'm trying to code a custom indicator which draws a line which is the average distance between two pairs. Below is the code. I don't know why it's not working. I know the Averages function is working correctly:
I'd appreciate your input. Thanks