Not logged in
Viewing 9 posts - 1 through 9 (of 9 total)

Forums>Quant Analyzer (formerly named EA Analyzer)>Extending the program>What-If: Exclude trades on the first Friday of each month.

  • #114463|
    Customer
    484 Posts

    Hi,

     

    Is it possible to create a What-if scenario that excludes the trades on the 1st Friday of each month?

    #134133
    Customer
    70 Posts

    Hello notch,

     

    yes, it is possible to do that.

     

    It is quite simple. Paste your code and I will check it ;)

     

    Idea:

    String lastMonth = null, currentMonth;
    
    for(Iterator i = originalOrders.listIterator(); i.hasNext();) {
      SQOrder order = i.next(); 	
    
      SQTime dateTime = new SQTime(order.OpenTime); //order.CloseTime
    
      currentMonth = dateTime.toString("MM.yyyy");
    
      int day = dateTime .getDayOfWeekOriginal(); //0-6
      if((lastMonth==null || !lastMonth.equals(currentMonth)) && day==5) { 
        i.remove();
      }
    
      lastMonth = currentMonth;
    }
    

    Tomas

    #134135
    Customer
    484 Posts

    Hello notch,

     

    yes, it is possible to do that.

     

    It is quite simple. Paste your code and I will check it ;)

     

    Idea:

    String lastMonth = null, currentMonth;
    
    for(Iterator i = originalOrders.listIterator(); i.hasNext();) {
      SQOrder order = i.next(); 	
    
      SQTime dateTime = new SQTime(order.OpenTime); //order.CloseTime
    
      currentMonth = dateTime.toString("MM.yyyy");
    
      int day = dateTime .getDayOfWeekOriginal(); //0-6
      if((lastMonth==null || !lastMonth.equals(currentMonth)) && day==5) { 
        i.remove();
      }
    
      lastMonth = currentMonth;
    }
    

    Tomas

     

    Thank you.  The code snippet will be very useful.

    #134825
    Customer
    484 Posts

    Hello notch,

     

    yes, it is possible to do that.

     

    It is quite simple. Paste your code and I will check it ;)

     

    Idea:

    String lastMonth = null, currentMonth;
    
    for(Iterator i = originalOrders.listIterator(); i.hasNext();) {
      SQOrder order = i.next(); 	
    
      SQTime dateTime = new SQTime(order.OpenTime); //order.CloseTime
    
      currentMonth = dateTime.toString("MM.yyyy");
    
      int day = dateTime .getDayOfWeekOriginal(); //0-6
      if((lastMonth==null || !lastMonth.equals(currentMonth)) && day==5) { 
        i.remove();
      }
    
      lastMonth = currentMonth;
    }
    

    Tomas

     

    Hi Tomas,

     

    I failed!!!  Please can you provide a snippet that excludes trades on the 1st Friday of each month?

    #134859
    Customer
    70 Posts

    Hello notch,

     

    did you try to embed my code above into the snippet?

     

    What is a problem exactly?

     

    Do you need a help how to create a snippet?

    https://strategyquant.com/doc/article/getting-started-extending-the-programs.html

     

    Best regards,

    Tomas

    #134881
    Customer
    484 Posts

    Hello notch,

     

    did you try to embed my code above into the snippet?

     

    What is a problem exactly?

     

    Do you need a help how to create a snippet?

    https://strategyquant.com/doc/article/getting-started-extending-the-programs.html

     

    Best regards,

    Tomas

     

    Hi Tamas,

     

    I tried the snippet and it was useful but it doesn’t allow me to exclude the first Friday of each month from my strategies.

     

    Cheers,

    Notch

    #134916
    Customer
    70 Posts

    Hello Notch,

     

    I have adjusted the snippet. Please find it bellow.

    Please notice that I added some debuge messages so you can check if it works correctly for you.

     

    Run the What if and then open the Log console in the top menu of QA.

     

    Tomas

    /*
     * Copyright (c) 2015, StrategyQuant - All rights reserved.
     *
     * Code in this file was made in a good faith that it is correct and does what it should. 
     * If you found a bug in this code OR you have an improvement suggestion OR you want to include 
     * your own code snippet into our standard library please contact us at:
     * http://tasks.strategyquant.com/projects/snippets/
     *
     * This code can be used only within StrategyQuant products. 
     * Every owner of valid (free, trial or commercial) license of any StrategyQuant product 
     * is allowed to freely use, copy, modify or make derivative work of this code without limitations,
     * to be used in all StrategyQuant products and share his/her modifications or derivative work 
     * with the StrategyQuant community.
     *  
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
     * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
     * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES 
     * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     *  
     */
    package com.strategyquant.extend.WhatIf;
    
    import java.util.Iterator;
    
    import com.strategyquant.lib.debugging.DebugConsole;
    import com.strategyquant.lib.language.L;
    import com.strategyquant.lib.results.SQOrder;
    import com.strategyquant.lib.results.SQOrderList;
    import com.strategyquant.lib.snippets.WhatIf;
    import com.strategyquant.lib.time.SQTime;
    
    public class ExcludeFirstFridayOfEachMonth extends WhatIf {
    
    	public ExcludeFirstFridayOfEachMonth() {
    		setName(L.t("Exclude first Friday of each month"));
    
    		setFormatedName(L.t("Exclude first Friday of each month"));
    	}
    
    	/**
    	 * Function receives list of all orders sorted by open time and it could manipulate
    	 * the list and remove any order that matches certain filter from the list.
    	 *
    	 * Order structure is available in the documentation here:
    	 * https://strategyquant.com/doc/api/com/strategyquant/lib/results/SQOrder.html
    	 *
    	 * @param originalOrders - list of original orders that can be changed. Each order has the order properties specified above
    	 */
    	@Override
    	public void filter(SQOrderList originalOrders) throws Exception {
    		String lastMonth = null, currentMonth;
    
    		int removed = 0;
    		
    		for(Iterator i = originalOrders.listIterator(); i.hasNext();) {
    		  SQOrder order = i.next(); 	
    
    		  SQTime dateTime = new SQTime(order.OpenTime); //order.CloseTime
    
    		  currentMonth = dateTime.toString("MM.yyyy");
    
    		  int day = dateTime.getDayOfWeekOriginal(); //0-6
    		   
    		  if((lastMonth==null || !lastMonth.equals(currentMonth)) && day==5) { 
    		    i.remove();
    		    
    		    DebugConsole.log("ExcludeFirstFridayOfEachMonth", "######REMOVED###### trade: "+order.Order+" "+dateTime.toDateString()+" "+dayToString(day)); //debug message
    		    removed++;
    		    
    		    lastMonth = currentMonth;
    		  } else {
    			DebugConsole.log("ExcludeFirstFridayOfEachMonth", "Trade: "+order.Order+" "+dateTime.toDateString()+" "+dayToString(day)); //debug message
    		  }
    		}
    		
    		DebugConsole.log("ExcludeFirstFridayOfEachMonth", "Total removed : "+removed); //debug message
    	}
    	
    	private String dayToString(int day) {
    		//0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday
    		switch(day) {
    			case 0: return "Sunday";
    			case 1: return "Monday";
    			case 2: return "Tuesday";
    			case 3: return "Wednesday";
    			case 4: return "Thursday";
    			case 5: return "Friday";
    			case 6: return "Saturday";
    			
    			default: return "?";
    		}
    	}
    }
    #135040
    Customer
    484 Posts

    I didn’t get around to finishing it but this was one of my attempts:

    package com.strategyquant.extend.WhatIf;
    
    import java.util.Iterator;
    import com.strategyquant.lib.snippets.WhatIf;
    import com.strategyquant.lib.results.SQOrderList;
    import com.strategyquant.lib.results.SQOrder;
    import com.strategyquant.lib.time.SQTime;
    
    public class RemoveNFPTrades extends WhatIf {
    
       public RemoveNFPTrades() {
          setName("Remove NFP Trades");
    
          //addIntParameter("_PARAMETER_", "Parameter", 20, 2, 1000, 1);
    
            // set name of this method that will be displayed with parameter(s)
          //setFormatedName("What If rule with parameter _PARAMETER_");
       }
       
       /**
        * Function receives list of all orders sorted by open time and it could manipulate 
        * the list and remove any order that matches certain filter from the list.     
        * 
        * Order structure is available in the documentation here:
        * https://strategyquant.com/doc/api/com/strategyquant/lib/results/SQOrder.html
        *
        * @param originalOrders - list of original orders that can be changed. Each order has the order properties specified above
        */    
       @Override
       public void filter(SQOrderList originalOrders) throws Exception {
          //int parameter = getIntParameterValue("_PARAMETER_");
       
       
       for (int y = 2003; y >= 2015; y++){
       
       int lastMonth1 = -1,lastMonth2= -1,lastMonth3= -1,lastMonth4= -1,lastMonth5= -1,lastMonth6= -1,lastMonth7= -1,
       lastMonth8= -1,lastMonth9= -1 ,lastMonth10= -1 ,lastMonth11= -1 ,lastMonth12= -1 , currentMonth;
       
          for(Iterator i = originalOrders.listIterator(); i.hasNext();) {
             SQOrder order = i.next();
    
               SQTime dateTime = new SQTime(order.OpenTime); //order.CloseTime
               currentMonth = dateTime.getMonth();
               int day = dateTime .getDayOfWeekOriginal(); //0-6
             // todo - your custom action
             // orders can be skipped or manipulated here
    
             switch (currentMonth){
                case 0:
                     
                     if((lastMonth1==-1) && (day==5)) { 
                     i.remove();
                     lastMonth1 = currentMonth;
      }
                case 1:
                     if((lastMonth2==-1) && (day==5)) { 
                     i.remove();
                     lastMonth2 = currentMonth;
                     }
                case 2:
                     if((lastMonth3==-1) && (day==5)) { 
                     i.remove();
                     lastMonth3 = currentMonth;
                     }
                case 3:
                     if((lastMonth4==-1) && (day==5)) { 
                     i.remove();
                     lastMonth4 = currentMonth;
                     }
                case 4:
                     if((lastMonth5==-1) && (day==5)) { 
                     i.remove();
                     lastMonth5 = currentMonth;
                     }
                case 5:
                     if((lastMonth6==-1) && (day==5)) { 
                     i.remove();
                     lastMonth6 = currentMonth;
                     }
                case 6:
                     if((lastMonth7==-1) && (day==5)) { 
                     i.remove();
                     lastMonth7 = currentMonth;
                     }
                case 7:
                     if((lastMonth8==-1) && (day==5)) { 
                     i.remove();
                     lastMonth8 = currentMonth;
                     }
                case 8:
                     if((lastMonth9==-1) && (day==5)) { 
                     i.remove();
                     lastMonth9 = currentMonth;
                     }
                case 9:
                     if((lastMonth10==-1) && (day==5)) { 
                     i.remove();
                     lastMonth10 = currentMonth;
                     }
                case 10:
                     if((lastMonth11==-1) && (day==5)) { 
                     i.remove();
                     lastMonth11 = currentMonth;
                     }
                case 11:
                     if((lastMonth12==-1) && (day==5)) { 
                     i.remove();
                     lastMonth12 = currentMonth;
                     }
             }
           
                
     if( day==5) { 
        i.remove();
      }
           
          }  
       }     
       }
    }
    
    #135047
    Customer
    70 Posts

    Hello notch,

     

    take a look how to use ‘switch’ in JAVA. You miss a break command in your statements.

    https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

     

    From the link

    Another point of interest is the break statement. Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following theswitch block. The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered. 

     

    Another mistake is a  code at the end of the loop

    if( day==5) {

    i.remove();
    }

     

    It removes each trade on Friday not only the first one.

     

    Sincerely,

    Tomas

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.