Announcement

Collapse
No announcement yet.

Trailing Stops Query

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

  • Trailing Stops Query

    Hello

    Brad Matheny, an esignal regulator compiled the attached formula designed for trailing stops, however I can't get it to work. I quite sure that the problem is with something that I'm doing as Brad Matheny writes code all the time. However, I can't figure it out. I wonder if someone could just take a quick look at it and let me know what I'm missing. I'm sure it won't take someone with an ounce of knowledge long to establish what I'm failing to see.

    Cheers

    Carlton
    Attached Files

  • #2
    My 1/2 oz of knowledge says you are never entering a trade?

    All of the if stmts check to see if a trade is on, but I fail to see where the trade is intiated.

    I would start with that problem.

    Comment


    • #3
      Hello Carlton,

      Like Brad's code examples from the Guides, this formula is not a stand-alone formula. It is intended to just show you some of the coding processes you need to incorporate into your custom formula to make them back testing compatible. Similar to the example formula I created for you in this thread.

      For the formula you have attached, look at lines 64 and 69. These commented lines would be where you need to insert your code that checks for your conditions to go long or short, respectively. You would also need to add the Strategy.doLong() and Strategy.doShort() functions with your preferred parameters.

      I’ll put together some more “completed” examples using the code snippets from the guide and add them to the guide as well.
      Jason K.
      Project Manager
      eSignal - an Interactive Data company

      EFS KnowledgeBase
      JavaScript for EFS Video Series
      EFS Beginner Tutorial Series
      EFS Glossary
      Custom EFS Development Policy

      New User Orientation

      Comment


      • #4
        So, Jason, can you guide on the code I would need to write to go long when a certain price was attained?

        Cheers

        Carlton

        Comment


        • #5
          Hello Carlton,

          It's looking like tomorrow for the code example. As for getting the guide updated, we may not be able to have that ready until sometime next week.

          To check for a price reaching a certain level you could use the close() function and compare it to a hard coded price or a variable set to a user defined price. For example,

          PHP Code:
          // hard coded example    
              
          if (close() > 90.00) {
                  
          // execute this code
              
          }
              

          // user defined input example
          // edit nPrice through "Edit Studies"    
          function main(nPrice) {
              if (
          nPrice == nullnPrice 90.00;
              
              if (
          close() > nPrice) {
                  
          // execute this code
              
          }

          Jason K.
          Project Manager
          eSignal - an Interactive Data company

          EFS KnowledgeBase
          JavaScript for EFS Video Series
          EFS Beginner Tutorial Series
          EFS Glossary
          Custom EFS Development Policy

          New User Orientation

          Comment


          • #6
            Hello JasonK,

            For simplicity sake I inserted the hardcoded example you gave me into the formula and still no joy. I inserted it exactly where yoiu suggested as shown in the attachment.

            Carlton
            Attached Files

            Comment


            • #7
              Hello Carlton,

              First, let's look at the section of the formula that handles the entries. In your code example you copied in the example I gave you and changed the price to 90.00. However, you still needed to add the Strategy.doLong() and Strategy.doShort() functions with your preferred parameters. Without those functions, no positions will be taken, which is why no trades occur in the back testing report. Also, you copied the same condition for both the long and short entry sections. For this example I just changed the sign in your short entry logic to < 60.00 from > 60.00. Here's an example of what this code block should look like. Please see the attached formula for the complete code.

              PHP Code:
                  //  Handle Entries.
                  
              if ((!Strategy.isInTrade()) || (Strategy.isShort())) {
                  
              // hard coded example    
                      
              if (close() > 60.00) {
                          
              // execute this code
                          
              Strategy.doLong("Go Long"Strategy.CLOSEStrategy.THISBAR);
                          
              nEntryPrice close();  //  assuming our entry is the current price.
                          
              nStopPrice nEntryPrice-StopLevel;
                      }
                  }
                  if ((!
              Strategy.isInTrade()) || (Strategy.isLong())) {
                      
              // hard coded example    
                      
              if (close() < 60.00) {
                          
              // execute this code
                          
              Strategy.doShort("Go Short"Strategy.CLOSEStrategy.THISBAR);
                          
              nEntryPrice close();  //  assuming our entry is the current price.
                          
              nStopPrice nEntryPrice+StopLevel;
                      }
                  } 
              Attached Files
              Jason K.
              Project Manager
              eSignal - an Interactive Data company

              EFS KnowledgeBase
              JavaScript for EFS Video Series
              EFS Beginner Tutorial Series
              EFS Glossary
              Custom EFS Development Policy

              New User Orientation

              Comment


              • #8
                Jason,

                I think I understand where I was going wrong. Could you therefore, guide me as to where I would need to insert the code you provided below in the attached formula you wrote for me a couple of days ago.

                Sorry if I'm being a nuisance.

                Carlton
                Attached Files

                Comment


                • #9
                  Hello Carlton,

                  The buy and sell conditions are located at lines 166-179. You would edit lines 170 and 175 to use your conditions, close() > 60.00 and close() < 60.00, respectively. Here's what that section looks like.

                  PHP Code:
                      //ADDED - bPostion logic to the following line to prevent new entry
                      //        on the same bar that an exit trade occured.
                      
                  if (Strategy.isInTrade() == false && bPosition == false) {
                          
                  //if (/*Buy Condition*/)) {
                          
                  if (close() > 60.00) {  //ADDED
                              
                  nNewTrade 1// New Trade Trigger
                              
                  nsignal 1// Buy Signal - Trade Type
                          
                  }
                          
                  //if (/*Sell Condition*/) {
                          
                  if (close() < 60.00) {  //ADDED
                              
                  nNewTrade 1// New Trade Trigger
                              
                  nsignal = -1// Sell Signal - Trade Type
                          
                  }
                      } 
                  Attached Files
                  Jason K.
                  Project Manager
                  eSignal - an Interactive Data company

                  EFS KnowledgeBase
                  JavaScript for EFS Video Series
                  EFS Beginner Tutorial Series
                  EFS Glossary
                  Custom EFS Development Policy

                  New User Orientation

                  Comment


                  • #10
                    OK, Jason,

                    I'm not going to beat around the bush! (do Americans say that as well ) What I really was hoping to acheive was to combine the two formulas you recentlywrote. I just thought I would try and do it myself, however if I just asked you in the first place there would not have been any great need for all the additional code you have written.

                    Basically, I need code that will allow me to input a profit target with trailing stops,(which you wrote for me called Sample_TargetStop.efs) combined with price alerts which you wrote for someone else called "Alert at x.efs.

                    I will attach the first formula here and the second one after (as you can only attach one file at a time)

                    If it is at all possible to combine the two formulas that would be amazing helpful.


                    As always I appreciate your help and the help from any other readers who might be able to lend a hand.

                    Cheers


                    Carlton
                    P.S.

                    If there are anybody else who might be able to help out I would be truly grateful.
                    Attached Files

                    Comment


                    • #11
                      Here is the second formula.

                      Cheers

                      Carlton
                      Attached Files

                      Comment


                      • #12
                        Jason,

                        Thank you, thank you, thank you.

                        The carlton_targetstop.efs worked beautifully.

                        Thanks ever-so-much mate.

                        I really appreciate your efforts.

                        Cheers

                        Carlton

                        Comment

                        Working...
                        X