I think if your intent is to go through bar by bar to determine whether the magnitude of MA change implies an Up or a Down direction, your current approach (as coded) is good enough, no need to calculate angle/tanh. Angle/tanh may only be useful if you want to deal with a number of bars collectively (i.e. computing the angle formed by MA changes over N bars as a whole, for example).
However I do see some bugs in your code, hope you've figured them out already.
I think if your intent is to go through bar by bar to determine whether the magnitude of MA change implies an Up or a Down direction, your current approach (as coded) is good enough, no need to calculate angle/tanh. Angle/tanh may only be useful if you want to deal with a number of bars collectively (i.e. computing the angle formed by MA changes over N bars as a whole, for example).
However I do see some bugs in your code, hope you've figured them out already.
Hi Seng,
Thank you for your reply! Yeah, the ange part makes sense when you compare it with the number of bars. You said that my current approach as coded is good enough. How would you code it?
And yes, I noticed some bugs, lol. varAux==1 isn't going to take me anywhere haha.
If you have anything else to add I will greatly appreciate it!
Regards,
PS
Hi Seng,
Thank you for your reply! Yeah, the ange part makes sense when you compare it with the number of bars. You said that my current approach as coded is good enough. How would you code it?
And yes, I noticed some bugs, lol. varAux==1 isn't going to take me anywhere haha.
If you have anything else to add I will greatly appreciate it!
Regards,
PS
Right... when I said "good enough" I was referring just to the direction determination conditions in general, which does give you what you want. E.g.:
((ema30Buffer[i]-ema30Buffer[i+1])/ema30Buffer[i+1])*100>ema30Threshold && ema30Buffer[i]>ema30Buffer[i+1]
And when I mentioned "some bugs", there were more bugs than just varAux==1 alone.
Ok, let's look at them one by one:
(1) For loops - not necessary since u'r just trying to determine the current direction. So using 1 and 2 will do, rather than i and i+1. Also, the statements within each of the for loop will return their corresponding varAux as 0 (after fixing the == bug) since as u move to earlier candles, sooner or later you'll get to a point where the direction checks result in 0 and break from the loop.
(2) Notice that when the direction is down, "ema30Buffer[i]-ema30Buffer[i+1])/ema30Buffer[i+1])*100" will be negative, and comparing with the same threshold as when the direction is up won't do you much good. Should consider using MathAbs to remove the negative sign and always check if it's greater than threshold regardless of direction.
Hello!
I'm trying to make a system based on 3 SMA. I want to take a trade if all 3 SMA are pointing in the same direction (up or down), but I'm struggling on how to represent that in a code.
I thought that it might be a good idea using a % change and set a bias, so if all the values are above the bias, it will be a valid.
Another approach (wich I have not tested yet) is to calculate the angle with tanh function. I imagined the points of the SMA as the hypotenuse of small triangles. With that said, the lenght of one side would be the difference between sma on bar 1 and sma on bar 2
and the other I guess that it would be just 1 (because its from 1 bar to the other bar). Is this approach correct?
This is the part of the code that I'm actually using. Any idea or suggestion of how I can do it better will be appreciated!
PD: ema30Threshold is an input that represents the % of change, ie: 0.0001.
what about using regressional channels instead avgs?
Right... when I said "good enough" I was referring just to the direction determination conditions in general, which does give you what you want. E.g.:
And when I mentioned "some bugs", there were more bugs than just varAux==1 alone.
Ok, let's look at them one by one:
(1) For loops - not necessary since u'r just trying to determine the current direction. So using 1 and 2 will do, rather than i and i+1. Also, the statements within each of the for loop will return their corresponding varAux as 0 (after fixing the == bug) since as u move to earlier candles, sooner or later you'll get to a point where the direction checks result in 0 and break from the loop.
(2) Notice that when the direction is down, "ema30Buffer[i]-ema30Buffer[i+1])/ema30Buffer[i+1])*100" will be negative, and comparing with the same threshold as when the direction is up won't do you much good. Should consider using MathAbs to remove the negative sign and always check if it's greater than threshold regardless of direction.
Thank you for your answers!
1. For the loops I use the last 3 values of the SMA. I wanted to know if I should use more, that's why the loop, but it still doesn't fully convince me. I mean, I don't know if it is well written and I'm getting what I really want.
2. Yes, thank you for pointing that. I corrected it and added to the code :)
what about using regressional channels instead avgs?
Hi Marco!
And how should I use the channels in this logic?
- Seng Joo Thio: no need to calculate angle/tanh. Angle/tanh
-
There is no angle from 2 anchor
points. An angle requires distance divided by distance; a unitless
number. A chart has price/time. What is the angle of going 30 miles in 45
minutes? Meaningless!
-
You can get the slope from a trendline. Delta price / delta time. Changing the
axes scales changes the apparent angle, the slope is constant. Angle is
meaningless.
-
You can create a angled line using one point and setting the angle (Object
Properties - Objects Constants - Standard Constants, Enumerations and
Structures - MQL4 Reference.)
The line is drawn that angle in pixels. Changing the axes scales does NOT change
the angle (and thus is meaningless.)
-
If you insist, take the two price/time coordinates, convert them to pixel
coordinates and then to apparent angle with arctan.
How to get the angle of a trendline and place a text object to it? - Trend Indicators - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 2
-
There is no angle from 2 anchor
points. An angle requires distance divided by distance; a unitless
number. A chart has price/time. What is the angle of going 30 miles in 45
minutes? Meaningless!
- Pedro Severin:I want to take a trade if all 3 SMA are pointing in the same direction (up or down), but I'm struggling on how to represent that in a code.It's pointing up if the current value is higher than a previous one.
Hi Marco!
And how should I use the channels in this logic?
Open a chart and add 2 regressional channels having different length....after observing it will be clear to you ;)
-
There is no angle from 2 anchor
points. An angle requires distance divided by distance; a unitless
number. A chart has price/time. What is the angle of going 30 miles in 45
minutes? Meaningless!
-
You can get the slope from a trendline. Delta price / delta time. Changing the
axes scales changes the apparent angle, the slope is constant. Angle is
meaningless.
-
You can create a angled line using one point and setting the angle (Object
Properties - Objects Constants - Standard Constants, Enumerations and
Structures - MQL4 Reference.)
The line is drawn that angle in pixels. Changing the axes scales does NOT change
the angle (and thus is meaningless.)
-
If you insist, take the two price/time coordinates, convert them to pixel
coordinates and then to apparent angle with arctan.
How to get the angle of a trendline and place a text object to it? - Trend Indicators - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 2
-
There is no angle from 2 anchor
points. An angle requires distance divided by distance; a unitless
number. A chart has price/time. What is the angle of going 30 miles in 45
minutes? Meaningless!
- It's pointing up if the current value is higher than a previous one.
Hi William! Thank you for your reply :)
1. I see your point, and you might be right, but I think what Seng suggested was if I need to calculate an angle, the most correctly approach would be that one. And by the way (correct me if I'm wrong), if I am on a daily chart and I want to calculate the angle of the 2 past bars (not the actual one), I could do it taking the base of the triangle as the distance between bars (which should be 1, right?) and then the height would be the high of the yesterday's bar minus the low of the day before yesterday's bar. That height shouldn't be fixed no matter what timefrime I look? Shouldn't the only variable remaining be the lenght of the base of the triangle, depending on the timeframe I look?
2. By the way, when you said "30 miles in 45 minutes" thats speed and that gave me an idea. Is there a way to measure "the speed of pips"? Like its changing at X speeds per minute/hour/day or any kind of time? Isn't ROC measuring that?
3. Yeah, its pointing up if the current value is higher than previous one, but what I want is to determine "how up" the SMA needs to be in order to consider it a healthy trend.
4. At last, but not least, I'm trying to code a pullback, regarding if it is a pullback to a SMA or not. How can I do this? It seems harder than it looks (or I'm missing something). I tried different ways, but it doesn't seems to work well. How would you do it?
As always, thank you for your advices!
Hello!
I'm trying to make a system based on 3 SMA. I want to take a trade if all 3 SMA are pointing in the same direction (up or down), but I'm struggling on how to represent that in a code.
I thought that it might be a good idea using a % change and set a bias, so if all the values are above the bias, it will be a valid.
Another approach (wich I have not tested yet) is to calculate the angle with tanh function. I imagined the points of the SMA as the hypotenuse of small triangles. With that said, the lenght of one side would be the difference between sma on bar 1 and sma on bar 2
and the other I guess that it would be just 1 (because its from 1 bar to the other bar). Is this approach correct?
This is the part of the code that I'm actually using. Any idea or suggestion of how I can do it better will be appreciated!
PD: ema30Threshold is an input that represents the % of change, ie: 0.0001.
Hello Pedro
You can simply use the slope of your EMAs and check if the price is completely above or below the slowest EMA.
- 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!
I'm trying to make a system based on 3 SMA. I want to take a trade if all 3 SMA are pointing in the same direction (up or down), but I'm struggling on how to represent that in a code.
I thought that it might be a good idea using a % change and set a bias, so if all the values are above the bias, it will be a valid.
Another approach (wich I have not tested yet) is to calculate the angle with tanh function. I imagined the points of the SMA as the hypotenuse of small triangles. With that said, the lenght of one side would be the difference between sma on bar 1 and sma on bar 2
and the other I guess that it would be just 1 (because its from 1 bar to the other bar). Is this approach correct?
This is the part of the code that I'm actually using. Any idea or suggestion of how I can do it better will be appreciated!
PD: ema30Threshold is an input that represents the % of change, ie: 0.0001.