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

Notification

Icon
Error

Options
Go to last post Go to first unread
xrousaios  
#1 Posted : Thursday, February 16, 2012 12:35:31 PM(UTC)
xrousaios

Rank: Member

Groups: Registered, Registered Users, Unverified Users
Joined: 1/30/2012(UTC)
Posts: 22
Location: Athens, Greece

Hi I'm a Metastock EOD ver 9.0 user. I'm trying to make a formula to calculate the bars passed since a certain event occured for the FIRST time, after an expression took place. I tried a lot but I did not accomplish it. I would be grateful for any help. Thanks in advance. George
wabbit  
#2 Posted : Thursday, February 16, 2012 3:15:42 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)
What code have you tried? Please post it here; put the code between * code* and * /code* tags (without the spaces). wabbit [:D]
xrousaios  
#3 Posted : Friday, February 17, 2012 5:38:10 AM(UTC)
xrousaios

Rank: Member

Groups: Registered, Registered Users, Unverified Users
Joined: 1/30/2012(UTC)
Posts: 22
Location: Athens, Greece

First of all, I’ll try to describe the logic of my formula in a pseudo-code, as follows:

IF Close crosses the Moving Avegare-26 Barssince ago THEN
IF Barssince equals to zero THEN do nothing
ELSE Lookup for the FIRST time when the Close became greater than the Close Barssince ago
IF the FIRST time occurs at the present bar THEN Plot a signal
ELSE Do nothing because I already plotted the appropriate signal in a previous bar
ENDIF
ENDIF
ENDIF

The code I used till now is shown below :
Code:

bs26:=0;
Vclose26:=0;

bs26:=BarsSince(
(Cross(C,Mov(C,26,S))
 AND
 ((C > Mov(C,50,S) AND C < Mov(C,200,S))
 OR
 (C < Mov(C,50,S) AND C > Mov(C,200,S))))
);

Vclose26:=ValueWhen(1,(Cross(C,Mov(C,26,S))
 AND
 ((C > Mov(C,50,S) AND C < Mov(C,200,S))
 OR
 ((C < Mov(C,50,S) AND C > Mov(C,200,S))))),C);



My real problem is that I did not find a appropriate function to detect the FIRST time the event occurred. Also I have encountered two similar problems : a. I can’t multiply a close by a decimal factor (e.g. close *1.05 produces an error b. The SUM and the REF functions can’t accept a variable in the place of PERIOD parameter.

Thanks a lot for your attention.


[edited by wabbit : tags and readability, forum cutting off parts of the post [:(] )
wabbit  
#4 Posted : Friday, February 17, 2012 7:02:43 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)
A few things to start with:

1) You do not need to declare or initialise variables in MS, so the first two lines of code:

Code:
bs26:=0;

Vclose26:=0;


are redundant.

2) In the forum, use *code* and */code* (note the asterisks, no the spaces) to properly format your code snippets.

3) You need to use a count-and-reset logic, something like:

Code:

event:=Cross(C,Mov(C,26,S))
 AND (C > Mov(C,50,S) AND C < Mov(C,200,S))
 OR
 (C < Mov(C,50,S) AND C > Mov(C,200,S));

eventPrice:=valuewhen(1,event,C);

count:=cum(C > eventPrice);
reset:=event;
counter:=count-valuewhen(1,reset,count);

{plot}
counter=1 and alert(counter<>1,2);



Hope this helps.

wabbit [:D]

xrousaios  
#5 Posted : Monday, February 20, 2012 2:01:00 PM(UTC)
xrousaios

Rank: Member

Groups: Registered, Registered Users, Unverified Users
Joined: 1/30/2012(UTC)
Posts: 22
Location: Athens, Greece

I do thank you for your help. I think I have understood quite well the real meaning of MS variables. That is, each variable stands for the expression assigned to it or alternatively it holds the value calculated by the corresponding procedure to it. So, for example, the event variable stands for the “Cross(C,Mov(C,26,S)) e.t.c. ” expression, and the EventPrice variable holds the value calculated by the “valuewhen(1,event,C)” procedure. However, I did not understand why we use the new variable “reset” instead of the variable “event”. Couldn’t we re-use the “event” variable on the “counter” variable calculation? Thank you once more for your guidance Best regards, George from Athens, Greece
wabbit  
#6 Posted : Monday, February 20, 2012 5:06:13 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)
I wrote the code and used very descriptive variables to clearly demonstrate what is going on in the code. Had I written the code in a more concise method, you might not have understood the workings and it might not have made as much sense:

Code:

reset:=Cross(C,Mov(C,26,S))
 AND (C > Mov(C,50,S) AND C < Mov(C,200,S))
 OR
 (C < Mov(C,50,S) AND C > Mov(C,200,S));

count:=cum(C > valuewhen(1,reset,C));
counter:=count-valuewhen(1,reset,count);

{plot}
counter=1 and alert(counter<>1,2);


Do with that what you will.



wabbit [:D]



xrousaios  
#7 Posted : Wednesday, February 22, 2012 12:15:05 PM(UTC)
xrousaios

Rank: Member

Groups: Registered, Registered Users, Unverified Users
Joined: 1/30/2012(UTC)
Posts: 22
Location: Athens, Greece

Thank you for the time you spent for me. You helped me to have a clearer view on the MS programming way. Best regards George
Users browsing this topic
Guest (Hidden)
Similar Topics
Is there any training material on the MetaStock formula language? (Custom Formulas)
by MS Support 5/8/2017 4:45:59 PM(UTC)
Set Color/Style/Weight Attributes using the Formula Language (Formula Assistance)
by WaveTrader 12/22/2016 4:21:13 PM(UTC)
Need Metastock Formula Language (Formula Assistance)
by VENU G 12/21/2012 6:23:23 AM(UTC)
metastock formula language for Pascal Willain's indicators (MetaStock)
by annonymous 12/2/2010 8:02:37 AM(UTC)
Trying to understand the MS Formula Language version of Ehler's Adaptive Moving Average (MAMA) (Basic Coding Techniques)
by jhughey 12/19/2008 1:02:50 PM(UTC)
Where to find the formula language manual? (MetaStock)
by arnie 4/27/2007 3:04:55 PM(UTC)
Learning Formula Language : Showing Values (Formula Assistance)
by jer99 8/3/2006 10:01:31 AM(UTC)
PWR and POWER Functions in MS Formula Language (Formula Assistance)
by cenergy 4/13/2006 1:20:41 AM(UTC)
Metastock Formula Language Limitations (Formula Assistance)
by Capablanca 10/29/2005 8:45:04 PM(UTC)
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.