utils.jl
1 @doc """ Log function for a simulation strategy. 2 3 $(TYPEDSIGNATURES) 4 5 Logs the function `f` with its arguments `args` for the simulation strategy `s`. 6 """ 7 log(s::SimStrategy, f, args...) = nothing 8 9 @doc """ Log function for a strategy. 10 11 $(TYPEDSIGNATURES) 12 13 Logs the function `f` with its arguments `args` for the strategy `s`. 14 """ 15 log(s::Strategy{<:Union{Paper,Live}}, f, args...) = @info f(args...) 16 17 @doc """ Apply function to iterable for a simulation strategy. 18 19 $(TYPEDSIGNATURES) 20 21 Applies the function `f` to each element of the iterable `iter` for the simulation strategy `s`. 22 """ 23 liveloop(f, s::SimStrategy, iter) = foreach(f, iter) 24 25 @doc """ Map function asynchronously for a real-time strategy. 26 27 $(TYPEDSIGNATURES) 28 29 Asynchronously maps the function `f` over the iterable `iter` for the real-time strategy `s`. 30 """ 31 liveloop(f, s::RTStrategy, iter) = @sync for i in iter 32 @async(f(i)) |> errormonitor 33 end 34 35 @doc """ Apply function to iterable for a strategy. 36 37 $(TYPEDSIGNATURES) 38 39 Applies the function `f` to each element of the iterable `iter` for the strategy `s`. 40 """ 41 liveloop(f, s::Strategy) = liveloop(f, s, s.universe) 42 43 @doc """ Sleep function for a real-time strategy. 44 45 $(TYPEDSIGNATURES) 46 47 Makes the real-time strategy `s` sleep for `n` seconds. 48 """ 49 livesleep(s::RTStrategy, n) = sleep(n) 50 51 @doc """ Sleep function for a simulation strategy. 52 53 $(TYPEDSIGNATURES) 54 55 Does nothing for the simulation strategy `s`. 56 """ 57 livesleep(s::SimStrategy, _) = nothing 58 59 @doc """ Check if last timestamp is within time frame for a simulation strategy. 60 61 $(TYPEDSIGNATURES) 62 63 Checks if the last timestamp `ts` is within the time frame `tf` for the simulation strategy `s`. 64 """ 65 islastts(::SimStrategy, _, ats, tf) = (true, ats) 66 function islastts(_, ai, ats, tf) 67 ts = ohlcv(ai).timestamp 68 if lastindex(ts) > 0 69 last_date = ts[end] 70 (last_date + period(tf) >= ats, last_date) 71 else 72 (false, DateTime(0)) 73 end 74 end 75 76 @doc """ Calculate angle of a slope. 77 78 $(TYPEDSIGNATURES) 79 80 Calculates the angle in degrees of the slope `slp`. 81 """ 82 function degrees(slp) 83 mod(atan(slp) * (180.0 / π) |> abs, 180.0) 84 end 85 86 @doc """ 87 TimeFrame division 88 89 $(TYPEDSIGNATURES) 90 """ 91 Base.:(/)(tf::TimeFrame, d; type=Millisecond) = begin 92 p = period(tf) 93 v = Millisecond(floor(timefloat(p) / d)) 94 round(v, Millisecond, RoundDown) 95 end