Announcement

Collapse
No announcement yet.

MACD Backtest help

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

  • MACD Backtest help

    im having trouble testing out this MACD backtest file. can anyone help me out and figuire why I only get one trade on the backtest trade analyzer and how to put in stops? i thought i coded it right, but its not working.

    thanks in advance,

    sam

    var study = new MACDStudy(8,16, 11, "Close", false);

    function preMain() {
    setPriceStudy(false);
    setColorPriceBars(true);
    setDefaultPriceBarColor(Color.green);
    setStudyTitle("MACD Sams Strategy");
    setCursorLabelName("MACD", 0);
    setCursorLabelName("SIGNAL", 1);
    setDefaultBarFgColor(Color.blue, 0);
    setDefaultBarFgColor(Color.red, 1);
    }

    function main() {
    var MACD = study.getValue(MACDStudy.MACD);
    var Signal = study.getValue(MACDStudy.SIGNAL);
    //var dStopS;
    var dStopL;
    var getOutL;
    //var getOutS;

    if( Signal<MACD && !Strategy.isLong())
    {
    Strategy.doLong("Long", Strategy.CLOSE, Strategy.NEXTBAR);
    dStopL=close()-.5;
    getOutL=close()+.5;
    }
    if (Strategy.isLong()==true)
    // Strategy.setStop(dStopL);
    {
    if (Strategy.CLOSE>getOutL)
    Strategy.doCover("cover", Strategy.CLOSE, Strategy.THISBAR);
    if (Strategy.CLOSE<dStopL)

    Strategy.doCover("stop", Strategy.MARKET, Strategy.THISBAR);
    }


    // if(Strategy.isShort())
    // Strategy.setStop(vStopPrice);



    //Strategy.doSell("sell", Strategy.Market, Strategy.THISBAR)
    // if(Strategy.isLong())
    // {
    // if (Strategy.Market==getOutL)
    // Strategy.Market;
    // }

    // if(Signal > MACD && !Strategy.isShort())
    // {
    // Strategy.doShort("Short", Strategy.MARKET, Strategy.NEXTBAR);
    // dStopS=close()+.5;
    // getOutS=close()-.5;
    // Strategy.setStop(dStopS);
    // }
    // if(Strategy.isShort())
    // {
    // if(Strategy.Market==getOutS)
    // Strategy.doCover("cover", Strategy.Market, Strategy.THISBAR)
    // }

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

    return new Array(MACD,Signal);
    }

  • #2
    Hello Kingsam,

    You were close. There is just one major change that needed to be made and a few other minor changes.

    1) Your getOutL and dStopL variables need to be global (var outside of main) so they retain their value between executions of the formula. Because they were local (inside of main), they only stored a valid value on the bar where you went long. After that bar they were null, which caused your exit conditions to fail. That's the reason you only had one trade. The other changes below were non-critical, but I did them anyway to show you some better ways to handle this type of study.

    2) Put the exit conditions in an else block following the entry logic. Your stop values are set pretty close, so depending on the symbol used it's possible to have the exit conditions evaluate to true on the same bar you went long, which isn't what you want to do in back testing. Typically, I force the entry to be Strategy.MARKET/Strategy.NEXTBAR, which enters you at the open of the next bar. It's a more realistic entry than CLOSE/THISBAR, since we don't really know what the close is until after the bar has closed and we're on the next bar. By adding the else, you ensure that the exit condition isn't tested on the entry bar.

    3) Use close(0) instead of Strategy.CLOSE in your exit conditions. Although Strategy.CLOSE may have worked fine, it's not intended to be used outside of the parameters for the Strategy functions.

    4) Use Strategy.doSell() to exit long positions. Strategy.doCover() is the command to buy shares to close an existing short position.

    Here's the code changes I made for you. Try it out and let me know if you have any questions.

    PHP Code:
    var study = new MACDStudy(8,1611"Close"false);

    function 
    preMain() {
        
    setPriceStudy(false);
        
    setColorPriceBars(true);
        
    setDefaultPriceBarColor(Color.green);
        
    setStudyTitle("MACD Sams Strategy");
        
    setCursorLabelName("MACD"0);
        
    setCursorLabelName("SIGNAL"1);
        
    setDefaultBarFgColor(Color.blue0);
        
    setDefaultBarFgColor(Color.red1);
    }

    var 
    dStopL;
    var 
    getOutL;

    function 
    main() {
        var 
    MACD study.getValue(MACDStudy.MACD);
        var 
    Signal study.getValue(MACDStudy.SIGNAL);
        
    //var dStopS;
        //var getOutS;
        
        
    if( Signal<MACD && !Strategy.isLong()) {
            
    Strategy.doLong("Long"Strategy.MARKETStrategy.NEXTBAR);
            
    dStopL close()-.5;
            
    getOutL close()+.5
        } else if (
    Strategy.isLong() == true) {
            if (
    close(0) > getOutL) {
                
    Strategy.doSell("cover"Strategy.CLOSEStrategy.THISBAR);
            }
            if (
    close(0) < dStopL) {
                
    Strategy.doSell("stop"Strategy.CLOSEStrategy.THISBAR);
            }
        }

        return new Array(
    MACD,Signal);

    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment


    • #3
      Because the entry price is the open of the NEXTBAR, shouldn't it be open(1) instead of close()?

      if( Signal<MACD && !Strategy.isLong()) {
      Strategy.doLong("Long", Strategy.MARKET, Strategy.NEXTBAR);
      dStopL = close()-.5;
      getOutL = close()+.5;
      Last edited by Lancer; 01-16-2005, 06:01 PM.

      Comment


      • #4
        Hello Lancer,

        Yes, you are correct. By referencing open(1), which is a future bar value, we should also add a check at the top of main to stop processing once we reach bar 0.

        if (getCurrentBarIndex() > -1) return;

        Another alternative would be to change the entry to CLOSE/THISBAR.
        Jason K.
        Project Manager
        eSignal - an Interactive Data company

        EFS KnowledgeBase
        JavaScript for EFS Video Series
        EFS Beginner Tutorial Series
        EFS Glossary
        Custom EFS Development Policy

        New User Orientation

        Comment

        Working...
        X