Moving average strategy reverse

Questions about MultiCharts and user contributed studies.
hendrix
Posts: 54
Joined: 27 Dec 2013
Has thanked: 21 times
Been thanked: 5 times

Moving average strategy reverse

Postby hendrix » 02 May 2014

Hi

I test the strategy below (you can look the attachement)

Code: Select all

[IntrabarOrderGeneration = true]

Input:
Price(close),
Length(20);


Variables:

intrabarpersist top(false),
intrabarpersist bottom(false),
intrabarpersist average_jtJMA (0),
intrabarpersist var0 (0),
intrabarpersist var1 (0);


//compute moyenne mobile
average_jtJMA = jtHMA(price, length);


//when the average changes direction : (moving average is going up)
if average_jtJMA[2] > average_jtJMA[1] and average_jtJMA[1] < average_jtJMA then
var0 = 1
else

//when the average changes direction : (moving average is going down)
if average_jtJMA[2] < average_jtJMA[1] and average_jtJMA[1] > average_jtJMA then
var0 = -1
else
var0 = 0 ;



//LONG
If ( var0=1 ) Then
Begin
bottom = true;
top = false;
End;


If bottom then buy ("LE") this bar at close ;
If ( Marketposition = 1 ) and top then sell ("LEX") 1 contract next bar market ;



//SHORT
If ( var0=-1 ) Then
Begin
bottom = false;
top = true;
End;

If top then sellshort ("SE") this bar at close ;
if ( Marketposition = -1 ) and bottom then buytocover ("SEX") 1 contract next bar market ;


I don't understand why some entries are executed whereas "var0" doesn't equal to 1 or -1..

Anybody would have an idea?

best regards
Attachments
screenshot.pdf
reverse strategy
(282.11 KiB) Downloaded 201 times

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Moving average strategy reverse

Postby JoshM » 02 May 2014

I don't understand why some entries are executed whereas "var0" doesn't equal to 1 or -1..

Anybody would have an idea?
Your strategy uses Intra-Bar Order Generation with a Bar Magnifier set to 1 tick. That means that your strategy is not only calculated on bar close, like your indicator, but also intra-bar, on every tick.

In other words, if "var0" equals 1 or -1 intra-bar, the order will be generated, even though on bar close "var0" on that same bar can have the value 0.

Consider using BarStatus so that you can control when the average is calculated and when the values of `var0` are assigned. For example:

Code: Select all

// Only calculate average on bar close
if (BarStatus(1) = 2) then begin

//compute moyenne mobile
average_jtJMA = jtHMA(price, length);


//when the average changes direction : (moving average is going up)
if average_jtJMA[2] > average_jtJMA[1] and average_jtJMA[1] < average_jtJMA then
var0 = 1
else

//when the average changes direction : (moving average is going down)
if average_jtJMA[2] < average_jtJMA[1] and average_jtJMA[1] > average_jtJMA then
var0 = -1
else
var0 = 0 ;

end;

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Moving average strategy reverse

Postby TJ » 03 May 2014

See this article:
How to make indicator and signal calculation results the same
https://www.multicharts.com/trading-sof ... s_the_same

hendrix
Posts: 54
Joined: 27 Dec 2013
Has thanked: 21 times
Been thanked: 5 times

Re: Moving average strategy reverse

Postby hendrix » 04 May 2014

Hi guys

Thanks for your advice, I had not thought about that.
the road is still long in order to lear all subtleties of the coding rules

see u


Return to “MultiCharts”