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

Notification

Icon
Error

Options
Go to last post Go to first unread
ksrt  
#1 Posted : Wednesday, May 6, 2009 4:29:36 AM(UTC)
ksrt

Rank: Member

Groups: Registered, Registered Users, Subscribers
Joined: 3/16/2009(UTC)
Posts: 11
Location: Sydney

Hi I am trying to build an indicator as an expert advisor, giving +1 for long positions and -1 for short positions but when I plot even a simple version like below it plots positive 1 for both! If(Cross(RSI(14),30),1,0) OR If(Cross(70,RSI(14)),-1,0) Is there something I am doing wrong??
ksrt  
#2 Posted : Wednesday, May 6, 2009 4:50:23 AM(UTC)
ksrt

Rank: Member

Groups: Registered, Registered Users, Subscribers
Joined: 3/16/2009(UTC)
Posts: 11
Location: Sydney

Its ok I figured it out.. If(Cross(RSI(14),30),1,If(Cross(70,RSI(14)),-1,0)) I had confused myself but broke it down and then it jumped out at me!
wabbit  
#3 Posted : Wednesday, May 6, 2009 5:21:20 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)
ksrt,

Because the signals are mutually exclusive, you can further simplify the expression by exploiting how MS returns 1 or 0 from comparison operations:

Code:
ind:=RSI(C,14);
Cross(ind,30) - Cross(70,ind);


will return 1 if the first part of the expression is true (which means the second part is false as they are mutually exclusive) and -1 if the first part is false and the second part true.



wabbit [:D]

ksrt  
#4 Posted : Wednesday, May 6, 2009 5:35:19 AM(UTC)
ksrt

Rank: Member

Groups: Registered, Registered Users, Subscribers
Joined: 3/16/2009(UTC)
Posts: 11
Location: Sydney

Thanks Wabbit, makes sense I have another one. I am trying to reference another indicator saying if the MAs cross and expert advisor occurred in the last 15 days then 1 (where the expert advisor returns 1 if the event occurred) Probably is very basic but I am just trying to play around while I learn the language etc. Here is my attempt but it returns 1 when clearly the expert advisor did not provide a signal in the last 15 days.. If(Cross(Mov(C,9,E),Mov(C,20,E)) AND Ref(Fml("ExpertAdvisor(3)"),15)<1,1,0) ksrt
ksrt  
#5 Posted : Wednesday, May 6, 2009 5:37:35 AM(UTC)
ksrt

Rank: Member

Groups: Registered, Registered Users, Subscribers
Joined: 3/16/2009(UTC)
Posts: 11
Location: Sydney

Formula should read: If(Cross(Mov(C,9,E),Mov(C,20,E)) AND Ref(Fml("Expert Advisor(3)"),15)<1,1,0)) {...}
wabbit  
#6 Posted : Wednesday, May 6, 2009 5:38:57 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)
You need the Alert() function.

wabbit [:D]

ksrt  
#7 Posted : Wednesday, May 6, 2009 5:41:19 AM(UTC)
ksrt

Rank: Member

Groups: Registered, Registered Users, Subscribers
Joined: 3/16/2009(UTC)
Posts: 11
Location: Sydney

Great thanks - will give it a go :)
jhughey  
#8 Posted : Wednesday, May 6, 2009 8:13:57 AM(UTC)
jhughey

Rank: Member

Groups: Registered, Registered Users, Subscribers
Joined: 12/19/2008(UTC)
Posts: 19
Location: New York State

Was thanked: 2 time(s) in 1 post(s)
Hello,

If you are looking for an indicator that gives the signals you want; then, try simply separating the expressions instead. This gives the buy and sell signals you are looking for.

If(Cross(RSI(14),30),1,0);
If(Cross(70,RSI(14)),-1,0);

The reason that you think you see the results for the buy signal is that MS evaluates the whole OR expression. Whenever "([censored]x) OR ([censored]x)" is TRUE, the entire expression evaluates to TRUE, which is one. So with the expression below, you get the Boolean results of 1's and 0's, not the output of the IF() construct for EITHER condition, and the BUY/SELL RSI conditions appear to give a "+1" signal.

