Announcement

Collapse
No announcement yet.

closing a trade after a number of days

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

  • closing a trade after a number of days

    Hello,

    I'm using Formula Wizard to back test a simple price based strategy, I need the strategy to close after lets say 10 days (or after a given number of bars), is this possible?

  • #2
    eetest
    It is possible but you will not be able to code the required logic using the Formula Wizard alone
    Alex

    Comment


    • #3
      Ok, What do I need to add to the code then?

      Comment


      • #4
        eetest
        The first thing you would need to do is create a global variable (ie declared outside of function main) called for example vCounter and set it to 0 ie var vCounter=0;
        Then in main you would add one of the following two examples depending if you want to count days or bars.

        PHP Code:
        //if counting days
            
        if(getDay(0)!=getDay(-1)&&getBarState()==BARSTATE_NEWBAR){//if new day and new bar
                
        vCounter++;//increase the counter by 1
            
        }
            
        //if instead counting bars
            
        if(getBarState()==BARSTATE_NEWBAR){//if new bar
                
        vCounter++;//increase counter by 1
            

        Following that you would have your trading conditions/logic to which you add the command to reset the counter to 0 when you enter a trade. At that point you verify if the counter is at the desired value at which point you implement your exit strategy (see following example)
        Alex

        PHP Code:
        if(!Strategy.isLong()){//if not long
                
        if(your condition==true){//if condition to go long is true
                    
        Strategy.doLong("Long",...etc)//go long
                    
        vCounter=0;//reset the counter
                    //other commands if required
                
        }
            }
            if(
        vCounter==your number of days/bars){//if counter is at the desired value
                
        if(Strategy.isLong(){//if long
                    
        Strategy.doSell("Sell"...etc);//sell
                
        }
            } 

        Comment

        Working...
        X