Skip to content

Instantly share code, notes, and snippets.

@flare9x
Last active June 19, 2022 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flare9x/abaca3af79d5880f4a0c5c0866678cc9 to your computer and use it in GitHub Desktop.
Save flare9x/abaca3af79d5880f4a0c5c0866678cc9 to your computer and use it in GitHub Desktop.
Noise Test - Adjust price up to a % of the bars true range
"""
```
tr(hlc::Matrix{T})::Array{Float64}
```
True range
"""
function tr(hlc::AbstractMatrix{T})::Array{Float64} where {T<:Real}
@assert size(hlc,2) == 3 "HLC array must have 3 columns."
n = size(hlc,1)
out = zeros(n)
out[1] = NaN
@inbounds for i=2:n
out[i] = max(hlc[i,1]-hlc[i,2], hlc[i,1]-hlc[i-1,3], hlc[i-1,3]-hlc[i,2])
end
return out[:,1]
end
function noise_perc(x::Array{Float64}; perc::Float64=.4)
noise_out = fill(0.0,size(t_r,1))
for i =1:size(t_r,1)
if isnan(t_r[i]) == 0
percs = (perc* (t_r[i]))
pos_range = collect(0.0:.25:percs)
neg_range = -(pos_range)
all_range = vcat(pos_range,neg_range)
noise_out[i] = (x[i]) + sample(all_range) # sample() to choose a random number within the range up to perc= maximum of TR
else
noise_out[i] = 0
end
end
return noise_out
end
noise_test = true
# Build H L C matrix
if noise_test == false
# OHLC
o = Float64.(data.Open)
h = Float64.(data.High)
l = Float64.(data.Low)
c = Float64.(data.Close)
c_sma200 = runmean(c; n=200, cumulative=false)
elseif noise_test == true
# OHLC
o = Float64.(data.Open)
h = Float64.(data.High)
l = Float64.(data.Low)
c = Float64.(data.Close)
c_sma200 = runmean(c; n=200, cumulative=false)
m = Array{Float64}(zeros(size(data,1),0))
m = hcat(m,h)
m = hcat(m,l)
m = hcat(m,c)
# True Range
t_r = tr(m)
o = noise_perc(o,perc=.4)
h = noise_perc(h,perc=.4)
l = noise_perc(l,perc=.4)
c = noise_perc(c,perc=.4)
end
plot(c[2000:2500])
plot!(data.Close[2000:2500])
# Run function
# perc= sets the maximum % to adjust the prices by
noise_perc(data1_c,perc=.4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment