Announcement

Collapse
No announcement yet.

Can I simulate "reversing" [ie] buying and selling on the same day?

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

  • Can I simulate "reversing" [ie] buying and selling on the same day?

    I am trying to develop backtest logic to reverse back and forth between long and short if a limit price is touched [eg] sell a long position at a limit price and simultaneously establish a short position at the same price ON THE SAME DAY.

    I will continue to experiment and to search the Forum for a solution.

    My question is: Is it possible to do such a thing in the eSignal backtester?

    I do not want to waste time trying to do it if it is impossible to do.

  • #2
    The following is a question which has arisen in trying to create the logic described in my original question[ie] sell a long position at a limit price and simultaneously establish a short position at the same price ON THE SAME DAY.

    In the "BtMovingAverage" strategy code in the "C:\Program Files\eSignal\Formulas\BackTesting" folder the following two statements appear:
    if( !Strategy.isLong() ) and if( !Strategy.isShort() ).

    What is the reason for using the "NOT" construct? Is it to force getting into a positon at start up when the position status is flat [ie] neither long or short?

    Comment


    • #3
      jcm21
      It is possible to open and close (or reverse) a position on the same bar. You can do that by simply initiating a short trade if long or vice-versa. The result is the same as first closing one trade and simultaneously opening another one in the opposite direction. I believe this is explained in the Back testing Tutorials
      Alex


      Originally posted by jcm21
      I am trying to develop backtest logic to reverse back and forth between long and short if a limit price is touched [eg] sell a long position at a limit price and simultaneously establish a short position at the same price ON THE SAME DAY.

      I will continue to experiment and to search the Forum for a solution.

      My question is: Is it possible to do such a thing in the eSignal backtester?

      I do not want to waste time trying to do it if it is impossible to do.

      Comment


      • #4
        jcm21
        The reason is to ensure that the block of code included within each conditional statement is not executed if the strategy is already long or short
        Alex


        Originally posted by jcm21
        The following is a question which has arisen in trying to create the logic described in my original question[ie] sell a long position at a limit price and simultaneously establish a short position at the same price ON THE SAME DAY.

        In the "BtMovingAverage" strategy code in the "C:\Program Files\eSignal\Formulas\BackTesting" folder the following two statements appear:
        if( !Strategy.isLong() ) and if( !Strategy.isShort() ).

        What is the reason for using the "NOT" construct? Is it to force getting into a positon at start up when the position status is flat [ie] neither long or short?

        Comment


        • #5
          More about the NOT character

          Thanks for the help Alex.

          I do understand that the block of code included within a conditional "if" statement is executed only if the condition is true. However doesn't the NOT, [ie] the "!" character, in "if( !Strategy.isLong() )" allow the conditional code to be executed if short OR flat - whereas "if(Strategy.isLong() )" would execute the conditional statements only if long? I see no other code in the strategy that initiates a position at the beginning of the simulation.

          The entire main function is as follows:
          function main() {
          if(study == null) study = sma(10);
          var v = study.getValue(0);
          if(v == null)
          return;
          if(close(0) >= v) {
          if(!Strategy.isLong()) {
          Strategy.doLong("Crossing Up",Strategy.CLOSE,Strategy.THISBAR);
          }
          } else {
          if(!Strategy.isShort()) {
          Strategy.doShort("Crossing Down",Strategy.CLOSE,Strategy.THISBAR);
          }
          }
          if(Strategy.isLong()) {
          setBarBgColor(Color.green);
          } else if(Strategy.isShort()) {
          setBarBgColor(Color.red);
          }
          return v;

          I never noticed the NOT character until after hours of trial and error going down many blind alleys trying to modify the code to make it accomplish what I am trying to do.

          Anyhow please verify that I am correct, or not, in my current interpretation of the logic.

          I really appreciate all you folks help.

          By the way, I am not receiving email notices of replies, even though I have that option checked. I don't think my firewall or mail server is blocking the reply notices. It used to work and now it doesn't - and I don't think I have changed anything?

          Comment


          • #6
            Regarding my last reply a few minutes ago ......

            Regarding my last reply a few minutes ago, I am using Daily bars. I have the EOD version of eSignal.
            Last edited by jcm21; 03-03-2008, 12:08 PM.

            Comment


            • #7
              Re: More about the NOT character

              jcm21

              The conditional statement if(!Strategy.isLong()) is the same as writing if(Strategy.isLong()==false)

              However doesn't the NOT, [ie] the "!" character, in "if( !Strategy.isLong() )" allow the conditional code to be executed if short OR flat..
              Yes it does which is why in my previous reply I explained that it will not execute the commands if already long (or short depending on the condition)
              Alex


              Originally posted by jcm21
              Thanks for the help Alex.

              I do understand that the block of code included within a conditional "if" statement is executed only if the condition is true. However doesn't the NOT, [ie] the "!" character, in "if( !Strategy.isLong() )" allow the conditional code to be executed if short OR flat - whereas "if(Strategy.isLong() )" would execute the conditional statements only if long? I see no other code in the strategy that initiates a position at the beginning of the simulation.

              The entire main function is as follows:
              function main() {
              if(study == null) study = sma(10);
              var v = study.getValue(0);
              if(v == null)
              return;
              if(close(0) >= v) {
              if(!Strategy.isLong()) {
              Strategy.doLong("Crossing Up",Strategy.CLOSE,Strategy.THISBAR);
              }
              } else {
              if(!Strategy.isShort()) {
              Strategy.doShort("Crossing Down",Strategy.CLOSE,Strategy.THISBAR);
              }
              }
              if(Strategy.isLong()) {
              setBarBgColor(Color.green);
              } else if(Strategy.isShort()) {
              setBarBgColor(Color.red);
              }
              return v;

              I never noticed the NOT character until after hours of trial and error going down many blind alleys trying to modify the code to make it accomplish what I am trying to do.

              Anyhow please verify that I am correct, or not, in my current interpretation of the logic.

              I really appreciate all you folks help.

              By the way, I am not receiving email notices of replies, even though I have that option checked. I don't think my firewall or mail server is blocking the reply notices. It used to work and now it doesn't - and I don't think I have changed anything?

              Comment


              • #8
                I am still trying to learn how to reverse a position at a limit price instead of at the close. The following test did not work - no trades were executed?

                As a test I modified BtMovingAverage.efs to close at the moving average value of v instead of at the close - even though the logic is then unrealistic in that I think we would have to know in the middle of the day what the close is going to be that day. I changed two lines of code as follows:

                //Strategy.doLong("Crossing Up", Strategy.CLOSE, Strategy.THISBAR);
                Strategy.doLong("Crossing Up", Strategy.LIMIT, Strategy.THISBAR,Strategy.ALL,v);
                //Strategy.doShort("Crossing Down", Strategy.CLOSE, Strategy.THISBAR);
                Strategy.doShort("Crossing Down", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL,v);

                The entire code for the strategy is as follows:
                /************************************************** ****************************************
                Copyright © eSignal, a division of Interactive Data Corporation. 2005. All rights reserved.
                This sample eSignal Formula Script (EFS) may be modified and saved under a new filename;
                however, eSignal is no longer responsible for the functionality once modified.
                eSignal reserves the right to modify and overwrite this EFS file with each new release.
                @version 2.0 by Alexis Montenegro for eSignal
                ************************************************** *****************************************/

                var study = null;

                function preMain() {
                setPriceStudy(true);
                setStudyTitle("BtMovingAverage limit price reversal test b")
                }

                function main() {

                if(study == null) study = sma(10);

                var v = study.getValue(0);
                if(v == null)
                return;
                if(close(0) >= v) {
                if(!Strategy.isLong()) {
                //Strategy.doLong("Crossing Up", Strategy.CLOSE,
                Strategy.THISBAR);
                Strategy.doLong("Crossing Up", Strategy.LIMIT,
                Strategy.THISBAR,Strategy.ALL,v);}
                } else {
                if(!Strategy.isShort()) {
                //Strategy.doShort("Crossing Down",
                Strategy.CLOSE, Strategy.THISBAR);
                Strategy.doShort("Crossing Down", Strategy.LIMIT,
                Strategy.THISBAR,Strategy.ALL,v);}
                }
                return v;
                }
                Last edited by jcm21; 03-04-2008, 01:03 PM.

                Comment


                • #9
                  Please ignore this thread. I have solved the problem.

                  Please ignore this thread. I have solved the problem. Replacing Strategy.ALL with 1 or Strategy.DEFAULT did the trick.

                  Comment

                  Working...
                  X