diff --git a/docs/src/test_data.md b/docs/src/test_data.md index 1bea004..f5572a1 100644 --- a/docs/src/test_data.md +++ b/docs/src/test_data.md @@ -4,12 +4,23 @@ Test data `const` objects are smaller datasets designed to be used in tests for other packages. They include both `Date` and `DateTime` objects. All but one of the objects has an empty TimeArray `meta` field. -| `const` | Description | First timestamp | Number of rows -|-----------|--------------------------------------|---------------------|----------------- -| op | single column `Date` data | 2000-01-03 | 500 -| cl | single column `Date` data | 2000-01-03 | 500 -| ohlc | four-column `Date` data | 2000-01-03 | 500 -| ohlcv | five-column `Date` data | 2000-01-03 | 500 -| mdata | 1-column `Date` data with meta field | 2000-01-03 | 500 -| datetime1 | 1-column `DateTime` data | 2013-12-31T00:00:00 | 5 -| datetime2 | 1-column `DateTim`e data | 1961-12-31 00:00:00 | 5 +| `const` | Description | First timestamp | Number of rows +|-------------|--------------------------------------|---------------------|----------------- +| `op` | single column `Date` data | 2000-01-03 | 500 +| `cl` | single column `Date` data | 2000-01-03 | 500 +| `ohlc` | four-column `Date` data | 2000-01-03 | 500 +| `ohlcv` | five-column `Date` data | 2000-01-03 | 500 +| `mdata` | 1-column `Date` data with meta field | 2000-01-03 | 500 +| `datetime1` | 1-column `DateTime` data | 2013-12-31T00:00:00 | 5 +| `datetime2` | 1-column `DateTim`e data | 1961-12-31 00:00:00 | 5 + +## Random + +| `Function` | Description | First timestamp(*) | Number of rows(*) +|----------------|--------------------------------------------------|---------------------|----------------- +| `random_cl` | random `Close` price | 2020-01-01 | 500 +| `random_vol` | random `Volume` | 2020-01-01 | 500 +| `random_ohlc` | random `Open`/`High`/`Low`/`Close` price data | 2020-01-01 | 500 +| `random_ohlcv` | random `Open`/`High`/`Low`/`Close`/`Volume` data | 2020-01-01 | 500 + +(*) these values are default values but can be changed passing a keyword argument such as `start=Dates.DateTime(2024, 1, 1)` and `length=1000`. diff --git a/src/MarketData.jl b/src/MarketData.jl index 8934f44..961c950 100644 --- a/src/MarketData.jl +++ b/src/MarketData.jl @@ -14,6 +14,7 @@ using JSON3 export AAPL, BA, CAT, DELL, EBAY, F, GE, TX, cl, op, ohlc, ohlcv, datetime1, datetime2, mdata, o, h, l, c, v +export random_cl, random_vol, random_ohlc, random_ohlcv # downloads.jl export AbstractQueryOpt, YahooOpt, @@ -23,6 +24,7 @@ export AbstractQueryOpt, include("utils.jl") include("const.jl") +include("random.jl") include("helpers.jl") include("downloads.jl") diff --git a/src/random.jl b/src/random.jl new file mode 100644 index 0000000..78e6e04 --- /dev/null +++ b/src/random.jl @@ -0,0 +1,116 @@ +using Random + + +function random_cl(rng::AbstractRNG = Random.GLOBAL_RNG; + start = Dates.DateTime(2020, 1, 1), + step = Dates.Minute(1), + length = 500, + price_init = nothing, + price_init_min = 1.00, + price_init_step = 0.01, + price_init_max = 1000.00, + price_var_min = -1.0, + price_var_step = 0.01, + price_var_max = 1.0, +) + idx = range(start, length = length, step = step) + if isnothing(price_init) + price_init = rand(rng, price_init_min:price_init_step:price_init_max) + end + return TimeArray( + collect(idx), + price_init .+ cumsum(rand(rng, price_var_min:price_var_step:price_var_max, length)), + [:Close], + ) +end + +function random_vol(rng::AbstractRNG = Random.GLOBAL_RNG; + start = Dates.DateTime(2020, 1, 1), + step = Dates.Minute(1), + length = 500, + vol_init_min = 0.0, + vol_init_step = 0.1, + vol_init_max = 100.0, +) + idx = range(start, length = length, step = step) + return TimeArray( + collect(idx), + rand(rng, vol_init_min:vol_init_step:vol_init_max, length), + [:Volume], + ) +end + +function random_ohlc(rng::AbstractRNG = Random.GLOBAL_RNG; + start = Dates.DateTime(2020, 1, 1), + step = Dates.Hour(1), + sub_step = Dates.Minute(1), + length = 500, + price_init = nothing, + price_init_min = 1.00, + price_init_step = 0.01, + price_init_max = 1000.00, + price_var_min = -1.0, + price_var_step = 0.01, + price_var_max = 1.0 +) + nsub = ceil(Int, step / sub_step) + price = random_cl(rng, + start = start, + step = sub_step, + length = length * nsub, + price_init = price_init, + price_init_min = price_init_min, + price_init_step = price_init_step, + price_init_max = price_init_max, + price_var_min = price_var_min, + price_var_step = price_var_step, + price_var_max = price_var_max, + ) + ta_o = collapse(price, step, first, first) + ta_h = collapse(price, step, first, maximum) + ta_l = collapse(price, step, first, minimum) + ta_c = collapse(price, step, first, last) + a_ohlc = hcat(values(ta_o), values(ta_h), values(ta_l), values(ta_c)) + return TimeArray(timestamp(ta_o), a_ohlc, [:Open, :High, :Low, :Close]) +end + + +function random_ohlcv(rng::AbstractRNG = Random.GLOBAL_RNG; + start = Dates.DateTime(2020, 1, 1), + step = Dates.Hour(1), + sub_step = Dates.Minute(1), + length = 500, + price_init = nothing, + price_init_min = 1.00, + price_init_step = 0.01, + price_init_max = 1000.00, + price_var_min = -1.0, + price_var_step = 0.01, + price_var_max = 1.0, + vol_init_min = 0.0, + vol_init_step = 0.1, + vol_init_max = 100.0, +) + ta_ohlc = random_ohlc(rng; + start = start, + step = step, + sub_step = sub_step, + length = length, + price_init = price_init, + price_init_min = price_init_min, + price_init_step = price_init_step, + price_init_max = price_init_max, + price_var_min = price_var_min, + price_var_step = price_var_step, + price_var_max = price_var_max + ) + ta_vol = random_vol(rng; + start = start, + step = step, + length = Base.length(ta_ohlc), + vol_init_min = vol_init_min, + vol_init_step = vol_init_step, + vol_init_max = vol_init_max, + ) + hcat(ta_ohlc, ta_vol) +end diff --git a/test/const.jl b/test/const.jl index a26f68d..cf76f88 100644 --- a/test/const.jl +++ b/test/const.jl @@ -50,3 +50,32 @@ end @test length(GE) == 13090 end end + + +@testset "random data have expected length" begin + + @testset "test random_cl" begin + data = random_cl() + @test length(data) == 500 + @test colnames(data) == [:Close] + end + + @testset "test random_vol" begin + data = random_vol() + @test length(data) == 500 + @test colnames(data) == [:Volume] + end + + @testset "test random_ohlc" begin + data = random_ohlc() + @test length(data) == 500 + @test colnames(data) == [:Open, :High, :Low, :Close] + end + + @testset "test random_ohlcv" begin + data = random_ohlcv() + @test length(data) == 500 + @test colnames(data) == [:Open, :High, :Low, :Close, :Volume] + end + +end