Announcement

Collapse
No announcement yet.

Entry/Exit at same bar?

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

  • Entry/Exit at same bar?

    Please help me:
    This code isn´t working, and I don´t know why.
    And: how can I add that there is only one entry at the same bar
    possible?

    thanks for any support
    martin

    ----------------------------------------------------------------------------------

    function preMain() {



    BARSTATE_ALLBARS;
    setPriceStudy(true);
    setStudyTitle("Breakout");



    }

    function main() {



    var LongLimitPrice = high(-1) +0.8
    var LongStopLoss = high(-1) -0.8
    var ShortLimitPrice = low(-1) -0.8
    var ShortStopPrice = low(-1) +0.8




    if (Strategy.isInTrade() == false && high(0) > high(-1))
    onAction1()


    else if (Strategy.isInTrade() == false && low(0) < low(-1))
    onAction2()


    function onAction1(){

    Strategy.doLong("", Strategy.MARKET, Strategy.THISBAR, Strategy.DEFAULT);
    if (Strategy.isLong == true){sellStop(getSymbol(), 100,LongStopLoss) || sellLimit(getSymbol(), 100, LongLimitPrice );}

    }
    return onAction1();

    function onAction2(){

    Strategy.doShort("", Strategy.MARKET, Strategy.THISBAR, Strategy.DEFAULT);
    if (Strategy.isShort == true) {sellShortStop(getSymbol(), 100, SellShortStop) || sellShortLimit(getSymbol(), ShortLimitPrice);}
    }
    return onAction2();
    }

  • #2
    Re: Why doesn´t it work?

    Originally posted by mkueentzle
    Please help me:
    This code isn´t working, and I don´t know why.
    And: how can I add that there is only one entry at the same bar
    possible?

    thanks for any support
    martin

    ----------------------------------------------------------------------------------

    function preMain() {



    BARSTATE_ALLBARS;
    setPriceStudy(true);
    setStudyTitle("Breakout");



    }

    function main() {



    var LongLimitPrice = high(-1) +0.8
    var LongStopLoss = high(-1) -0.8
    var ShortLimitPrice = low(-1) -0.8
    var ShortStopPrice = low(-1) +0.8




    if (Strategy.isInTrade() == false && high(0) > high(-1))
    onAction1()


    else if (Strategy.isInTrade() == false && low(0) < low(-1))
    onAction2()


    function onAction1(){

    Strategy.doLong("", Strategy.MARKET, Strategy.THISBAR, Strategy.DEFAULT);
    if (Strategy.isLong == true){sellStop(getSymbol(), 100,LongStopLoss) || sellLimit(getSymbol(), 100, LongLimitPrice );}

    }
    return onAction1();

    function onAction2(){

    Strategy.doShort("", Strategy.MARKET, Strategy.THISBAR, Strategy.DEFAULT);
    if (Strategy.isShort == true) {sellShortStop(getSymbol(), 100, SellShortStop) || sellShortLimit(getSymbol(), ShortLimitPrice);}
    }
    return onAction2();
    }
    Martin,

    There are two basic catagories of functions within EFS to "initiate trades". The first is Strategy backtester objects and methods(functions) which are all prefixed the "Strategy."

    These Strategy methods are understood and processed by the Tools-Back Test option which will generate the backtesting reports.

    The other catagory of "trade initiation" functions are the Generic Broker Functions. These are designed to run either real-time or in Tick or Bar Replay. Depending on your Trade Options configuration, they send trades to either the eSignal Paper Broker or whatever real or demo broker you specified.

    Your strategy is using both types of trade functions which will not work.

    You need to replace the trade functions that are not prefixed by t Strategy. for the backtester to work properly.

    In addition Strategy.Market will actually record the "open" price as the backtester processes completed bars only. Unless you are evaluating entry and exit based on the previous bar, this will produce inaccurate backtesting results.

    There are some excellent Backtesting documents in the knowledge base that are probably the best way to familiarize yourself with the backtester capabilities and are highly recommended.

    Hope this helps.


    Glen
    Glen Demarco
    [email protected]

    Comment


    • #3
      I´m confused

      Thanks for your help Glen.
      I tried to give my code the correct structure, but it doesn´t work.
      I don´t know what else to do.....

      Martin

      --------------------------------------------------------------------------
      function preMain() {



      BARSTATE_ALLBARS;
      setStudyTitle("Breakout");
      var NewTrade


      }

      function main() {


      var LongEntryPrice = high(-1) +0.01
      var ShortEntryPrice = low(-1) -0.01
      var LongLimitPrice = high(-1) +0.8
      var LongStopLoss = high(-1) -0.8
      var ShortLimitPrice = low(-1) -0.8
      var ShortStopPrice = low(-1) +0.8


      if(Strategy.isInTrade() == false && (high(0) > high(-1) || low(0) < low(-1))){NewTrade = 1}
      else (NewTrade = 0)

      if(NewTrade = 1){

      if (Strategy.isInTrade() == false && high(0) > high(-1))

      Strategy.doLong("", Strategy.STOP, Strategy.THISBAR, Strategy.DEFAULT, LongEntryPrice);
      if (Strategy.isInTrade == true && Strategy.isLong == true){Strategy.doSell("", MARKET, Strategy.THISBAR, 100, LongStopLoss) ||
      Strategy.doSell("", MARKET, Strategy.THISBAR, 100, LongLimitPrice);}



      else if (Strategy.isInTrade() == false && low(0) < low(-1))
      Strategy.doShort("", Strategy.STOP, Strategy.THISBAR, Strategy.DEFAULT, ShortEntryPrice);
      if (Strategy.isInTrade == true && Strategy.isShort == true) {Strategy.doCover("", MARKET, Strategy.THISBAR, 100, ShortStopLoss) ||
      Strategy.doCover("", MARKET, Strategy.THISBAR, 100, ShortLimitPrice);}
      else (NewTrade = 0)
      }
      }

      Comment


      • #4
        mkueentzle,

        You had several problems with your posted code. I fixed those problems and added some additional code so you can see where trades are taking place and where the stop and limit points are. This code does enter and exit trades, but it may not enter/exit as you want. Use it as a starting point.

        In the future you may want to add comments to your code so yourself and others understand what you are trying to do.

        Steve
        Attached Files

        Comment


        • #5
          Martin,

          The prior one wasn't exiting if a stop was hit. Try this one instead.

          Steve
          Attached Files

          Comment


          • #6
            Exit at the entry bar possible?

            Steve,
            I´m entirely thankful for your help and all your work.
            You really did a great job.
            One essencial question remains: Can I exit my position by stop or target also at the entry bar? I read that it was not possible -
            is it now?

            thanks very much,
            Martin

            Comment


            • #7
              Martin,

              In theory you could exit on the same bar. The if statement on line 42 controls if you look for exits or not. Once you're in a trade it will check for exit conditions on the next tick. So if the price moves to your stop or limit price you would exit on the same bar. You would then re-enter the trade again since the initial trade trigger conditions would still be true. The second trade would also exit immediately since the stop/profit targets would immediately be hit. So you could get lots of trades hitting on the same bar under the right conditions.

              Note you will only see this problem in real time or tick replay mode. On historical data the efs should not enter/exit more than once per bar.

              Steve

              Comment


              • #8
                Re: I´m confused

                Originally posted by mkueentzle
                Thanks for your help Glen.
                I tried to give my code the correct structure, but it doesn´t work.
                I don´t know what else to do.....

                Martin

                --------------------------------------------------------------------------
                function preMain() {



                BARSTATE_ALLBARS;
                setStudyTitle("Breakout");
                var NewTrade


                }

                function main() {


                var LongEntryPrice = high(-1) +0.01
                var ShortEntryPrice = low(-1) -0.01
                var LongLimitPrice = high(-1) +0.8
                var LongStopLoss = high(-1) -0.8
                var ShortLimitPrice = low(-1) -0.8
                var ShortStopPrice = low(-1) +0.8


                if(Strategy.isInTrade() == false && (high(0) > high(-1) || low(0) < low(-1))){NewTrade = 1}
                else (NewTrade = 0)

                if(NewTrade = 1){

                if (Strategy.isInTrade() == false && high(0) > high(-1))

                Strategy.doLong("", Strategy.STOP, Strategy.THISBAR, Strategy.DEFAULT, LongEntryPrice);
                if (Strategy.isInTrade == true && Strategy.isLong == true){Strategy.doSell("", MARKET, Strategy.THISBAR, 100, LongStopLoss) ||
                Strategy.doSell("", MARKET, Strategy.THISBAR, 100, LongLimitPrice);}



                else if (Strategy.isInTrade() == false && low(0) < low(-1))
                Strategy.doShort("", Strategy.STOP, Strategy.THISBAR, Strategy.DEFAULT, ShortEntryPrice);
                if (Strategy.isInTrade == true && Strategy.isShort == true) {Strategy.doCover("", MARKET, Strategy.THISBAR, 100, ShortStopLoss) ||
                Strategy.doCover("", MARKET, Strategy.THISBAR, 100, ShortLimitPrice);}
                else (NewTrade = 0)
                }
                }
                Martin,

                You are very welcome, glad it was helpful.

                I see Steve Meyer is helping you with this strategy. Trust me, his help is infinitely more valuable then mine could ever be, you are in good hands.

                Best of luck with your strategy.

                Glen
                Glen Demarco
                [email protected]

                Comment


                • #9
                  How?

                  Steve,
                  I tried that by changing the target and stop loss. But the exit occurs for the first time at the next bar.
                  Thanks again,
                  Martin

                  Comment


                  • #10
                    Entry/Exit at same bar?

                    Does anybody know if it works at backtesting to enter and
                    exit at the same bar? And if yes, whats the command for that?

                    Tanks for helping,
                    Martin

                    Comment


                    • #11
                      Martin
                      It is possible to enter and exit on the same bar. As an example see the enclosed code and resulting backtest report
                      Alex

                      Comment


                      • #12
                        Alexis,
                        thank you for your reply. I´m disturbed. In your example you
                        enter at bar -1 and to exit you need to know what the close of bar 0 is. How is it possible that there can occur an exit at the entry bar?

                        Martin

                        Comment


                        • #13
                          Martin,

                          close(0) means the close of THIS bar, not the close of bar 0 on the chart.

                          So what Alex is doing is entering a trade at the open on the next to last bar of a chart as the chart is loading historical data. Then if the next to last bar has a close > open the trade is closed. close(0) refers to the close of the current bar being processed, which is bar -1 in this example.

                          Then we move on to the last bar of the chart and close(0) now refers to the last bar's close. close(-1) would refer to the bar we just traded on.

                          I know it can be confusing, but it's something you have to understand to properly use efs.

                          If you are running in realtime, then close(0) does refer to the last bar on the chart after the chart completely loads. As the chart is loading, close(0) refers to the bar currently being loaded, which may not be the last bar on the chart.

                          Steve

                          Comment


                          • #14
                            Martin
                            To add to Steve's reply the strategy would have exited the trade on the same bar in which it had entered even if the exit condition was just the following
                            PHP Code:
                            if(Strategy.isLong()){
                                
                            Strategy.doSell(""Strategy.CLOSEStrategy.THISBAR);

                            Also in this case the constants Strategy.CLOSE and Strategy.THISBAR would have referenced the bar being processed at that time (ie the one currently at bar index -1)
                            Alex

                            Comment


                            • #15
                              Thanks Steve and Alex,

                              as see it is possible, and I looked again several times through the code Steve posted below. I tried to change several things´
                              but the code didn´t exit at the entry bar if neccessary.
                              What is the reason?

                              Thank you again very much for your time and help

                              Martin

                              Comment

                              Working...
                              X