Announcement

Collapse
No announcement yet.

Multiple Strategy positions

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

  • Multiple Strategy positions

    I have a script which tests 4 different strategies on an instrument. It is possible to have several positions open (and to close them on each bar).

    In the attached screen shots on 1/6/2005, I am trying to close 2 outstanding open positions and open and close a different position. The graphic representation on my graph shows what the script is trying to do, whereas the Strategy Analyser screen shot shows that the position is not being successfully closed.

    I have attached my code below. I'm not sure why this doesn't work.
    PHP Code:
    function main() {
        if (
    openstrats.length 0) {
            do {
                var 
    tmp openstrats.shift();
                var 
    tmpPos arguments[tmp];
                var 
    stillopenstrat = new Array();
            switch(
    aStrategies[tmp][0]){
                case 
    "a" :
                
    vStrat_result a(arguments);
                break;
                case 
    "b" :
                
    vStrat_result b(arguments);
                break;
                case 
    "c" :
                
    vStrat_result c(arguments);
                break;
                case 
    "d" :
                
    vStrat_result d(arguments);
                break;
                default :
                
    debugPrint("no entry ");
            }
            
    arguments[tmp] = vStrat_result;
                if (
    arguments[tmp] == tmpPos) {
                    var 
    stillopenstrat.unshift(tmp);
                }
           }
            while (
    openstrats.length 0);
            
    openstrats stillopenstrat;
        }
        for (
    0aList.lengthx++) {
            switch(
    aStrategies[x][0]){
                case 
    "a" :
                
    vStrat_result a(arguments);
                break;
                case 
    "b" :
                
    vStrat_result b(arguments);
                break;
                case 
    "c" :
                
    vStrat_result c(arguments);
                break;
                case 
    "d" :
                
    vStrat_result d(arguments);
                default :
                
    debugPrint("no entry ");
            }
            
    arguments[x] = vStrat_result;
            }
            if (
    arguments[x] == 1) {
                var 
    openstrats.push(x);
            }
            if (
    vStrat_result[3] == 1) {
                
    aList.length;
            }
        }
        return 
    null;
    }

    function 
    postMain() {
        }

    function 
    a(arguments) {
        var 
    vTraded 0;
        if (
    there is a position that should be closed) {
            
    Strategy.doCover("a"Strategy.MARKETStrategy.THISBARStrategy.DEFAULT, 0);
            
    vStrat_price=0;
            
    vStrat_stop_price=0;
            
    vPos --;
        }
        else if (
    there is a position that could be stopped) {
            
    Strategy.setStop(vStrat_stop_price);
            if (
    high() > vStrat_stop_price) {
                
    vStrat_price=0;
                
    vStrat_stop_price=0;
                
    vPos --;
            }
        }
        else if (
    a short trigger exists) {
            
    Strategy.doShort("a"Strategy.MARKETStrategy.THISBARStrategy.DEFAULT, 0);
            
    Strategy.setStop(stop_price);
            
    vStrat_price position price;
            
    vStrat_stop_price stop price;
            
    vTraded 1;
        }

        return new Array(
    vPosvStrat_pricevStrat_stop_pricevTraded);
    }   
        
    function 
    b(arguments) {
        var 
    vTraded 0;
        if (
    there is a position that should be closed) {
            
    Strategy.doCover("b"Strategy.MARKETStrategy.THISBARStrategy.DEFAULT, 0);
            
    vStrat_price=0;
            
    vStrat_stop_price=0;
            
    vPos --;
        }  
        else if (
    there is a position that could be stopped) {
            
    Strategy.setStop(vStrat_stop_price);
            if (
    high() > vStrat_stop_price) {
                
    vStrat_price=0;
                
    vStrat_stop_price=0;
                
    vPos --;
            }
        }
        else if (
    a short trigger exists) {
            
    Strategy.doShort("b"Strategy.MARKETStrategy.THISBARStrategy.DEFAULT, 0);
            
    Strategy.setStop(stop_price);
            
    vStrat_price position price;
            
    vStrat_stop_price stop price;
            }
            
    vTraded 1;
        }
                
        return new Array(
    vPosvStrat_pricevStrat_stop_pricevTraded);


    Attached Files

  • #2
    I apologise for the long code sample. I am not a very experienced coder and this could just be a problem with my code, but having used as much debugging as I can, I am confident that the Strategy.doCover is being called correctly from my code.

    Screenshort of chart attached below.
    Basically what is happening on 1/6/05 is that Strategy.doCover is being called by function a and by function b on the same bar, function d (not included in the script sample is calling Strategy.doLong and Strategy.setStop). Strategy a went short on 30/5 and remained unstopped out on 31/5 whilst strategy b went short on 31/5.

    Thanks
    Attached Files

    Comment


    • #3
      Hello ajcryan,

      In order to help answer your questions, you need to post the complete working formula example that you used to generate the back test results. In the code snippet you posted, you have invalid conditional statements in functions a and b.

      In general, the Strategy Analyzer only allows you to hold one position at a time. You can add to it and sell or cover portions of that position, but you can not hold a long and short position simultaneously. Hope that helps.
      Jason K.
      Project Manager
      eSignal - an Interactive Data company

      EFS KnowledgeBase
      JavaScript for EFS Video Series
      EFS Beginner Tutorial Series
      EFS Glossary
      Custom EFS Development Policy

      New User Orientation

      Comment

      Working...
      X