Announcement

Collapse
No announcement yet.

MACD Trading Backtest Not Backtesting.

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

  • MACD Trading Backtest Not Backtesting.

    Hi guys,

    Can someone help me with regards to a backtested strategy which is not executing as it should in the wizard.

    Briefly, the model is based upon MACD crossovers and MA wth RSI oscilators for EUR/USD.

    The first trade executes according to the wizard. However the second trade ignores the take profit and the stop levels and continues in an open position until the end of the back test where the position is closed out.

    The code is as follows.

    //{{EFSWizard_Description
    //
    // This formula was generated by the Alert Wizard
    //
    //}}EFSWizard_Description


    //{{EFSWizard_Declarations
    var vMACD12_26 = new MACDStudy(12, 26, 9, "Close", true);
    var vRSI14 = new RSIStudy(14, "Close");
    var vRSI10 = new RSIStudy(10, "Close");
    var vEMA12 = new MAStudy(12, 0, "Close", MAStudy.EXPONENTIAL);
    var vEMA26 = new MAStudy(26, 0, "Close", MAStudy.EXPONENTIAL);
    var vEMA40 = new MAStudy(40, 0, "Close", MAStudy.EXPONENTIAL);
    var vLastAlert = -1;
    //}}EFSWizard_Declarations


    function preMain() {
    //{{EFSWizard_Code_PreMain_setPriceBarColor
    setColorPriceBars(true);
    //}}EFSWizard_Code_PreMain_setPriceBarColor
    /**
    * This function is called only once, before any of the bars are loaded.
    * Place any study or EFS configuration commands here.
    */
    //{{EFSWizard_PreMain
    setPriceStudy(true);
    setStudyTitle("MACD Trading Strategy Backtest");
    //}}EFSWizard_PreMain

    }

    function main() {
    /**
    * The main() function is called once per bar on all previous bars, once per
    * each incoming completed bar, and if you don't have 'setComputeOnClose(true)'
    * in your preMain(), it is also called on every tick.
    */

    //{{EFSWizard_Expressions
    //{{EFSWizard_Expression_1
    if (
    Strategy.isInTrade() == true &&
    Strategy.isLong() == true &&
    vRSI10.getValue(RSIStudy.RSI) > 70
    ) onAction1()
    //}}EFSWizard_Expression_1

    //{{EFSWizard_Expression_2
    else if (
    Strategy.isInTrade() == true &&
    Strategy.isShort() == true &&
    vRSI10.getValue(RSIStudy.RSI) < 30
    ) onAction2()
    //}}EFSWizard_Expression_2

    //{{EFSWizard_Expression_3
    else if (
    Strategy.isInTrade() == false &&
    Strategy.isLong() == false &&
    vMACD12_26.getValue(MACDStudy.SIGNAL) < vMACD12_26.getValue(MACDStudy.MACD) &&
    vRSI14.getValue(RSIStudy.RSI) < 70 &&
    close() > vEMA12.getValue(MAStudy.MA)
    ) onAction3()
    //}}EFSWizard_Expression_3

    //{{EFSWizard_Expression_4
    else if (
    Strategy.isInTrade() == false &&
    Strategy.isShort() == false &&
    vMACD12_26.getValue(MACDStudy.SIGNAL) > vMACD12_26.getValue(MACDStudy.MACD) &&
    vRSI14.getValue(RSIStudy.RSI) > 30 &&
    close() < vEMA12.getValue(MAStudy.MA)
    ) onAction4();
    //}}EFSWizard_Expression_4

    //}}EFSWizard_Expressions


    //{{EFSWizard_Return
    return null;
    //}}EFSWizard_Return

    }

    function postMain() {
    /**
    * The postMain() function is called only once, when the EFS is no longer used for
    * the current symbol (ie, symbol change, chart closing, or application shutdown).
    */
    }

    //{{EFSWizard_Actions
    //{{EFSWizard_Action_1
    function onAction1() {
    if (vLastAlert != 1) Strategy.doSell("Take Profit", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT, 0);
    if (vLastAlert != 1) setPriceBarColor(Color.RGB(0,0,128));
    vLastAlert = 1;
    }
    //}}EFSWizard_Action_1

    //{{EFSWizard_Action_2
    function onAction2() {
    if (vLastAlert != 2) Strategy.doCover("Take Cover", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT, 0);
    if (vLastAlert != 2) setPriceBarColor(Color.RGB(128,128,0));
    vLastAlert = 2;
    }
    //}}EFSWizard_Action_2

    //{{EFSWizard_Action_3
    function onAction3() {
    if (vLastAlert != 3) setPriceBarColor(Color.RGB(0,255,0));
    if (vLastAlert != 3) Strategy.doLong("Enter Long", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT, 0.005);
    if (vLastAlert != 3) drawShapeRelative(0, high(), Shape.UPARROW, "", Color.RGB(0,255,0), Shape.BOTTOM);
    if (vLastAlert != 3) Strategy.setStop(low()==vEMA40.getValue(MAStudy.MA ,0,-1));
    vLastAlert = 3;
    }
    //}}EFSWizard_Action_3

    //{{EFSWizard_Action_4
    function onAction4() {
    if (vLastAlert != 4) setPriceBarColor(Color.RGB(255,0,0));
    if (vLastAlert != 4) drawShapeRelative(0, low(), Shape.DOWNARROW, "", Color.RGB(255,0,0), Shape.TOP);
    if (vLastAlert != 4) Strategy.doShort("Enter Short Position", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT, 0);
    if (vLastAlert != 4) Strategy.setStop(high()==vEMA40.getValue(MAStudy.M A,0,-1));
    vLastAlert = 4;
    }
    //}}EFSWizard_Action_4

    //}}EFSWizard_Actions

    I think it may have something to do with the structure of the wizard, in that it reads from left to right. But I can not figure out where the problem lies itself.

    Many thanks
    Harry

  • #2
    Harry
    In both instances of Strategy.setStop() you are using an invalid parameter. This should be Strategy.setStop(nValue) where nValue is the value at which you want to set the stop.
    If this value is for example the current Low minus some constant then you would use low()-0.0005 or if it is the prior Low you would use low(-1) etc. If instead this is based on the moving average then you would use vEMA40.getValue(MAStudy.MA) for the current value or vEMA40.getValue(MAStudy.MA,-1) for the prior value, etc.
    Also you are missing a default color for the price bar which is required when painting price bars. See this thread for an explanation of how to implement a default color in the Formula Wizard
    Alex

    Comment

    Working...
    X