I need help to program a part of a strategy in easy language. The part I need help with is how to calculate the % difference between 2 SMA. Below is copy from the strategy in text
Thanks
"when the 2 period moving average begins to move away from the 5 period moving average it’s analogous to a jaw being opened. Once the gap between the two moving averages exceeds 3% an entry condition occurs, "
Can you help answer these questions from other members on futures io?
welcome to futures.io. Your code has access to the average values, so you should be able to compute the percentage the faster SMA is above/below the slower SMA using math. You might want to post the code that you have, so others can point you in the right direction.
Then you can monitor the percentage and either allow an entry when the difference is > X % or only on the bars where the difference crosses over X.
computing the percentage difference between two averages is the same as for example computing the percentage difference between two stock prices or any two values for that matter.
You could compute it as:
( Fast Average - Slow Average ) / Slow Average
To make your programming life easier I would suggest using meaningful variable names, as this will make your code much easier to read.
a good way to check if the code is doing what you have in mind is to use the "Print" reserved word to check the internal values within your code. This can be very helpful in understanding what the code does.
Alternatively you could convert part of your logic into an indicator and plot the percentage that you computed on the same chart as the strategy is applied to.
you could still run into a division by zero error in the unlikely case that var1 = -0.00000001. A good coding practice within Easylanguage is to check the denominator before every division to make sure that it's not zero. In case it is, you can use an alternative value then (or do nothing and have the variable keep its current value).
Regards,
ABCTG
The following user says Thank You to ABCTG for this post: