Maz

Momentum-Range Differential Acceleration Momentum-Range Coefficient Acceleration

Category: Strategies By: Maz Created: April 18, 2017, 1:44 PM
April 18, 2017, 1:44 PM
Strategies
22 Comments
Momentum-Range Differential Acceleration Momentum-Range Coefficient Acceleration

Introduction

This is a pair of trading systems that both work on the same premise. The only difference between them is that once system uses a differential (system#1) whilst the other system uses a coefficient (system#2) to measure rapid changes in price action. Although the example submitted here is optimised for EUR_USD M15 (Long only), the system is market agnostic and can be adapted for a selection of markets / asset classes. For quickness of illustration purposes I have included only the long version here which should be enough to demonstrate the premise; Due to the differences between bull and bear price action any short adaptationist should be forked with separate optimizations for the short side.

Trading System premise

There exists a good probability of continuation of price action when the following pattern is found:
1. Short term average range / momentum is accelerating by a certain amount faster than long term average range / momentum (AKA increasing differential)
2. Short term moving average (AKA trend) is moving in the same direction as price action

Indicator

For more information about the underlining entry system indicator see my indicator here:
https://www.prorealcode.com/prorealtime-indicators/range-coefficient/
This can be used to predict an increase in volatility.

Variables

Thorough and regular optimization is required. Current optimization here is for EUR_USD M15

wintarget = 43       // Clip target to maximum of x points
minTradeTime = 43    // stay in trade for at least x bars
mcShortPeriod = 5    // Short period momentum
rcShortPeriod = 4    // Short period average range
mcLongPeriod  = 100  // Long period momentum
rcLongPeriod  = 100  // Long period average range
mcThreshold = 0.1    // Momentum coef or dif threshold
rcThreshold = 0.9    // Range coef or dif threshold
maShortPeriod = 18   // Short term moving average
maLongPeriod  = 53   // Long term moving average

Note

This is not a complete “black box” trading system. There is no risk management or exception handling code. This back test is purely to demonstrate risk:reward and hit rate of the above premise. Open to all suggestions. For any questions please discuss in a forum topic.

System 1/ (differential system)

// ====================================== \\
// :: MDif-RDif
//   Long Name : Momentum Differential and Range Differential Accelleration
//   Version   : v1.11
//   Author    : Maz @ prorealcode.com
//   Date      : 07-04-2017
//   Contact   : prtmaz at gmail dot com
// -------------------------------------- //


// ====================================== \\
// :: Optimizations --
// -------------------------------------- //
once optimization = 1 // 1 = EUR/USD M15
// Set to 0 for re-optimization and walk-forward

if optimization = 1 then
 // Static Optimization for EUR/USD M15
 // May 2015 - April 2017
 wintarget = 43        // Clip target to maximum of x points
 minTradeTime = 43     // stay in trade for at least x bars
 // try also 27
 mcShortPeriod = 5     // Short period momentum
 rcShortPeriod = 4     // Short period average range
 mcLongPeriod  = 100   // Long period momentum
 rcLongPeriod  = 100   // Long period average range

 mcThreshold = 2       // Momentum coefficient threshold
 rcThreshold = 8.5     // Range coefficient threshold

 maShortPeriod = 18    // Short term moving average
 maLongPeriod  = 53    // Long term moving average
 // ( try also 150 / or 53 for 70% hit rate / or 51

elsif optimization = 2 then
 // Insert your optimizations here
 // wintarget = 43        // Clip target to maximum of x points
 // minTradeTime = 43     // stay in trade for at least x bars
 // ...

endif

// ====================================== \\
// :: Indicators --
// -------------------------------------- //

// Momentum Differential ============= \\
mShort = momentum[mcShortPeriod]
mLong  = momentum[mcLongPeriod]
mc = max(abs(mShort) - abs(mLong), 0)
// ----------------------------------- //

// Range Differential ================= \\
r = abs(range)
arLong  = average[max(1, rcLongPeriod)](r)
arShort = average[max(1, rcShortPeriod)](r)
rc = max(0, arShort - arLong)
// ----------------------------------- //

// General Indicators ================= \\
upBar = close > open
maShort = exponentialAverage[maShortPeriod](Close)
maLong  = exponentialAverage[maLongPeriod](Close)
// ----------------------------------- //



// ====================================== \\
// :: Entry Logic
// -------------------------------------- //

// long entry rules (buy condition)
bc1 = not longOnMarket
bc1 = bc1 and (mc >= mcThreshold)
bc1 = bc1 and (rc >= rcThreshold)
bc1 = bc1 and upBar
bc1 = bc1 and maShort > maShort[1]

// long exit rules (exit long conditions)
le1 = longOnMarket
le1 = le1 and ( barIndex >= barIndexAtBuy + minTradeTime )
le1 = le1 and (maLong < maLong[4])


// short entry rule
// -- na --

// short exit rules
// -- na --


// ====================================== \\
// :: Execution Handlers
// -------------------------------------- //
if bc1 then
 buy 1 shares at market
 set target pprofit wintarget
 barIndexAtBuy = barIndex
endif

if le1 and longOnMarket then
 sell at market
endif

System 2/ (coefficient system)

// ====================================== \\
// :: M-R Dif.coef.long
//   Long Name : Momentum and Range Coefficient Accelleration Long only
//   Version   : v1.11
//   Author    : Maz @ prorealcode.com
//   Date      : 07-04-2017
//   Contact   : prtmaz at gmail dot com
// -------------------------------------- //


// ====================================== \\
// :: Optimizations --
// -------------------------------------- //
once optimization = 1 // 1 = EUR/USD M15
// Set to 0 for re-optimization and walk-forward

if optimization = 1 then
 // Static Optimization for EUR/USD M15
 // May 2015 - April 2017
 wintarget = 43        // Clip target to maximum of x points
 minTradeTime = 43     // stay in trade for at least x bars
 // try also 27
 mcShortPeriod = 5     // Short period momentum
 rcShortPeriod = 4     // Short period average range
 mcLongPeriod  = 100   // Long period momentum
 rcLongPeriod  = 100   // Long period average range

 //mcThreshold = 0.1 // 0.5   // Momentum coefficient threshold
 //rcThreshold = 0.9 // 1     // Range coefficient threshold

 maShortPeriod = 18    // Short term moving average
 maLongPeriod  = 53    // Long term moving average
 // ( try also 150 / or 53 for 70% hit rate / or 51

elsif optimization = 2 then
 // Insert your optimizations here
 // wintarget = 43        // Clip target to maximum of x points
 // minTradeTime = 43     // stay in trade for at least x bars
 // ...

endif

// ====================================== \\
// :: Indicators --
// -------------------------------------- //

// Momentum Coefficient ============= \\
mShort = momentum[mcShortPeriod]
mLong  = momentum[mcLongPeriod]
mc = max(0, (abs(mShort) / abs(mLong)) -1)
// ----------------------------------- //

// Range Coefficient ================= \\
r = abs(range)
arLong  = average[max(1, rcLongPeriod)](r)
arShort = average[max(1, rcShortPeriod)](r)
rc = max(0, (arShort / arLong) -1)
//rc = max(0, arShort - arLong)
// ----------------------------------- //

// General Indicators ================= \\
upBar = close > open
maShort = exponentialAverage[maShortPeriod](Close)
maLong  = exponentialAverage[maLongPeriod](Close)
// ----------------------------------- //



// ====================================== \\
// :: Entry Logic
// -------------------------------------- //

// long entry rules (buy condition)
bc1 = not longOnMarket
bc1 = bc1 and (mc >= mcThreshold)
bc1 = bc1 and (rc >= rcThreshold)
bc1 = bc1 and upBar
bc1 = bc1 and maShort > maShort[1]

// long exit rules (exit long conditions)
le1 = longOnMarket
le1 = le1 and ( barIndex >= barIndexAtBuy + minTradeTime )
le1 = le1 and (maLong < maLong[4])


// short entry rule
// -- na --

// short exit rules
// -- na --


// ====================================== \\
// :: Execution Handlers
// -------------------------------------- //
if bc1 then
 buy 1 shares at market
 set target pprofit wintarget
 barIndexAtBuy = barIndex
endif

if le1 and longOnMarket then
 sell at market
endif

 

Download
Filename: M-R-Coef-EURUSD-M15-Long.itf
Downloads: 799
Download
Filename: M-R-Dif-EURUSD-M15-Long.itf
Downloads: 671
Maz
Maz Veteran
Quant fund manager; Founder of institutional HFT engine. Background in market making, arbitrage, machine learning, indicator & algo design. Affection for PRC community - good discussions here. Welcome anyone to reach out, see posts :-)
Author’s Profile

Comments

ilvillans
6 years ago
#

HI, I modified the system, from good results but from this error: Replace the variables with a specific value: to prepare this system for automatic trading, remove all variables from the "ProBacktesta my system" section of the programming window and replace these variables with a specific numeric value in the trading system code Someone more experienced could rewrite it without giving errors Thanks Alessandro // ====================================== \\ // :: M-R Dif.coef.long // Long Name : Momentum and Range Coefficient Accelleration Long only // Version : v1.11 // Author : Maz @ prorealcode.com // Date : 07-04-2017 // Contact : prtmaz at gmail dot com // -------------------------------------- // // ====================================== \\ // :: Optimizations -- // -------------------------------------- // once optimization = 43 // 1 = EUR/USD M15 // Set to 0 for re-optimization and walk-forward if optimization = 43 then // Static Optimization for EUR/USD M15 // May 2015 - April 2017 wintarget = 43 // Clip target to maximum of x points minTradeTime = 43 // stay in trade for at least x bars // try also 27 mcShortPeriod = 5 // Short period momentum rcShortPeriod = 16 // Short period average range mcLongPeriod = 24 // Long period momentum rcLongPeriod = 85 // Long period average range //mcThreshold = 0.1 // 0.5 // Momentum coefficient threshold //rcThreshold = 0.9 // 1 // Range coefficient threshold maShortPeriod = 4 // Short term moving average maLongPeriod = 17 // Long term moving average // ( try also 150 / or 53 for 70% hit rate / or 51 elsif optimization = 43 then // Insert your optimizations here // wintarget = 43 // Clip target to maximum of x points // minTradeTime = 43 // stay in trade for at least x bars // ... endif // ====================================== \\ // :: Indicators -- // -------------------------------------- // // Momentum Coefficient ============= \\ mShort = momentum[mcShortPeriod] mLong = momentum[mcLongPeriod] mc = max(0, (abs(mShort) / abs(mLong)) -1) // ----------------------------------- // // Range Coefficient ================= \\ r = abs(range) arLong = average[max(1, rcLongPeriod)](r) arShort = average[max(1, rcShortPeriod)](r) rc = max(0, (arShort / arLong) -1) //rc = max(0, arShort - arLong) // ----------------------------------- // // General Indicators ================= \\ upBar = close > open maShort = exponentialAverage[maShortPeriod](Close) maLong = exponentialAverage[maLongPeriod](Close) // ----------------------------------- // // ====================================== \\ // :: Entry Logic // -------------------------------------- // // long entry rules (buy condition) bc1 = not longOnMarket bc1 = bc1 and (mc >= mcThreshold) bc1 = bc1 and (rc >= rcThreshold) bc1 = bc1 and upBar bc1 = bc1 and maShort > maShort[1] // long exit rules (exit long conditions) le1 = longOnMarket le1 = le1 and ( barIndex >= barIndexAtBuy + minTradeTime ) le1 = le1 and (maLong < maLong[4]) // short entry rule // -- na -- // short exit rules // -- na -- // ====================================== \\ // :: Execution Handlers // -------------------------------------- // if bc1 then buy 2 shares at market set target pprofit wintarget barIndexAtBuy = barIndex endif if le1 and longOnMarket then sell at market endif

