Announcement

Collapse
No announcement yet.

Exits

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

  • Exits

    I want to express my appreciation for the mentoring qualities of this forum. Thanks a bunch!

    I'm trying to get my exits to perform. Currently I'm just trying to get a stop loss to function. My general question is: why doesn't the back test honor my stop losses. My specific question is: Does the nStopLevel at lines 98, 103, 110 and 115 fix a specific number that will be used to exit the trade at lines 44 and 55 or does that number keep changing with each succeeding bar?

    A second area of questions has to do with coloring the bars of the trades. Do I need the return to grey command at lines 46 and 58? Or if I get the exits to perform will the program automatically return to the default grey from the premain settings?

    Here's the formula I'm currently working with:

    /*-------------------------------------------------------------------------
    PB4 is a Point Break Strategy based on PB3 with Strategy.isInTrade == False added and return to grey added
    --------------------------------------------------------------------------*/
    var nNewTrade; // New Trade Trigger 0 = OFF / 1 = ON
    var nsignal; // returns the direction of the trading signal
    var nTradeEntryPrice;
    var nStopLevel;

    function preMain() {

    setPriceStudy(true);
    setStudyTitle("PB4");
    setCursorLabelName("PB4");
    setColorPriceBars(true); //ADDED
    setDefaultPriceBarColor(Color.grey); //ADDED
    }

    function main() {

    /*----------------------------------------------------------------
    // If new trade, get entry price - used for our profit target
    ----------------------------------------------------------
    This portion of the code identifies if a new trade has been issued
    and records the entry price of our trade. If no new trade has been
    triggered (nNewTrade == 1), then this portion of the code is ignored.
    ----------------------------------------------------------*/

    if (Strategy.isInTrade() == true && (nNewTrade == 1)) {
    // This sets the expected entry price of the current short trade
    nTradeEntryPrice = open();
    // This switches off the nNewTrade variable
    nNewTrade = 0; // Turn off NEW TRADE switch
    }

    /*-----------------------------------------------
    This portion of the code tests for a stop level breach and
    executes trades accordingly.
    ----------------------------------------------------------*/

    if (Strategy.isInTrade() == true && Strategy.isShort() == true) {
    // Check if the profit target has been reached/breached
    if (high() >= nStopLevel) {
    // Stop Breached, Execute Cover order.
    Strategy.doCover("Short Stop Exit", Strategy.STOP, Strategy.THISBAR, Strategy.ALL, nStopLevel);
    if (Strategy.isInTrade() == false) { // Reset grey color bars
    setPriceBarColor(Color.grey);
    }
    }
    }

    if (Strategy.isInTrade() == true && Strategy.isLong() == true) {
    // Check if the profit target has been reached/breached
    if (low() <= nStopLevel) {
    // Stop Breached, Execute Sell order.
    Strategy.doSell("Long Stop Exit", Strategy.STOP, Strategy.THISBAR, Strategy.ALL,nStopLevel);

    if (Strategy.isInTrade() == false) { //Reset grey color bars
    setPriceBarColor(Color.grey);
    }
    }
    }

    /*----------------------------------------------------------------
    // Identify new trade signals
    ----------------------------------------------------------------- */

    if (Strategy.isInTrade() == false) {
    if (close(-1) < low(-2) && close(-2) < low(-3) && close(-3) < low(-4) && close() > high (-3)) {
    nNewTrade = 1; // New Trade Trigger
    nsignal = 1; // Buy Signal - Trade Type
    }
    if ( close(-1) > high(-2) && close(-2) > high(-3) && close(-3) > high(-4) && close() < low(-3)) {
    nNewTrade = 1; // New Trade Trigger
    nsignal = -1; // Sell Signal - Trade Type
    }
    }

    if (Strategy.isInTrade() == true) { //ADDED
    if (Strategy.isLong() == true) {
    setPriceBarColor(Color.lime); //ADDED
    }
    if (Strategy.isShort() == true) {
    setPriceBarColor(Color.red); //ADDED
    }
    }

    /*----------------------------------------------------------------
    // Execute Trades ONLY if nNewTrade is triggered ....
    ----------------------------------------------------------------- */

    if (nNewTrade == 1) { //Execute New Trade
    // new or reversed trade position
    if (Strategy.isInTrade() == true) {
    if ((nsignal > 0) && Strategy.isShort() == true) {
    Strategy.doCover("Exit Short", Strategy.MARKET, Strategy.NEXTBAR, Strategy.ALL);
    Strategy.doLong("Rev Long", Strategy.MARKET, Strategy.NEXTBAR, Strategy.getDefaultLotSize() * 3);
    nStopLevel = low(-1) - 5.0;
    //lowest = low(-1); has been removed
    }
    if (nsignal < 0 && Strategy.isLong() == true) {
    Strategy.doSell("Exit Long", Strategy.MARKET, Strategy.NEXTBAR, Strategy.ALL);
    Strategy.doShort("Rev Short", Strategy.MARKET, Strategy.NEXTBAR, Strategy.getDefaultLotSize() * 3);
    nStopLevel = high(-1) + 5.0;
    //highest = high(-1); has been removed
    }
    } else {
    if (Strategy.isInTrade() == false) {
    if (nsignal > 0) {
    Strategy.doLong("Go Long", Strategy.MARKET, Strategy.NEXTBAR, Strategy.getDefaultLotSize() * 3);
    nStopLevel = low(-1) - 5.0;
    //lowest = low(-1); has been removed
    }
    if (nsignal < 0)
    Strategy.doShort("Go Short", Strategy.MARKET, Strategy.NEXTBAR, Strategy.getDefaultLotSize() *3);
    nStopLevel = high(-1) + 5.0;
    //highest = high(-1); has been removed
    }
    } // end if IN TRADE
    } // END EXECUTE NEW TRADE
    }

  • #2
    Why do all the indents disappear when I copy and paste a formula from the EFS Editor?!!!

    Comment


    • #3
      pjwinner

      Why do all the indents disappear when I copy and paste a formula from the EFS Editor?!!!

      To post properly formatted code you need to use either the PHP or CODE tags.
      Click the F.A.Q link in the menu of the Bulletin Board and then on the Are there any special codes/tags I can use to format my posts and messages? link. In that FAQ you will find a link to the required vB codes
      Alex

      Comment


      • #4
        pjwinner

        Does the nStopLevel at lines 98, 103, 110 and 115 fix a specific number that will be used to exit the trade at lines 44 and 55 or does that number keep changing with each succeeding bar?

        To see what the stop is doing add return nStopLevel; at the end of the script (before the very last } bracket). This way you will be plotting the stop on the chart and will be able to verify why it is not functioning as you expect it to.
        You may also want to isolate parts of the strategy in a separate efs to verify that the conditions you have set are actually generating signals.

        A second area of questions has to do with coloring the bars of the trades. Do I need the return to grey command at lines 46 and 58? Or if I get the exits to perform will the program automatically return to the default grey from the premain settings?

        Given that you have defined a default color in preMain you do not need to set the color when the strategy is not in a trade
        Alex

        Comment


        • #5
          Exits

          Thanks for the heads up on indents. I'll try it this time.

          In this new but related code neither the ProfitTarget exit nor the StopLevel exit seems to be functioning. I tried the [i]return nStopLevel;[i]. I'm not sure how to interpret the results. I got some grey bars at the beginning, but it ended before the Back Tester indicated that the trade should start.

          So my question for today is simply, Why is neither the StopLevel exit nor the ProfitTarget exits working? Here's the code:

          [code]
          /*------------------
          //PB5 is Longs Only
          ------------------*/

          var nNewTrade; // New Trade Trigger 0 = OFF / 1 = ON
          var nsignal; // returns the direction of the trading signal
          var nTradeEntryPrice;
          var nStopLevel;
          var ProfitTarget1 = 10.0;
          var ProfitTargetTrigger1;


          function preMain() {

          setPriceStudy(true);
          setStudyTitle("PB5");
          setCursorLabelName("PB5");
          setColorPriceBars(true); //ADDED
          setDefaultPriceBarColor(Color.grey); //ADDED
          }

          function main() {

          /*----------------------------------------------------------------
          // If new trade, get entry price - used for our profit target
          ----------------------------------------------------------
          This portion of the code identifies if a new trade has been issued
          and records the entry price of our trade. If no new trade has been
          triggered (nNewTrade == 1), then this portion of the code is ignored.
          ----------------------------------------------------------*/

          if (Strategy.isInTrade() == true && (nNewTrade == 1)) {
          // This sets the expected entry price of the current short trade
          nTradeEntryPrice = open();
          // This switches off the nNewTrade variable
          nNewTrade = 0; // Turn off NEW TRADE switch
          }

          /*-----------------------------------------------
          This portion of the code tests for a stop level breach and
          executes trades accordingly.
          ----------------------------------------------------------*/


          if (Strategy.isInTrade() == true && Strategy.isLong() == true) {
          // Check if the profit target has been reached/breached
          if (low() <= nStopLevel) {
          // Stop Breached, Execute Sell order.
          Strategy.doSell("Long Stop Exit", Strategy.STOP, Strategy.THISBAR, Strategy.ALL,nStopLevel);
          }
          }

          /*----------------------------------------------------------------
          // Test for Profit Target Breach (ProfitTarget1)
          ----------------------------------------------------------
          This portion of the code identifies if our profit target has
          been reached and exits our trades.
          ----------------------------------------------------------*/

          if (Strategy.isInTrade() == true && (Strategy.isLong() == true)) {
          // Check if the profit target has been reached/breached
          if ((high() >= (nTradeEntryPrice + ProfitTarget1)) && ProfitTargetTrigger1 != 1) {
          // Profit Target Breached, Execute Sell order.
          Strategy.doSell("Long PT1 Exit", Strategy.STOP, Strategy.THISBAR,
          Strategy.getDefaultLotSize(), (nTradeEntryPrice + ProfitTarget1));
          //ProfitTargetTrigger1 = 1;
          }
          }

          /*----------------------------------------------------------------
          // Identify new trade signals
          ----------------------------------------------------------------- */

          if (Strategy.isInTrade() == false) {
          if (close(-1) < low(-2) && close(-2) < low(-3) && close(-3) < low(-4) && close() > high (-3)) {
          nNewTrade = 1; // New Trade Trigger
          nsignal = 1; // Buy Signal - Trade Type
          }
          }

          if (Strategy.isInTrade() == true) {
          if (Strategy.isLong() == true) {
          setPriceBarColor(Color.lime); //ADDED
          }
          }
          /*---------------------------------------------------------------
          // Execute Trades ONLY if nNewTrade is triggered ....
          ----------------------------------------------------------------- */

          if (nNewTrade == 1) { //Execute New Trade
          if (Strategy.isInTrade() == false) {

          if (nsignal > 0) {
          Strategy.doLong("Go Long", Strategy.MARKET, Strategy.NEXTBAR,Strategy.getDefaultLotSize() );
          //Removed *3 at end of .getDefaultLotSize ()
          nStopLevel = low(-1) - 5.0;

          } // end nSignal
          } // end if IN TRADE
          } // END EXECUTE NEW TRADE
          }
          [code]

          Comment


          • #6
            pjwinner
            If there is no plot on the chart for nStopLevel there is a possibility that none is being set. Try adding a debug statement after the line in which you set the stop level ie debugPrintln(nStopLevel) and see what value(s) you get in the Formula Output Window.
            More importantly are you sure you are getting entry signals? As far as I can see the strategy is not generating any trades on the symbols and intervals I have tried. So before you go ahead you may want to check the entry conditions by themselves.
            Also you may want to go through the Help Guides in the EFS KnowledgeBase and specifically the one on Developing eSignal Strategies
            Alex

            Comment


            • #7
              Exits

              Alexis, you were right about the entry being the problem. The offending bit of code was (nsignal >0). When I changed it to (nsignal == 1) both the entry and exit showed up on the chart. Nice to see it working!

              Comment

              Working...
              X