Hey mate.
I haven't tested the code myself,
But there are already several things that pop out.
I am not a native English speaker, so I'll do my best to explain myself as clear as I can.
1)
I am not even sure how this code compiles,
But on your _rate function you have a comma sign instead of a division sign.
So-
double ret = 0.5 * division((tw + bw + (cond ? 2 * body : 0)) , (tw + bw + body));
Should become-
double ret = 0.5 * division((tw + bw + (cond ? 2 * body : 0)) / (tw + bw + body));
2)
From the original code it seems that cumdelta is a sum,
But you calculate its value as if it was an average
So-
cumdelta[i] = findSum(delta[i], ArraySize(delta))/ArraySize(delta);
Should become-
cumdelta[i] = findSum(delta[i], ArraySize(delta));
3)
Again, I am not sure how this code even compiles,
But your findSum function expects a pointer to an array as an input
int findSum(double &arr[], int size)
But you pass it an array element which is of type double
cumdelta[i] = findSum(delta[i], ArraySize(delta))/ArraySize(delta);
4)
My guess is that cumdelta[i+1] doesn't return infinite,
But it returns EMPTY_VALUE.
In your loop your are trying to use the value of cumdelta[i+1],
But its value had not been calculated yet.
Your loop first calculate the most recent candle ([0]),
And only then goes to calculate the candle before him ([1]),
And then the candle before him ([2]),
etc'
So you are trying to retrieve a value that is not yet calculated.
If you want to use the value of the previous candle calculation,
You should reverse the 'direction' of your loop to start calculating from the oldest candle, and then advancing from there, candle by candle, until it reaches the most recent one.
These are just several things that popped out as I looked at your code.
Again- I haven't tested it, but the above are issues that needed to be fixed.
If something in my explanation isn't clear enough,
Please let me know and I'll try to rephrase it in a more clear way.
Cheers.
Another big issue I've just noticed-
5)
MathIsValidNumber return type is type bool,
So your findSum function doesn't actually adds the value of the input array,
But it adds a boolean value, which is typecast as double, to the sum,
So essentially you are adding either 1 or 0 to the calculation.
I think you can skip using MathIsValidNumber in your calculation,
But if your still want to use it,
Then-
int findSum(double &arr[], int size) { double sum = 0; for(int i=0; i<size; i++) { sum += MathIsValidNumber(arr[i]); } return sum; }
Should become-
int findSum(double &arr[], int size) { double sum = 0; for(int i=0; i<size; i++) { if(MathIsValidNumber(arr[i])) { sum +=arr[i]; } } return sum; }
Hey mate.
I haven't tested the code myself,
But there are already several things that pop out.
I am not a native English speaker, so I'll do my best to explain myself as clear as I can.
1)
I am not even sure how this code compiles,
But on your _rate function you have a comma sign instead of a division sign.
So-
Should become-
2)
From the original code it seems that cumdelta is a sum,
But you calculate its value as if it was an average
So-
Should become-
3)
Again, I am not sure how this code even compiles,
But your findSum function expects a pointer to an array as an input
But you pass it an array element which is of type double
4)
My guess is that cumdelta[i+1] doesn't return infinite,
But it returns EMPTY_VALUE.
In your loop your are trying to use the value of cumdelta[i+1],
But its value had not been calculated yet.
Your loop first calculate the most recent candle ([0]),
And only then goes to calculate the candle before him ([1]),
And then the candle before him ([2]),
etc'
So you are trying to retrieve a value that is not yet calculated.
If you want to use the value of the previous candle calculation,
You should reverse the 'direction' of your loop to start calculating from the oldest candle, and then advancing from there, candle by candle, until it reaches the most recent one.
These are just several things that popped out as I looked at your code.
Again- I haven't tested it, but the above are issues that needed to be fixed.
If something in my explanation isn't clear enough,
Please let me know and I'll try to rephrase it in a more clear way.
Cheers.
Thanks for reaching out.
1) I have a function called division. That's why I have a comma sign, it take input 1 and divide by input 2 and check if input 2 is zero do avoid divide by zero.
2) You are right, I am doing an average like that. But I need to get a Cumulative of sums and the first step I think is getting the average. I don't know what's the next step. I might be wrong.
3) I'm now using the array as a normal variable but it doesn't do a sum.
double Sum(double arr) { double sum = 0; sum +=arr; return sum; }
When using an array and looping through it all, it just doesn't seem right with all the big numbers it give.
4) I reversed the direction of my loop and now it work better. Thanks a lot.
5) Fixed! Now it looks closer to a functionning indicator. All that's left is to calculate the cumulatif of sums (number 3).
I found another problem, when I test the indicator, it doesn't render new candles. It only create candles in the past at start but not new one when I press play.
Thanks for reaching out.
1) I have a function called division. That's why I have a comma sign, it take input 1 and divide by input 2 and check if input 2 is zero do avoid divide by zero.
2) You are right, I am doing an average like that. But I need to get a Cumulative of sums and the first step I think is getting the average. I don't know what's the next step. I might be wrong.
3) I'm now using the array as a normal variable but it doesn't do a sum.
When using an array and looping through it all, it just doesn't seem right with all the big numbers it give.
4) I reversed the direction of my loop and now it work better. Thanks a lot.
5) Fixed! Now it looks closer to a functionning indicator. All that's left is to calculate the cumulatif of sums (number 3).
I found another problem, when I test the indicator, it doesn't render new candles. It only create candles in the past at start but not new one when I press play.
Oh, yeah,
My mistake.
I didn't notice that you have a function call in that line.
So you can ignore issue #1.
Regarding the rest-
I would leave findSum function the way it originally was,
And I would just pass the whole array, instead of a double value.
I see that you've edited your code and you've already done that.
Good job!
Just a little tip,
because you're summing the whole array each time,
Instead of making a loop that each tick will loop thru the whole array,
Just create another buffer, or a static variable,
And each calculation just add in the new value.
It will be much more 'resource friendly' and efficient.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
So I tried to code something from tradingview and make it happen in MQL4.
I updated this post to show new problem I have now.
The indicator doesn't render new candles when I press play. It only show past candles but not new one. I have no clue why.
Then I need to get the cumulatif of sums. The first step is to get the average sums and the next step I have no clue. But now I tried to get the average sums but it give ridiculously large number when I'm only adding between -500 and +500 for each candles. And wierdly enough, my graphic show a big number only decreasing over time when it should be going up and down.
Below is the Mql4 code and below it there is the tradingview code which I want to copy.