Announcement

Collapse
No announcement yet.

No Backtesting

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

  • No Backtesting

    Hi do you know why this code doesn't make the strategy analyzer?

    Thanks


    /*



    */

    var fpArray = new Array();
    var IsLong = false;
    var IsShort = false;


    function preMain(){

    setStudyTitle("RI_PSAR3 BT");
    setPriceStudy(true);
    setDefaultBarFgColor(Color.green, 0);
    setDefaultBarFgColor(Color.darkgreen, 1);
    setDefaultBarFgColor(Color.blue, 2);
    setDefaultBarFgColor(Color.red, 3);
    setDefaultBarFgColor(Color.purple, 4);
    setDefaultBarFgColor(Color.brown, 5);
    setPlotType(PLOTTYPE_DOT, 0);
    setPlotType(PLOTTYPE_DOT, 1);
    setPlotType(PLOTTYPE_DOT, 2);
    setPlotType(PLOTTYPE_DOT, 3);
    setPlotType(PLOTTYPE_DOT, 4);
    setPlotType(PLOTTYPE_DOT, 5);
    setDefaultBarThickness(2, 1);
    setCursorLabelName("PSAR1", 0);
    setCursorLabelName("PSAR2", 1);
    setCursorLabelName("TP", 2);
    setCursorLabelName("SL", 3);
    setCursorLabelName("EntryPr.", 4);
    setCursorLabelName("ExitPr.", 5);
    setComputeOnClose();

    var x=0;
    fpArray[x] = new FunctionParameter("psar1Int", FunctionParameter.NUMBER);
    with(fpArray[x++]){
    setName("psar1 interval");
    setLowerLimit(0);
    setDefault(240);
    }
    fpArray[x] = new FunctionParameter("psar2Int", FunctionParameter.NUMBER);
    with(fpArray[x++]){
    setName("psar2 interval");
    setLowerLimit(0);
    setDefault(20);
    }
    fpArray[x] = new FunctionParameter("startTime", FunctionParameter.STRING);
    with(fpArray[x++]){
    setName("start time");
    addOption("09:10");
    setDefault("09:10");
    }
    fpArray[x] = new FunctionParameter("finishTime", FunctionParameter.STRING);
    with(fpArray[x++]){
    setName("finish time");
    addOption("14:00");
    setDefault("14:00");
    }
    fpArray[x] = new FunctionParameter("enableTime", FunctionParameter.BOOLEAN);
    with(fpArray[x++]){
    setName("enable time");
    setDefault(false);
    }
    fpArray[x] = new FunctionParameter("ticksize", FunctionParameter.NUMBER);
    with(fpArray[x++]){
    setName("tick size");
    setLowerLimit(0);
    setDefault(1);
    }
    fpArray[x] = new FunctionParameter("tp", FunctionParameter.NUMBER);
    with(fpArray[x++]){
    setName("tp");
    setLowerLimit(0);
    //setDefault(atr(8)*2);
    setDefault(150);
    }
    fpArray[x] = new FunctionParameter("sl", FunctionParameter.NUMBER);
    with(fpArray[x++]){
    setName("sl");
    setLowerLimit(0);
    //setDefault(atr(8));
    setDefault(75);
    }
    debugClear();
    }

    var bInit = false;
    var xPSAR1;
    var xPSAR2;
    var vPSAR1;
    var vPSAR2;
    var cond1;
    var cond2;
    var cond3;
    var tpPrice;
    var slPrice;
    var lastlongsar;
    var lastshortsar;
    var lastexitbar;
    var barcnt;
    var EntryPrice;
    var ExitPrice;
    var MaxClose;
    var MinClose;

    function getPrice(price){
    if(low(0) > price) return low(0);
    if(high(0) < price) return high(0);
    return price;
    }

    function main(psar1Int, psar2Int, startTime, finishTime, enableTime, ticksize, tp, sl)
    {
    var nBarState = getBarState();

    if(!bInit)
    {
    xPSAR1 = sar(0.02, 0.02, 0.2, inv(psar1Int));
    xPSAR2 = sar(0.02, 0.02, 0.2, inv(psar2Int));
    //xPSAR1 = sar(0.02, 0.02, 0.2, inv(20));
    //xPSAR2 = sar(0.02, 0.02, 0.2, inv(20));
    barcnt = 0;
    bInit = true;
    debugClear();
    }

    if(nBarState == BARSTATE_NEWBAR)
    {

    ++barcnt;

    //condition1: bar time >= startTime
    //cond1 = true;
    cond1 = false;
    var tm = startTime.split(":");
    var dt1 = new Date(getYear(0), getMonth(0)-1, getDay(0), tm[0], tm[1], 0);
    var dtBar1 = new Date(getYear(0), getMonth(0)-1, getDay(0), getHour(0), getMinute(0), 0);
    if(dtBar1 >= dt1) cond1 = true;
    debugPrintln("cond1: "+cond1);
    /*var str = "[" + dt1 + "]" + " " + dtBar1;
    debugPrintln(str);*/


    //condition2: bar time < finishTime
    cond2 = false;
    var tm = finishTime.split(":");
    var dt2 = new Date(getYear(0), getMonth(0)-1, getDay(0), tm[0], tm[1], 0);
    var dtBar2 = new Date(getYear(0), getMonth(0)-1, getDay(0), getHour(0), getMinute(0), 0);
    if(dtBar2 < dt2) cond2 = true;
    else if(!enableTime) cond2 = true;
    debugPrintln("cond2: "+cond2);
    /*var str = "[" + dt2 + "]" + " " + dtBar2;
    debugPrintln(str);*/

    //condition3: psar1 is long
    cond3 = false;
    if(vPSAR1 < high(0)) cond3 = true;
    debugPrintln("cond3: "+cond3);

    //sar values
    //vPSAR1 = xPSAR1.getValue(0);
    //vPSAR2 = xPSAR2.getValue(0);
    //var vPSAR2old = xPSAR2.getValue(-1);

    if (vPSAR2 < high(0))
    lastlongsar = vPSAR2;
    else if (vPSAR2 > low(0))
    lastshortsar = vPSAR2;

    //var deltaShortSAR = vPSAR2 - vPSAR2old;
    //debugPrintln("sar2: "+vPSAR2);
    }

    if(!Strategy.isInTrade())
    {
    //if((IsLong == false) || (IsShort == false)){
    if(cond1 && cond2)
    {
    //long trade
    if(cond3)
    {
    if(lastshortsar != null && close(0) > lastshortsar)
    {
    var str = "";
    EntryPrice = getPrice(open(0));
    MaxClose=EntryPrice;
    if (Strategy.isShort())
    {
    Strategy.doCover("Exit Short Signal", Strategy.MARKET, Strategy.NEXTBAR , Strategy.ALL, EntryPrice);
    debugPrintln("EntryPrice: "+EntryPrice);
    //EntryPrice = getPrice(open(-1));
    str = "Cover/";
    drawShape(Shape.SQUARE, BelowBar1, Color.blue);
    }
    if(!Strategy.isLong())
    {
    Strategy.doLong("Long Signal", Strategy.LIMIT, Strategy.NEXTBAR , Strategy.DEFAULT, EntryPrice);
    debugPrintln("EntryPrice: "+EntryPrice);
    //EntryPrice = getPrice(open(-1));
    tpPrice = EntryPrice + 2*atr(psar2Int);
    slPrice = EntryPrice - atr(psar2Int);
    //tpPrice = EntryPrice + (tp*ticksize);
    //slPrice = EntryPrice - (sl*ticksize);
    //var tPrice = getMostRecentTrade();
    //sellLimit(getSymbol(), 1, tPrice + (tp*ticksize), "Idem", SB_DAY);
    //sellLimit(getSymbol(), 1, tPrice - (sl*ticksize), "Idem", SB_DAY);
    drawShape(Shape.UPTRIANGLE, BelowBar1, Color.blue);
    str += "Long";
    drawText(str, AboveBar1, Color.blue);
    }
    lastshortsar = null;
    }
    }
    //short trade
    else
    {
    if(lastlongsar != null && close(0) < lastlongsar) // longsar ha invertito
    {
    var str = "";
    EntryPrice = getPrice(open(0));
    MinClose = EntryPrice;
    if(Strategy.isLong())
    {
    Strategy.doSell("Exit Long Signal", Strategy.STOP, Strategy.NEXTBAR , Strategy.ALL, EntryPrice);
    debugPrintln("EntryPrice: "+EntryPrice);
    //EntryPrice = getPrice(lastlongsar);
    str = "Sell/";
    drawShape(Shape.SQUARE, AboveBar1, Color.red);
    }
    if(!Strategy.isShort()){
    Strategy.doShort("Short Signal", Strategy.LIMIT, Strategy.NEXTBAR , Strategy.DEFAULT, EntryPrice);
    debugPrintln("EntryPrice: "+EntryPrice);
    tpPrice = EntryPrice - 2*atr(psar2Int);
    slPrice = EntryPrice + atr(psar2Int);
    //tpPrice = EntryPrice - (tp*ticksize);
    //slPrice = EntryPrice + (sl*ticksize);
    //var tPrice = getMostRecentTrade();

    //buyLimit(getSymbol(), 1, tPrice + (sl*ticksize), "Idem", SB_DAY);
    //buyLimit(getSymbol(), 1, tPrice - (tp*ticksize), "Idem", SB_DAY);
    drawShape(Shape.DOWNTRIANGLE, AboveBar1, Color.red);
    str += "Short";
    drawText(str, BelowBar1, Color.blue);
    }
    lastlongsar = null;
    }
    }
    }
    }

    if(Strategy.isInTrade())
    {
    if(Strategy.isLong())
    { //in questa parte c'era IsLong() == true che impediva la lettura di questa parte
    if (close(0) > MaxClose)
    {
    MaxClose = close(0);
    //tpPrice = MaxClose + (tp*ticksize);
    //slPrice = MaxClose - (sl*ticksize);
    tpPrice = MaxClose + 2*atr(psar2Int);
    slPrice = MaxClose - atr(psar2Int);
    }
    if(!cond2)
    {
    lastexitbar = barcnt;
    Strategy.doSell("Exit Long Signal (time)", Strategy.CLOSE, Strategy.NEXTBAR); //
    drawShape(Shape.CIRCLE, AboveBar1, Color.black);
    }
    else if(close() >= tpPrice)
    {
    ExitPrice = getPrice(tpPrice);
    lastexitbar = barcnt;
    //var ExitPrice = getMostRecentTrade();

    //sellLimit(getSymbol(), 1, EntryPrice + (tp*ticksize), "Idem", SB_DAY);
    drawText("Sell TP", AboveBar1, Color.green);
    //Strategy.doSell("Exit Long Signal (target)", Strategy.STOP, Strategy.THISBAR , Strategy.ALL, ExitPrice);
    Strategy.doSell("Exit Long Signal (target)", Strategy.MARKET, Strategy.NEXTBAR , Strategy.ALL); //, ExitPrice);
    drawShape(Shape.CIRCLE, AboveBar1, Color.brown);
    }
    else if(close() <= slPrice)
    {
    ExitPrice = getPrice(slPrice);
    lastexitbar = barcnt;
    //var ExitPrice = getMostRecentTrade();
    //sellStop(getSymbol(), 1, EntryPrice - (sl*ticksize), "Idem", SB_DAY);
    drawText("Sell SL", BelowBar1, Color.red);
    //Strategy.doSell("Exit Long Signal (stop)", Strategy.STOP, Strategy.THISBAR , Strategy.ALL, ExitPrice);
    Strategy.doSell("Exit Long Signal (stop)", Strategy.MARKET, Strategy.NEXTBAR , Strategy.ALL); //, ExitPrice);
    drawShape(Shape.CIRCLE, AboveBar1, Color.cyan);
    debugPrintln("ExitPrice: "+ExitPrice);
    }
    }
    else if(Strategy.isShort())
    {
    if (close(0) < MinClose)
    {
    MinClose = close(0);
    //tpPrice = MinClose - (tp*ticksize);
    //slPrice = MinClose + (sl*ticksize);
    tpPrice = MinClose - 2*atr(psar2Int);
    slPrice = MinClose + atr(psar2Int);
    }
    if(!cond2)
    {
    lastexitbar = barcnt;

    //buyMarket(getSymbol(), 1, "Idem", SB_DAY); // E NON MI PLOTTA IL CLOSE SUL GRAFICO
    Strategy.doCover("Exit Short Signal (time)", Strategy.CLOSE, Strategy.NEXTBAR);
    drawShape(Shape.DIAMOND, BelowBar1, Color.black);
    }
    else if(close() <= tpPrice)
    {
    ExitPrice = getPrice(tpPrice);
    lastexitbar = barcnt;
    //var ExitPrice = getMostRecentTrade();
    //buyLimit(getSymbol(), 1, ExitPrice - (tp*ticksize), "Idem", SB_DAY);
    drawText("Cover TP", BelowBar1, Color.green);
    //Strategy.doCover("Exit Short Signal (target)", Strategy.STOP, Strategy.THISBAR , Strategy.ALL, ExitPrice);
    Strategy.doCover("Exit Short Signal (target)", Strategy.MARKET, Strategy.NEXTBAR , Strategy.ALL); //, ExitPrice);
    drawShape(Shape.DIAMOND, BelowBar1, Color.brown);
    }
    else if(close() >= slPrice){
    ExitPrice = getPrice(slPrice);
    lastexitbar = barcnt;
    //var ExitPrice = getMostRecentTrade();
    //buyLimit(getSymbol(), 1, ExitPrice + (sl*ticksize), "Idem", SB_DAY);
    drawText("Cover SL", AboveBar1, Color.red);
    //Strategy.doCover("Exit Short Signal (stop)", Strategy.STOP, Strategy.THISBAR , Strategy.ALL, ExitPrice);
    Strategy.doCover("Exit Short Signal (stop)", Strategy.MARKET, Strategy.NEXTBAR , Strategy.ALL); //, ExitPrice);
    drawShape(Shape.DIAMOND, BelowBar1, Color.cyan);
    debugPrintln("ExitPrice: "+ExitPrice);
    }
    }
    }
    else
    {
    if(barcnt > lastexitbar)
    {
    EntryPrice = null;
    ExitPrice = null;
    tpPrice = null;
    slPrice = null;
    }
    }
    vPSAR1 = xPSAR1.getValue(0);
    vPSAR2 = xPSAR2.getValue(0);
    debugPrintln("sar1: "+vPSAR1+"-sar2: "+vPSAR2+"-tp: "+tpPrice+"-sl: "+slPrice);
    return new Array(vPSAR1, vPSAR2, tpPrice, slPrice, EntryPrice, ExitPrice);
    }

    function dbmsg(msg){
    var dt = new Date(getYear(0),getMonth(0)-1,getDay(0),getHour(0),getMinute(0),0);
    var str = "[" + dt + "]" + " " + msg;
    debugPrintln(str);
    return;
    }

  • #2
    Wish I could help more, but I don't have the time. You need to start using debugPrintln statements to verify that your conditional variables and logic controls are allowing the trades to fire. Also, you should stick with either MARKET or LIMIT type orders to make things easier.

    If you need more help, PM me.
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #3
      hello everybody,
      trying to solve this question, I commented out the "inv" function inside the sar study, and it seems that backtesting is executing correctly. I think that however the logic behind this script should be completly messed up by a thing like this.

      Is there anyone there that has already fallen into similar problem?

      Thanks

      Comment

      Working...
      X