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

Notification

Icon
Error

Options
Go to last post Go to first unread
jhughey  
#1 Posted : Tuesday, May 5, 2009 7:18:28 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)

Hello,

As an exercise in Basic Metastock Programming, is it possible to use the MSFL to create the Parabolic SAR from scratch?

Yes, I do realize MSFL has the SAR() function, MS has the SAR indicator, and there are probably many DLLs out there, and I have studied the MSFL functions in the User's Guide until I am cross-eyed.

If it is possible to program the SAR, perhaps someone in this forum might use this as an example to demonstrate the power of the MSFL to us less-experienced basic programmers? PREV and Ref()? If it cannot be done, perhaps this would a good place to explain the restrictions?

If anyone is interested, here is the attempt with lots of commented documentation.

Thanks!


=======================================================

{ SAR METASTOCK LANGUAGE }

{ }

{ POS = 1 means Long Position; 0 means Short. }

{ We start with Long }

{ Use Long and Short Booleans to improve code readability }

{ Start with the maximum Acceleration Factor}

{ Start with SAR equal to the Low}

{ Start with the Extreme Price equal to the High}

{ Calculate the Acceleration Factor First{ If the position changes, reset AF to 0.02{ If the position does not change: }

{ }

{ }

{ AND H>Ref(H,-1) ) OR (Short AND L<Ref(L,-1)),

{ increment, but don’t exceed max AF}

{If Long, the Extreme Price is the highest high }

{If Short, it is the lowest low. If(long AND H>PREV, H, If(short AND L<PREV, L, PREV));

{ Calculate SAR from yesterday’s values{ This had to be broken down into NewSAR{ to avoid MSFL binary overflow. :=

{Now we have a new SAR, do we need a new position?}

{ Switch to Short If(Short AND MySAR<H,1, { Switch to LongPREV)); { Otherwise don't change }

{ Change Short and Long as appropriate for new position.Long := If(POS,1,0);


Back to top
wabbit  
#2 Posted : Tuesday, May 5, 2009 8:19:17 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)
One of the "features" of MS is that it uses "fully evaluated" code. What that means is that each line of code is analysed for every bar of the chart, before moving to the next line of code, (except in the EST where some other strange goings-on happen) meaning is that you cannot reassign values to a previously assigned variable as MS sees it as a new variable. (This is a common way to circumvent the 20 variable limitation in MS).

e.g.

Code:
{ POS = 1 means Long Position; 0 means Short. }
{ We start with Long }
POS := If(Cum(1)=1, 1, PREV);

{...}

POS := If(Long AND MySAR>L,0, { Switch to Short }
 If(Short AND MySAR<H,1, { Switch to Long }
 PREV)); { Otherwise don't change }



Here the first POS variable is not the same as the second POS variable, or in another way, changing the value of the second POS variable does not affect the first POS variable as by the time the code at the second POS is computed the first POS has been fully evaluated.

As such, the PREV function is self-recursive and will return the value only of itself on the previous bar through recursion.

e.g.

Code:
POS := If(Cum(1)=1, 1, PREV);


will return a value of 1 for every bar regardless of any attempt to set another value of POS somewhere else in the code.

--

I haven't bothered to rebuild the SAR in MS; to be brutally honest, I haven't thought too much about it because its just so much simpler to use a stronger programming language to do this sort of work, but to achieve the aim, you'll probably need to use one statement that will be ugly to write and maintain, contain lots of PREV functions as you're only going to be able to directly track one value when you actually need to track three values, so you'll be using a lot of ValueWhen() functions which alos contain PREVs: something along the lines of the SAR concept:

UserPostedImage

mySAR:={will return a price value}
if(cum(1)=1,LOW {start with the LOW price},
if({things get messy here, will involve lots of PREVs and Valuewhen(1,PREV<>somevalue,data)s},
{true, compute the new SAR value, again lots of PREVs and Valuewhen()s},{otherwise}PREV);


I'm not saying it cannot be done... but its easier done elsewhere.


wabbit [:D]

jhughey  
#3 Posted : Wednesday, May 6, 2009 10:44:47 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)
Thank you, Wabbit. You spent a lot of time answering my question, and I really appreciate it. You have also provided me with a quantum leap in understanding how the MSFL works.

Let me see if I get this right: Each variable on the left side of an assignment statement is unique to that assignment, even if you name it the same in subsequent assignment statements. Consequently, if you are going to do any operations on a variable, you get ONE shot at it … in that line only. In subsequent statements, you can use its VALUE, but that is all you can do with it.

In all my comp sci experiences, this is the first time I have encountered a “Fully Evaluated” code approach. It makes sense, though; and, that explains how things can get messy. Oh well, I shall tinker with it a bit more.

Thanks again!

(ps) There is hope. I just recently purchased the Developer’s Toolkit, and I installed Microsoft’s Visual C++ 2008 Edition. So, I guess I shall have to brush up on my C++ and go through the learning curve.
wabbit  
#4 Posted : Wednesday, May 6, 2009 6:09:15 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 again Joe,

Depending on which version of MS Visual C++ 2008 you have, whilst waiting for the arrival of the MDK in your mailbox you might want to spend a little bit time figuring out how to configure VC++ to compile win32.dll (if you have the Express edition, this isn't standard and you have to do some tinkering to its settings). There is plenty of help through google.



wabbit [:D]

jhughey  
#5 Posted : Wednesday, May 6, 2009 10:14:22 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)
Oh me ! … I have 64-bit Vista.
Back to top
minnamor  
#6 Posted : Sunday, May 10, 2009 11:52:37 AM(UTC)
minnamor

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 4/27/2005(UTC)
Posts: 126
Location: Italy

have a look at the five parameter parabolic by Meyers Analytic in http://www.meyersanalytics.com/parabxot.php
jhughey  
#7 Posted : Sunday, May 10, 2009 1:43:44 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)
Thank you Minnamor,

I wish it were free.

I pretty sure I know how it works, which is along the line of my original reasoning for creating a SAR from scratch. If I ever get to this point, I shall post it free in this forum.

Now, if I can just get MDK to work!

Thanks!
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.