IGOSNELL
9 years ago
#

Hi

Interesting strategy, when I used $ M-R Dif EURUSD M15 Long if does not enter any positions, any clue why?

The coef one does work thought on the EURUSD

 

Regards

Ian

Wilko
9 years ago
#

And not only readable.... makes it much easier to iterate different entry/exit-conditions, deleting and adding conditions during development of algo.

Wilko
9 years ago
#

 Nice method of making linked AND-statements more readable!

Pierluigi Cavagnoli
9 years ago
#

It works well even short on EUR/USD 15 min. I did this way: maLong / maShort instead of maShort / maLong  see the code.

Could it be a case of over-optimization?

What do you think about this ?

 

mShort = momentum[7]
mLong  = momentum[100]
mc = max(0, (abs(mLong) / abs(mShort)) -1)
// ----------------------------------- //

// Range Coefficient ================= \\
r = abs(range)
arLong  = average[max(1, 100)](r)
arShort = average[max(1, 4)](r)
rc = max(0, (arLong / arShort) -1)

 

Maz
Maz
9 years ago
#

Thanks guys. Please see forum post for more details. I encourage you guys to play / pull it apart / build on top / suggest improvements. 

victormork
9 years ago
#

@GraHal shall we move this to the forum thread advised above?

GraHal
9 years ago
#

 

