in Search

Can the Parabolic SAR be programmed from scratch with the MSFL?

Last post 05-10-2009, 13:43 by jhughey. 6 replies.
Sort Posts: Previous Next
  •  05-05-2009, 19:18 29634

    Can the Parabolic SAR be programmed from scratch with the MSFL?


    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.   However, I would like to play with variations on the basic SAR algorithm; and, just for the nut of it, determine whether or not such an indicator can be done solely with the MSFL. 

     

    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?      I think my problem is understanding the initialization and propagation of parameters with the MSFL, along with the roles of  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.   I checked the syntax with the formula editor, so it should be cut-and-paste ready.     I shall be happy to post the final results if anyone can get me past the fundamental glitches.


    Thanks!


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

    {  PARABOLIC  SAR  IN  METASTOCK  FORMULA  LANGUAGE }

    {  Using an Acceleration Factor Step of 0.02 with a max of 0.20             }

     

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

    { We start with Long                                             }

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

       

    { Use Long and Short Booleans to improve code readability }

    Long := If(Cum(1)=1, 1, PREV);

    Short := If(Cum(1)=1, 0, PREV);

     

    { Start with the maximum Acceleration Factor}

    AF := If(Cum(1)=1, 0.20, PREV);

     

    { Start with SAR equal to the Low}

    MySAR := If(Cum(1)=1, L, PREV);

     

    { Start with the Extreme Price equal to the High}

    ExtPrc := If(Cum(1)=1, H, PREV);

     

    { Calculate the Acceleration Factor First          }

    { If the position changes, reset AF to 0.02        }

    { If the position does not change:                      }

    {    - increment AF if long and higher highs        }

    {    - increment AF if short and lower lows        }

    {    - otherwise, AF does not change.                }

    AF := If(POS <> Ref(POS,-1) , 0.02,

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

    If(PREV+0.02<0.20, PREV+0.02, 0.20),     { increment, but don’t exceed max AF}

    PREV));

     

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

    {If Short, it is the lowest low.                         }

    ExtPrc :=  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.          }

    NewSAR := (Ref(ExtPrc,-1) - Ref(MySAR,-1))*Ref(AF,-1) + Ref(MySAR,-1);

    MySAR  :=  If(POS <> Ref(POS,-1), Ref(ExtPrc,-1), NewSAR);


     

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

    POS := If(Long AND MySAR>L,0,   { Switch to Short  }

           If(Short AND MySAR<H,1,       { Switch to Long    }

           PREV));                              { Otherwise don't change }

     

    { Change Short and Long as appropriate for new position.  }

    Short := If(POS,0,1);

    Long := If(POS,1,0);


    MySAR;





    Joe
  •  05-05-2009, 20:19 29635 in reply to 29634

    Re: Can the Parabolic SAR be programmed from scratch with the MSFL?

    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:



    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 Big Smile [:D]


    "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
    Edsgar W. Dijkstra

     

    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status :
    My SkyPE account : wabbit.com.au

  •  05-06-2009, 10:44 29648 in reply to 29635

    Re: MSFL Fully Evaluated Code Properties - One variable, one line, one shot.

    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.
    Joe
  •  05-06-2009, 18:09 29653 in reply to 29648

    Re: MSFL Fully Evaluated Code Properties - One variable, one line, one shot.

    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 Big Smile [:D]


    "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
    Edsgar W. Dijkstra

     

    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status :
    My SkyPE account : wabbit.com.au

  •  05-06-2009, 22:14 29664 in reply to 29653

    Re: MS Developer's Toolkit

    Oh me ! …  and  I have 64-bit Vista.   Thanks for the heads up!
    Joe
  •  05-10-2009, 11:52 29685 in reply to 29664

    Re: MS Developer's Toolkit

    have a look at the five parameter parabolic by Meyers Analytic in http://www.meyersanalytics.com/parabxot.php
  •  05-10-2009, 13:43 29686 in reply to 29685

    Re: MS Developer's Toolkit

    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!

    Joe
View as RSS news feed in XML