Announcement

Collapse
No announcement yet.

Converting a study into a strategy

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Converting a study into a strategy

    Hi--I have a simple one: What code to add to go Long if it crosses the zero line + and go short if it crosses the zero line to make it a backtestable strategy. Thanks
    ==================================

    var ma = null;

    function preMain()
    {
    setStudyTitle("Z-Score");
    setCursorLabelName("Z-Score", 0);
    setDefaultBarFgColor(Color.red, 0);
    addBand(0, PS_SOLID, 1, Color.lightgrey);
    }

    function main(Period) {
    var StdDev = 0;
    var SumSqr = 0;
    var counter = 0;

    if(Period == null)
    Period = 20;

    if(ma == null)
    ma = new MAStudy(Period, 0, "Close", MAStudy.SIMPLE);

    for(counter = - Period + 1; counter <= 0; counter++)
    SumSqr += Math.pow((close(counter) - ma.getValue(MAStudy.MA)),2);

    StdDev = Math.sqrt(SumSqr / Period);

    return (close() - ma.getValue(MAStudy.MA)) / StdDev;
    }

  • #2
    elippert

    After this Line
    StdDev = Math.sqrt(SumSqr / Period);
    you could substitute with this

    PHP Code:
    var vZ = (close() -  ma.getValue(MAStudy.MA)) / StdDev;

    if (
    vZ>&& Strategy.isLong() == false)
    Strategy.doLong(""Strategy.CLOSEStrategy.THISBARStrategy.DEFAULT, 0);

    if(
    vZ<0&&Strategy.isShort()==false)
    Strategy.doShort(""Strategy.CLOSEStrategy.THISBARStrategy.DEFAULT, 0);

    return 
    vZ;

    Alex

    Comment


    • #3
      RE: Reply to post 'Converting a study into a strategy'

      Thanks Alex.

      -----Original Message-----
      From: [email protected] [mailto:[email protected]]
      Sent: Friday, June 06, 2003 8:07 AM
      To: [email protected]
      Subject: Reply to post 'Converting a study into a strategy'


      Hello elippert,

      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

      Comment

      Working...
      X