How To Calculate Parabolic SAR (Stop and Reverse) using Lua?

Member

by lizzie , in category: Third Party Scripts , 2 months ago

How To Calculate Parabolic SAR (Stop and Reverse) using Lua?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by wilmer.lemke , 2 months ago

@lizzie 

To calculate the Parabolic SAR (Stop and Reverse) using Lua, follow these steps:

  1. Define the necessary variables:
1
2
3
4
5
6
7
8
local acceleration = 0.02
local maxAcceleration = 0.2
local initialAcceleration = acceleration

local high = {}
local low = {}
local sar = {}
local trend = {}


  1. Create a function to calculate the SAR 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
34
35
36
37
38
39
40
41
function calculateSAR()
    for i = 1, #close do
        if i == 1 then
            sar[i] = low[i]
            trend[i] = "up"
            acceleration = initialAcceleration
        else
            if trend[i - 1] == "up" then
                sar[i] = sar[i - 1] + acceleration * (high[i - 1] - sar[i - 1])
                if sar[i] > low[i] then
                    sar[i] = low[i]
                end
                if high[i] > high[i - 1] then
                    acceleration = acceleration + initialAcceleration
                end
            else
                sar[i] = sar[i - 1] + acceleration * (low[i - 1] - sar[i - 1])
                if sar[i] < high[i] then
                    sar[i] = high[i]
                end
                if low[i] < low[i - 1] then
                    acceleration = acceleration + initialAcceleration
                end
            end

            if trend[i] == "up" then
                if low[i] < sar[i] then
                    trend[i] = "down"
                    sar[i] = high[i - 1]
                    acceleration = initialAcceleration
                end
            else
                if high[i] > sar[i] then
                    trend[i] = "up"
                    sar[i] = low[i - 1]
                    acceleration = initialAcceleration
                end
            end
        end
    end
end


  1. Call the calculateSAR() function to calculate the SAR values:
1
calculateSAR()


This code snippet outlines the basic approach to calculating the Parabolic SAR values using Lua. Modify and adapt the code to fit your specific requirements and preferences.