Announcement

Collapse
No announcement yet.

Trailing Stop EFS Syntax Error

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

  • Trailing Stop EFS Syntax Error

    Hello everyone,

    This is the first time I have posted a thread here so forgive me if I am not following etiquette.
    Anyway, I copied and pasted the code below, taken from the EFS database. It is designed to set trailing stops, however I keep on getting a message informing me that there is a syntax error with the following code :

    if (Strategy.isInTrade() == true) && (nNewTrade == 1)) {


    The full code is as follows:

    *----------------------------------------------------------

    Sample Profit Target/Trailing Stop Code

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

    Declare new global variables for the code. Notice we are

    Only adding one new variables to handle the stop loss

    Function – nStopLevel.

    nStopLevel = our stop/trailing stop level.

    ----------------------------------------------------------*/

    var nNewTrade; // New Trade Trigger 0 = OFF / 1 = ON

    var nsignal; // returns the direction of the trading signal

    var nTradeEntryPrice;

    var ProfitTarget1 = 5.0;

    var nStopLevel;



    function premain() {

    setPriceStudy(true);

    setStudyTitle("Sample Profit Code");

    setCursorLabelName("PT CODE");

    }



    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

    }



    /*----------------------------------------------------------------

    // 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.isShort() == true)) {

    // Check if the profit target has been reached/breached

    if (low() <= (nTradeEntryPrice - ProfitTarget1)) {

    // Profit Target Breached… Execute Cover order.

    Strategy.doCover("Short PT1 Exit", Strategy.STOP, Strategy.THISBAR, Strategy.getDefaultLotSize(), (nTradeEntryPrice – ProfitTarget1));

    }

    }

    if (Strategy.isInTrade() == true) && (Strategy.isLong() == true)) {

    // Check if the profit target has been reached/breached

    if (high() >= (nTradeEntryPrice + ProfitTarget1)) {

    // Profit Target Breached… Execute Sell order.

    Strategy.doSell("Long PT1 Exit", Strategy.STOP, Strategy.THISBAR, Strategy.getDefaultLotSize(), (nTradeEntryPrice + ProfitTarget1));

    }

    }



    /*----------------------------------------------------------

    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.getDefaultLotSize(), nStopLevel);

    }

    }

    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.getDefaultLotSize(),nStopLevel);

    }

    }





    /*----------------------------------------------------------

    This portion of the code calculates and sets the new stop levels.

    ----------------------------------------------------------*/



    if (Strategy.isInTrade() == true) && (Strategy.isShort() == true)) {

    nStopLevel = high(-1) + 0.25;

    }

    if (Strategy.isInTrade() == true) && (Strategy.isLong() == true)) {

    nStopLevel = low(-1) - 0.25;

    }







    /*----------------------------------------------------------------

    // Identify new trade signals…

    ----------------------------------------------------------------- */

    if (Strategy.isInTrade() == false) {

    if (/*Buy Condition*/)) {

    nNewTrade = 1; // New Trade Trigger

    nsignal = 1; // Buy Signal – Trade Type

    }

    if (/*Sell Condition*/) {

    nNewTrade = 1; // New Trade Trigger

    nsignal = -1; // Sell Signal – Trade Type

    }

    }



    /*----------------------------------------------------------------

    // 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.doLong("Rev Long", Strategy.MARKET, Strategy.NEXTBAR);

    nStopLevel = low(-1) - 0.25;

    }

    if ((nsignal < 0) && (Strategy.isLong() == true)) {

    Strategy.doSell("Exit Long", Strategy.MARKET, Strategy.NEXTBAR);

    Strategy.doShort("Rev Short", Strategy.MARKET, Strategy.NEXTBAR);

    nStopLevel = high(-1) + 0.25;

    }

    } else {

    if ((nsignal > 0)) {

    Strategy.doLong("Go Long", Strategy.MARKET, Strategy.NEXTBAR);

    nStopLevel = low(-1) - 0.25;

    }

    if ((nsignal < 0)) {

    Strategy.doShort("Go Short", Strategy.MARKET, Strategy.NEXTBAR);

    nStopLevel = high(-1) + 0.25;

    }

    } // end if IN TRADE

    } // END EXECUTE NEW TRADE

    }


    If one of you programming gurus could help me I would be very appreciative.

    Cheers

    Carlton

  • #2
    Please help me with the post below!

    All,

    Will someone please help me figure out why I keep on getting the syntax error below - please!

    Carlton

    Comment


    • #3
      I include the code that missed some parenthesis.

      I am also trying to integrate that code (found from a document called ''Guide to developing eSignal Indicators'', do a search for it) with a simple but working IB buysell script.

      Does the following: '' if (/*Buy Condition*/) '' the point where I include the test for the Buy?

      Thank you for helping.

      Cheers,

      Andrei


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

      if (Strategy.isInTrade() == false) {
      if (/*Buy Condition*/) {
      nNewTrade = 1; // New Trade Trigger
      nsignal = 1; // Buy Signal – Trade Type
      }
      Attached Files

      Comment


      • #4
        Yes, that is correct

        Does the following: '' if (/*Buy Condition*/) '' the point where I include the test for the Buy?

        in other words

        if( vSMA20>close(-1) )

        would be a valid criteria for a buy if you wanted to have vSMA20 above the previous bar close

        Comment


        • #5
          David/Andrei,

          Can you therefore tell me why my formula is not activated when a price is set in my formula?


          Cheers
          Attached Files

          Comment


          • #6
            Guys,

            I know the code is a bit jumbled up, but the aim is to trigger the profit target and trailing stops when a certain price is attained rather than crossovers. I hope to get better at writing code in the future.

            Thanks in advance for your help.

            Cheers

            Carlton

            Comment


            • #7
              Jumbled up?

              The code is jumbled, there is just too much of to comprehend -

              Do you have an idea where the error might be?

              A range of line numbers would help.

              Comment


              • #8
                Yeah, between lines 223 - 232. Basically, I thought that by adding that code a that point I would trigger the formula when I certain price was entered in the 'edit studies' tab.

                Oh dear

                Comment


                • #9
                  Lets walk thru this one line at a time...

                  if (Strategy.isInTrade() == false && bPosition == false) {
                  //if (/*Buy Condition*/)) {
                  if (vMA1_1 < vMA2_1 && vMA1 > vMA2) { //ADDED
                  nNewTrade = 1; // New Trade Trigger
                  nsignal = 1; // Buy Signal - Trade Type
                  }
                  //if (/*Sell Condition*/) {
                  if (vMA1_1 > vMA2_1 && vMA1 < vMA2) { //ADDED
                  nNewTrade = 1; // New Trade Trigger
                  nsignal = -1; // Sell Signal - Trade Type
                  }
                  }

                  Unless I am mistaken this section of code sets up a later section that does the buying and selling, per se.

                  You seem to be checking the ma's correctly and setting the triggers fine, but this section of code does not exit the trade if the stoploss limits are exceeded. In fact, this section doesnt even set the stoploss parameters, that is done in lines 252 and 261 - and 209 and 213 - that seems strange - should be done once maybe? I dunno.

                  Exiting the trade on a violation of the stoploss is done in lines 187 and 196.

                  If you suspect a line of code is not exectuing, you need to put some

                  debugPrintln("The line just executed");

                  code to double check, once you find the line thatis not executing properly, then you can start testing the variables involved to determine which one is messed up.

                  Comment


                  • #10
                    I'm trying to decipher what you've just said below, give me second while I try to understand

                    Comment


                    • #11
                      David,

                      I'm still reading but I want to first point out that I'm actually adding to an existing code. You correctly pointed out what sections of the code are working ok (cos that piece was written by the moderator by the name of JasonK) However, I'm not interested at this moment in establishing a trigger using the MA's. That piece could be completely deleted if that would make things easier read. My main concern is getting the profit targets and the trailing stops to kick in when a certain price is attained. That price would be determined by the figures I would enter in the 'edit studies' tab. You would need to selec the formula first before making the changes in the 'edit studies' tab.

                      I really hope all of the above makes sense.

                      If its ok with you I'm going to attach the formula that I have being try incorporate into the one that you have been looking at.

                      I really appreciate your patience.

                      Cheers mate..

                      Carlton
                      Attached Files

                      Comment


                      • #12
                        Hello David,

                        Were you able to understand my last email?

                        Carlton

                        Comment


                        • #13
                          re your recent efs, alert at x...

                          There is a problem with the close() - PriceLevel comparasion and I can't figure it out either.

                          function main(PriceLevel,HiLo,Popup,Sound,Email) {
                          PriceLevel=1300*1;
                          Popup=1;

                          if (PriceLevel == null || vAlertTriggered==true) return;

                          if (HiLo == "At_Or_Above") {
                          debugPrintln("C="+close()+"P="+PriceLevel);
                          if(close() >= PriceLevel) {
                          debugPrintln("y");

                          The "y" never executes, which means the

                          if(close() >= PriceLevel) {

                          line is false, but the debug says it is true, so let's appeal to a higher authority = Alexis? Beside Jason, he is the best.

                          Comment


                          • #14
                            How about Jason,

                            Do you think you'll be able help us out mate? I do hope so.


                            Cheers

                            Carlton

                            Comment


                            • #15
                              Hello JayF,

                              I have tried again to get the code to work but to no avail. I do hope you'll be able to help out, or at the very least point me in the right direction.

                              Cheers

                              Carlton

                              Comment

                              Working...
                              X