applying the KISS principle

  •  07-19-2007, 15:16

    applying the KISS principle

    Whilst recently looking around for poorly written MetaStock code to use as an example for demonstrating the KISS principle (an acronym for "Keep It Simple, Stupid"), I came across this gem:

    --------------------------------------------

    {Restricted Display}
    {Written by Preston Umrysh}
    {Displays an indicator for a specified period of time}
    pds:=Input("MA Lookback Periods ",2,100,10);
    A:=Mov(C,pds,S);{Your Indicator Here}
    len:=Input("TotalDisplayPeriods ",8,100,10);
    Barbk:=Input("Right Setback",0,100,0);
    len:=len+Barbk;{resets display to setback}
    Signal:=Ref(A,-len);
    Signal2:=Ref(A,-Barbk);
    LastBar:=Cum(1)=LastValue(Cum(1)-(len));
    bacdis1:=ValueWhen(1,LastBar,LastValue(Signal));
    1stRefPnt:=Ref(Ref(bacdis1,-Barbk),Barbk);{end}
    bacdis2:=ValueWhen(1,LastBar,LastValue(Signal2));
    2ndRefPnt:=Ref(Ref(bacdis2,-Barbk),Barbk);{end}
    {Line Plot }
    D1:=LastValue(Cum(1))-LastValue(LEN);
    {D1 is first high of line}
    D2:=LastValue(Cum(1))-LastValue(BARBK);
    {D2 is last high of line}
    P1:=1stRefPnt; {start-of-line data array}
    P2:=2ndRefPnt; {end-of-line data array}
    V1:=ValueWhen(1,D1,P1);
    V2:=ValueWhen(1,D2,P2);
    B1:=LastValue(Cum(1))-D1;
    B2:=LastValue(Cum(1))-D2;
    LINE:=V1-(BarsSince(Cum(1)=D1)*( (V1-V2)/(B1-B2)));
    If(LINE,A,line) {end}

    --------------------------------------------


    Apart from the fact that the "author" doesn't seem to understand the MetaStock code he has plagiarized, the (mostly redundant) 18 lines of plot-restriction code above can be condensed into 3 simpler lines:

    --------------------------------------------


    { Bar-based plot display restriction v1.0
    http://www.metastocktools.com }

    { User inputs }
    pds:=Input("Indicator periods ",1,1000,10);
    totPds:=Input("Total display periods",1,1000,10);
    backPds:=Input("Right setback periods",0,1000,0);

    { Indicator example }
    x:=Mov(C,pds,S);

    { Plot restriction }
    start:=Cum(1)=LastValue(Cum(1))-totPds-backPds;
    rightClip:=Ref(Ref(x,-backPds),backPds);
    restricted:=rightClip*ValueWhen(1,start,1);

    { Output }
    restricted

    --------------------------------------------


    Not only is this formula much shorter, but its logic makes some sense.

    KISS - the easiest way to become proficient at writing reasonable MetaStock code.


    jose '-)
    MetaStockTools.com
View Complete Thread