Announcement

Collapse
No announcement yet.

Sample Profit Target/Trailing Stop Code

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

  • #31
    Sorry for my delay..

    I've been busier than a --- well - don't know what..

    Anyway...

    Stops are relatively easy... Here is some sample code..

    PHP Code:
    var nStopPrice 0;
    var 
    StopOffset 2.0;


    function 
    main()
    {

    //--------------------------------------------------------------------
    //  Test for Stop Exit Conditions
    //--------------------------------------------------------------------
       
    if (Strategy.isShort()) {
         if (
    high() >= nStopPrice)  // stop hit
           
    if (low() > nStopPrice) {
             
    Strategy.doCover("Short Stop Hit"Strategy.LIMITStrategy.THISBARStrategy.ALLopen());
           } else {
             
    Strategy.doCover("Short Stop Hit"Strategy.LIMITStrategy.THISBARStrategy.ALLnStopPrice);
           }
         }
       }
       if (
    Strategy.isLong()) {
         if (
    low() <= nStopPrice)  // stop hit
           
    if (high() < nStopPrice) {
             
    Strategy.doSell("Long Stop Hit"Strategy.LIMITStrategy.THISBARStrategy.ALLopen());
           } else {
             
    Strategy.doSell("Long Stop Hit"Strategy.LIMITStrategy.THISBARStrategy.ALLnStopPrice);
           }
         }
       }
    //--------------------------------------------------------------------
    //  END - Test for Stop Exit Conditions
    //--------------------------------------------------------------------


    //--------------------------------------------------------------------
    //  Test for ENTRY Conditions here - sample entry conditions included
    //--------------------------------------------------------------------
       
    if (!Strategy.isInTrade()) {
         if (
    close() > high(-1)) {
             
    Strategy.doLong("Long Entry"Strategy.MARKETStrategy.NEXTBARStrategy.DEFAULT);
          
    nStopPrice high(-1) - StopOffset;
         }
         if (
    close() < low(-1)) {
             
    Strategy.doShort("Short Entry"Strategy.MARKETStrategy.NEXTBARStrategy.DEFAULT);
          
    nStopPrice low(-1) + StopOffset;
         }
       }

    //--------------------------------------------------------------------
    //  Adjust Stop Levels Here...
    //--------------------------------------------------------------------
      
      
    if (Strategy.isLong()) {
         
    nStopPrice Math.max(high(-1),high(-2),high(-3),high(-4))-StopOffset;
      }
      if (
    Strategy.isShort()) {
         
    nStopPrice Math.min(low(-1),low(-2),low(-3),low(-4))+StopOffset;
      }


    }
    //  end of main() 
    This routine accomplishes the following...

    1. Test for Entries
    2. Sets initial stop levels
    3. Adjusts stop levels
    4. Tests for Stop Exits (and handles unexpected conditions for stops - such as gaps thru stop levels).

    Notice the structure of the code..
    1. Test for stop exits (if we are in a trade).
    2. Test for new entry conditions (if we are NOT in a trade).
    3. Adjust stop levels (if we are in a trade).

    Now, you might set some other conditions - such as..
    a. When to adjust the stop - maybe after a profit target is hit or a certain number of bars have passed since entry.
    b. filter in some additional technical indicators for the ENTRY SIGNALS.
    c. vary the stop system to include market volatility.

    Hope this helps.

    PS, the example code in my FILESHARE group works, but as I have become more experienced with EFS, I've changed the way I handle certain things. Practice makes perfect - eh??

    Good luck.

    B
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #32
      Hello Andrei,

      Is there a way to submit a bid (or offer) rather than the IBbroker MARKET through the getMostRecentBid() function?
      You could send a limit order and specify the limit price instead. Set a variable to the limit price you want using getMostRecentBid() or getMostRecentAsk() and pass that variable to the trading functions as the last parameter.

      trade.Sell(getSymbol(),200,IBBroker. LIMIT, [your limit price])
      trade.Buy(getSymbol(),200,IBBroker. LIMIT, [your limit price])
      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


      • #33
        Brad,

        Thanks for getting back to me. I attempted to run your formula but I'm getting a number of syntax errors. Can you help?

        Cheers

        Carlton

        Comment


        • #34
          Example code...

          Cpatte,

          That code is an example of how you should try to emulate the stop level conditions..

          You can COPY and PASTE the stop testing code into your system, then add a global variable called "nStopPrice".

          The syntax error you are receiving is because you are trying to run "example code" as real code. Let me take my example code and make it REAL for you.

          Attached is a working example..


          Brad
          Attached Files
          Brad Matheny
          eSignal Solution Provider since 2000

          Comment


          • #35
            Hello Brad,

            Below is the output from the stopexample.efs when applied to a playback.

            Can you briefly explain what is happening?

            Thx

            Carlton
            Attached Files

            Comment


            • #36
              Hello Brad,

              I think I'm starting to understand EFS a little better now. However, can you tell me what you're trying to achieve with the following code:

              if (close() > high(-1)) {


              Cheers

              Carlton

              Comment


              • #37
                Brad,

                Could you also explain to me what the following code sets out to achieve:

                if (Strategy.isLong()) {
                nStopPrice = Math.max(high(-1),high(-2),high(-3),high(-4))-StopOffset;
                }
                if (Strategy.isShort()) {
                nStopPrice = Math.min(low(-1),low(-2),low(-3),low(-4))+StopOffset;
                }


                Thanks

                Carlton

                Comment


                • #38
                  That code sets the stop privce to the maximum of the 4 prior highs minus the stop offest.

                  Comment


                  • #39
                    dloomis,

                    Are you therefore saying that if, for example, bar one had a price of 2, bar two a price of 3, bar 3 a price of 4 and bar 4 a price of 5 then the calculation would be 5 - (stop offset). correct? If so why are all minuses?

                    Thx

                    Carlton

                    Comment


                    • #40
                      Brad,

                      This formula sounds exactly like what I need - I'm just a little confused on how to get it to work. I have attached one of my playback replays. I was wondering if when you get a moment if you could run your formula against the attached playback. I'm trying to enter a trade when the price reaches above $60, which occurs at precisely 9.55. At that point I would like to set a stop at $58 and trailing stop after that.

                      I hope the above makes sense.

                      If its too much trouble I'll understand - you have been more than helpful.

                      Cheers

                      Carlton

                      P.S. You'll have to change the extension to an epf as its not within the allowed extensions.

                      Comment


                      • #41
                        Your example would be as follows..

                        Here is a working file that will accomplish what you want..
                        Attached Files
                        Brad Matheny
                        eSignal Solution Provider since 2000

                        Comment


                        • #42
                          Re: Reply to post 'Sample Profit Target/Trailing Stop Code'

                          The minus' refer to bars in the past - that's how eSignal knows which bar
                          you are referring to, -2 is 2 bars back.

                          Note +2 is not 2 bars in the future. This feature not yet available, expect
                          in limited beta.


                          >

                          Comment


                          • #43
                            Accessing Future Bars..

                            David,

                            I believe you CAN access future bars with a positive number (ie open(1) or open(2)). I have been doing it for months when I need to record an estimated ENTRY PRICE (using MARKET) orders with the strategy tester.

                            Just so you know. I believe this is possible in the current version.

                            Now, I don't suggest new EFS coders try to do this unless they really know what they are doing. You can dramatically alter the results of your system by refering to future bars data when building a strategy. WARNING WARNING - this can really mess you up.

                            B
                            Brad Matheny
                            eSignal Solution Provider since 2000

                            Comment


                            • #44
                              Thanks Guys

                              Carlton

                              Comment

                              Working...
                              X