Help with MA mean Reversion strategy - futures io
futures io



Help with MA mean Reversion strategy


Discussion in EasyLanguage Programming

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




Welcome to futures io: the largest futures trading community on the planet, with well over 125,000 members
  • Genuine reviews from real traders, not fake reviews from stealth vendors
  • Quality education from leading professional traders
  • We are a friendly, helpful, and positive community
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts
  • We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

(If you already have an account, login at the top of the page)

 
Search this Thread
 

Help with MA mean Reversion strategy

(login for full post details)
  #1 (permalink)
Rosenberg12
Malmö,skåne,sweden
 
 
Posts: 4 since Jun 2017
Thanks: 0 given, 0 received

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 futures io?
MacdBB V402 NT804 + gradient + pullback not working on N …
NinjaTrader
Is there inherently more opportunity in smaller markets?
Traders Hideout
Eurex Denali vs dxFeed
Sierra Chart
NinjaTrader Market Replay downloads now in 100Mb chunks
Feedback and Announcements
Script to add a moving average
ThinkOrSwim
 
Best Threads (Most Thanked)
in the last 7 days on futures io
Question about intraday margins
61 thanks
How quickly do algos go bad?
56 thanks
Spoo-nalysis ES e-mini futures S&P 500
19 thanks
ZombiePack3 Indicators
19 thanks
Webinar: Building a System for Trading Psychology w/Jare …
17 thanks
 
(login for full post details)
  #3 (permalink)
 ABCTG   is a Vendor
 
 
Posts: 2,262 since Apr 2013
Thanks: 417 given, 1,507 received


Rosenberg12,

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.

Regards,

ABCTG

Follow me on Twitter Reply With Quote
 
(login for full post details)
  #4 (permalink)
Rosenberg12
Malmö,skåne,sweden
 
 
Posts: 4 since Jun 2017
Thanks: 0 given, 0 received

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
 
(login for full post details)
  #5 (permalink)
 ABCTG   is a Vendor
 
 
Posts: 2,262 since Apr 2013
Thanks: 417 given, 1,507 received

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 Twitter Reply With Quote
 
(login for full post details)
  #6 (permalink)
Rosenberg12
Malmö,skåne,sweden
 
 
Posts: 4 since Jun 2017
Thanks: 0 given, 0 received

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
 
(login for full post details)
  #7 (permalink)
 ABCTG   is a Vendor
 
 
Posts: 2,262 since Apr 2013
Thanks: 417 given, 1,507 received

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 Twitter Reply With Quote
 
(login for full post details)
  #8 (permalink)
Rosenberg12
Malmö,skåne,sweden
 
 
Posts: 4 since Jun 2017
Thanks: 0 given, 0 received


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
 
(login for full post details)
  #9 (permalink)
 ABCTG   is a Vendor
 
 
Posts: 2,262 since Apr 2013
Thanks: 417 given, 1,507 received

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 Twitter Reply With Quote
The following user says Thank You to ABCTG for this post:


futures io Trading Community Platforms and Indicators EasyLanguage Programming > Help with MA mean Reversion strategy


Last Updated on February 13, 2019


Upcoming Webinars and Events
 

NinjaTrader Indicator Challenge!

Ongoing

A Transparent Glimpse Into a Prop Traders Life w/Chris Gray @ Earn2Trade

Elite only
 

Our 12-year anniversary w/ $$,$$$ prizes (check soon)

August
     



Copyright © 2021 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada), info@futures.io
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.
no new posts