Your code is so 'disciplined and well laid out' ... a pleasure to work with!

Is below correct please Maz?  

// Momentum Coefficient ============= \\
if barIndex >= max(mcLongPeriod, rcLongPeriod) then
mShort = momentum[mcShortPeriod]
mLong  = momentum[mcLongPeriod]
mc = max(0, (abs(mShort) / abs(mLong)) -1)
endif
// ----------------------------------- //

// Range Coefficient ================= \\
if barIndex >= max(mcLongPeriod, rcLongPeriod) then
r = abs(range)
arLong  = average[max(1, rcLongPeriod)](r)
arShort = average[max(1, rcShortPeriod)](r)
rc = max(0, (arShort / arLong) -1)
//rc = max(0, arShort - arLong)
endif
// ----------------------------------- //

// General Indicators ================= \\
upBar = close > open
maShort = exponentialAverage[maShortPeriod](Close)
maLong  = exponentialAverage[maLongPeriod](Close)
// ----------------------------------- //

Thank You
GraHal
 

GraHal
9 years ago
#

Yes I got that same error message on Live earlier today Victor, I was kicking myself for not doing a screen shot of the error message, but seems we have a fix already, thank you Maz.

GraHal  

victormork
9 years ago
#

When I tried this system on demo it was stopped due to division of zero. Anyone who got the same result or ideas on how to prevent this?

Maz
Maz
9 years ago
#

Just wrap the indicator code in a 

if barIndex >= max(mcLongPeriod, rcLongPeriod) then

...

endif

victormork
9 years ago
#

The first one works on DAX, but on currency pairs I get no trades :( and yes I'm using EUR/USD 15min

GraHal
9 years ago
#

Wow just Wow! Thank you Maz.

Both work 'straight out the box' for me Victor, you got it on EUR/USD on 15M TF and not touched any variable settings?

GraHal 

victormork
9 years ago
#

I'm not getting any trades from system one. Any suggestions of what I might do wrong?

Oskar Bergvall
9 years ago
#

I get good results on USD/SEK also. Will try it if I can.

One problem though, on my demoaccount on IG I get when trying to autotrade: "Replace variables with specific values: To prepare this strategy for automatic tradin, remove all variables from the "ProBacktest" section of the programming window and replace these variables with specific numeric values in the code of the trading system."

Oskar Bergvall
9 years ago
#

Which one is it? I tried to comment it out but same error. 

Maz
Maz
9 years ago
#

Just remove the variable optimizer

Maz
Maz
9 years ago
#

Yep it requires you to move optimized variables into hard coded constants. Remove the variable optimizer and put them in the code instead

Oskar Bergvall
9 years ago
#

Maz: Yes but I don't understand how. The values seems fixed already. Sorry I'm new in programming. 

Francesco78
9 years ago
#

Thank you. 

Any chance to do 200,000 bars backtest with the same variables?

Nicolas
9 years ago
#

I'd like to fork this one! anyone else? :)

You should open now a new thread Maz!

Maz
Maz
9 years ago
#

https://www.prorealcode.com/topic/momentum-range-differential-acceleration-system/

ProRealCode ProRealCode
Loading...