logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
Gregor  
#1 Posted : Tuesday, July 22, 2008 1:18:24 PM(UTC)
Gregor

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 6/12/2007(UTC)
Posts: 32

I have a system with two setups which I have written formula for call B1 and B2 which act as gates giving me aheads up on a possible trade. I want to know if I can combine these two setups into one formula creating a composite signal within the expert advisor?

The system is base on there moving averages called EMA (middle band) along with an upper band and lower band which are equal percentages from the EMA. These band can be adjusted to cover most of the price action in a chart. B1 is when the high of price bar is greater than the upper band. B2 is the low of the price bar is higher than the EMA and the high of the price bar is lower than the upper band. The coding for each is

Formula for B1

x:=7;
y:=1.07;
z:=0.93;
UB:= Mov(C,x,S)*y;
EMA:= Mov(C,x,E);
LB:= Mov(C,x,S)*z;
B1:= H>UB;

Formula for B2

x:=7;
y:=1.07;
z:=0.93;
UB:= Mov(C,x,S)*y;
EMA:= Mov(C,x,E);
LB:= Mov(C,x,S)*z;
B2:= L>EMA AND H<UB;

Gregor[:)]



wabbit  
#2 Posted : Tuesday, July 22, 2008 7:26:35 PM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
Hi Gregor,

The first thing that strikes me with this code is the use of the different moving average types for the middle line (exponential moving average) and the upper and lower limits on the envelope which are simple moving averages. Is this intentional? Some people might suggest that mixing the moving average types is not sound practice.

Another thing to notice is that neither function actually returns a value, as all the computations are still tied up the defined variables. Even if you don't intend plotting the indicator on a chart (as you are only using this code in an expert symbols or highlights etc) you should still return at least one value. (It's a long story as to all the reasons why, we might just have to accept this as good practice for now?)

If we consider this moving average envelope system is supposed to be employed using moving averages of the same type, we might be able to better employ the variables to make the code easier to read and maintain, like maybe:

Code:
{Formula for B1}
x:=7; {envelope width : percentage}
EMA:= Mov(C,x,E);
UB:= EMA*(1+x/100);
{LB:= EMA*(1-x/100); variable not used, so not required}
B1:= H>UB;
{plot}
B1;

{Formula for B2}
x:=7; {envelope width : percentage}
EMA:= Mov(C,x,E);
UB:= EMA*(1+x/100);
{LB:= EMA*(1-x/100); variable not used, so not required}
B2:= L>EMA AND H<UB;
{plot}
B2;


We now have two conditions; we don't care which condition is true just so long as at least one of the conditions is true we want to paint a bar or draw a symbol or whatever. (Actually it is impossible for B1 and B2 to be true at the same time as the HIGH cannot be simultaneously above and below the UB; not considered is the event where the H=UB? Is this intentional?)

In order to combine the events when we don't care which of the two events is the triggering factor, use the "OR" command to join the conditions:

Code:
{Combined signals}
x:=7; {envelope width : percentage}
EMA:= Mov(C,x,E);
UB:= EMA*(1+x/100);
{LB:= EMA*(1-x/100); variable not used, so not required}
B1:= H>UB;
B2:= L>EMA AND H<UB;

{plot}
B1 OR B2;


Hope this helps with the coding. I think you need to do some more work with the logic of B1 and B2. (Compare the combined logic results with the case: L>EMA)

wabbit [:D]

Gregor  
#3 Posted : Tuesday, August 5, 2008 2:32:57 PM(UTC)
Gregor

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 6/12/2007(UTC)
Posts: 32

Thank you wabbit for your responce.

I am very much a novice in understanding programing along with the logic involved and have some questions on the solution provided.

  1. In the orginal formula the "x" variable provided the period lenght for the moving average and the "y" and "z" variable adjusted the precentages of the upper and lower bands of the channel from the central moving average EMA. How are these pecentages adjusted in the combined signals formula?
  2. Could you expand on what is meant by "neither funcation actaully returning a value, as all the computations are still tied up the defined variables"?
  3. You have talk about the importance of returning at least one value within a formula, is this the "1" with in the UB and LB variables of the combine signals formula? And does this refer to the need for metastock to define a statement with a one as true and zero as false (such as you see in indicator biulder).
  4. Could you expand on what is meant by " need to do some work with the logic B1 and B2 ( compare the combine logic results with the case L>EMA)"?

Your Turly

Gregor

wabbit  
#4 Posted : Saturday, August 9, 2008 1:13:02 AM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
Gregor wrote:
In the orginal formula the "x" variable provided the period lenght for the moving average and the "y" and "z" variable adjusted the precentages of the upper and lower bands of the channel from the central moving average EMA. How are these pecentages adjusted in the combined signals formula?

Just just the value of the "x" variable. If you want a envelope that is 10 percent above and below the center line then change x to represent a value of 10, i.e. x:=10;

Gregor wrote:
Could you expand on what is meant by "neither funcation actaully returning a value, as all the computations are still tied up the defined variables"?

Take this as an example:
Code:
x:=10;
All you have done is tell MS to assign a value of 10 to the variable x. Compare the previous code to:
Code:
x:=10;
x;
which assigns the value to variable and then tells MS to plot the variable on the chart.

Gregor wrote:
You have talk about the importance of returning at least one value within a formula, is this the "1" with in the UB and LB variables of the combine signals formula? And does this refer to the need for metastock to define a statement with a one as true and zero as false (such as you see in indicator biulder).
This comment follows on from the previous comment where you should make sure that your function returns at least one value to the chart. Takefor example:
Code:
x:=10;
y:=15;
z:=20;
Like above, all this does is assign values to variables. The function plots nothing, and if you were to try to find the value of this function using a Fml() function call then it would return N/A because there is no assigned value. Compare this to:
Code:
x:=10;
y:=15;
z:=20;
{return this value}
y;
Although the code assigns values to the variables, and plots the value of the y variable on the chart, this is also the value of the function which would be returned if you used a Fml() function call. For interest, the value of the fucntion returned by a Fml() call is the last value of a function as listed, so:
Code:
x:=10;
y:=15;
z:=20;
{plot/return}
z;
y;
x;
would return the value of the x variable as it is the last in the code.

Gregor wrote:
Could you expand on what is meant by " need to do some work with the logic B1 and B2 ( compare the combine logic results with the case L>EMA)"?

Have a close look at what you are trying to detect:
Code:
B1:= H>UB;
B2:= L>EMA AND H<UB;
B1 OR B2;
If we extract the important parts and use some bracketting for clarity:
Code:
(H>UB) OR (L>EMA AND H<UB)
As the conditions for the HIGH price is either above or below the UB line (but not exactly equals), consider the possibilites for the code to return a TRUE value of 1:
H > UB L > EMA returns TRUE
H > UB L = EMA returns TRUE
H > UB L < EMA returns TRUE

H = UB L > EMA returns FALSE
H = UB L = EMA returns FALSE
H = UB L < EMA returns FALSE

H < UB L > EMA returns TRUE
H < UB L = EMA returns FALSE
H < UB L < EMA returns FALSE

Are these the results you really wanted?


wabbit [:D]

Users browsing this topic
Guest (Hidden)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.