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

Notification

Icon
Error

Options
Go to last post Go to first unread
TicMarc  
#1 Posted : Monday, July 19, 2010 2:40:16 PM(UTC)
TicMarc

Rank: Newbie

Groups: Registered, Registered Users, Subscribers
Joined: 4/15/2010(UTC)
Posts: 3

How do you show the first occurrence of a condition until another condition supersedes it?

For example, the buy condition is:

H>Ref(H,-1) AND Ref(H,-1)>Ref(H,-2) AND Ref(H,-2)>Ref(H,-3)


The sell condition is:

L<Ref(L,-1) AND Ref(L,-1)<Ref(L,-2) AND Ref(L,-2)<Ref(L,-3)


I want to show a "buy up arrow" for the first occurrence, only.

Then, when the sell condition is met I want to show a "sell down arrow" for this occurrence, only... showing alternating up and down arrows.
mstt  
#2 Posted : Monday, July 19, 2010 7:16:33 PM(UTC)
mstt

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 7/25/2005(UTC)
Posts: 1,042

Was thanked: 57 time(s) in 54 post(s)

Hi TM

Here are two forms of a simple latch that you can use to capture the state of a trade and from that reduce the buy and Sell signals down to just the active bar of each signal. The revised Buy and Sell signals use the Alert() function to monitor for a change in the Trade variable. Trade*Alert(Trade=0,2) requires Trade to be TRUE for the current bar and FALSE for the previous bar. The reverse applies to (Trade=0)*Alert(Trade,2). I've substituted * for AND and + for OR in these formulas - technically it might not look right but in practice it works fine when used properly, and the advantage is that it economises on space.

{Latch method 1 - Roy Larsen}
Buy:=H>Ref(H,-1) AND Ref(H,-1)>Ref(H,-2) AND Ref(H,-2)>Ref(H,-3);
Sell:=L<Ref(L,-1) AND Ref(L,-1)<Ref(L,-2) AND Ref(L,-2)<Ref(L,-3);
I:=Cum(IsDefined(Buy+Sell))=1;
Trade:=BarsSince(I+Buy)<BarsSince(I+Sell);
Buy:=Trade*Alert(Trade=0,2);
Sell:=(Trade=0)*Alert(Trade,2);
Buy;
Sell;

{Latch method 2 - Jose Silva}
Buy:=H>Ref(H,-1) AND Ref(H,-1)>Ref(H,-2) AND Ref(H,-2)>Ref(H,-3);
Sell:=L<Ref(L,-1) AND Ref(L,-1)<Ref(L,-2) AND Ref(L,-2)<Ref(L,-3);
I:=Cum(IsDefined(Buy+Sell))=1;
Trade:=ValueWhen(1,I+Buy+Sell,Buy);
Buy:=Trade*Alert(Trade=0,2);
Sell:=(Trade=0)*Alert(Trade,2);
Buy;
Sell;

Hope this helps

Roy

TicMarc  
#3 Posted : Friday, July 23, 2010 10:25:05 AM(UTC)
TicMarc

Rank: Newbie

Groups: Registered, Registered Users, Subscribers
Joined: 4/15/2010(UTC)
Posts: 3

Roy,

Thanks for your help. I appreciate it very much.

Please excuse my ignorance. Is this code used as an indicator, expert advisor or a system?

Thanks,

TM

mstt  
#4 Posted : Friday, July 23, 2010 2:27:42 PM(UTC)
mstt

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 7/25/2005(UTC)
Posts: 1,042

Was thanked: 57 time(s) in 54 post(s)

Hi TM

General coding principles like this apply to all tools where the formula language is used. There are very few functions that are either unique to or cannot be used in a particular tool. For example, the Input() function can only be used in the Indicator Builder, the "colA" function only in the Explorer Filter and the WriteVal() function only in the Expert Advisor. Almost everything else will work anywhere that you need it.

Roy

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.