Announcement

Collapse
No announcement yet.

backtest

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

  • backtest

    Hi

    I am after some help please. I am new to furmular language and have tried to back test a simple formular that i have posted below.

    I intend to see results if the MA crosses up below 0 to go long and to short when it crosses above 0.

    I would hold the trade until the turning point.

    It would need further work but any help would be greatly appreciated.

    The formular is:

    function preMain() {
    setStudyTitle("MACD X-Over ");
    setDefaultBarFgColor(Color.magenta, 0); // hist
    setDefaultBarFgColor(Color.red, 1); // signal
    setDefaultBarFgColor(Color.blue, 2); // macd

    setPlotType(PLOTTYPE_HISTOGRAM, 0);

    setCursorLabelName("Hist", 0);
    setCursorLabelName("Signal", 1);
    setCursorLabelName("MACD", 2);
    }

    var study = null;
    var vMACD = null;
    var vMACD1 = null;
    var vSignal = null;
    var vSignal1 = null;
    var vAlert = false;
    var AlertCntr = 0;

    function main(nFastLength, nSlowLength, nSmoothing, nPriceSource, nSimpleMA_TorF) {
    var nState = getBarState();

    if (study == null) {
    if (nFastLength == null)
    nFastLength = 12;
    if (nSlowLength == null)
    nSlowLength = 26;
    if (nSmoothing == null)
    nSmoothing = 9;
    if (nPriceSource == null) {
    nPriceSource = "Close";
    } else if(nPriceSource == "C" || nPriceSource == "O" || nPriceSource == "H" || nPriceSource == "L")
    {
    nPriceSource = nPriceSource;
    } else if(nPriceSource == "Close" || nPriceSource == "Open" || nPriceSource == "High" ||
    nPriceSource
    ==
    "Low")
    {
    nPriceSource = nPriceSource;
    } else if(nPriceSource == "HL/2" || nPriceSource == "HLC/3" || nPriceSource == "OHLC/4") {
    nPriceSource = nPriceSource;
    } else {
    nPriceSource = "Close";
    }
    if (nSimpleMA_TorF == null) {
    nSimpleMA_TorF = false;
    } else if (nSimpleMA_TorF == "T" || nSimpleMA_TorF == "t" || nSimpleMA_TorF == "true" ||
    nSimpleMA_TorF == "True") {
    nSimpleMA_TorF = true;
    } else if (nSimpleMA_TorF == "F" || nSimpleMA_TorF == "f" || nSimpleMA_TorF == "false" ||
    nSimpleMA_TorF == "False") {

    nSimpleMA_TorF = false;
    } else {
    nSimpleMA_TorF = false;
    }
    study = new MACDStudy(nFastLength, nSlowLength, nSmoothing, nPriceSource, eval
    (nSimpleMA_TorF));
    }

    if (nState == BARSTATE_NEWBAR) {
    if (vMACD != null)
    vMACD1 = vMACD;
    if (vSignal != null)
    vSignal1 = vSignal;
    vAlert = false;
    }
    vMACD = study.getValue(MACDStudy.MACD);
    if (vMACD == null)
    return;
    vSignal = study.getValue(MACDStudy.SIGNAL);
    if (vSignal == null)
    return;
    var vHist = study.getValue(MACDStudy.HIST);
    if (vHist == null)
    return;

    if (vAlert == false) {
    if (vMACD1 < vSignal1 && vMACD >= vSignal) { // crossing up
    vAlert = true;
    AlertCntr += 1;
    drawShapeRelative(0, vSignal, Shape.UPARROW, null, Color.green, Image.TOP | Image.ONTOP,
    "Alert" + AlertCntr);
    }
    if (vMACD1 > vSignal1 && vMACD <= vSignal) { // crossing down
    vAlert = true;
    AlertCntr += 1;
    drawShapeRelative(0, vSignal, Shape.DOWNARROW, null, Color.red, Image.BOTTOM |
    Image.ONTOP,
    "Alert" + AlertCntr);
    }
    } else {
    if ((vMACD1 < vSignal1 && vMACD < vSignal) || (vMACD1 > vSignal1 && vMACD > vSignal)) {
    vAlert = false;
    removeShape("Alert" + AlertCntr);
    }
    }

    return new Array(vHist, vSignal, vMACD);
    }
    Last edited by allenm; 05-11-2004, 02:22 PM.

  • #2
    allenm
    The enclosed revision of your script now includes the strategy commands for back testing.
    Alex


    PHP Code:
    function preMain() { 
        
    setStudyTitle("MACD X-Over "); 
        
    setDefaultBarFgColor(Color.magenta0); // hist 
        
    setDefaultBarFgColor(Color.red1); // signal 
        
    setDefaultBarFgColor(Color.blue2); // macd 
         
        
    setPlotType(PLOTTYPE_HISTOGRAM0); 
         
        
    setCursorLabelName("Hist"0); 
        
    setCursorLabelName("Signal"1); 
        
    setCursorLabelName("MACD"2); 


    var 
    study null
    var 
    vMACD null
    var 
    vMACD1 null
    var 
    vSignal null
    var 
    vSignal1 null
    var 
    vAlert false
    var 
    AlertCntr 0

    function 
    main(nFastLengthnSlowLengthnSmoothingnPriceSourcenSimpleMA_TorF) { 
        var 
    nState getBarState(); 

        if (
    study == null) { 
            if (
    nFastLength == null
                
    nFastLength 12
            if (
    nSlowLength == null
                
    nSlowLength 26
            if (
    nSmoothing == null
                
    nSmoothing 9
            if (
    nPriceSource == null) { 
                
    nPriceSource "Close"
            } else if(
    nPriceSource == "C" || nPriceSource == "O" || nPriceSource == "H" || nPriceSource == "L")
     { 
                
    nPriceSource nPriceSource
            } else if(
    nPriceSource == "Close" || nPriceSource == "Open" || nPriceSource == "High" || 
    nPriceSource 
    == 
    "Low"

                
    nPriceSource nPriceSource
            } else if(
    nPriceSource == "HL/2" || nPriceSource == "HLC/3" || nPriceSource == "OHLC/4") { 
                
    nPriceSource nPriceSource
            } else { 
                
    nPriceSource "Close"
            } 
            if (
    nSimpleMA_TorF == null) { 
                
    nSimpleMA_TorF false
            } else if (
    nSimpleMA_TorF == "T" || nSimpleMA_TorF == "t" || nSimpleMA_TorF == "true" ||
     
    nSimpleMA_TorF == "True") { 
                
    nSimpleMA_TorF true
            } else if (
    nSimpleMA_TorF == "F" || nSimpleMA_TorF == "f" || nSimpleMA_TorF == "false" || 
    nSimpleMA_TorF == "False") { 

                
    nSimpleMA_TorF false
            } else { 
                
    nSimpleMA_TorF false
            } 
            
    study = new MACDStudy(nFastLengthnSlowLengthnSmoothingnPriceSource, eval
    (
    nSimpleMA_TorF)); 
        } 
         
        if (
    nState == BARSTATE_NEWBAR) { 
            if (
    vMACD != null
                
    vMACD1 vMACD
            if (
    vSignal != null
                
    vSignal1 vSignal
            
    vAlert false
        } 
        
    vMACD study.getValue(MACDStudy.MACD); 
        if (
    vMACD == null
            return; 
        
    vSignal study.getValue(MACDStudy.SIGNAL); 
        if (
    vSignal == null
            return; 
        var 
    vHist study.getValue(MACDStudy.HIST); 
        if (
    vHist == null
            return; 
         
        if (
    vAlert == false) { 
            if (
    vMACD1 vSignal1 && vMACD >= vSignal) {  // crossing up 
                
    vAlert true
                
    AlertCntr += 1
                
    drawShapeRelative(0vSignalShape.UPARROWnullColor.greenImage.TOP Image.ONTOP
    "Alert" AlertCntr); 
            } 
            if (
    vMACD1 vSignal1 && vMACD <= vSignal) {  // crossing down 
                
    vAlert true
                
    AlertCntr += 1
                
    drawShapeRelative(0vSignalShape.DOWNARROWnullColor.redImage.BOTTOM 
    Image.ONTOP
    "Alert" AlertCntr); 
            } 
        } else { 
            if ((
    vMACD1 vSignal1 && vMACD vSignal) || (vMACD1 vSignal1 && vMACD vSignal)) { 
                
    vAlert false
                
    removeShape("Alert" AlertCntr); 
            } 
        } 
        
      
    //Strategy for back testing only
        
      
    if(study.getValue(MACDStudy.HIST)>0&&study.getValue(MACDStudy.HIST,-1)<0&&Strategy.isLong()==false){
          
    Strategy.doLong("Buy",Strategy.CLOSE,Strategy.THISBAR);
      }
      if(
    study.getValue(MACDStudy.HIST)<0&&study.getValue(MACDStudy.HIST,-1)>0&&Strategy.isShort()==false){
          
    Strategy.doShort("Sell",Strategy.CLOSE,Strategy.THISBAR);
      }   
       
         
        return new Array(
    vHistvSignalvMACD); 

    Comment

    Working...
    X