How To Compute Average True Range (ATR) using Fortran?

Member

by dorothea , in category: SEO Tools , a month ago

How To Compute Average True Range (ATR) using Fortran?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

Member

by susan , a month ago

@dorothea 

Here is an example code in Fortran to compute the Average True Range (ATR) based on the True Range values:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
program ATR
    implicit none
    
    integer :: i, n
    real :: high(100), low(100), close(100)
    real :: TR(100), ATR(100)
    
    ! Input values for high, low, close data
    n = 100
    do i = 1, n
        high(i) = !insert high value for day i!
        low(i) = !insert low value for day i!
        close(i) = !insert close value for day i!
    end do
    
    ! Calculate True Range for each day
    TR(1) = high(1) - low(1)
    do i = 2, n
        TR(i) = maxval([high(i) - low(i), abs(close(i-1) - high(i)), abs(close(i-1) - low(i))])
    end do
    
    ! Calculate ATR for each day using Wilder's smoothing method
    ATR(1) = TR(1)
    do i = 2, n
        ATR(i) = (ATR(i-1)*(n-1) + TR(i)) / real(n)
    end do
    
    ! Print out ATR values for each day
    do i = 1, n
        print *, 'Day ', i, ' ATR = ', ATR(i)
    end do
    
end program ATR


Make sure to replace the placeholders !insert high/low/close value for day i! with the actual values for your data. This code assumes that you have the high, low, and close values for each day stored in arrays high, low, and close respectively. It calculates the True Range for each day and then computes the Average True Range using Wilder's smoothing method.


Feel free to adjust the code to suit your specific requirements or data format.