Announcement

Collapse
No announcement yet.

backtest question

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

  • backtest question

    I've started to learn to write backtest code. Been using the great stuff from Brad, and looking at some other examples.

    I have written one simple backtest efs. It is a stop and reverse sma crossover. No stops, no targets. The problem is that it does not work. It flips back and forth on every bar. Testing on a 15 min chart FWIW.

    Any ideas why?

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

    var vmafast = null;
    var vmaslow = null;

    function preMain() {
    setPriceStudy(true);

    }

    function main() {

    if (vmafast == null) vmafast = new MAStudy(5, 0, "Close", MAStudy.SIMPLE);
    if (vmaslow == null) vmaslow = new MAStudy(20, 0, "Close", MAStudy.SIMPLE);


    var vf = vmafast.getValue(MAStudy.MA);
    var vs = vmaslow.getValue(MAStudy.MA);

    if(vf == null || vs == null)
    return;

    if ((!Strategy.isInTrade())|| (Strategy.isShort())) { // not in a trade currently or currently short
    if(vf > vs ) { // test for long entry
    Strategy.doLong("Enter Long", Strategy.MARKET, Strategy.NEXTBAR);
    }
    }

    else{
    if ((!Strategy.isInTrade())|| (Strategy.isLong())) { // not in trade or currently long
    if(vs < vf ){ // test for short entry
    Strategy.doShort("Enter Short", Strategy.MARKET, Strategy.NEXTBAR);
    }
    } // end if strategy not in a trade

    }

    return;
    }

  • #2
    bigtee
    Try the enclosed revision.
    I have also added (but commented out) the commands to paint the background when the strategy is Long or Short so that you can compare it against the Back Tester
    Alex

    PHP Code:
    var vmafast null;
    var 
    vmaslow null;

    function 
    preMain() {
    setPriceStudy(true); 

    }

    function 
    main() {

    if (
    vmafast == nullvmafast = new MAStudy(50"Close"MAStudy.SIMPLE);
    if (
    vmaslow == nullvmaslow = new MAStudy(200"Close"MAStudy.SIMPLE);

    var 
    vf vmafast.getValue(MAStudy.MA);
    var 
    vs vmaslow.getValue(MAStudy.MA);

    if(
    vf == null || vs == null)
    return;

    if (!
    Strategy.isInTrade()|| !Strategy.isLong()) { // currently not in a trade or long
    if(vf vs ) { // test for long entry 
    Strategy.doLong("Enter Long"Strategy.MARKETStrategy.NEXTBAR); 

    }

    if (!
    Strategy.isInTrade()|| !Strategy.isShort()) { // currently not in trade or short
    if(vf vs ){ // test for short entry 
    Strategy.doShort("Enter Short"Strategy.MARKETStrategy.NEXTBAR); 

    // end if strategy not in a trade

    //if(Strategy.isLong()) setBarBgColor(Color.lime);
    //if(Strategy.isShort()) setBarBgColor(Color.red);

    return;

    Comment


    • #3
      Thanks, Alex.

      From comparing your code vs mine, I see the problem was the "else" structure, apparently.

      bigtee

      Comment


      • #4
        bigtee
        Actually the problem was in the test for long/short entry.
        One condition was if(vf > vs) while the other was if(vs < vf) which is the same thing. If you notice I reversed the second one.
        Alex

        Comment


        • #5
          thanks again, I had missed that.

          Comment

          Working...
          X