Announcement

Collapse
No announcement yet.

Basic Moving Average Backtesting issue

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

  • Basic Moving Average Backtesting issue

    I am new to the forum and to EFS, so apologies if this is very basic. All help is sincerely appreciated!

    I am using a simple moving average, long and short strategy. The problem is that when I make any parameter changes on the LONG side, i.e say I change the 'close' from (-1) to (-2), I don't see any changes in the chart when the old study is removed and the new study is applied. For that matter, even if I change the 'close' to 'open', I don't see any changes to the resultant chart when the new study is applied. However, if I comment out, the SHORT part of the code, then all changes I make are taking effect.
    What am I doing wrong?
    .....I have commented the different parts for LONG and SHORT side of the code for your reference....

    function preMain() {
    setPriceStudy(true);
    setStudyTitle("Back Test: Simple Moving Average");
    setCursorLabelName("SMA", 0);
    setDefaultBarFgColor(Color.RGB(0x00,0x94,0xFF), 0);
    }

    // Global Variables
    var xSMA = null;
    var bInit = false; // initialization flag


    function main() {

    if (getCurrentBarIndex() == 0) return;

    if(bInit == false) {

    xSMA = sma(13);
    bInit = true;
    }
    var nSma = xSMA.getValue(-1);


    if(nSma == null) return;

    // LONG Trade Signal
    if (Strategy.isLong() == false && close (-1) >= nSma ) {

    Strategy.doLong("Long Signal", Strategy.MARKET, Strategy.THISBAR);
    } else if (Strategy.isLong() == true && close(-1) < nSma ) { // Long Exit Signal

    Strategy.doSell("Long Exit Signal", Strategy.MARKET, Strategy.THISBAR);
    }
    // SHORT Trade Signal
    if (Strategy.isShort() == false && close(-1) <= nSma) {

    Strategy.doShort("Short Signal", Strategy.MARKET, Strategy.THISBAR);
    } else if (Strategy.isShort() == true && close(-1) > nSma) { // Short Exit Signal


    Strategy.doLong("Short Exit Signal", Strategy.MARKET, Strategy.THISBAR);
    }

    if(Strategy.isLong()) {
    setBarBgColor(Color.darkgreen);
    }else if(Strategy.isShort()) {
    setBarBgColor(Color.grey);
    }

    return xSMA.getValue(0);
    }

  • #2
    Re: Basic Moving Average Backtesting issue

    EFSlearners
    The reason for what you are seeing is that you are using Strategy.doLong() instead of Strategy.doCover() for your Short Exit Signal
    Alex


    Originally posted by EFSlearners
    I am new to the forum and to EFS, so apologies if this is very basic. All help is sincerely appreciated!

    I am using a simple moving average, long and short strategy. The problem is that when I make any parameter changes on the LONG side, i.e say I change the 'close' from (-1) to (-2), I don't see any changes in the chart when the old study is removed and the new study is applied. For that matter, even if I change the 'close' to 'open', I don't see any changes to the resultant chart when the new study is applied. However, if I comment out, the SHORT part of the code, then all changes I make are taking effect.
    What am I doing wrong?
    .....I have commented the different parts for LONG and SHORT side of the code for your reference....

    function preMain() {
    setPriceStudy(true);
    setStudyTitle("Back Test: Simple Moving Average");
    setCursorLabelName("SMA", 0);
    setDefaultBarFgColor(Color.RGB(0x00,0x94,0xFF), 0);
    }

    // Global Variables
    var xSMA = null;
    var bInit = false; // initialization flag


    function main() {

    if (getCurrentBarIndex() == 0) return;

    if(bInit == false) {

    xSMA = sma(13);
    bInit = true;
    }
    var nSma = xSMA.getValue(-1);


    if(nSma == null) return;

    // LONG Trade Signal
    if (Strategy.isLong() == false && close (-1) >= nSma ) {

    Strategy.doLong("Long Signal", Strategy.MARKET, Strategy.THISBAR);
    } else if (Strategy.isLong() == true && close(-1) < nSma ) { // Long Exit Signal

    Strategy.doSell("Long Exit Signal", Strategy.MARKET, Strategy.THISBAR);
    }
    // SHORT Trade Signal
    if (Strategy.isShort() == false && close(-1) <= nSma) {

    Strategy.doShort("Short Signal", Strategy.MARKET, Strategy.THISBAR);
    } else if (Strategy.isShort() == true && close(-1) > nSma) { // Short Exit Signal


    Strategy.doLong("Short Exit Signal", Strategy.MARKET, Strategy.THISBAR);
    }

    if(Strategy.isLong()) {
    setBarBgColor(Color.darkgreen);
    }else if(Strategy.isShort()) {
    setBarBgColor(Color.grey);
    }

    return xSMA.getValue(0);
    }

    Comment


    • #3
      Re: Re: Basic Moving Average Backtesting issue

      Originally posted by Alexis C. Montenegro
      EFSlearners
      The reason for what you are seeing is that you are using Strategy.doLong() instead of Strategy.doCover() for your Short Exit Signal
      Alex
      Alex,
      I posted a loooooong problem and you responded with one simple and clean line that completely solved the problem... just like a Samurai weilds his Katana....smooth and precise. Thank you so much...I am glad I am in the company of the 'Masters'.

      Comment


      • #4
        Re: Re: Re: Basic Moving Average Backtesting issue

        EFSlearners
        You are welcome
        Alex


        Originally posted by EFSlearners
        Alex,
        I posted a loooooong problem and you responded with one simple and clean line that completely solved the problem... just like a Samurai weilds his Katana....smooth and precise. Thank you so much...I am glad I am in the company of the 'Masters'.

        Comment

        Working...
        X