If(Cross(RSI(14),30),1,0) OR If(Cross(70,RSI(14)),-1,0);
wabbit  
#9 Posted : Wednesday, May 6, 2009 8:48:56 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)
jhughey wrote:
Hello,

If you are looking for an indicator that gives the signals you want; then, try simply separating the expressions instead. This gives the buy and sell signals you are looking for.

If(Cross(RSI(14),30),1,0);
If(Cross(70,RSI(14)),-1,0);

The reason that you think you see the results for the buy signal is that MS evaluates the whole OR expression. Whenever "([censored]x) OR ([censored]x)" is TRUE, the entire expression evaluates to TRUE, which is one. So with the expression below, you get the Boolean results of 1's and 0's, not the output of the IF() construct for EITHER condition, and the BUY/SELL RSI conditions appear to give a "+1" signal.

If(Cross(RSI(14),30),1,0) OR If(Cross(70,RSI(14)),-1,0);


Here:

If(Cross(RSI(14),30),1,0);

can also be written:

Cross(RSI(14),30);

by letting MS take care of the return of TRUE (=1) or FALSE (=0) from the expression.


We can also take a short cut in MS for x*(-1) by writing:

If(Cross(70,RSI(14)),-1,0);

as:

-Cross(70,RSI(14));


Coders should be acutely aware of what they try to get MS to evaluate and how MS does its evaluation; being particularly cognisant of what constitutes TRUE and FALSE when using representative value. Core MS sees any positive value as TRUE and any zero or negative number as FALSE. (NOTE: some add-ins don't so you have to be even more careful using other products). So in:

If(Cross(RSI(14),30),1,0) OR If(Cross(70,RSI(14)),-1,0);

Let's strip down the components and rewrite the fucntionally exact code:

x:=Cross(RSI(14),30);
y:=Cross(70,RSI(14));

If(x,1,0) OR If(y,-1,0);

As 'x' can be evaluated as TRUE (1) or FALSE (0) so too can the first part of the expresson in the last line, so it is functionally exactly the same as just writing 'x'; but things get more interesting in the second part where the 'y' part can only have two values, -1 or 0, both of which evaluate to FALSE in core MS, so the expression 'OR If(y,-1,0)' will always be false and is therefore redundant. The code drops out some of the condition for which the trader needs for decision making.



Hope this helps.

wabbit [:D]

jhughey  
#10 Posted : Wednesday, May 6, 2009 10:09:33 PM(UTC)
jhughey

Rank: Member

Groups: Registered, Registered Users, Subscribers
Joined: 12/19/2008(UTC)
Posts: 19
Location: New York State

Was thanked: 2 time(s) in 1 post(s)
Hi Wabbit,

Thanks for the follow-up. Help me understand something about this expression.

If(Cross(RSI(14),30),1,0) OR If(Cross(70,RSI(14)),-1,0);

You said: "Core MS sees any positive value as TRUE and any zero or negative number as FALSE. "

I am not sure that is accurate in this situation. Experimentation with this indicator’s behavior implies that the right side’s return of -1 is evaluated as TRUE.

For example, I tested the above expression side-by-side with the RSI(14) on a stock, and this indicator clearly gives a “1” under both conditions: when it crosses 30% and when it crosses 70%. This implies that the right side has evaluated -1 as TRUE.

The next step, as a sanity check, I determined the following indicators gave a return values of 1.

(-1) OR (-1) returns TRUE
(0) OR (-1) returns TRUE
(-1) OR (0) returns TRUE
(0) OR (-25) returns TRUE
(0) OR (-0.0001) returns TRUE

Based on this observation, it would seem that the return of a non-zero entity, positive or negative, would be evaluated by core MS as TRUE?
wabbit  
#11 Posted : Wednesday, May 6, 2009 10:27:32 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)
mea culpa.... you're right.

As I said ,"some add-ins don't so you have to be even more careful using other products." The core MS values are:

TRUE : any non-zero value
FALSE : zero


It always pays to check.


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.