NexusFi: Find Your Edge


Home Menu

 





Help with MA mean Reversion strategy


Discussion in EasyLanguage Programming

Updated
      Top Posters
    1. looks_one Rosenberg12 with 4 posts (0 thanks)
    2. looks_two ABCTG with 4 posts (1 thanks)
    3. looks_3 Quick Summary with 1 posts (0 thanks)
    4. looks_4 Sharon9999 with 1 posts (0 thanks)
    1. trending_up 2,383 views
    2. thumb_up 1 thanks given
    3. group 4 followers
    1. forum 8 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
Rosenberg12
Malmö,skåne,sweden
 
Posts: 4 since Jun 2017
Thanks Given: 0
Thanks Received: 0

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, "


Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Warsh Confirmed 54-45 on PPI Day -- 97% Say He Holds in …
Prediction Markets & Event Contracts
NinjaTrader Parent Payward Acquires Bitnomial for $550M …
Platforms and Indicators
Saylors 41-Month HODL Breaks: Strategy Sells 32 BTC as $ …
Prediction Markets & Event Contracts
Iran Peace Expired NO: Ceasefire on Life Support, OPEC a …
Prediction Markets & Event Contracts
Irans Answer Due Today: Peace Surges to 33.5%, Invasion …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
21 thanks
2026 Jlab journal
10 thanks
Algo automated / semi-automated trading anyone?
6 thanks
Lady Vols Primer: Trading Volatility Journal
6 thanks
2026 Fire Horse
5 thanks
  #3 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,447 since Apr 2013
Thanks Given: 493
Thanks Received: 1,639


Rosenberg12,

welcome to NexusFi. 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.

Regards,

ABCTG


Follow me on X Reply With Quote
  #4 (permalink)
Rosenberg12
Malmö,skåne,sweden
 
Posts: 4 since Jun 2017
Thanks Given: 0
Thanks Received: 0

Bellow is what i have got so far. Tt is the part in condition1 where i have only made % that i am not sure how to solve

inputs: Price( Close ), SlowLength( 5 ), fastLength (2) ;
variables: var1( 0 ), var2( 0 ), var3 (0) ;

var1 = Average( close , SlowLength );
var2= Average( close , fastLength );
// var3 = (var1 - var2)/100 ;

condition1= var2 is 3 % or more from var1 :



If condition1 then buy this bar on close ;



IF barssinceentry = 3 then sell this bar on close;


Reply With Quote
  #5 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,447 since Apr 2013
Thanks Given: 493
Thanks Received: 1,639

Rosenberg12,

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.

Regards,

ABCTG


Follow me on X Reply With Quote
  #6 (permalink)
Rosenberg12
Malmö,skåne,sweden
 
Posts: 4 since Jun 2017
Thanks Given: 0
Thanks Received: 0

Hellow

Bellow is the code i came up with not sure if it is correct

inputs: Price( Close ), SlowLength( 5 ), fastLength (2) ;
variables: var2( 0 ), var1( 0 ), var3 (0) ;

var1 = Average( close , SlowLength );
var2= Average( close , fastLength );
var3= ((var1-var2)/var1)*100 ;


condition1= var3 >=3;


If condition1 then buy this bar on close ;

condition2= var2 > var1;

if condition2 then sell this bar on close ;


Reply With Quote
  #7 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,447 since Apr 2013
Thanks Given: 493
Thanks Received: 1,639

Hi Rosenberg12,

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.

Regards,

ABCTG


Rosenberg12 View Post
Hellow

Bellow is the code i came up with not sure if it is correct

inputs: Price( Close ), SlowLength( 5 ), fastLength (2) ;
variables: var2( 0 ), var1( 0 ), var3 (0) ;

var1 = Average( close , SlowLength );
var2= Average( close , fastLength );
var3= ((var1-var2)/var1)*100 ;


condition1= var3 >=3;


If condition1 then buy this bar on close ;

condition2= var2 > var1;

if condition2 then sell this bar on close ;


Follow me on X Reply With Quote
  #8 (permalink)
Rosenberg12
Malmö,skåne,sweden
 
Posts: 4 since Jun 2017
Thanks Given: 0
Thanks Received: 0


Sharon9999 View Post
To prevent division by 0, you might change

var3= ((var1-var2)/var1)*100 ;

to

var3= ((var1-var2)/(var1+0.00000001))*100 ;

Thansk Sharon I will try this


Reply With Quote
  #9 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,447 since Apr 2013
Thanks Given: 493
Thanks Received: 1,639

Rosenberg12,

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).

 
Code
if var1 <> 0 then
   var3= ((var1-var2)/(var1))*100 ;
else //use an alternative  calculation
   var3= ((var1-var2)/(var1+0.00000001))*100 ;
Regards,

ABCTG


Rosenberg12 View Post
Thansk Sharon I will try this


Follow me on X Reply With Quote
Thanked by:




Last Updated on February 13, 2019


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts