Daily ATR calculation in TradeStation

Discussion in 'Strategy Development' started by Jock, Feb 19, 2007.

  1. Jock


    I need a function to calculate the daily Average True Range, when testing on 5 minute data in TradeStation.

    The following code doesn't generate the correct ATR. Any thoughts on how I can fix this?

    Inputs: Length(NumericSimple);
    Vars: DTHigh(0), DTLow(0), DTR(0);

    if CloseD(1) > HighD(0) then
    DTHigh = CloseD(1)
    DTHigh = HighD(0);

    if CloseD(1) < LowD(0) then
    DTLow = CloseD(1)
    DTLow = LowD(0);

    DTR = DTHigh - DTLow;

    DATR=Average(DTR, Length);
  2. Uh, think about what you said..."5 minute data".
    In liquid stocks and futures, THERE ARE NO GAPS in the intraday quotes. The open of the next bar is never more than 1 or 2 ticks from the close of the prior bar.
    For intraday data, ATR = Average Range
  3. Hi there. I read your code. The standard 'Average True Range' is not a function of comparing yesterday's close to today's high and low. Perhaps you are using the term 'ATR' in a nonstandard way.

    Anyway....to calculate 'true' ATR on a 5 min chart, you could add a second data symbol for the Daily data (which TS refers to as "data2"). Then use the "AvgTrueRange( Length )" function to calculate the ATR of data2.

    Alternatively you could just use one data symbol (the 5 min) and calculate daily ATR using:

    ( ( HighD(1) - LowD(1) ) + ( HighD(2) - LowD(2) ) ) / 2

    .... modify the formula for as many days as you would like in your average.

  4. Jock


    syswizard, not sure I follow you. Even with very liquid futures I have still found occasional gaps between 5min bars. In any case while I’m carrying out testing on 5min data, I am actually using daily data for my “True” range calculations so I certainly need to account for overnight gaps.

    EllisWyat, thanks very much, your suggestion to use data2 for daily data is exactly what I need.

    Also your suggested ( ( HighD(1) - LowD(1) ) + ( HighD(2) - LowD(2) ) ) / 2 calculation is providing the average range, not the average “true” range. The daily ATR needs to take into account overnight gaps between yesterday’s close and today’s high or low.

    To simplify I will certainly use your idea of data2, that said I’m still not sure what’s wrong with my coding, even though I will no longer need to use it.
  5. OK, glad the suggestion was helpful.

    I understand what you're trying to do now. Your code looks correct, except for one aspect. I think the problem may be your use of the length parameter in the average function in the context of a 5 min chart. I think this would take the same data and averages it across 'n' 5 min bars, instead of 'n' Days as you probably intend. ...If you were getting a single day true range, instead of a multiday average, then this is probably the cause.

  6. I don't believe the above...."show me".
    (Hint: your realtime datafeed may be deficient !)
  7. I have seen this many times in my 25 years in the market. Check the first Friday of every month when the employment data are released and if they are way way out of expectation. Even the very very liquid IMM Globex Euro currency futures do gap on 5 min charts and can gap very badly. I have been hurt by such a gap. During such crazy moments, the gap aside, even the most efficient datafeed cannot catch up in realtime.
  8. Hi Jock,

    There is already an Average True Range indicator in TradeStation. It uses the AvgTrueRange function. When you apply this (and any inidicator) for Data2, you have to make it based on Data2 otherwise it defaults to Data1.

    In TradeStation 8.1:
    Format Indicator - General tab - Base study on

    In Tradestation 2000i:
    Format Indicator - Properties tab - Base study on

  9. How about...

    inputs: length(20);
    vars: atr(0);
    if d>d[1] then begin
        if atr=0 then atr=highd(1)-lowd(1) else atr=(((length-1)*atr)+(highd(1)-lowd(1)))/length;
  10. I'm running even with a "sampled" datafeed and I'm looking at the e-mini Russell 2000, arguably, the fastest moving emini future. Today, we had some incredibly fast moves (all up). Even at the 12:05 pm and 12:30 pm moves, I see no gaps at all.
    I'm running my platform on Windows 2000 with the priority set to "realtime" #24. If you are running Windows XP and/or not running your charting platform at the highest priority, that could be the problem. I doubt seriously you could find exchange data with intraday gaps of more than a tick or two.
    #10     Feb 20, 2007