Announcement

Collapse
No announcement yet.

Help please with code for backtesting intraday breakout strategy

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

  • Help please with code for backtesting intraday breakout strategy

    Hi, I’m new to Esignal and new to EFS code, and not very computer literate!

    I’m trying to code the following breakout strategy to back test 2-3 years worth of futures data:

    1. The chart is using hourly bars, starting at 8:00 and finishing at 21:00 (London time)
    2. The high-low range of the first 2 bars (hours) of each day is recorded.
    3. From 10:00am on, if price moves above the high (+ 2 ticks) then a long position is entered. Target is the entry price plus the range (i.e. high minus low), and the stop is entry price minus half the range.
    4. If price moves below the low of the range (- 2 ticks), then a short position is taken with the target again being the range and the stop half the range.
    5. If a stop is triggered, then the strategy ‘resets’ to take any further long or short trades that are triggered.
    6. If a target is hit, then no more trades are taken until the following day.
    7. At 21:00, any open positions are closed at market.

    I have spent some time trawling through the various resources available and have found some of the basic code to enter trades, but can’t find any details for code that will generate a range based on the first 2 intraday bars. Any help gratefully received!

    Thanks,
    Duncan

  • #2
    /*----------------------------------------------------------
    Parameters
    pCheckStart = 8
    pCheckStop = 10
    pLiquidation = 21
    ----------------------------------------------------------*/
    /*----------------------------------------------------------
    New Day
    ----------------------------------------------------------*/
    if (getBarStateInterval("D") == BARSTATE_NEWBAR) {
    vDnStop = null
    vUpStop = null;
    bTradeHour = false
    }

    /*----------------------------------------------------------
    Calculate Entry Stop
    ----------------------------------------------------------*/
    if (vDnStop == null
    && getHour() >= pCheckStop
    && getHour() < pLiquidation
    && getBarStateInterval("60") == BARSTATE_NEWBAR){
    vDnStop = lowest((pCheckStop-pCheckStart),inv(60),-1) +
    2*getMinTick();
    vUpStop = highest(pCheckStop-pCheckStart,inv(60),-1) -
    2*getMinTick();
    bTradeHour = true
    }

    Comment


    • #3
      Hi Geme,

      Thanks very much for the code.

      From my very limited understanding of EFS code, I guess I need to add the explicit commands to open positions and stop/targets.

      Unfortunately, I do have a problem - when I perform a syntax check on the code you kindly provided, I get the following error message:

      , line 297: getRawTime() can only be called in main()

      Is this because I need to complete the rest of the code?

      Thanks again,
      Duncan

      Comment


      • #4
        Hi Ducan,

        I would add gladly a complete example.
        Unfortunately there are problems with the attach file.

        Best, geme

        P.S. You can use a chart with interval <= 60 minutes!
        Attached Files
        Last edited by geme; 10-15-2010, 05:16 AM.

        Comment


        • #5
          Hi Geme,

          This looks brilliant - thank you so much! You have probably saved me weeks of work trying to work out all the code.

          I particularly like the fact that number of periods for the range can be specified before each backtest.

          Obviously there is nothing wrong with the syntax of the code (no error messages anyway), so have you any idea why the backtest results are not being displayed, or that the display gets compressed when running realtime? Does anyone else have suggestions?

          Thanks again - I had no idea that my simple strategy would need so much code!

          Duncan

          Comment


          • #6
            Hi Ducan,

            Adjust Scaling

            display gets compressed when running realtime
            Set Time Template more as one day

            backtest results are not being displayed
            The order handling is somewhat special with eSignal.
            You must the order executions self simulate.

            strategy would need so much code

            Best, geme

            Comment


            • #7
              Hi Geme

              I finally got it to work last night - and its great! I thought from your second post that there was a problem with the file, but once I got the scaling and the time template right (like you said in the 3rd post) it produces great results.

              However, this morning (Saturday) when I run the back test nothing happens even though I am using the same time template. Maybe I have a problem with Esignal (e.g. it will never display equity graphs even when I run the preloaded esignal back test programs) - I will download the program again to see if this solves the problem.

              I have 3 questions about the breakout strategy:

              1. My original strategy says that if the target is met then no more trades for the day, but if stop is hit then we enter more trades when they are triggered. I notice that on some tight ranging days this can cause big losses as the stop is hit over and over again.

              Would it be possible to have adjustable settings for number of trades after target/stop is hit, so that I can choose (for example) to limit the number of times the strategy enters new positions after being stopped out to 2 times, i.e. if the stop is hit twice in a row then no more trades until the next day? Alternatively, can you highlight the section of code that controls this so I can change this?

              2. I notice that the high low lines are slightly less than the actual high low values i.e. if range high is 100, high range line is drawn at 98, and if low is at 50, line is drawn at 52. Can we reverse this so that high line is 2 ticks above actual high, and low line is 2 ticks below?

              3. Trades seem to be entered at the closing price of the bar that penetrates the high or low of the range. Does esignal always do this or can it be programmed to use the actual high or low price for entry?

              Thank you again for sharing your expertise,

              Duncan

              Comment


              • #8
                Hi Duncan,

                the Coding was meant only as integration example.
                Your strategy has some weak points.
                I let your suggestions for improvement flow into a new version.
                I regret that eSignal no more optimizer for Back Testing offer.

                to 3.
                Here STOP LIMIT order is simulated. You must think of a possible GAP.

                Best, geme

                Losses limit. Profits let run.

                P.S. Excuse please my bad English
                Attached Files
                Last edited by geme; 10-17-2010, 04:22 AM.

                Comment


                • #9
                  Hi Geme

                  Thanks for the modified code.

                  I still have some problems with Esignal i.e. when I backtest 3 years of data it it does not include most of 2007 and 2008 data. I will contact esignal about this.

                  I think maybe you are correct that the target should be more than the range ('let the profits run').

                  Also, if I want to start the range at 14:30, how do I input this?

                  Thanks again for your help. Do you have any other suggestions for improving this strategy?

                  All the best,
                  Duncan

                  Comment

                  Working...
                  X