Announcement

Collapse
No announcement yet.

Backtesting and Strategy Analyzer

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

  • Backtesting and Strategy Analyzer

    Hello,

    I have the problem the "Strategy Analyzer" do not give me the right results. In the EFS-Code below I can display the entry candels and exit candels at the "Formula Output" but the Strategy Analyzer does not make the entries and exits like are displayed at the "Formula Output". I don't understand why the Strategy Analyzer make a Long-Short Position every 5 minutes (see backtest1.jpg). My aim is it to get only the entry the exit candels(times) like they are in the efs - code. Maybe I am doing something wrong regarding the Strategy Analyzer Settings?!

    Thanks for help.

    regards

    Roland


    CODE
    ------------------------------------------------
    var setcolor = true;
    var aver = null;
    var diffprev = null;
    var diff0 = null;

    var allhigh=null;
    var alllow=null;
    var highprev=null;
    var lowprev=null;

    var emastudy8 = ema(8);
    var emastudy21 = ema(21);
    var EMA8_0=null;
    var EMA21_0=null;

    var EMA_signal = false;
    var close1 = null;
    var close2 = null;

    function preMain(){
    setStudyTitle("Backtesting3");
    setPriceStudy(true);
    setColorPriceBars(true);
    setDefaultPriceBarColor(Color.grey);
    setShowCursorLabel(false);

    var iAlert = new FunctionParameter("iAlert", FunctionParameter.BOOLEAN);
    iAlert.setDefault( true );

    var iDecimals = new FunctionParameter("iDecimals", FunctionParameter.NUMBER);
    iDecimals.setDefault( 2 );
    iDecimals.setName("Round to X decimals");


    var iSymbol = new FunctionParameter("iSymbol", FunctionParameter.STRING);
    iSymbol.setDefault( "AAPL" );


    var iButtonX = new FunctionParameter("iButtonX", FunctionParameter.NUMBER);
    iButtonX.setDefault( 150 );

    var iButtonY = new FunctionParameter("iButtonY", FunctionParameter.NUMBER);
    iButtonY.setDefault( 95 );


    var iFontSize = new FunctionParameter("iFontSize", FunctionParameter.NUMBER);
    iFontSize.setName("Button Font Size");
    iFontSize.setDefault( 10 );

    }

    function main( iAlert, iDecimals, iFontSize, iButtonX, iButtonY ) {

    if (getBarState() == BARSTATE_NEWBAR)
    drawTextPixel(iButtonX, iButtonY, " TREND_ALERT @URL=EFS:editParameters", Color.white, Color.green,
    Text.RELATIVETOLEFT|Text.RELATIVETOBOTTOM|Text.ONT OP|Text.BOLD|Text.BUTTON,
    "Comic Sans MS", 13, "UpExp");

    xSymbol1 = getSymbol().toUpperCase() ;

    vSymbol1 = xSymbol1 +","+ "D";

    var vHigh = high();
    var vLow = low();
    var vClose = close();
    var open0 = open(0);
    var close0 = close(0);
    var high0 = high(0);
    var low0 = low(0);
    EMA8_0 = emastudy8.getValue(0);
    EMA21_0 = emastudy21.getValue(0);

    if(vHigh == null || vLow == null) {
    return;
    }

    if (EMA_signal == false) {
    debugPrintln (EMA_signal);
    if (EMA8_0 >= EMA21_0) {
    setPriceBarColor(Color.lime)
    debugPrintln ("UP - "+close(0));
    if ((Strategy.isInTrade() == true) && (Strategy.isShort() == true)) {
    Strategy.doCover("Close Short Position",Strategy.CLOSE,Strategy.THISBAR); // Exits prior SHORT trade
    debugPrintln ("Close Short!!!");
    }
    Strategy.doLong("Crossing Up",Strategy.CLOSE,Strategy.THISBAR); // Enters new LONG trade

    EMA_signal = true;
    }

    }


    if (EMA_signal == true) {
    debugPrintln (EMA_signal);
    if (EMA8_0 < EMA21_0) {
    setPriceBarColor(Color.red)
    debugPrintln ("DOWN - "+close(0));
    if ((Strategy.isInTrade() == true) && (Strategy.isLong() == true)) {
    Strategy.doSell("Close Long Position",Strategy.CLOSE,Strategy.THISBAR); // Exits prior LONG trade
    debugPrintln ("Close Long!!!");
    }

    Strategy.doShort("Crossing Down",Strategy.CLOSE,Strategy.THISBAR); // Enters new SHORT trade

    EMA_signal = false;
    }

    }


    return;
    }
    function editParameters() {
    askForInput(" TREND_ALERT ");
    return;
    }

    //==rnd will round to N digits.
    function rnd(value, N) {
    var n;
    var mult=1;
    for(n=0;n<N;n++) mult*=10;
    value*=mult;
    return Math.round( value,N)/mult;
    }
    --------------------------------------------------
    Attached Files

  • #2
    Hi Roland,

    I suspect the problem is your ema series aren't stable yet and your if statements that compare the values are always returning true.

    For the 5 minute issue, I suspect you are running on a 5 minute chart, so you get new trades at the beginning of each bar.

    Add a check to see if your ema series are stable by making the following change to your code:

    if(vHigh == null || vLow == null ||
    EMA8_0 == null || EMA21_0 == null) {
    return;
    }


    The other change I would suggest is use an if/else block for your EMA_signal check.
    if(EMA_signal) {
    // do true processing
    }
    else
    {
    // do false processing
    }

    This will prevent the problem where you enter and exit a long trade on the same bar.

    Steve

    Comment

    Working...
    X