Announcement

Collapse
No announcement yet.

GetTime on backtests

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

  • GetTime on backtests

    Just working on backtests using the formula wizard and trying to work with getTIme to determine times of trades.. having some problem making it work correctly. As in the extract below.. in the wizard I add lines to select the date to == 26 in this case for yesterday... the hour to be >= 14 (my local time ) and minute >=35

    now what that does is allow any trades after 14.35 which is what I want... but it also does NOT allow any trades that the minutes are not past 35 also.. eg a trade is alowed at 16.40 but not 16.12.. how do I get it to allow trades at ANY time past 14.35? Can that be done in the wizard or do I need to edit separaetly in editor?


    if (
    vEMA20.getValue(MAStudy.MA) > vEMA50.getValue(MAStudy.MA) &&
    getDay() == 26 &&
    getHour() >= 14 &&
    getMinute() >= 35
    ) onAction1()
    Last edited by Neil; 08-27-2004, 01:56 AM.

  • #2
    Neil
    Try the following (you can do it in the Formula Wizard also if you want)
    Alex

    PHP Code:
    if (
    vEMA20.getValue(MAStudy.MA) > vEMA50.getValue(MAStudy.MA) &&
    getDay() == 26 &&
    (
    getHour()*100)+getMinute() >= 1435
    onAction1() 

    Comment


    • #3
      Thanks again Alex that has sorted it fine.. just one other question.. if I want to test a single day.. such as the 25th and check the box in the backtest tool it always closes the last trade at the end of the chart period.. ie the 26th in this case.. do I have to set a close at end of day (the 25th) in the code and uncheck that box?

      I tried unchecking to see the result on the 25th and it did its last trade on the 25th but for some reason the last cover was at 20.20 (close being 21.00 for me)...... just tried that on another and its last sell was at 20.53... same stock on another time chart at 20.41.... not at the xover point either, so guess I have to try and understand why that is happening... getting closer anyway..

      Comment


      • #4
        Neil
        Click here and look at the efs I wrote to limit the back testing period.
        Alex

        Comment


        • #5
          Neil
          To answer your questions yes you do need to close the trade(s) in the efs itself. You then do not need to uncheck the box as the condition to enter a new trade would be limited by the if(getDay()==25) condition.
          As to your second issue were those the last bars on the 25th?
          You may want to post the efs and indicate on what symbol/interval you were getting those results
          Alex

          Comment


          • #6
            Looking at your trade permissions efs I added this below to the function main and that works perfectly... am now trying to figure if can make the input for the date to work via the edit study from the chart so is easy to change the date rather than from within the efs editor.. I am sure the answer lies within your efs



            if((getHour()*100)+getMinute() == 2055){
            if(Strategy.isLong()==true){
            Strategy.doSell("Closeout Long(s)",Strategy.STOP,Strategy.THISBAR,Strategy.A LL,close(-1));
            }
            if(Strategy.isShort()==true){
            Strategy.doCover("Closeout Short(s)",Strategy.STOP,Strategy.THISBAR,Strategy. ALL,close(-1));
            }

            Comment


            • #7
              Neil
              The conditions are in the section of main shown below with explanations
              Hope this helps
              Alex

              PHP Code:
              var vTrade 1//this flag is set to 1 ie trades are allowed
              var vYear,vMonth,vDay//declaring the variables

                  
              vYear=getYear(0,-1)+""//define vYear and turn it into a string
                  
              vMonth=getMonth()+"";  //define vMonth and turn it into a string
                      
              if(vMonth<10)  //if the Month is less than 10
                          
              vMonth 0+vMonth//add a 0 in front of the number - see Notes below
                  
              vDay getDay()+""//define vDay and turn it into a string
                      
              if(vDay<10//if the Date is less than 10
                          
              vDay 0+vDay//add a 0 in front - see Notes below
                  
              if(Switch=="On"){ //if the parameter Switch in Edit Studies is "On"
                      
              if(Start!=null&&End==nullEnd=vYear+vMonth+vDay//if Start and End are null ie undefined then
                                                                        //End is equal to the three variables added together
                                                                        //Because they are strings and not numbers they will
                                                                        //be concatenated rather than added
                          
              if((vYear+vMonth+vDay)<Start || (vYear+vMonth+vDay)>End){//if the concatenated value is less than
                                                                                   //Start or greater than End
                              
              vTrade 0;//set the flag vTrade to 0 ie trades are not allowed
                      
              }
                  }
              //Notes: 
              //The reason for adding a 0 is to ensure that the dates will always be in the correct sequence
              //If I did not do that then 200452 (May 2, 2004) would be an earlier date than 20031025 (October 10, 2003)
              //because 200452 is a smaller value than 20031025. If instead I add a 0 when required then 
              //20040502 is a larger value than 20031025 therefore it can only come afterwards when used as a date 

              Comment


              • #8
                Alex, perhaps you can help with this problem now.. everything is working fine from the help you gave before except I am still getting missed last trade of day for some reason. It seems to do all trades fine until the last one and then closes at the xover before eod.. ie if there are 6 xovers shown on my chart for a particular day and the last one begins at 19.55 (my time) the backtest will show that as the close of the last trade... doing just 5 trades.. I will paste in the wizard generated efs below and see if you can figure out what I am doing wrong.. (this one is set for the 21st)

                thanks as always
                Neil



                //{{EFSWizard_Description
                //
                // This formula was generated by the Alert Wizard
                //
                //}}EFSWizard_Description 7532


                //{{EFSWizard_Declarations

                var vEMA20 = new MAStudy(20, 0, "Close", MAStudy.EXPONENTIAL);
                var vEMA50 = new MAStudy(50, 0, "Close", MAStudy.EXPONENTIAL);
                var vLastAlert = -1;

                //}}EFSWizard_Declarations 17716


                function preMain() {
                //{{EFSWizard_Code_PreMain_setPriceBarColor
                setColorPriceBars(true);
                //}}EFSWizard_Code_PreMain_setPriceBarColor 3448
                /**
                * This function is called only once, before any of the bars are loaded.
                * Place any study or EFS configuration commands here.
                */
                //{{EFSWizard_PreMain
                setPriceStudy(true);
                setStudyTitle("Range Xover");
                setCursorLabelName("Line1", 0);
                setCursorLabelName("Line2", 1);
                setDefaultBarStyle(PS_SOLID, 0);
                setDefaultBarStyle(PS_SOLID, 1);
                setDefaultBarFgColor(Color.blue, 0);
                setDefaultBarFgColor(Color.red, 1);
                setDefaultBarThickness(2, 0);
                setDefaultBarThickness(2, 1);
                setPlotType(PLOTTYPE_LINE, 0);
                setPlotType(PLOTTYPE_LINE, 1);
                //}}EFSWizard_PreMain 52678

                }

                function main() {
                /**
                * The main() function is called once per bar on all previous bars, once per
                * each incoming completed bar, and if you don't have 'setComputeOnClose(true)'
                * in your preMain(), it is also called on every tick.
                */

                //{{EFSWizard_Expressions
                //{{EFSWizard_Expression_1
                if (
                vEMA20.getValue(MAStudy.MA) > vEMA50.getValue(MAStudy.MA) &&
                getDay() == 21 &&
                (getHour()*100)+getMinute() >= 1435 &&
                (getHour()*100)+getMinute() <= 2055
                ) onAction1()
                //}}EFSWizard_Expression_1 25231

                //{{EFSWizard_Expression_2
                else if (
                vEMA20.getValue(MAStudy.MA) < vEMA50.getValue(MAStudy.MA) &&
                getDay() == 21 &&
                (getHour()*100)+getMinute() >= 1435 &&
                (getHour()*100)+getMinute() <= 2055
                ) onAction2();
                //}}EFSWizard_Expression_2 25613

                //}}EFSWizard_Expressions 77509


                //{{EFSWizard_Return
                return new Array(
                vEMA20.getValue(MAStudy.MA),
                vEMA50.getValue(MAStudy.MA)
                );
                //}}EFSWizard_Return 12662

                }

                function postMain() {
                /**
                * The postMain() function is called only once, when the EFS is no longer used for
                * the current symbol (ie, symbol change, chart closing, or application shutdown).
                */
                }

                //{{EFSWizard_Actions
                //{{EFSWizard_Action_1
                function onAction1() {
                if (vLastAlert != 1) Strategy.doLong("", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT, 0);
                setPriceBarColor(Color.RGB(255,0,0));
                if (vLastAlert != 1) Alert.playSound("C:\\eSignal\\Sounds\\Buzz.wav");
                vLastAlert = 1;
                }
                //}}EFSWizard_Action_1 32380

                //{{EFSWizard_Action_2
                function onAction2() {
                if (vLastAlert != 2) Strategy.doShort("", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT, 0);
                setPriceBarColor(Color.RGB(255,0,0));
                if (vLastAlert != 2) Alert.playSound("C:\\eSignal\\Sounds\\Buzz.wav");
                vLastAlert = 2;
                }
                //}}EFSWizard_Action_2 33266

                //}}EFSWizard_Actions 85983

                Comment


                • #9
                  Neil
                  What time zone are you in (or what times do the ones in the efs correspond to in Eastern Standard)?
                  Also with what symbol/interval did you have the issue you mentioned and how is the Time Template set in that chart?
                  Alex

                  Comment


                  • #10
                    Originally posted by Alexis C. Montenegro
                    Neil
                    What time zone are you in (or what times do the ones in the efs correspond to in Eastern Standard)?
                    Also with what symbol/interval did you have the issue you mentioned and how is the Time Template set in that chart?
                    Alex
                    UK time and is on any stock that it happens and the interval is 1 min and the time template is set for normal market hours.. in my case 1430 to 2100

                    Comment


                    • #11
                      Neil
                      I ran a few tests and everything seems to work fine.
                      In the image below for example you can find the backtest for INTC on a 1 minute chart. I can count 8 crossovers on the chart for yesterday and that is the same that is being reported by the Strategy Analyzer.
                      Alex

                      Comment


                      • #12
                        Alex in that test it closes on the 22nd.. try running it with the close at end of test button unchecked and see what happens

                        Comment


                        • #13
                          Neil
                          In my understanding the Strategy Analyzer does not leave a trade open-ended hence if you uncheck the Close Trades at end of Back Test option it will only consider trades that are completed within the conditions you have defined.
                          In this case the last short on the 21st would be open-ended as there are no provisions to close a trade at the end of the day so it will not register that trade.
                          Alex

                          Comment


                          • #14
                            Neil
                            To confirm what I suggested in my earlier reply you may want to try the attached efs. This is the same permissioning efs that can be found here to which I have added the logic lifted directly off of your efs.
                            You can find the indicator logic in lines 8-10 and the trading logic (condensed from your efs) in lines 65-83.
                            Because this efs now has a provision to close all trades at the end of the defined trading window it will also trade the last signal and although it closes the trade on the first bar of the 22nd it uses the Close of the last bar of the 21st as the exit price.
                            The back test shown below was run with the Close Trades at end of Back Test option unchecked.
                            Open the efs and change the trading hours to the ones suitable to your time zone (notice BTW that the date logic is no longer in your conditions) then save the efs.
                            When you run the Strategy Analyzer set the Switch to On and set both the Start and End dates to 20040921 (an example of the required format is shown in Edit Studies)
                            Hope this helps
                            Alex

                            Attached Files

                            Comment


                            • #15
                              Thanks Alex I appreciate that.. will work on that later and let you know how it goes

                              Neil

                              Comment

                              Working...
                              X