Announcement

Collapse
No announcement yet.

Sample Profit Target/Trailing Stop Code

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

  • Sample Profit Target/Trailing Stop Code

    Hello

    I have copied and pastet the 'Sample Profit Target/Trailing Stop Code' for the efs library, however I'm unable to get it to work. I keep on getting a the following syntax error.

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

    I do believe this was originally written by Brad Matheny.

    The full code is outlined below. I have already sent out a request asking if any of you kind people would help me get it to work but no one seems to either know or may be just too busy to help out so I thought I would repost.

    Once again I would really appreciate any help I can.

    Cheers

    Carlton



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

    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

    }


    Again thanks in advance.

  • #2
    Hello Carlton,

    The code from the guide is not a stand-alone formula ready for back testing. It's only showing the code examples you need to incorporate into a formula to make it a back testing formula. Alternatively, you can take the code from the guide and incorporate a study into it to determine what your buy and sell conditions will be. Find the section towards the end of the code example right after the comment that reads, "// Identify new trade signals." You'll notice that the conditions for the buy and sell condition do not exist yet.

    PHP Code:
        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
            
    }
        } 
    The /*Buy Condition*/ and /*Sell Condition*/ need to be developed. I'll put together a working code example for you shortly and include this formula in the guide for future readers.
    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


    • #3
      JasonK,

      Thanks ever-so-much mate. I eagerly await your formula.

      Cheers

      Carlton

      Comment


      • #4
        Hello Carlton,

        Here's that example formula, Sample_TargetStop.efs. The results are certainly nothing to brag about, but it should give you a good idea of how to modify the code example from the guide to create a custom back testing formula. There were a couple syntax errors in the guide. I will correct those and get the guide republished.

        This is an example of what you will see with this formula. The price bars will be colored green when long, red when short and grey when neutral. The profit targets and stop levels appear as green and red flat lines, respectively.
        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


        • #5
          Hello Jason,

          Thanks a lot. I will check out code and let you know how I get on.

          If you could let us know when you have corrected the errors in the guide that would also be greatly appreciated.

          Anyway, thanks a lot mate.

          Cheers

          Carlton

          Comment


          • #6
            Jason,

            This is looking really great.

            There is an awful lot I think I can learn from this code.

            Cheers mate.

            Carlton

            P.S. Please give us a shout when you have corrected the syntax errors in the guide.

            Comment


            • #7
              Jason,

              I just have a couple of questions. I'm using you're formula on an playback file. Does it work on playbacks? When I enter a trade using esignal paper trade I never see the bars indicate that I have gone long.

              Cheers

              Carlton

              Comment


              • #8
                Uh oh,

                Jason, the code is not behaving like it should a quarter of the way through the playback. Its very difficult to explain what its actually doing.

                I will need to check out the code - although I have never ever done any programming before. May be I'll just wait until you've have time to fix the errors in the guide - or is that being lazy??

                Cheers

                Carlton

                Comment


                • #9
                  JasonK,

                  Please ignore my last post, I'm just being a dumb arse. I think I fully understand whats going on.

                  Cheers

                  Carlton

                  Comment


                  • #10
                    However, I can't see from your code how one determines the profit target.

                    Carlton

                    Comment


                    • #11
                      the profit target is a variable defined at the top of the efs file =5.

                      Comment


                      • #12
                        David,

                        Looking at the code again, I think you're wrong about the profit target being at the top of the file =5. I think it defaults to 3 - which is changeable through the 'edit studies' section.

                        However, my question to you is, how does it determine when a trade is made?

                        Cheers

                        Carlton

                        Comment


                        • #13
                          Carlton
                          If I understand the code correctly the trades are executed in Lines 188-206 depending on whether the strategy is already in a trade or not.
                          The conditions (ie nSignal) for the trade itself are set in Lines 168-177 on the cross of the two moving averages.
                          Alex

                          Comment


                          • #14
                            Thanks Alex,

                            I see what you mean. Would you happen to know any code that would allow to signal a trade just by clicking on the formula, rather than waiting for certain conditions to occur?

                            Cheers

                            Carlton

                            Comment


                            • #15
                              Jason,

                              Do you think you could help me write some code that will allow me trigger the formula (Sample_TargetStop.efs.) when a certain price is reached rather than based on the ema crossovers?

                              Cheers mate.

                              Carlton

                              Comment

                              Working...
                              X